Skip to content

Commit 85ef6f2

Browse files
awkorendzlier-gcp
authored andcommitted
Add IAM doc code snippets (GoogleCloudPlatform#1172)
* Add IAM quickstart * Add service accounts * Add service account keys and tests * Remove unused imports * Add grantable roles * Lint code * Change project env variable for tests * Update project env variable for tests * Reformat license * Add output verification to tests * Fix tests * Lint license and tests, update pom version
1 parent 68f12df commit 85ef6f2

File tree

9 files changed

+603
-0
lines changed

9 files changed

+603
-0
lines changed

iam/api-client/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Cloud Identity & Access Management Samples
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iam/api-client/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
[Google Cloud Identity & Access Management](https://cloud.google.com/iam/) (IAM)
7+
lets administrators authorize who can take action on specific resources.
8+
These sample applications demonstrate how to interact with Cloud IAM using
9+
the Google API Client Library for Java.
10+
11+
## Quickstart
12+
13+
Install [Maven](http://maven.apache.org/).
14+
15+
Build the project with:
16+
17+
```xml
18+
mvn clean package
19+
```
20+
21+
Run the Quickstart, which lists roles in a project:
22+
23+
```xml
24+
mvn exec:java
25+
```

iam/api-client/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!--
2+
Copyright 2018 Google Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
15+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
16+
<modelVersion>4.0.0</modelVersion>
17+
<groupId>com.google.iam.snippets</groupId>
18+
<artifactId>iam-snippets</artifactId>
19+
<packaging>jar</packaging>
20+
<version>1.0</version>
21+
<name>iam-snippets</name>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.10</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.apis</groupId>
41+
<artifactId>google-api-services-iam</artifactId>
42+
<version>v1-rev247-1.23.0</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>commons-cli</groupId>
46+
<artifactId>commons-cli</artifactId>
47+
<version>1.4</version>
48+
</dependency>
49+
50+
<!-- Test dependencies -->
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<version>4.12</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.google.truth</groupId>
59+
<artifactId>truth</artifactId>
60+
<version>0.40</version>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.codehaus.mojo</groupId>
69+
<artifactId>exec-maven-plugin</artifactId>
70+
<version>1.4.0</version>
71+
<configuration>
72+
<mainClass>com.google.iam.snippets.GrantableRoles</mainClass>
73+
</configuration>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2018 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.google.iam.snippets;
17+
18+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
19+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
20+
import com.google.api.client.json.jackson2.JacksonFactory;
21+
import com.google.api.services.iam.v1.Iam;
22+
import com.google.api.services.iam.v1.IamScopes;
23+
import com.google.api.services.iam.v1.model.QueryGrantableRolesRequest;
24+
import com.google.api.services.iam.v1.model.QueryGrantableRolesResponse;
25+
import com.google.api.services.iam.v1.model.Role;
26+
import java.util.Collections;
27+
28+
public class GrantableRoles {
29+
30+
public static void main(String[] args) throws Exception {
31+
32+
GoogleCredential credential =
33+
GoogleCredential.getApplicationDefault()
34+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
35+
36+
Iam service =
37+
new Iam.Builder(
38+
GoogleNetHttpTransport.newTrustedTransport(),
39+
JacksonFactory.getDefaultInstance(),
40+
credential)
41+
.setApplicationName("grantable-roles")
42+
.build();
43+
44+
String fullResourceName = args[0];
45+
46+
// [START iam_view_grantable_roles]
47+
QueryGrantableRolesRequest request = new QueryGrantableRolesRequest();
48+
request.setFullResourceName(fullResourceName);
49+
50+
QueryGrantableRolesResponse response = service.roles().queryGrantableRoles(request).execute();
51+
52+
for (Role role : response.getRoles()) {
53+
System.out.println("Title: " + role.getTitle());
54+
System.out.println("Name: " + role.getName());
55+
System.out.println("Description: " + role.getDescription());
56+
System.out.println();
57+
}
58+
// [START iam_view_grantable_roles]
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2018 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// [START iam_quickstart]
17+
18+
package com.google.iam.snippets;
19+
20+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
21+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
22+
import com.google.api.client.json.jackson2.JacksonFactory;
23+
import com.google.api.services.iam.v1.Iam;
24+
import com.google.api.services.iam.v1.IamScopes;
25+
import com.google.api.services.iam.v1.model.ListRolesResponse;
26+
import com.google.api.services.iam.v1.model.Role;
27+
import java.util.Collections;
28+
import java.util.List;
29+
30+
public class Quickstart {
31+
32+
public static void main(String[] args) throws Exception {
33+
// Get credentials
34+
GoogleCredential credential =
35+
GoogleCredential.getApplicationDefault()
36+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
37+
38+
// Create the Cloud IAM service object
39+
Iam service =
40+
new Iam.Builder(
41+
GoogleNetHttpTransport.newTrustedTransport(),
42+
JacksonFactory.getDefaultInstance(),
43+
credential)
44+
.setApplicationName("quickstart")
45+
.build();
46+
47+
// Call the Cloud IAM Roles API
48+
ListRolesResponse respose = service.roles().list().execute();
49+
List<Role> roles = respose.getRoles();
50+
51+
// Process the response
52+
for (Role role : roles) {
53+
System.out.println("Title: " + role.getTitle());
54+
System.out.println("Name: " + role.getName());
55+
System.out.println("Description: " + role.getDescription());
56+
System.out.println();
57+
}
58+
}
59+
}
60+
// [END iam_quickstart]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* Copyright 2018 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.google.iam.snippets;
17+
18+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
19+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
20+
import com.google.api.client.json.jackson2.JacksonFactory;
21+
import com.google.api.services.iam.v1.Iam;
22+
import com.google.api.services.iam.v1.IamScopes;
23+
import com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest;
24+
import com.google.api.services.iam.v1.model.ServiceAccountKey;
25+
import java.io.IOException;
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
public class ServiceAccountKeys {
30+
31+
private final Iam service;
32+
33+
public ServiceAccountKeys() throws Exception {
34+
GoogleCredential credential =
35+
GoogleCredential.getApplicationDefault()
36+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
37+
38+
service =
39+
new Iam.Builder(
40+
GoogleNetHttpTransport.newTrustedTransport(),
41+
JacksonFactory.getDefaultInstance(),
42+
credential)
43+
.setApplicationName("service-account-keys")
44+
.build();
45+
}
46+
47+
// [START iam_create_key]
48+
public ServiceAccountKey createKey(String serviceAccountEmail) throws IOException {
49+
50+
ServiceAccountKey key =
51+
service
52+
.projects()
53+
.serviceAccounts()
54+
.keys()
55+
.create(
56+
"projects/-/serviceAccounts/" + serviceAccountEmail,
57+
new CreateServiceAccountKeyRequest())
58+
.execute();
59+
60+
System.out.println("Created key: " + key.getName());
61+
return key;
62+
}
63+
// [END iam_create_key]
64+
65+
// [START iam_list_keys]
66+
public List<ServiceAccountKey> listKeys(String serviceAccountEmail) throws IOException {
67+
68+
List<ServiceAccountKey> keys =
69+
service
70+
.projects()
71+
.serviceAccounts()
72+
.keys()
73+
.list("projects/-/serviceAccounts/" + serviceAccountEmail)
74+
.execute()
75+
.getKeys();
76+
77+
for (ServiceAccountKey key : keys) {
78+
System.out.println("Key: " + key.getName());
79+
}
80+
return keys;
81+
}
82+
// [END iam_list_keys]
83+
84+
// [START iam_delete_key]
85+
public void deleteKey(String fullKeyName) throws IOException {
86+
87+
service.projects().serviceAccounts().keys().delete(fullKeyName).execute();
88+
89+
System.out.println("Deleted key: " + fullKeyName);
90+
}
91+
// [END iam_delete_key]
92+
}

0 commit comments

Comments
 (0)