Search in sources :

Example 16 with Policy

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

the class AddMember method addMember.

// Adds a member to a preexisting role.
public static void addMember(Policy policy) {
    // policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
    String role = "roles/existing-role";
    String member = "user:member-to-add@example.com";
    List<Binding> bindings = policy.getBindings();
    for (Binding b : bindings) {
        if (b.getRole().equals(role)) {
            b.getMembers().add(member);
            System.out.println("Member " + member + " added to role " + role);
            return;
        }
    }
    System.out.println("Role not found in policy; member not added");
}
Also used : Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding)

Example 17 with Policy

use of com.google.api.services.cloudresourcemanager.v3.model.Policy 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 18 with Policy

use of com.google.api.services.cloudresourcemanager.v3.model.Policy 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 19 with Policy

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

the class DicomStoreGetIamPolicy method dicomStoreGetIamPolicy.

public static void dicomStoreGetIamPolicy(String dicomStoreName) throws IOException {
    // String dicomStoreName =
    // String.format(
    // DICOM_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-dicom-id");
    // Initialize the client, which will be used to interact with the service.
    CloudHealthcare client = createClient();
    // Create request and configure any parameters.
    DicomStores.GetIamPolicy request = client.projects().locations().datasets().dicomStores().getIamPolicy(dicomStoreName);
    // Execute the request and process the results.
    Policy policy = request.execute();
    System.out.println("DICOM store IAMPolicy retrieved: \n" + policy.toPrettyString());
}
Also used : Policy(com.google.api.services.healthcare.v1.model.Policy) DicomStores(com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores) CloudHealthcare(com.google.api.services.healthcare.v1.CloudHealthcare)

Example 20 with Policy

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

the class FhirStoreSetIamPolicy method fhirStoreSetIamPolicy.

public static void fhirStoreSetIamPolicy(String fhirStoreName) throws IOException {
    // String fhirStoreName =
    // String.format(
    // FHIR_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-fhir-id");
    // Initialize the client, which will be used to interact with the service.
    CloudHealthcare client = createClient();
    // Configure the IAMPolicy to apply to the store.
    // For more information on understanding IAM roles, see the following:
    // https://cloud.google.com/iam/docs/understanding-roles
    Binding binding = new Binding().setRole("roles/healthcare.fhirResourceReader").setMembers(Arrays.asList("domain:google.com"));
    Policy policy = new Policy().setBindings(Arrays.asList(binding));
    SetIamPolicyRequest policyRequest = new SetIamPolicyRequest().setPolicy(policy);
    // Create request and configure any parameters.
    FhirStores.SetIamPolicy request = client.projects().locations().datasets().fhirStores().setIamPolicy(fhirStoreName, policyRequest);
    // Execute the request and process the results.
    Policy updatedPolicy = request.execute();
    System.out.println("FHIR policy has been updated: " + updatedPolicy.toPrettyString());
}
Also used : Binding(com.google.api.services.healthcare.v1.model.Binding) Policy(com.google.api.services.healthcare.v1.model.Policy) FhirStores(com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.FhirStores) SetIamPolicyRequest(com.google.api.services.healthcare.v1.model.SetIamPolicyRequest) CloudHealthcare(com.google.api.services.healthcare.v1.CloudHealthcare)

Aggregations

IOException (java.io.IOException)16 Policy (com.google.api.services.cloudresourcemanager.v3.model.Policy)15 Binding (com.google.api.services.cloudresourcemanager.v3.model.Binding)14 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)8 CloudHealthcare (com.google.api.services.healthcare.v1.CloudHealthcare)8 Policy (com.google.api.services.healthcare.v1.model.Policy)8 Test (org.junit.jupiter.api.Test)8 Policy (com.google.api.services.iam.v1.model.Policy)7 CloudKMS (com.google.api.services.cloudkms.v1.CloudKMS)6 Policy (com.google.api.services.cloudkms.v1.model.Policy)6 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest)6 FlightMap (bio.terra.stairway.FlightMap)5 ArrayList (java.util.ArrayList)5 StepResult (bio.terra.stairway.StepResult)4 StepStatus (bio.terra.stairway.StepStatus)4 Binding (com.google.api.services.healthcare.v1.model.Binding)4 SetIamPolicyRequest (com.google.api.services.healthcare.v1.model.SetIamPolicyRequest)4 Binding (com.google.api.services.iam.v1.model.Binding)4 SetIamPolicyRequest (com.google.api.services.iam.v1.model.SetIamPolicyRequest)4 ServiceAccountName (bio.terra.cloudres.google.iam.ServiceAccountName)3