Search in sources :

Example 1 with ResourceManager

use of com.google.cloud.resourcemanager.ResourceManager in project google-cloud-java by GoogleCloudPlatform.

the class GetOrCreateProject method main.

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();
    // Change to a unique project ID.
    String projectId = "my-globally-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.");
}
Also used : Project(com.google.cloud.resourcemanager.Project) ResourceManager(com.google.cloud.resourcemanager.ResourceManager)

Example 2 with ResourceManager

use of com.google.cloud.resourcemanager.ResourceManager in project google-cloud-java by GoogleCloudPlatform.

the class ModifyPolicy method main.

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
    // Use an existing project's ID
    String projectId = "some-project-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);
}
Also used : Policy(com.google.cloud.Policy) Project(com.google.cloud.resourcemanager.Project) ResourceManager(com.google.cloud.resourcemanager.ResourceManager) Identity(com.google.cloud.Identity)

Example 3 with ResourceManager

use of com.google.cloud.resourcemanager.ResourceManager in project google-cloud-java by GoogleCloudPlatform.

the class ResourceManagerExample method main.

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);
    }
}
Also used : ResourceManager(com.google.cloud.resourcemanager.ResourceManager) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with ResourceManager

use of com.google.cloud.resourcemanager.ResourceManager in project google-cloud-java by GoogleCloudPlatform.

the class UpdateAndListProjects method main.

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
    // Use an existing project's ID
    Project project = resourceManager.get("some-project-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());
    }
}
Also used : Project(com.google.cloud.resourcemanager.Project) ResourceManager(com.google.cloud.resourcemanager.ResourceManager)

Aggregations

ResourceManager (com.google.cloud.resourcemanager.ResourceManager)4 Project (com.google.cloud.resourcemanager.Project)3 Identity (com.google.cloud.Identity)1 Policy (com.google.cloud.Policy)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1