From 8e82f8b84578deb7969bf5a2943707e2105f968e Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Wed, 5 Sep 2018 15:46:10 -0400 Subject: [PATCH 1/7] Add service-accounts --- cmd/serviceaccount.go | 140 +++++++++++++++++++++++++++++++ houston/houston.go | 114 +++++++++++++++++++++---- houston/types.go | 44 ++++++---- serviceaccount/serviceaccount.go | 55 ++++++++++++ 4 files changed, 324 insertions(+), 29 deletions(-) create mode 100644 cmd/serviceaccount.go create mode 100644 serviceaccount/serviceaccount.go diff --git a/cmd/serviceaccount.go b/cmd/serviceaccount.go new file mode 100644 index 000000000..0b8e970f2 --- /dev/null +++ b/cmd/serviceaccount.go @@ -0,0 +1,140 @@ +package cmd + +import ( + "fmt" + + sa "github.com/astronomerio/astro-cli/serviceaccount" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var ( + workspaceUuid string + deploymentUuid string + userUuid string + systemSA bool + category string + label string + + saRootCmd = &cobra.Command{ + Use: "service-account", + Aliases: []string{"sa"}, + Short: "Manage astronomer service accounts", + Long: "Service-accounts represent a revokable token with access to the Astronomer platform", + } + + saCreateCmd = &cobra.Command{ + Use: "create ENTITY-UUID", + Aliases: []string{"cr"}, + Short: "Create a service-account in the astronomer platform", + Long: "Create a service-account in the astronomer platform", + RunE: saCreate, + } + + // saDeleteCmd = &cobra.Command{ + // Use: "delete UUID", + // Aliases: []string{"de"}, + // Short: "Delete a service-account in the astronomer platform", + // Long: "Delete a service-account in the astronomer platform", + // RunE: saDelete, + // Args: cobra.ExactArgs(1), + // } + + saGetCmd = &cobra.Command{ + Use: "get", + Short: "Get a service-account by entity type and entity uuid", + Long: "Get a service-account by entity type and entity uuid", + RunE: saGet, + } +) + +func init() { + // User root + RootCmd.AddCommand(saRootCmd) + + // Service-account create + saRootCmd.AddCommand(saCreateCmd) + saCreateCmd.Flags().StringVarP(&workspaceUuid, "workspace-uuid", "w", "", "[UUID]") + saCreateCmd.Flags().StringVarP(&deploymentUuid, "deployment-uuid", "d", "", "[UUID]") + saCreateCmd.Flags().StringVarP(&userUuid, "user-uuid", "u", "", "[UUID]") + saCreateCmd.Flags().BoolVarP(&systemSA, "system-sa", "s", false, "") + saCreateCmd.Flags().StringVarP(&category, "category", "c", "default", "CATEGORY") + saCreateCmd.Flags().StringVarP(&label, "label", "l", "", "LABEL") + + saRootCmd.AddCommand(saGetCmd) + saGetCmd.Flags().StringVarP(&workspaceUuid, "workspace-uuid", "w", "", "[UUID]") + saGetCmd.Flags().StringVarP(&deploymentUuid, "deployment-uuid", "d", "", "[UUID]") + saGetCmd.Flags().StringVarP(&userUuid, "user-uuid", "u", "", "[UUID]") + saGetCmd.Flags().BoolVarP(&systemSA, "system-sa", "s", false, "") +} + +func getValidEntity() (string, string, error) { + var uuid string + var entityType string + singleArgVerify := 0 + + if systemSA { + entityType = "SYSTEM" + singleArgVerify++ + } + + if len(workspaceUuid) > 0 { + entityType = "WORKSPACE" + uuid = workspaceUuid + singleArgVerify++ + } + + if len(deploymentUuid) > 0 { + entityType = "DEPLOYMENT" + uuid = deploymentUuid + singleArgVerify++ + } + + if len(userUuid) > 0 { + entityType = "USER" + uuid = userUuid + singleArgVerify++ + } + + if singleArgVerify != 1 { + return "", "", errors.New("must specify exactly one service-account type (system, workspace deployment, user") + } + + return entityType, uuid, nil +} + +func saCreate(cmd *cobra.Command, args []string) error { + // Validation + entityType, uuid, err := getValidEntity() + if err != nil { + return err + } + + if len(label) == 0 { + label = fmt.Sprintf("%s %s Service Account", entityType, uuid) + } + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + + return sa.Create(uuid, label, category, entityType) +} + +// func saDelete(cmd *cobra.Command, args []string) error { +// // Silence Usage as we have now validated command input +// cmd.SilenceUsage = true + +// return sa.Delete() +// } + +func saGet(cmd *cobra.Command, args []string) error { + entityType, uuid, err := getValidEntity() + if err != nil { + return err + } + + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true + + return sa.Get(entityType, uuid) +} diff --git a/houston/houston.go b/houston/houston.go index 4e0d9f5b9..546c30ca6 100644 --- a/houston/houston.go +++ b/houston/houston.go @@ -123,6 +123,53 @@ var ( } }` + serviceAccountCreateRequest = ` + mutation CreateServiceAccount { + createServiceAccount( + label: "%s", + category: "%s", + entityType: %s + ) { + apiKey + label + category + entityType + active + } + }` + + serviceAccountDeleteRequest = ` + mutation DeleteServiceAccount { + deleteServiceAccount( + serviceAccountUuid:"%s" + ) { + uuid + label + category + entityType + entityUuid + } + }` + + serviceAccountsGetRequest = ` + query GetServiceAccount { + serviceAccounts( + entityType:%s, + entityUuid:"%s" + ) { + uuid + apiKey + label + category + entityType + entityUuid + active + createdAt + updatedAt + lastUsedAt + } + }` + tokenBasicCreateRequest = ` mutation createBasicToken { createToken( @@ -388,6 +435,19 @@ func (c *Client) CreateBasicToken(email, password string) (*AuthUser, error) { return response.Data.CreateToken, nil } +// CreateServiceAccount sends a request to Houston in order to fetch a newly created service account +// Returns a ServiceAccount object +func (c *Client) CreateServiceAccount(uuid, label, category, entityType string) (*ServiceAccount, error) { + request := fmt.Sprintf(serviceAccountCreateRequest, label, category, entityType) + + response, err := c.QueryHouston(request) + if err != nil { + return nil, errors.Wrap(err, "CreateServiceAccount Failed") + } + + return response.Data.CreateServiceAccount, nil +} + // CreateUser sends request to request to Houston in order to create a new platform User // Returns an AuthUser object containing an token func (c *Client) CreateUser(email string, password string) (*AuthUser, error) { @@ -426,7 +486,7 @@ func (c *Client) DeleteDeployment(uuid string) (*Deployment, error) { return response.Data.DeleteDeployment, nil } -// DeleteWorkspace will send a request to Houston to create a new workspace +// DeleteWorkspace will send a request to Houston to delete a workspace // Returns an object representing deleted workspace func (c *Client) DeleteWorkspace(uuid string) (*Workspace, error) { request := fmt.Sprintf(workspaceDeleteRequest, uuid) @@ -452,6 +512,19 @@ func (c *Client) GetAllDeployments() ([]Deployment, error) { return response.Data.GetDeployments, nil } +// DeleteServiceAccount will send a request to Houston to delete a service account +// Returns an object representing deleted service account +func (c *Client) DeleteServiceAccount(uuid string) (*ServiceAccount, error) { + request := fmt.Sprintf(serviceAccountDeleteRequest, uuid) + + response, err := c.QueryHouston(request) + if err != nil { + return nil, errors.Wrap(err, "DeleteServiceAccount Request") + } + + return response.Data.DeleteServiceAccount, nil +} + // GetDeployments will request all airflow deployments from Houston // Returns a []Deployment structure with deployment details func (c *Client) GetDeployments(ws string) ([]Deployment, error) { @@ -467,19 +540,19 @@ func (c *Client) GetDeployments(ws string) ([]Deployment, error) { // GetDeployment will request a specific airflow deployments from Houston by uuid // Returns a Deployment structure with deployment details -func (c *Client) GetDeployment(deploymentUuid string) (*Deployment, error) { - request := fmt.Sprintf(deploymentGetRequest, deploymentUuid) +// func (c *Client) GetDeployment(deploymentUuid string) (*Deployment, error) { +// request := fmt.Sprintf(deploymentGetRequest, deploymentUuid) - response, err := c.QueryHouston(request) - if err != nil { - return nil, errors.Wrap(err, "GetDeployment Failed") - } +// response, err := c.QueryHouston(request) +// if err != nil { +// return nil, errors.Wrap(err, "GetDeployment Failed") +// } - if len(response.Data.GetDeployments) == 0 { - return nil, fmt.Errorf("deployment not found for uuid \"%s\"", deploymentUuid) - } - return &response.Data.GetDeployments[0], nil -} +// if len(response.Data.GetDeployments) == 0 { +// return nil, fmt.Errorf("deployment not found for uuid \"%s\"", deploymentUuid) +// } +// return &response.Data.GetDeployments[0], nil +// } // GetAuthConfig will get authentication configuration from houston // Returns the requested AuthConfig object @@ -494,8 +567,21 @@ func (c *Client) GetAuthConfig() (*AuthConfig, error) { return response.Data.GetAuthConfig, nil } -// GetWorkspaceAll returns all available workspaces from houston API -// Returns a slice of all Workspaces a user has access to +// GetServiceAccounts will get GetServiceAccounts from houston +// Returns slice of GetServiceAccounts +func (c *Client) GetServiceAccounts(entityType, uuid string) ([]ServiceAccount, error) { + request := fmt.Sprintf(serviceAccountsGetRequest, entityType, uuid) + + response, err := c.QueryHouston(request) + if err != nil { + return nil, errors.Wrap(err, "GetServiceAccounts Failed") + } + + return response.Data.GetServiceAccounts, nil +} + +// GetWorkspace returns all available workspaces from houston API +// Returns a workspace struct func (c *Client) GetWorkspace(uuid string) (*Workspace, error) { request := fmt.Sprintf(workspaceGetRequest, uuid) diff --git a/houston/types.go b/houston/types.go index daada2621..4a1efb40d 100644 --- a/houston/types.go +++ b/houston/types.go @@ -3,21 +3,24 @@ package houston // HoustonResponse wraps all houston response structs used for json marashalling type HoustonResponse struct { Data struct { - AddWorkspaceUser *Workspace `json:"workspaceAddUser,omitempty"` - RemoveWorkspaceUser *Workspace `json:"workspaceRemoveUser,omitempty"` - CreateDeployment *Deployment `json:"createDeployment,omitempty"` - CreateToken *AuthUser `json:"createToken,omitempty"` - CreateUser *AuthUser `json:"createUser,omitempty"` - CreateWorkspace *Workspace `json:"createWorkspace,omitempty"` - DeleteDeployment *Deployment `json:"deleteDeployment,omitempty"` - DeleteWorkspace *Workspace `json:"deleteWorkspace,omitempty"` - GetDeployments []Deployment `json:"deployments,omitempty"` - GetAuthConfig *AuthConfig `json:"authConfig,omitempty"` - GetUsers []User `json:"users,omitempty"` - GetWorkspace *Workspace `json:"workspace,omitempty"` - GetWorkspaces []Workspace `json:"workspaces,omitempty"` - UpdateDeployment *Deployment `json:"updateDeployment,omitempty"` - UpdateWorkspace *Workspace `json:"updateWorkspace,omitempty"` + AddWorkspaceUser *Workspace `json:"workspaceAddUser,omitempty"` + RemoveWorkspaceUser *Workspace `json:"workspaceRemoveUser,omitempty"` + CreateDeployment *Deployment `json:"createDeployment,omitempty"` + CreateToken *AuthUser `json:"createToken,omitempty"` + CreateServiceAccount *ServiceAccount `json:"createServiceAccount,omitempty"` + CreateUser *AuthUser `json:"createUser,omitempty"` + CreateWorkspace *Workspace `json:"createWorkspace,omitempty"` + DeleteDeployment *Deployment `json:"deleteDeployment,omitempty"` + DeleteServiceAccount *ServiceAccount `json:"deleteServiceAccount,omitempty"` + DeleteWorkspace *Workspace `json:"deleteWorkspace,omitempty"` + GetDeployments []Deployment `json:"deployments,omitempty"` + GetAuthConfig *AuthConfig `json:"authConfig,omitempty"` + GetServiceAccounts []ServiceAccount `json:"serviceAccounts,omitempty"` + GetUsers []User `json:"users,omitempty"` + GetWorkspace *Workspace `json:"workspace,omitempty"` + GetWorkspaces []Workspace `json:"workspaces,omitempty"` + UpdateDeployment *Deployment `json:"updateDeployment,omitempty"` + UpdateWorkspace *Workspace `json:"updateWorkspace,omitempty"` } `json:"data"` Errors []Error `json:"errors,omitempty"` } @@ -85,6 +88,17 @@ type Status struct { Id string `json:"id"` } +// ServiceACcount defines a structure of a ServiceAccountResponse object +type ServiceAccount struct { + Uuid string `json:"uuid"` + ApiKey string `json:"apiKey"` + Label string `json:"label"` + Category string `json:"category"` + EntityType string `json:"entityType"` + EntityUuid string `json:"entityUuid"` + Active bool `json:"active"` +} + // Token contains a houston auth token as well as it's payload of components type Token struct { Value string `json:"value"` diff --git a/serviceaccount/serviceaccount.go b/serviceaccount/serviceaccount.go new file mode 100644 index 000000000..5b9386dd9 --- /dev/null +++ b/serviceaccount/serviceaccount.go @@ -0,0 +1,55 @@ +package deployment + +import ( + "fmt" + + "github.com/astronomerio/astro-cli/houston" + "github.com/astronomerio/astro-cli/pkg/httputil" +) + +var ( + http = httputil.NewHTTPClient() + api = houston.NewHoustonClient(http) +) + +func Create(uuid, label, category, entityType string) error { + sa, err := api.CreateServiceAccount(uuid, label, category, entityType) + if err != nil { + return err + } + + msg := fmt.Sprintf("%s %s token: %s", sa.EntityType, sa.Uuid, sa.ApiKey) + fmt.Println(msg) + + return nil +} + +func Delete(uuid string) error { + resp, err := api.DeleteServiceAccount(uuid) + if err != nil { + return err + } + + msg := fmt.Sprintf("Service Account %s (%s) successfully deleted", resp.Label, resp.Uuid) + fmt.Println(msg) + + return nil +} + +func Get(entityType, uuid string) error { + r := " %-30s %-50s %-30s" + + resp, err := api.GetServiceAccounts(entityType, uuid) + if err != nil { + return err + } + + h := fmt.Sprintf(r, "NAME", "UUID", "CATEGORY") + fmt.Println(h) + + for _, sa := range resp { + fullStr := fmt.Sprintf(r, sa.Label, sa.Uuid, sa.Category) + fmt.Println(fullStr) + } + return nil +} From a76076515e19c86fa03df8dc1e4c74a46712fec3 Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Fri, 21 Sep 2018 09:05:15 -0400 Subject: [PATCH 2/7] Fix deployment ls spacing --- deployment/deployment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployment/deployment.go b/deployment/deployment.go index 87309630c..06d8c5c6a 100644 --- a/deployment/deployment.go +++ b/deployment/deployment.go @@ -57,7 +57,7 @@ func List(ws string, all bool) error { var deployments []houston.Deployment var err error - r := " %-30s %-50s %-30s %-50s" + r := " %-30s %-50s %-50s %-50s" h := fmt.Sprintf(r, "NAME", "RELEASE NAME", "DEPLOYMENT ID", "WORKSPACE") // colorFmt := "\033[33;m" // colorTrm := "\033[0m" From 119d684fbeb41259512ab798d2750ca7530dd64c Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Fri, 21 Sep 2018 09:25:10 -0400 Subject: [PATCH 3/7] Add ApiKey output --- houston/types.go | 1 + serviceaccount/serviceaccount.go | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/houston/types.go b/houston/types.go index 4a1efb40d..6563a5b93 100644 --- a/houston/types.go +++ b/houston/types.go @@ -96,6 +96,7 @@ type ServiceAccount struct { Category string `json:"category"` EntityType string `json:"entityType"` EntityUuid string `json:"entityUuid"` + LastUsedAt string `json:"lastUsedAt"` Active bool `json:"active"` } diff --git a/serviceaccount/serviceaccount.go b/serviceaccount/serviceaccount.go index 5b9386dd9..89bb0cede 100644 --- a/serviceaccount/serviceaccount.go +++ b/serviceaccount/serviceaccount.go @@ -37,18 +37,18 @@ func Delete(uuid string) error { } func Get(entityType, uuid string) error { - r := " %-30s %-50s %-30s" + r := " %-30s %-30s %-50s %-30s" resp, err := api.GetServiceAccounts(entityType, uuid) if err != nil { return err } - h := fmt.Sprintf(r, "NAME", "UUID", "CATEGORY") + h := fmt.Sprintf(r, "NAME", "CATEGORY", "UUID", "APIKEY") fmt.Println(h) for _, sa := range resp { - fullStr := fmt.Sprintf(r, sa.Label, sa.Uuid, sa.Category) + fullStr := fmt.Sprintf(r, sa.Label, sa.Category, sa.Uuid, sa.ApiKey) fmt.Println(fullStr) } return nil From 723352d4728272ce9d214e979cca34e2c7ec9283 Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Fri, 21 Sep 2018 09:34:11 -0400 Subject: [PATCH 4/7] Add sa delete command --- cmd/serviceaccount.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/cmd/serviceaccount.go b/cmd/serviceaccount.go index 0b8e970f2..ffa387982 100644 --- a/cmd/serviceaccount.go +++ b/cmd/serviceaccount.go @@ -24,21 +24,21 @@ var ( } saCreateCmd = &cobra.Command{ - Use: "create ENTITY-UUID", + Use: "create", Aliases: []string{"cr"}, Short: "Create a service-account in the astronomer platform", Long: "Create a service-account in the astronomer platform", RunE: saCreate, } - // saDeleteCmd = &cobra.Command{ - // Use: "delete UUID", - // Aliases: []string{"de"}, - // Short: "Delete a service-account in the astronomer platform", - // Long: "Delete a service-account in the astronomer platform", - // RunE: saDelete, - // Args: cobra.ExactArgs(1), - // } + saDeleteCmd = &cobra.Command{ + Use: "delete [SA-UUID]", + Aliases: []string{"de"}, + Short: "Delete a service-account in the astronomer platform", + Long: "Delete a service-account in the astronomer platform", + RunE: saDelete, + Args: cobra.ExactArgs(1), + } saGetCmd = &cobra.Command{ Use: "get", @@ -66,6 +66,8 @@ func init() { saGetCmd.Flags().StringVarP(&deploymentUuid, "deployment-uuid", "d", "", "[UUID]") saGetCmd.Flags().StringVarP(&userUuid, "user-uuid", "u", "", "[UUID]") saGetCmd.Flags().BoolVarP(&systemSA, "system-sa", "s", false, "") + + saRootCmd.AddCommand(saDeleteCmd) } func getValidEntity() (string, string, error) { @@ -120,12 +122,12 @@ func saCreate(cmd *cobra.Command, args []string) error { return sa.Create(uuid, label, category, entityType) } -// func saDelete(cmd *cobra.Command, args []string) error { -// // Silence Usage as we have now validated command input -// cmd.SilenceUsage = true +func saDelete(cmd *cobra.Command, args []string) error { + // Silence Usage as we have now validated command input + cmd.SilenceUsage = true -// return sa.Delete() -// } + return sa.Delete(args[0]) +} func saGet(cmd *cobra.Command, args []string) error { entityType, uuid, err := getValidEntity() From 9d8170cec7820022c1f9831a0be6020d9d8a4f32 Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Fri, 21 Sep 2018 10:02:23 -0400 Subject: [PATCH 5/7] Cleanup SA creation success output --- cmd/serviceaccount.go | 4 +++- houston/houston.go | 15 ++++++++++----- serviceaccount/serviceaccount.go | 11 +++++++++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/serviceaccount.go b/cmd/serviceaccount.go index ffa387982..364b5d9f0 100644 --- a/cmd/serviceaccount.go +++ b/cmd/serviceaccount.go @@ -112,8 +112,10 @@ func saCreate(cmd *cobra.Command, args []string) error { return err } + r := []rune(uuid) + last12 := string(r[len(r)-12:]) if len(label) == 0 { - label = fmt.Sprintf("%s %s Service Account", entityType, uuid) + label = fmt.Sprintf("%s %s Service Account", entityType, last12) } // Silence Usage as we have now validated command input diff --git a/houston/houston.go b/houston/houston.go index 546c30ca6..bf66d36b5 100644 --- a/houston/houston.go +++ b/houston/houston.go @@ -130,11 +130,16 @@ var ( category: "%s", entityType: %s ) { - apiKey - label - category - entityType - active + uuid + apiKey + label + category + entityType + entityUuid + active + createdAt + updatedAt + lastUsedAt } }` diff --git a/serviceaccount/serviceaccount.go b/serviceaccount/serviceaccount.go index 89bb0cede..d61d50b46 100644 --- a/serviceaccount/serviceaccount.go +++ b/serviceaccount/serviceaccount.go @@ -13,18 +13,25 @@ var ( ) func Create(uuid, label, category, entityType string) error { + r := " %-40s %-50s %-50s" + sa, err := api.CreateServiceAccount(uuid, label, category, entityType) if err != nil { return err } - msg := fmt.Sprintf("%s %s token: %s", sa.EntityType, sa.Uuid, sa.ApiKey) - fmt.Println(msg) + h := fmt.Sprintf(r, "NAME", "UUID", "APIKEY") + fmt.Println(h) + fullStr := fmt.Sprintf(r, sa.Label, sa.Uuid, sa.ApiKey) + fmt.Println(fullStr) + + fmt.Println("\n Service account successfully created.") return nil } func Delete(uuid string) error { + resp, err := api.DeleteServiceAccount(uuid) if err != nil { return err From 8527c898f76680d515c6834488332d150779c185 Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Fri, 21 Sep 2018 10:21:41 -0400 Subject: [PATCH 6/7] Cleanup default label output --- cmd/serviceaccount.go | 9 ++++++--- serviceaccount/serviceaccount.go | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/serviceaccount.go b/cmd/serviceaccount.go index 364b5d9f0..d0ab3541e 100644 --- a/cmd/serviceaccount.go +++ b/cmd/serviceaccount.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "strings" sa "github.com/astronomerio/astro-cli/serviceaccount" "github.com/pkg/errors" @@ -112,10 +113,12 @@ func saCreate(cmd *cobra.Command, args []string) error { return err } - r := []rune(uuid) - last12 := string(r[len(r)-12:]) if len(label) == 0 { - label = fmt.Sprintf("%s %s Service Account", entityType, last12) + // Scrub attrs for label + r := []rune(uuid) + last12 := string(r[len(r)-12:]) + + label = fmt.Sprintf("%s %s Service Account", strings.Title(strings.ToLower(entityType)), last12) } // Silence Usage as we have now validated command input diff --git a/serviceaccount/serviceaccount.go b/serviceaccount/serviceaccount.go index d61d50b46..80acdc142 100644 --- a/serviceaccount/serviceaccount.go +++ b/serviceaccount/serviceaccount.go @@ -13,7 +13,7 @@ var ( ) func Create(uuid, label, category, entityType string) error { - r := " %-40s %-50s %-50s" + r := " %-45s %-50s %-50s" sa, err := api.CreateServiceAccount(uuid, label, category, entityType) if err != nil { @@ -44,7 +44,7 @@ func Delete(uuid string) error { } func Get(entityType, uuid string) error { - r := " %-30s %-30s %-50s %-30s" + r := " %-45s %-30s %-50s %-30s" resp, err := api.GetServiceAccounts(entityType, uuid) if err != nil { From 42e6d1dda209889720e705e140c7e846140d7290 Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Fri, 21 Sep 2018 10:47:29 -0400 Subject: [PATCH 7/7] Require label to be passed on sa creation --- cmd/serviceaccount.go | 9 +-------- houston/houston.go | 5 +++-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/cmd/serviceaccount.go b/cmd/serviceaccount.go index d0ab3541e..a78cf3d21 100644 --- a/cmd/serviceaccount.go +++ b/cmd/serviceaccount.go @@ -1,9 +1,6 @@ package cmd import ( - "fmt" - "strings" - sa "github.com/astronomerio/astro-cli/serviceaccount" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -114,11 +111,7 @@ func saCreate(cmd *cobra.Command, args []string) error { } if len(label) == 0 { - // Scrub attrs for label - r := []rune(uuid) - last12 := string(r[len(r)-12:]) - - label = fmt.Sprintf("%s %s Service Account", strings.Title(strings.ToLower(entityType)), last12) + return errors.New("must provide a service-account label with the --label (-l) flag") } // Silence Usage as we have now validated command input diff --git a/houston/houston.go b/houston/houston.go index bf66d36b5..e29175b50 100644 --- a/houston/houston.go +++ b/houston/houston.go @@ -126,6 +126,7 @@ var ( serviceAccountCreateRequest = ` mutation CreateServiceAccount { createServiceAccount( + entityUuid: "%s", label: "%s", category: "%s", entityType: %s @@ -442,8 +443,8 @@ func (c *Client) CreateBasicToken(email, password string) (*AuthUser, error) { // CreateServiceAccount sends a request to Houston in order to fetch a newly created service account // Returns a ServiceAccount object -func (c *Client) CreateServiceAccount(uuid, label, category, entityType string) (*ServiceAccount, error) { - request := fmt.Sprintf(serviceAccountCreateRequest, label, category, entityType) +func (c *Client) CreateServiceAccount(entityUuid, label, category, entityType string) (*ServiceAccount, error) { + request := fmt.Sprintf(serviceAccountCreateRequest, entityUuid, label, category, entityType) response, err := c.QueryHouston(request) if err != nil {