Search in sources :

Example 1 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class QuickstartTests method testQuickstart.

@Test
public void testQuickstart() throws Exception {
    String member = "serviceAccount:" + serviceAccount.getEmail();
    String role = "roles/logging.logWriter";
    // Tests initializeService()
    CloudResourceManager crmService = Quickstart.initializeService();
    // Tests addBinding()
    Quickstart.addBinding(crmService, "projects/" + PROJECT_ID, member, role);
    // Get the project's polcy and confirm that the member is in the policy
    Policy policy = Quickstart.getPolicy(crmService, "projects/" + PROJECT_ID);
    Binding binding = null;
    List<Binding> bindings = policy.getBindings();
    for (Binding b : bindings) {
        if (b.getRole().equals(role)) {
            binding = b;
            break;
        }
    }
    assertThat(binding.getMembers(), hasItem(member));
    // Tests removeMember()
    Quickstart.removeMember(crmService, "projects/" + PROJECT_ID, member, role);
    // Confirm that the member has been removed
    policy = Quickstart.getPolicy(crmService, "projects/" + PROJECT_ID);
    binding = null;
    bindings = policy.getBindings();
    for (Binding b : bindings) {
        if (b.getRole().equals(role)) {
            binding = b;
            break;
        }
    }
    if (binding != null) {
        assertThat(binding.getMembers(), not(hasItem(member)));
    }
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding) CloudResourceManager(com.google.api.services.cloudresourcemanager.v3.CloudResourceManager) Test(org.junit.Test)

Example 2 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class Quickstart method setPolicy.

private static void setPolicy(CloudResourceManager crmService, String projectId, Policy policy) {
    // Cloud Resource Manager Projects API.
    try {
        SetIamPolicyRequest request = new SetIamPolicyRequest();
        request.setPolicy(policy);
        crmService.projects().setIamPolicy(projectId, request).execute();
    } catch (IOException e) {
        System.out.println("Unable to set policy: \n" + e.getMessage() + e.getStackTrace());
    }
}
Also used : SetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest) IOException(java.io.IOException)

Example 3 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class Quickstart method removeMember.

public static void removeMember(CloudResourceManager crmService, String projectId, String member, String role) {
    // Gets the project's policy.
    Policy policy = getPolicy(crmService, projectId);
    // Removes the member from the role.
    Binding binding = null;
    for (Binding b : policy.getBindings()) {
        if (b.getRole().equals(role)) {
            binding = b;
            break;
        }
    }
    if (binding.getMembers().contains(member)) {
        binding.getMembers().remove(member);
        if (binding.getMembers().isEmpty()) {
            policy.getBindings().remove(binding);
        }
    }
    // Sets the updated policy.
    setPolicy(crmService, projectId, policy);
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding)

Example 4 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class TestPermissions method testPermissions.

// Tests if the caller has the listed permissions.
public static void testPermissions(String projectId) {
    // projectId = "my-project-id"
    CloudResourceManager service = null;
    try {
        service = createCloudResourceManagerService();
    } catch (IOException | GeneralSecurityException e) {
        System.out.println("Unable to initialize service: \n" + e.toString());
        return;
    }
    List<String> permissionsList = Arrays.asList("resourcemanager.projects.get", "resourcemanager.projects.delete");
    TestIamPermissionsRequest requestBody = new TestIamPermissionsRequest().setPermissions(permissionsList);
    try {
        TestIamPermissionsResponse testIamPermissionsResponse = service.projects().testIamPermissions(projectId, requestBody).execute();
        System.out.println("Of the permissions listed in the request, the caller has the following: " + testIamPermissionsResponse.getPermissions().toString());
    } catch (IOException e) {
        System.out.println("Unable to test permissions: \n" + e.toString());
    }
}
Also used : CloudResourceManager(com.google.api.services.cloudresourcemanager.v3.CloudResourceManager) TestIamPermissionsRequest(com.google.api.services.cloudresourcemanager.v3.model.TestIamPermissionsRequest) TestIamPermissionsResponse(com.google.api.services.cloudresourcemanager.v3.model.TestIamPermissionsResponse) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 5 with CloudResourceManager

use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.

the class SetPolicy method setPolicy.

// Sets a project's policy.
public static void setPolicy(Policy policy, String projectId) {
    // policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
    // projectId = "my-project-id"
    CloudResourceManager service = null;
    try {
        service = createCloudResourceManagerService();
    } catch (IOException | GeneralSecurityException e) {
        System.out.println("Unable to initialize service: \n" + e.toString());
        return;
    }
    try {
        SetIamPolicyRequest request = new SetIamPolicyRequest();
        request.setPolicy(policy);
        Policy response = service.projects().setIamPolicy(projectId, request).execute();
        System.out.println("Policy set: " + response.toString());
    } catch (IOException e) {
        System.out.println("Unable to set policy: \n" + e.toString());
    }
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) CloudResourceManager(com.google.api.services.cloudresourcemanager.v3.CloudResourceManager) SetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Aggregations

CloudResourceManager (com.google.api.services.cloudresourcemanager.v3.CloudResourceManager)9 Policy (com.google.api.services.cloudresourcemanager.v3.model.Policy)7 IOException (java.io.IOException)6 Binding (com.google.api.services.cloudresourcemanager.v3.model.Binding)4 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)4 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)4 GeneralSecurityException (java.security.GeneralSecurityException)4 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)2 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest)2 TestIamPermissionsRequest (com.google.api.services.cloudresourcemanager.v3.model.TestIamPermissionsRequest)1 TestIamPermissionsResponse (com.google.api.services.cloudresourcemanager.v3.model.TestIamPermissionsResponse)1 Test (org.junit.Test)1