-
Notifications
You must be signed in to change notification settings - Fork 67
/
user.go
103 lines (85 loc) · 2.4 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package workspace
import (
"fmt"
"github.com/astronomer/astro-cli/houston"
"github.com/astronomer/astro-cli/pkg/printutil"
)
var (
utab = printutil.Table{
Padding: []int{30, 50, 50},
Header: []string{"NAME", "WORKSPACE ID", "EMAIL"},
}
)
// Add a user to a workspace with specified role
func Add(workspaceId, email, role string) error {
req := houston.Request{
Query: houston.WorkspaceUserAddRequest,
Variables: map[string]interface{}{"workspaceId": workspaceId, "email": email, "role": role},
}
r, err := req.Do()
if err != nil {
return err
}
w := r.Data.AddWorkspaceUser
tab := printutil.Table{
Padding: []int{44, 50},
DynamicPadding: true,
Header: []string{"NAME", "WORKSPACE ID", "EMAIL", "ROLE"},
}
tab.AddRow([]string{w.Label, w.Id, email, role}, false)
tab.SuccessMsg = fmt.Sprintf("Successfully added %s to %s", email, w.Label)
tab.Print()
return nil
}
// Remove a user from a workspace
func Remove(workspaceId, email string) error {
req := houston.Request{
Query: houston.WorkspaceUserRemoveRequest,
Variables: map[string]interface{}{"workspaceId": workspaceId, "email": email},
}
r, err := req.Do()
if err != nil {
return err
}
w := r.Data.RemoveWorkspaceUser
utab.AddRow([]string{w.Label, w.Id, email}, false)
utab.SuccessMsg = "Successfully removed user from workspace"
utab.Print()
return nil
}
// ListRoles print users and roles from a workspace
func ListRoles(workspaceId string) error {
req := houston.Request{
Query: houston.WorkspacesGetRequest,
Variables: map[string]interface{}{"workspaceId": workspaceId},
}
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
}
func UpdateRole(workspaceId, email, role string) error {
req := houston.Request{
Query: houston.WorkspaceUserUpdateRequest,
Variables: map[string]interface{}{"workspaceUuid": workspaceId, "email": email, "role": role},
}
r, err := req.Do()
if err != nil {
return err
}
newRole := r.Data.WorkspaceUpdateUserRole
fmt.Printf("Role has been changed from %s to %s for user %s", role, newRole, email)
return nil
}