• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * EDITING INSTRUCTIONS
19  * This file is referenced in READMEs and javadoc. Any change to this file should be reflected in
20  * the project's READMEs and package-info.java.
21  */
22 
23 package com.google.cloud.examples.resourcemanager.snippets;
24 
25 import com.google.cloud.resourcemanager.Project;
26 import com.google.cloud.resourcemanager.ProjectInfo;
27 import com.google.cloud.resourcemanager.ResourceManager;
28 import com.google.cloud.resourcemanager.ResourceManagerOptions;
29 
30 /**
31  * A snippet for Google Cloud Resource Manager showing how to create a project if it does not exist.
32  */
33 public class GetOrCreateProject {
34 
main(String... args)35   public static void main(String... args) {
36     // Create Resource Manager service object.
37     // By default, credentials are inferred from the runtime environment.
38     ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
39 
40     String projectId = "my-globally-unique-project-id"; // Change to a unique project ID.
41     // Get a project from the server.
42     Project project = resourceManager.get(projectId);
43     if (project == null) {
44       // Create a project.
45       project = resourceManager.create(ProjectInfo.newBuilder(projectId).build());
46     }
47     System.out.println("Got project " + project.getProjectId() + " from the server.");
48   }
49 }
50