// Code generated by sdkgen-v2. DO NOT EDIT.
package iamsdk

import (
	"context"

	iam "github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1"
	"github.com/yandex-cloud/go-sdk/v2/pkg/sdkresolvers"
)

type serviceAccountResolver struct {
	client ServiceAccountClient
	sdkresolvers.BaseNameResolver
}

func ServiceAccountResolver(name string, client ServiceAccountClient, opts ...sdkresolvers.ResolveOption) sdkresolvers.Resolver {
	return &serviceAccountResolver{
		client:           client,
		BaseNameResolver: sdkresolvers.NewBaseNameResolver(name, "ServiceAccount", opts...),
	}
}

func (r *serviceAccountResolver) Run(ctx context.Context) error {
	err := r.EnsureFolderID()
	if err != nil {
		return err
	}

	resp := r.client.Iterator(ctx, &iam.ListServiceAccountsRequest{
		FolderId: r.FolderID(),
		Filter:   sdkresolvers.CreateResolverFilter("name", r.Name),
		PageSize: sdkresolvers.DefaultResolverPageSize,
	})
	return r.FindName(resp.TakeAll())
}
