1
1
package com .datastax .astra .shell .cmd .iam ;
2
2
3
3
import java .util .HashMap ;
4
+ import java .util .List ;
4
5
import java .util .Map ;
5
6
import java .util .Optional ;
7
+ import java .util .stream .Collectors ;
6
8
9
+ import com .datastax .astra .sdk .organizations .OrganizationsClient ;
7
10
import com .datastax .astra .sdk .organizations .domain .Role ;
11
+ import com .datastax .astra .sdk .organizations .domain .User ;
12
+ import com .datastax .astra .sdk .utils .IdUtils ;
8
13
import com .datastax .astra .shell .ExitCode ;
9
14
import com .datastax .astra .shell .ShellContext ;
10
15
import com .datastax .astra .shell .cmd .BaseCommand ;
@@ -88,7 +93,8 @@ public static ExitCode listUsers(BaseCommand cmd) {
88
93
}
89
94
90
95
/**
91
- * Show Role details
96
+ * Show Role details.
97
+ *
92
98
* @param cmd
93
99
* command
94
100
* @param role
@@ -103,7 +109,7 @@ public static ExitCode showRole(BaseCommand cmd, String role) {
103
109
.getApiDevopsOrganizations ()
104
110
.findRoleByName (role );
105
111
106
- if (!optRole .isPresent ()) {
112
+ if (!optRole .isPresent () && IdUtils . isUUID ( role ) ) {
107
113
optRole = ShellContext
108
114
.getInstance ()
109
115
.getApiDevopsOrganizations ()
@@ -122,18 +128,25 @@ public static ExitCode showRole(BaseCommand cmd, String role) {
122
128
sht .addPropertyRow ("Name" , r .getName ());
123
129
sht .addPropertyRow ("Description" , r .getPolicy ().getDescription ());
124
130
sht .addPropertyRow ("Effect" , r .getPolicy ().getEffect ());
125
- sht .addPropertyListRows ("Resources" , r .getPolicy ().getResources ());
126
- sht .addPropertyListRows ("Actions" , r .getPolicy ().getActions ());
127
131
switch (cmd .getFormat ()) {
128
- case human :
129
- sht .show ();
132
+ case csv :
133
+ sht .addPropertyRow ("Resources" , r .getPolicy ().getResources ().toString ());
134
+ sht .addPropertyRow ("Actions" , r .getPolicy ().getActions ().toString ());
135
+ ShellPrinter .printShellTable (sht , cmd .getFormat ());
136
+ break ;
130
137
case json :
131
138
LoggerShell .json (new JsonOutput (ExitCode .SUCCESS , "role show " + role , r ));
132
- case csv :
139
+ break ;
140
+ case human :
133
141
default :
142
+ sht .addPropertyListRows ("Resources" , r .getPolicy ().getResources ());
143
+ sht .addPropertyListRows ("Actions" , r .getPolicy ().getActions ());
144
+ ShellPrinter .printShellTable (sht , cmd .getFormat ());
134
145
break ;
135
146
}
136
147
148
+
149
+
137
150
} catch (RuntimeException e ) {
138
151
cmd .outputError (ExitCode .INTERNAL_ERROR ,"Cannot show role, technical error " + e .getMessage ());
139
152
return ExitCode .INTERNAL_ERROR ;
@@ -142,5 +155,147 @@ public static ExitCode showRole(BaseCommand cmd, String role) {
142
155
return ExitCode .SUCCESS ;
143
156
}
144
157
158
+ /**
159
+ * Show User details.
160
+ *
161
+ * @param cmd
162
+ * command
163
+ * @param user
164
+ * user email
165
+ * @return
166
+ * exit code
167
+ */
168
+ public static ExitCode showUser (BaseCommand cmd , String user ) {
169
+ try {
170
+ Optional <User > optUser = ShellContext
171
+ .getInstance ()
172
+ .getApiDevopsOrganizations ()
173
+ .findUserByEmail (user );
174
+
175
+ if (!optUser .isPresent () && IdUtils .isUUID (user )) {
176
+ optUser = ShellContext
177
+ .getInstance ()
178
+ .getApiDevopsOrganizations ()
179
+ .user (user )
180
+ .find ();
181
+ }
182
+
183
+ if (!optUser .isPresent ()) {
184
+ cmd .outputError (ExitCode .NOT_FOUND , "User '" + user + "' has not been found." );
185
+ return ExitCode .NOT_FOUND ;
186
+ }
187
+
188
+ User r = optUser .get ();
189
+ ShellTable sht = ShellTable .propertyTable (15 , 40 );
190
+ sht .addPropertyRow ("Identifier" , r .getUserId ());
191
+ sht .addPropertyRow ("Email" , r .getEmail ());
192
+ sht .addPropertyRow ("Status" , r .getStatus ().name ());
193
+
194
+ List <String > roleNames = r .getRoles ()
195
+ .stream ()
196
+ .map (Role ::getName )
197
+ .collect (Collectors .toList ());
198
+
199
+ switch (cmd .getFormat ()) {
200
+ case csv :
201
+ sht .addPropertyRow ("Roles" , roleNames .toString ());
202
+ ShellPrinter .printShellTable (sht , cmd .getFormat ());
203
+ break ;
204
+ case json :
205
+ LoggerShell .json (new JsonOutput (ExitCode .SUCCESS , "user show " + user , r ));
206
+ break ;
207
+ case human :
208
+ default :
209
+ sht .addPropertyListRows ("Roles" , roleNames );
210
+ ShellPrinter .printShellTable (sht , cmd .getFormat ());
211
+ break ;
212
+ }
213
+
214
+ } catch (RuntimeException e ) {
215
+ cmd .outputError (ExitCode .INTERNAL_ERROR ,"Cannot show user, technical error " + e .getMessage ());
216
+ return ExitCode .INTERNAL_ERROR ;
217
+ }
218
+
219
+ return ExitCode .SUCCESS ;
220
+ }
221
+
222
+ /**
223
+ * Invite User.
224
+ *
225
+ * @param cmd
226
+ * command
227
+ * @param user
228
+ * user email
229
+ * @param role
230
+ * target role for the user
231
+ * @return
232
+ * exit code
233
+ */
234
+ public static ExitCode inviteUser (BaseCommand cmd , String user , String role ) {
235
+ try {
236
+ OrganizationsClient oc = ShellContext .getInstance ().getApiDevopsOrganizations ();
237
+ Optional <User > optUser = oc .findUserByEmail (user );
238
+
239
+ if (optUser .isPresent ()) {
240
+ cmd .outputWarning (ExitCode .ALREADY_EXIST , "User '" + user + "' already exist in the organization." );
241
+ return ExitCode .ALREADY_EXIST ;
242
+ }
243
+
244
+ Optional <Role > optRole = oc .findRoleByName (role );
245
+ if (!optRole .isPresent () && IdUtils .isUUID (role )) {
246
+ optRole = oc .role (role ).find ();
247
+ }
248
+
249
+ if (!optRole .isPresent ()) {
250
+ cmd .outputError (ExitCode .NOT_FOUND , "Role '" + role + "' has not been found" );
251
+ return ExitCode .NOT_FOUND ;
252
+ }
253
+
254
+ oc .inviteUser (user , optRole .get ().getId ());
255
+
256
+ cmd .outputSuccess (role );
257
+
258
+ } catch (RuntimeException e ) {
259
+ cmd .outputError (ExitCode .INTERNAL_ERROR ,"Cannot invite user, technical error " + e .getMessage ());
260
+ return ExitCode .INTERNAL_ERROR ;
261
+ }
262
+ return ExitCode .SUCCESS ;
263
+ }
145
264
265
+ /**
266
+ * Delete a user if exist.
267
+ *
268
+ * @param cmd
269
+ * current command options
270
+ * @param user
271
+ * user email of technial identifier
272
+ * @return
273
+ * status
274
+ */
275
+ public static ExitCode deleteUser (BaseCommand cmd , String user ) {
276
+ try {
277
+ OrganizationsClient oc = ShellContext .getInstance ().getApiDevopsOrganizations ();
278
+
279
+ Optional <User > optUser = oc .findUserByEmail (user );
280
+
281
+ if (!optUser .isPresent () && IdUtils .isUUID (user )) {
282
+ optUser = oc .user (user ).find ();
283
+ }
284
+
285
+ if (!optUser .isPresent ()) {
286
+ cmd .outputError (ExitCode .NOT_FOUND , "User '" + user + "' has not been found." );
287
+ return ExitCode .NOT_FOUND ;
288
+ }
289
+
290
+ oc .user (optUser .get ().getUserId ()).delete ();
291
+ cmd .outputSuccess ("Deleting user '" + user + "' (async operation)" );
292
+
293
+ } catch (RuntimeException e ) {
294
+ cmd .outputError (ExitCode .INTERNAL_ERROR ,"Cannot delete user, technical error " + e .getMessage ());
295
+ return ExitCode .INTERNAL_ERROR ;
296
+ }
297
+
298
+ return ExitCode .SUCCESS ;
299
+ }
300
+
146
301
}
0 commit comments