public static void main(String... args) { String actionName = args.length > 0 ? args[0].toLowerCase() : DEFAULT_ACTION; ResourceManagerAction action = ACTIONS.get(actionName); if (action == null) { StringBuilder actionAndParams = new StringBuilder(); for (Map.Entry<String, ResourceManagerAction> entry : ACTIONS.entrySet()) { addUsage(entry.getKey(), entry.getValue(), actionAndParams); actionAndParams.append('|'); } actionAndParams.setLength(actionAndParams.length() - 1); System.out.printf( "Usage: %s [%s]%n", ResourceManagerExample.class.getSimpleName(), actionAndParams); return; } // If you want to access a local Resource Manager emulator (after creating and starting the // LocalResourceManagerHelper), use the following code instead: // ResourceManager resourceManager = LocalResourceManagerHelper.getOptions().getService(); ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService(); args = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[] {}; if (args.length < action.getRequiredParams().length) { StringBuilder usage = new StringBuilder(); usage.append("Usage: "); addUsage(actionName, action, usage); System.out.println(usage); } else { action.run(resourceManager, args); } } }
public static void main(String... args) { // Create Resource Manager service object. // By default, credentials are inferred from the runtime environment. ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService(); String projectId = "my-globally-unique-project-id"; // Change to a unique project ID. // Get a project from the server. Project project = resourceManager.get(projectId); if (project == null) { // Create a project. project = resourceManager.create(ProjectInfo.newBuilder(projectId).build()); } System.out.println("Got project " + project.getProjectId() + " from the server."); } }
public static void main(String... args) { // Create Resource Manager service object // By default, credentials are inferred from the runtime environment. ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService(); // Get a project from the server Project project = resourceManager.get("some-project-id"); // Use an existing project's ID // Update a project if (project != null) { Project newProject = project.toBuilder().addLabel("launch-status", "in-development").build().replace(); System.out.println( "Updated the labels of project " + newProject.getProjectId() + " to be " + newProject.getLabels()); } // List all the projects you have permission to view. System.out.println("Projects I can view:"); for (Project currentProject : resourceManager.list().iterateAll()) { System.out.println(currentProject.getProjectId()); } } }
public static void main(String... args) { // Create Resource Manager service object // By default, credentials are inferred from the runtime environment. ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService(); // Get a project from the server String projectId = "some-project-id"; // Use an existing project's ID Project project = resourceManager.get(projectId); // Get the project's policy Policy policy = project.getPolicy(); // Add a viewer Policy.Builder modifiedPolicy = policy.toBuilder(); Identity newViewer = Identity.user("<insert user's email address here>"); modifiedPolicy.addIdentity(Role.viewer(), newViewer); // Write policy Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build()); // Print policy System.out.printf("Updated policy for %s: %n%s%n", projectId, updatedPolicy); } }