From e8be21553c04a3a3da298680054142ee88a512f3 Mon Sep 17 00:00:00 2001 From: Andy Cooper Date: Mon, 9 Jul 2018 13:48:25 -0400 Subject: [PATCH] Add update workspace integration with houston --- auth/auth.go | 1 - cmd/workspace.go | 16 ++++++++++++---- houston/houston.go | 24 +++++++++++++++++++++++- houston/types.go | 1 + messages/messages.go | 1 + workspace/workspace.go | 15 +++++++++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 55ad3f01a..0033af72b 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -97,6 +97,5 @@ func Login(oAuthOnly bool) error { // Logout logs a user out of the docker registry. Will need to logout of Houston next. func Logout() { - // forget jwt config.CFG.CloudAPIToken.SetProjectString("") } diff --git a/cmd/workspace.go b/cmd/workspace.go index e745eb9f5..897392ff2 100644 --- a/cmd/workspace.go +++ b/cmd/workspace.go @@ -6,7 +6,8 @@ import ( ) var ( - createDesc string + workspaceUpdateAttrs = []string{"label"} + createDesc string workspaceRootCmd = &cobra.Command{ Use: "workspace", @@ -46,7 +47,10 @@ var ( Aliases: []string{"up"}, Short: "Update an Astronomer workspace", Long: "Update a workspace name, as well as users and roles assigned to a workspace", - RunE: workspaceUpdate, + Args: func(cmd *cobra.Command, args []string) error { + return updateArgValidator(args[1:], workspaceUpdateAttrs) + }, + RunE: workspaceUpdate, } workspaceUserRootCmd = &cobra.Command{ @@ -116,9 +120,13 @@ func workspaceDelete(cmd *cobra.Command, args []string) error { return workspace.Delete(args[0]) } -// TODO func workspaceUpdate(cmd *cobra.Command, args []string) error { - return nil + argsMap, err := argsToMap(args[1:]) + if err != nil { + return err + } + + return workspace.Update(args[0], argsMap) } func workspaceUserAdd(cmd *cobra.Command, args []string) error { diff --git a/houston/houston.go b/houston/houston.go index c2718aacc..289714f15 100644 --- a/houston/houston.go +++ b/houston/houston.go @@ -202,6 +202,18 @@ var ( } }` + workspaceUpdateRequest = ` + mutation UpdateWorkspace { + updateWorkspace(workspaceUuid:"%s", + payload: %s + ) { + uuid + description + label + active + } + }` + workspaceUserAddRequest = ` mutation AddWorkspaceUser { workspaceAddUser( @@ -479,7 +491,6 @@ func (c *Client) RemoveWorkspaceUser(workspaceId, email string) (*Workspace, err func (c *Client) UpdateDeployment(deploymentId, jsonPayload string) (*Deployment, error) { request := fmt.Sprintf(deploymentUpdateRequest, deploymentId, jsonPayload) - fmt.Println(request) response, err := c.QueryHouston(request) if err != nil { return nil, errors.Wrap(err, "UpdateDeployment Failed") @@ -487,3 +498,14 @@ func (c *Client) UpdateDeployment(deploymentId, jsonPayload string) (*Deployment return response.Data.UpdateDeployment, nil } + +func (c *Client) UpdateWorkspace(workspaceId, jsonPayload string) (*Workspace, error) { + request := fmt.Sprintf(workspaceUpdateRequest, workspaceId, jsonPayload) + + response, err := c.QueryHouston(request) + if err != nil { + return nil, errors.Wrap(err, "UpdateWorkspace Failed") + } + + return response.Data.UpdateWorkspace, nil +} diff --git a/houston/types.go b/houston/types.go index 7288de216..59f84a99c 100644 --- a/houston/types.go +++ b/houston/types.go @@ -16,6 +16,7 @@ type HoustonResponse struct { GetUsers []User `json:"users,omitempty"` GetWorkspace []Workspace `json:"workspaces,omitempty"` UpdateDeployment *Deployment `json:"updateDeployment,omitempty` + UpdateWorkspace *Workspace `json:updateWorkspace,omitempty` } `json:"data"` Errors []Error `json:"errors,omitempty"` } diff --git a/messages/messages.go b/messages/messages.go index 83c16d1cc..c5db28739 100644 --- a/messages/messages.go +++ b/messages/messages.go @@ -60,6 +60,7 @@ var ( HOUSTON_WORKSPACE_DELETE_SUCCESS = "Succesfully deleted %s (%s)\n" HOUSTON_WORKSPACE_USER_ADD_SUCCESS = "Successfully added user %s from workspace (%s)\n" HOUSTON_WORKSPACE_USER_REMOVE_SUCCESS = "Successfully removed user %s from workspace (%s)\n" + HOUSTON_WORKSPACE_UPDATE_SUCCESS = "Successfully updated workspace %s" HOUSTON_USER_CREATE_SUCCESS = "Successfully created user (%s) with email %s\n" INPUT_PASSWORD = "Password: " diff --git a/workspace/workspace.go b/workspace/workspace.go index 692e0fcda..4a429fd9c 100644 --- a/workspace/workspace.go +++ b/workspace/workspace.go @@ -6,6 +6,7 @@ import ( "github.com/astronomerio/astro-cli/houston" "github.com/astronomerio/astro-cli/messages" "github.com/astronomerio/astro-cli/pkg/httputil" + "github.com/astronomerio/astro-cli/pkg/jsonstr" ) var ( @@ -48,3 +49,17 @@ func Delete(uuid string) error { fmt.Printf(messages.HOUSTON_WORKSPACE_DELETE_SUCCESS, ws.Label, ws.Uuid) return nil } + +// Update an astronomer workspace +func Update(workspaceId string, args map[string]string) error { + s := jsonstr.MapToJsonObjStr(args) + + ws, err := api.UpdateWorkspace(workspaceId, s) + if err != nil { + return err + } + + fmt.Printf(messages.HOUSTON_WORKSPACE_UPDATE_SUCCESS, ws.Uuid) + + return nil +}