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.ResourceManager; 27 import com.google.cloud.resourcemanager.ResourceManagerOptions; 28 29 /** 30 * A snippet for Google Cloud Resource Manager showing how to update a project and list all projects 31 * the user has permission to view. 32 */ 33 public class UpdateAndListProjects { 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 // Get a project from the server 41 Project project = resourceManager.get("some-project-id"); // Use an existing project's ID 42 43 // Update a project 44 if (project != null) { 45 Project newProject = 46 project.toBuilder().addLabel("launch-status", "in-development").build().replace(); 47 System.out.println( 48 "Updated the labels of project " 49 + newProject.getProjectId() 50 + " to be " 51 + newProject.getLabels()); 52 } 53 54 // List all the projects you have permission to view. 55 System.out.println("Projects I can view:"); 56 for (Project currentProject : resourceManager.list().iterateAll()) { 57 System.out.println(currentProject.getProjectId()); 58 } 59 } 60 } 61