From 0e07e2f06417788908a421b577c998b809725441 Mon Sep 17 00:00:00 2001 From: Andrii Soldatenko Date: Wed, 8 May 2019 00:03:36 +0300 Subject: [PATCH] Add astro workspace user list --- Makefile | 10 ++++++++++ cmd/workspace.go | 6 +++++- houston/queries.go | 15 +++++++-------- houston/types.go | 13 +++++++++++-- workspace/user.go | 24 +++++++++++++++++++----- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 3b8ef5fa7..886055634 100644 --- a/Makefile +++ b/Makefile @@ -55,3 +55,13 @@ install: build uninstall: $(eval DESTDIR ?= $(GOBIN)) rm $(GOBIN)/$(OUTPUT) + +ifeq (debug,$(firstword $(MAKECMDGOALS))) + # use the rest as arguments for "debug" + DEBUG_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) + $(eval $(DEBUG_ARGS):;@:) +endif + +debug: + echo $(RUN_ARGS) + dlv debug github.com/astronomer/astro-cli -- $(DEBUG_ARGS) diff --git a/cmd/workspace.go b/cmd/workspace.go index e4c62e647..8ab67a5f8 100644 --- a/cmd/workspace.go +++ b/cmd/workspace.go @@ -209,5 +209,9 @@ func workspaceSwitch(cmd *cobra.Command, args []string) error { } func workspaceUserList(cmd *cobra.Command, args []string) error { - return workspace.ListRoles() + ws, err := coalesceWorkspace() + if err != nil { + return errors.Wrap(err, "failed to find a valid workspace") + } + return workspace.ListRoles(ws) } \ No newline at end of file diff --git a/houston/queries.go b/houston/queries.go index e95f40bd5..30daa4093 100644 --- a/houston/queries.go +++ b/houston/queries.go @@ -1,6 +1,5 @@ package houston - var ( AuthConfigGetRequest = ` query GetAuthConfig($redirect: String) { @@ -220,6 +219,13 @@ var ( active createdAt updatedAt + roleBindings { + role + user { + id + username + } + } } }` @@ -299,13 +305,6 @@ var ( updatedAt } }` - WorkspaceUserListRolesRequest = ` - query GetUserRoles( - ) { - getUserRoles( - ) { - } - }` DeploymentLogsGetRequest = ` query GetLogs( $deploymentId: Uuid! diff --git a/houston/types.go b/houston/types.go index 613bab5ea..432e41a0e 100644 --- a/houston/types.go +++ b/houston/types.go @@ -124,6 +124,14 @@ type User struct { // profile } +type RoleBinding struct { + Role string `json:"role"` + User struct { + Id string `json:"id"` + Username string `json:"username"` + } `json:"user"` +} + // Workspace contains all components of an Astronomer Workspace type Workspace struct { Id string `json:"id"` @@ -132,8 +140,9 @@ type Workspace struct { Active bool `json:"active"` Users []User `json:"users"` // groups - CreatedAt string `json:"createdAt"` - UpdatedAt string `json:"updatedAt"` + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + RoleBindings []RoleBinding `json:"roleBindings"` } // DeploymentLog contains all log related to deployment components diff --git a/workspace/user.go b/workspace/user.go index 06b2da339..5e33e282a 100644 --- a/workspace/user.go +++ b/workspace/user.go @@ -51,14 +51,28 @@ func Remove(workspaceId, email string) error { return nil } -func ListRoles() error { +func ListRoles(workspaceId string) error { req := houston.Request{ - Query: houston.WorkspaceUserListRolesRequest, - Variables: map[string]interface{}{}, + Query: houston.WorkspacesGetRequest, + Variables: map[string]interface{}{"workspaceId": workspaceId}, } - _, err := req.Do() + r, err := req.Do() + if err != nil { return err } + workspace := r.Data.GetWorkspaces[0] + + tab := printutil.Table{ + Padding: []int{44, 50}, + DynamicPadding: true, + Header: []string{"USERNAME", "ID", "ROLE"}, + } + for _, role := range workspace.RoleBindings { + var color bool + tab.AddRow([]string{role.User.Username, role.User.Id, role.Role}, color) + } + + tab.Print() return nil -} \ No newline at end of file +}