Skip to content

Commit

Permalink
fix GetServiceAccountByName
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Jasinski <jasinskia@vmware.com>
  • Loading branch information
adezxc committed Jun 15, 2023
1 parent 8b441f3 commit f7c4b4f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
5 changes: 4 additions & 1 deletion govcd/api_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func (vcdClient *VCDClient) CreateToken(org, tokenName string) (*Token, error) {
return nil, fmt.Errorf("failed to register API token: %s", err)
}

tokenUrn := "urn:vcloud:token:" + newTokenParams.ClientID
tokenUrn, err := BuildUrnWithUuid("urn:vcloud:token:", newTokenParams.ClientID)
if err != nil {
return nil, fmt.Errorf("failed to build URN: %s", err)
}

token, err := vcdClient.GetTokenById(tokenUrn)
if err != nil {
Expand Down
33 changes: 27 additions & 6 deletions govcd/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (org *Org) GetServiceAccountById(serviceAccountId string) (*ServiceAccount,
return newServiceAccount, nil
}

func (org *Org) GetServiceAccountByName(name string) (*ServiceAccount, error) {
func (org *Org) GetAllServiceAccounts(queryParams url.Values) ([]*ServiceAccount, error) {
client := org.client
if client.APIVCDMaxVersionIs("< 37.0") {
version, err := client.GetVcdFullVersion()
Expand All @@ -76,20 +76,41 @@ func (org *Org) GetServiceAccountByName(name string) (*ServiceAccount, error) {
return nil, err
}

newServiceAccount := &ServiceAccount{
ServiceAccount: &types.ServiceAccount{},
org: org,
// VCD has a pageSize limit on this specifi endpoint
queryParams.Add("pageSize", "32")
typeResponses := []*types.ServiceAccount{{}}
err = client.OpenApiGetAllItems(apiVersion, urlRef, queryParams, &typeResponses, nil)

if err != nil {
return nil, fmt.Errorf("failed to get service accounts: %s", err)
}

results := make([]*ServiceAccount, len(typeResponses))
for sliceIndex := range typeResponses {
results[sliceIndex] = &ServiceAccount{
ServiceAccount: typeResponses[sliceIndex],
org: org,
}
}

return results, nil
}

func (org *Org) GetServiceAccountByName(name string) (*ServiceAccount, error) {
queryParams := url.Values{}
queryParams.Add("filter", fmt.Sprintf("name==%s", name))

err = client.OpenApiGetItem(apiVersion, urlRef, queryParams, newServiceAccount.ServiceAccount, nil)
serviceAccounts, err := org.GetAllServiceAccounts(queryParams)
if err != nil {
return nil, fmt.Errorf("error getting service account by name: %s", err)
}

serviceAccount, err := oneOrError("name", name, serviceAccounts)
if err != nil {
return nil, err
}

return newServiceAccount, nil
return serviceAccount, nil
}

// RegisterServiceAccount creates a Service Account and sets it in `Created` status
Expand Down

0 comments on commit f7c4b4f

Please sign in to comment.