From fd18ad2cd8ba133931c605971260680196c6f022 Mon Sep 17 00:00:00 2001 From: thde Date: Fri, 15 Mar 2024 09:56:11 +0100 Subject: [PATCH] refactor: move connection secret to function --- get/apiserviceaccount.go | 10 ++-------- get/get.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/get/apiserviceaccount.go b/get/apiserviceaccount.go index 3c630d4..d8b3d44 100644 --- a/get/apiserviceaccount.go +++ b/get/apiserviceaccount.go @@ -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 } diff --git a/get/get.go b/get/get.go index 07adcce..705435d 100644 --- a/get/get.go +++ b/get/get.go @@ -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" @@ -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 +} +