Search in sources :

Example 6 with GetIamPolicyRequest

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

the class RemoveMember method removeMember.

// Removes member from a role; removes binding if binding contains 0 members.
public static void removeMember(Policy policy) {
    // policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
    String role = "roles/existing-role";
    String member = "user:member-to-remove@example.com";
    List<Binding> bindings = policy.getBindings();
    Binding binding = null;
    for (Binding b : bindings) {
        if (b.getRole().equals(role)) {
            binding = b;
        }
    }
    if (binding.getMembers().contains(member)) {
        binding.getMembers().remove(member);
        System.out.println("Member " + member + " removed from " + role);
        if (binding.getMembers().isEmpty()) {
            policy.getBindings().remove(binding);
        }
        return;
    }
    System.out.println("Role not found in policy; member not removed");
    return;
}
Also used : Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding)

Example 7 with GetIamPolicyRequest

use of com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest 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)

Example 8 with GetIamPolicyRequest

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

the class GetPolicy method getPolicy.

// Gets a project's policy.
public static Policy getPolicy(String projectId) {
    // projectId = "my-project-id"
    Policy policy = null;
    CloudResourceManager service = null;
    try {
        service = createCloudResourceManagerService();
    } catch (IOException | GeneralSecurityException e) {
        System.out.println("Unable to initialize service: \n" + e.toString());
        return policy;
    }
    try {
        GetIamPolicyRequest request = new GetIamPolicyRequest();
        policy = service.projects().getIamPolicy(projectId, request).execute();
        System.out.println("Policy retrieved: " + policy.toString());
        return policy;
    } catch (IOException e) {
        System.out.println("Unable to get policy: \n" + e.toString());
        return policy;
    }
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) CloudResourceManager(com.google.api.services.cloudresourcemanager.v3.CloudResourceManager) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)

Example 9 with GetIamPolicyRequest

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

the class Quickstart method getPolicy.

public static Policy getPolicy(CloudResourceManager crmService, String projectId) {
    // Gets the project's policy by calling the
    // Cloud Resource Manager Projects API.
    Policy policy = null;
    try {
        GetIamPolicyRequest request = new GetIamPolicyRequest();
        policy = crmService.projects().getIamPolicy(projectId, request).execute();
    } catch (IOException e) {
        System.out.println("Unable to get policy: \n" + e.getMessage() + e.getStackTrace());
    }
    return policy;
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) IOException(java.io.IOException) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)

Example 10 with GetIamPolicyRequest

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

the class AddBinding method addBinding.

// Adds a member to a role with no previous members.
public static void addBinding(Policy policy) {
    // policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
    String role = "roles/role-to-add";
    List<String> members = new ArrayList<String>();
    members.add("user:member-to-add@example.com");
    Binding binding = new Binding();
    binding.setRole(role);
    binding.setMembers(members);
    policy.getBindings().add(binding);
    System.out.println("Added binding: " + binding.toString());
}
Also used : Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding) ArrayList(java.util.ArrayList)

Aggregations

IOException (java.io.IOException)10 Binding (com.google.api.services.cloudresourcemanager.v3.model.Binding)9 Policy (com.google.api.services.cloudresourcemanager.v3.model.Policy)9 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)8 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest)4 FlightMap (bio.terra.stairway.FlightMap)3 StepStatus (bio.terra.stairway.StepStatus)3 WsmIamRole (bio.terra.workspace.service.iam.model.WsmIamRole)3 CloudResourceManagerCow (bio.terra.cloudres.google.cloudresourcemanager.CloudResourceManagerCow)2 FlightDebugInfo (bio.terra.stairway.FlightDebugInfo)2 FlightState (bio.terra.stairway.FlightState)2 FlightStatus (bio.terra.stairway.FlightStatus)2 StepResult (bio.terra.stairway.StepResult)2 RetryException (bio.terra.stairway.exception.RetryException)2 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)2 StairwayTestUtils (bio.terra.workspace.common.StairwayTestUtils)2 UserAccessUtils (bio.terra.workspace.connected.UserAccessUtils)2 WorkspaceConnectedTestUtils (bio.terra.workspace.connected.WorkspaceConnectedTestUtils)2 CrlService (bio.terra.workspace.service.crl.CrlService)2 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)2