-
Notifications
You must be signed in to change notification settings - Fork 67
/
workspace.go
215 lines (172 loc) · 5.71 KB
/
workspace.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package cmd
import (
"fmt"
"github.com/astronomer/astro-cli/workspace"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
workspaceUpdateAttrs = []string{"label"}
createDesc string
workspaceRootCmd = &cobra.Command{
Use: "workspace",
Aliases: []string{"wo"},
Short: "Manage Astronomer workspaces",
Long: "Workspaces contain a group of Airflow Cluster Deployments. The creator of the workspace can invite other users into it",
}
workspaceListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List astronomer workspaces",
Long: "List astronomer workspaces",
RunE: workspaceList,
}
workspaceCreateCmd = &cobra.Command{
Use: "create WORKSPACE",
Aliases: []string{"cr"},
Short: "Create an astronomer workspaces",
Long: "Create an astronomer workspaces",
Args: cobra.ExactArgs(1),
RunE: workspaceCreate,
}
workspaceDeleteCmd = &cobra.Command{
Use: "delete WORKSPACE",
Aliases: []string{"de"},
Short: "Delete an astronomer workspace",
Long: "Delete an astronomer workspace",
Args: cobra.ExactArgs(1),
RunE: workspaceDelete,
}
workspaceSwitchCmd = &cobra.Command{
Use: "switch WORKSPACE",
Aliases: []string{"sw"},
Short: "Switch to a different astronomer workspace",
Long: "Switch to a different astronomer workspace",
Args: cobra.MaximumNArgs(1),
RunE: workspaceSwitch,
}
workspaceUpdateCmd = &cobra.Command{
Use: "update",
Aliases: []string{"up"},
Short: "Update an Astronomer workspace",
Long: "Update a workspace name, as well as users and roles assigned to a workspace",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) <= 1 {
return errors.New("must specify a workspace ID and at least one attribute to update.")
}
return updateArgValidator(args[1:], workspaceUpdateAttrs)
},
RunE: workspaceUpdate,
}
workspaceUserRootCmd = &cobra.Command{
Use: "user",
Short: "Manage workspace user resources",
Long: "Users can be added or removed from workspaces",
}
workspaceUserAddCmd = &cobra.Command{
Use: "add EMAIL",
Short: "Add a user to a workspace",
Long: "Add a user to a workspace",
Args: cobra.ExactArgs(1),
RunE: workspaceUserAdd,
}
workspaceUserRmCmd = &cobra.Command{
Use: "remove EMAIL",
Aliases: []string{"rm"},
Short: "Remove a user from a workspace",
Long: "Remove a user from a workspace",
Args: cobra.ExactArgs(1),
RunE: workspaceUserRm,
}
workspaceUserListCmd = &cobra.Command{
Use: "list",
Short: "Print list of user roles",
Long: "Print list of user roles",
RunE: workspaceUserList,
}
)
func init() {
// workspace root
RootCmd.AddCommand(workspaceRootCmd)
// workspace list
workspaceRootCmd.AddCommand(workspaceListCmd)
// workspace create
workspaceRootCmd.AddCommand(workspaceCreateCmd)
workspaceCreateCmd.Flags().StringVarP(&createDesc, "desc", "d", "", "description for your new workspace")
// workspace delete
workspaceRootCmd.AddCommand(workspaceDeleteCmd)
// workspace switch
workspaceRootCmd.AddCommand(workspaceSwitchCmd)
// workspace update
workspaceRootCmd.AddCommand(workspaceUpdateCmd)
// workspace user root
workspaceRootCmd.AddCommand(workspaceUserRootCmd)
// workspace user add
workspaceUserRootCmd.AddCommand(workspaceUserAddCmd)
workspaceUserAddCmd.PersistentFlags().StringVar(&workspaceId, "workspace-id", "", "workspace assigned to deployment")
// workspace user remove
workspaceUserRootCmd.AddCommand(workspaceUserRmCmd)
workspaceUserRmCmd.PersistentFlags().StringVar(&workspaceId, "workspace-id", "", "workspace assigned to deployment")
// workspace user list
workspaceUserRootCmd.AddCommand(workspaceUserListCmd)
}
func workspaceCreate(cmd *cobra.Command, args []string) error {
if len(createDesc) == 0 {
createDesc = "N/A"
}
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return workspace.Create(args[0], createDesc)
}
func workspaceList(cmd *cobra.Command, args []string) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return workspace.List()
}
func workspaceDelete(cmd *cobra.Command, args []string) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return workspace.Delete(args[0])
}
func workspaceUpdate(cmd *cobra.Command, args []string) error {
argsMap, err := argsToMap(args[1:])
if err != nil {
return err
}
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return workspace.Update(args[0], argsMap)
}
func workspaceUserAdd(cmd *cobra.Command, args []string) error {
ws, err := coalesceWorkspace()
if err != nil {
return errors.Wrap(err, "failed to find a valid workspace")
// fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag")
}
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return workspace.Add(ws, args[0])
}
func workspaceUserRm(cmd *cobra.Command, args []string) error {
ws, err := coalesceWorkspace()
if err != nil {
return errors.Wrap(err, "failed to find a valid workspace")
// fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag")
}
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return workspace.Remove(ws, args[0])
}
func workspaceSwitch(cmd *cobra.Command, args []string) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
id := ""
if len(args) == 1 {
id = args[0]
}
return workspace.Switch(id)
}
func workspaceUserList(cmd *cobra.Command, args []string) error {
fmt.Println("user list")
return nil
}