Skip to content

Commit

Permalink
refactor: move connection secret to function
Browse files Browse the repository at this point in the history
  • Loading branch information
thde committed Mar 15, 2024
1 parent 5d7ba4d commit fd18ad2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 2 additions & 8 deletions get/apiserviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,12 @@ func (asa *apiServiceAccountsCmd) print(sas []iam.APIServiceAccount, get *Cmd, h
}

func (asa *apiServiceAccountsCmd) printToken(ctx context.Context, client *api.Client, sa *iam.APIServiceAccount) error {
secret, err := client.GetConnectionSecret(ctx, sa)
token, err := getConnectionSecret(ctx, client, "token", sa)
if err != nil {
return fmt.Errorf("unable to get connection secret: %w", err)
}

token, ok := secret.Data[tokenKey]
if !ok {
return fmt.Errorf("secret of API Service Account %s has no token", sa.Name)
return err
}

fmt.Printf("%s\n", token)

return nil
}

Expand Down
17 changes: 17 additions & 0 deletions get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"

"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/gobuffalo/flect"
management "github.com/ninech/apis/management/v1alpha1"
"github.com/ninech/nctl/api"
Expand Down Expand Up @@ -142,3 +143,19 @@ func projects(ctx context.Context, client *api.Client, onlyName string) ([]manag
}
return projectList.Items, nil
}


func getConnectionSecret(ctx context.Context, client *api.Client, key string, mg resource.Managed) (string, error) {
secret, err := client.GetConnectionSecret(ctx, mg)
if err != nil {
return "", fmt.Errorf("unable to get connection secret: %w", err)
}

content, ok := secret.Data[key]
if !ok {
return "", fmt.Errorf("secret %s has no key %s", mg.GetName(), key)
}

return string(content), nil
}

0 comments on commit fd18ad2

Please sign in to comment.