README.md
1# Google Resource Manager API Client for Java
2
3Java idiomatic client for [Resource Manager API][product-docs].
4
5[![Maven][maven-version-image]][maven-version-link]
6![Stability][stability-image]
7
8- [Product Documentation][product-docs]
9- [Client Library Documentation][javadocs]
10
11
12## Quickstart
13
14
15If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file:
16
17```xml
18<dependencyManagement>
19 <dependencies>
20 <dependency>
21 <groupId>com.google.cloud</groupId>
22 <artifactId>libraries-bom</artifactId>
23 <version>26.14.0</version>
24 <type>pom</type>
25 <scope>import</scope>
26 </dependency>
27 </dependencies>
28</dependencyManagement>
29
30<dependencies>
31 <dependency>
32 <groupId>com.google.cloud</groupId>
33 <artifactId>google-cloud-resourcemanager</artifactId>
34 </dependency>
35```
36
37If you are using Maven without the BOM, add this to your dependencies:
38
39<!-- {x-version-update-start:google-cloud-resourcemanager:released} -->
40
41```xml
42<dependency>
43 <groupId>com.google.cloud</groupId>
44 <artifactId>google-cloud-resourcemanager</artifactId>
45 <version>1.19.0</version>
46</dependency>
47```
48
49If you are using Gradle without BOM, add this to your dependencies:
50
51```Groovy
52implementation 'com.google.cloud:google-cloud-resourcemanager:1.19.0'
53```
54
55If you are using SBT, add this to your dependencies:
56
57```Scala
58libraryDependencies += "com.google.cloud" % "google-cloud-resourcemanager" % "1.19.0"
59```
60<!-- {x-version-update-end} -->
61
62## Authentication
63
64See the [Authentication][authentication] section in the base directory's README.
65
66## Authorization
67
68The client application making API calls must be granted [authorization scopes][auth-scopes] required for the desired Resource Manager API APIs, and the authenticated principal must have the [IAM role(s)][predefined-iam-roles] required to access GCP resources using the Resource Manager API API calls.
69
70## Getting Started
71
72### Prerequisites
73
74You will need a [Google Cloud Platform Console][developer-console] project with the Resource Manager API [API enabled][enable-api].
75
76[Follow these instructions][create-project] to get your project set up. You will also need to set up the local development environment by
77[installing the Google Cloud Command Line Interface][cloud-cli] and running the following commands in command line:
78`gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`.
79
80### Installation and setup
81
82You'll need to obtain the `google-cloud-resourcemanager` library. See the [Quickstart](#quickstart) section
83to add `google-cloud-resourcemanager` as a dependency in your code.
84
85## About Resource Manager API
86
87
88[Resource Manager API][product-docs] enables you to programmatically manage resources by project, folder, and organization.
89
90See the [Resource Manager API client library docs][javadocs] to learn how to
91use this Resource Manager API Client Library.
92
93
94#### Creating an authorized service object
95To make authenticated requests to Google Cloud Resource Manager, you must create a service object
96with Google Cloud SDK credentials. You can then make API calls by calling methods on the Resource
97Manager service object. The simplest way to authenticate is to use
98[Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
99These credentials are automatically inferred from your environment, so you only need the following
100code to create your service object:
101
102```java
103import com.google.cloud.resourcemanager.ResourceManager;
104import com.google.cloud.resourcemanager.ResourceManagerOptions;
105
106ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
107```
108
109#### Getting a specific project
110You can load a project if you know its project ID and have read permissions to the project.
111To get a project, add the following import at the top of your file:
112
113```java
114import com.google.cloud.resourcemanager.Project;
115```
116
117Then use the following code to get the project:
118
119```java
120String projectId = "my-globally-unique-project-id"; // Change to a unique project ID
121Project project = resourceManager.get(projectId);
122```
123
124#### Creating a project
125All you need to create a project is a globally unique project ID. You can also optionally attach a
126non-unique name and labels to your project. Read more about naming guidelines for project IDs,
127names, and labels [here](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects).
128To create a project, add the following imports at the top of your file:
129
130```java
131import com.google.cloud.resourcemanager.Project;
132import com.google.cloud.resourcemanager.ProjectInfo;
133```
134
135Then add the following code to create a project (be sure to change `projectId` to your own unique
136project ID).
137
138```java
139String projectId = "my-globally-unique-project-id"; // Change to a unique project ID
140Project project = resourceManager.create(ProjectInfo.newBuilder(projectId).build());
141```
142
143Note that the return value from `create` is a `Project` that includes additional read-only
144information, like creation time, project number, and lifecycle state. Read more about these fields
145on the [Projects page](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects).
146`Project`, a subclass of `ProjectInfo`, adds a layer of service-related functionality over
147`ProjectInfo`.
148
149#### Editing a project
150To edit a project, create a new `ProjectInfo` object and pass it in to the `Project.replace` method.
151For example, to add a label to a project to denote that it's launch status is "in development", add
152the following code:
153
154```java
155Project newProject = project.toBuilder()
156 .addLabel("launch-status", "in-development")
157 .build()
158 .replace();
159```
160
161Note that the values of the project you pass in to `replace` overwrite the server's values for
162non-read-only fields, namely `projectName` and `labels`. For example, if you create a project with
163`projectName` "some-project-name" and subsequently call replace using a `ProjectInfo` object that
164didn't set the `projectName`, then the server will unset the project's name. The server ignores any
165attempted changes to the read-only fields `projectNumber`, `lifecycleState`, and `createTime`.
166The `projectId` cannot change.
167
168#### Listing all projects
169Suppose that we want a list of all projects for which we have read permissions. Add the following
170import:
171
172```java
173import java.util.Iterator;
174```
175
176Then add the following code to print a list of projects you can view:
177
178```java
179Iterator<Project> projectIterator = resourceManager.list().iterateAll().iterator();
180System.out.println("Projects I can view:");
181while (projectIterator.hasNext()) {
182 System.out.println(projectIterator.next().getProjectId());
183}
184```
185
186#### Managing IAM Policies
187You can edit [Google Cloud IAM](https://cloud.google.com/iam/) (Identity and Access Management)
188policies on the project-level using this library as well. We recommend using the read-modify-write
189pattern to make policy changes. This entails reading the project's current policy, updating it
190locally, and then sending the modified policy for writing, as shown in the snippet below. First,
191add these imports:
192
193```java
194import com.google.cloud.Identity;
195import com.google.cloud.Policy;
196import com.google.cloud.Role;
197```
198
199Assuming you have completed the steps above to create the `ResourceManager` service object and load
200a project from the server, you just need to add the following code:
201
202```java
203// Get the project's policy
204Policy policy = project.getPolicy();
205
206// Add a viewer
207Policy.Builder modifiedPolicy = policy.toBuilder();
208Identity newViewer = Identity.user("<insert user's email address here>");
209modifiedPolicy.addIdentity(Role.viewer(), newViewer);
210
211// Write policy
212Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build());
213```
214
215Note that the policy you pass in to `replacePolicy` overwrites the original policy. For example, if
216the original policy has two bindings and you call `replacePolicy` with a new policy containing only
217one binding, the two original bindings are lost.
218
219#### Complete source code
220
221We put together all the code shown above into three programs. The programs assume that you are
222running from your own desktop and used the Google Cloud SDK to authenticate yourself.
223
224The first program creates a project if it does not exist. Complete source code can be found at
225[GetOrCreateProject.java](https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/resourcemanager/snippets/GetOrCreateProject.java).
226
227The second program updates a project if it exists and lists all projects the user has permission to
228view. Complete source code can be found at
229[UpdateAndListProjects.java](https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/resourcemanager/snippets/UpdateAndListProjects.java).
230
231The third program modifies the IAM policy associated with a project using the read-modify-write
232pattern. Complete source code can be found at
233[ModifyPolicy.java](https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/resourcemanager/snippets/ModifyPolicy.java)
234
235
236
237
238
239## Troubleshooting
240
241To get help, follow the instructions in the [shared Troubleshooting document][troubleshooting].
242
243## Transport
244
245Resource Manager API uses HTTP/JSON for the transport layer.
246
247## Supported Java Versions
248
249Java 8 or above is required for using this client.
250
251Google's Java client libraries,
252[Google Cloud Client Libraries][cloudlibs]
253and
254[Google Cloud API Libraries][apilibs],
255follow the
256[Oracle Java SE support roadmap][oracle]
257(see the Oracle Java SE Product Releases section).
258
259### For new development
260
261In general, new feature development occurs with support for the lowest Java
262LTS version covered by Oracle's Premier Support (which typically lasts 5 years
263from initial General Availability). If the minimum required JVM for a given
264library is changed, it is accompanied by a [semver][semver] major release.
265
266Java 11 and (in September 2021) Java 17 are the best choices for new
267development.
268
269### Keeping production systems current
270
271Google tests its client libraries with all current LTS versions covered by
272Oracle's Extended Support (which typically lasts 8 years from initial
273General Availability).
274
275#### Legacy support
276
277Google's client libraries support legacy versions of Java runtimes with long
278term stable libraries that don't receive feature updates on a best efforts basis
279as it may not be possible to backport all patches.
280
281Google provides updates on a best efforts basis to apps that continue to use
282Java 7, though apps might need to upgrade to current versions of the library
283that supports their JVM.
284
285#### Where to find specific information
286
287The latest versions and the supported Java versions are identified on
288the individual GitHub repository `github.com/GoogleAPIs/java-SERVICENAME`
289and on [google-cloud-java][g-c-j].
290
291## Versioning
292
293
294This library follows [Semantic Versioning](http://semver.org/).
295
296
297
298## Contributing
299
300
301Contributions to this library are always welcome and highly encouraged.
302
303See [CONTRIBUTING][contributing] for more information how to get started.
304
305Please note that this project is released with a Contributor Code of Conduct. By participating in
306this project you agree to abide by its terms. See [Code of Conduct][code-of-conduct] for more
307information.
308
309
310## License
311
312Apache 2.0 - See [LICENSE][license] for more information.
313
314## CI Status
315
316Java Version | Status
317------------ | ------
318Java 8 | [![Kokoro CI][kokoro-badge-image-2]][kokoro-badge-link-2]
319Java 8 OSX | [![Kokoro CI][kokoro-badge-image-3]][kokoro-badge-link-3]
320Java 8 Windows | [![Kokoro CI][kokoro-badge-image-4]][kokoro-badge-link-4]
321Java 11 | [![Kokoro CI][kokoro-badge-image-5]][kokoro-badge-link-5]
322
323Java is a registered trademark of Oracle and/or its affiliates.
324
325[product-docs]: https://cloud.google.com/resource-manager
326[javadocs]: https://cloud.google.com/java/docs/reference/google-cloud-resourcemanager/latest/overview
327[kokoro-badge-image-1]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java7.svg
328[kokoro-badge-link-1]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java7.html
329[kokoro-badge-image-2]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8.svg
330[kokoro-badge-link-2]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8.html
331[kokoro-badge-image-3]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-osx.svg
332[kokoro-badge-link-3]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-osx.html
333[kokoro-badge-image-4]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-win.svg
334[kokoro-badge-link-4]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java8-win.html
335[kokoro-badge-image-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java11.svg
336[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/google-cloud-java/java11.html
337[stability-image]: https://img.shields.io/badge/stability-stable-green
338[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-resourcemanager.svg
339[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-resourcemanager/1.18.0
340[authentication]: https://github.com/googleapis/google-cloud-java#authentication
341[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
342[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
343[iam-policy]: https://cloud.google.com/iam/docs/overview#cloud-iam-policy
344[developer-console]: https://console.developers.google.com/
345[create-project]: https://cloud.google.com/resource-manager/docs/creating-managing-projects
346[cloud-cli]: https://cloud.google.com/cli
347[troubleshooting]: https://github.com/googleapis/google-cloud-java/blob/main/TROUBLESHOOTING.md
348[contributing]: https://github.com/googleapis/google-cloud-java/blob/main/CONTRIBUTING.md
349[code-of-conduct]: https://github.com/googleapis/google-cloud-java/blob/main/CODE_OF_CONDUCT.md#contributor-code-of-conduct
350[license]: https://github.com/googleapis/google-cloud-java/blob/main/LICENSE
351
352[enable-api]: https://console.cloud.google.com/flows/enableapi?apiid=cloudresourcemanager.googleapis.com
353[libraries-bom]: https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM
354[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
355
356[semver]: https://semver.org/
357[cloudlibs]: https://cloud.google.com/apis/docs/client-libraries-explained
358[apilibs]: https://cloud.google.com/apis/docs/client-libraries-explained#google_api_client_libraries
359[oracle]: https://www.oracle.com/java/technologies/java-se-support-roadmap.html
360[g-c-j]: http://github.com/googleapis/google-cloud-java
361