-
Notifications
You must be signed in to change notification settings - Fork 46
/
CreateGroupCommand.java
78 lines (70 loc) · 3.04 KB
/
CreateGroupCommand.java
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
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloudsearch.samples;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.api.services.cloudidentity.v1beta1.CloudIdentity;
import com.google.api.services.cloudidentity.v1beta1.model.EntityKey;
import com.google.api.services.cloudidentity.v1beta1.model.Group;
import com.google.api.services.cloudidentity.v1beta1.model.Operation;
import java.util.Collections;
@Parameters(commandDescription = "Create a group")
class CreateGroupCommand implements Runnable {
@Parameter(names = "--id-source", required = true,
description = "Identity source ID")
private String idSource;
@Parameter(names = "--group-id", required = true, description = "Group ID")
private String groupId;
@Parameter(names = "--display-name", description = "Group display name")
private String groupName;
public void run() {
// [START cloud_identity_create_group]
String namespace = "identitysources/" + idSource;
Group group = new Group()
.setGroupKey(new EntityKey().setNamespace(namespace).setId(groupId))
.setDescription("Demo group")
.setDisplayName(groupName)
.setLabels(Collections.singletonMap("system/groups/external", ""))
.setParent(namespace);
try {
CloudIdentity service = Utils.buildCloudIdentityService();
Operation createOperation = service.groups().create(group).execute();
if (createOperation.getDone()) {
// Note: The response contains the data for a Group object, but as
// individual fields. To convert to a Group instance, either populate
// the fields individually or serialize & deserialize to/from JSON.
//
// Example:
// String json = service.getJsonFactory().toString(response);
// Group createdGroup = service.getObjectParser()
// .parseAndClose(new StringReader(json), Group.class);
System.out.printf("Group: %s\n",
createOperation.getResponse().toString());
} else {
// Handle case where operation not yet complete, poll for
// completion. API is currently synchronous and all operations return
// as completed.
// [START_EXCLUDE]
System.out.printf("Response: %s\n", createOperation.toPrettyString());
// [END_EXCLUDE]
}
} catch (Exception e) {
System.err.printf("Unable to create group: %s", e.getMessage());
e.printStackTrace(System.err);
}
// [END cloud_identity_create_group]
}
}