Search in sources :

Example 26 with Policy

use of com.google.api.services.notebooks.v1.model.Policy 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 27 with Policy

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

the class Quickstart method main.

public static void main(String[] args) {
    // TODO: Replace with your project ID.
    String projectId = "your-project";
    // TODO: Replace with the ID of your member in the form "user:member@example.com"
    String member = "your-member";
    // The role to be granted.
    String role = "roles/logging.logWriter";
    // Initializes the Cloud Resource Manager service.
    CloudResourceManager crmService = null;
    try {
        crmService = initializeService();
    } catch (IOException | GeneralSecurityException e) {
        System.out.println("Unable to initialize service: \n" + e.getMessage() + e.getStackTrace());
    }
    // Grants your member the "Log writer" role for your project.
    addBinding(crmService, projectId, member, role);
    // Get the project's policy and print all members with the "Log Writer" role
    Policy policy = getPolicy(crmService, projectId);
    Binding binding = null;
    List<Binding> bindings = policy.getBindings();
    for (Binding b : bindings) {
        if (b.getRole().equals(role)) {
            binding = b;
            break;
        }
    }
    System.out.println("Role: " + binding.getRole());
    System.out.print("Members: ");
    for (String m : binding.getMembers()) {
        System.out.print("[" + m + "] ");
    }
    System.out.println();
    // Removes member from the "Log writer" role.
    removeMember(crmService, projectId, member, role);
}
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) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 28 with Policy

use of com.google.api.services.notebooks.v1.model.Policy 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 29 with Policy

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

the class Quickstart method addBinding.

public static void addBinding(CloudResourceManager crmService, String projectId, String member, String role) {
    // Gets the project's policy.
    Policy policy = getPolicy(crmService, projectId);
    // Finds binding in policy, if it exists
    Binding binding = null;
    for (Binding b : policy.getBindings()) {
        if (b.getRole().equals(role)) {
            binding = b;
            break;
        }
    }
    if (binding != null) {
        // If binding already exists, adds member to binding.
        binding.getMembers().add(member);
    } else {
        // If binding does not exist, adds binding to policy.
        binding = new Binding();
        binding.setRole(role);
        binding.setMembers(Collections.singletonList(member));
        policy.getBindings().add(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 30 with Policy

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

the class Hl7v2StoreGetIamPolicy method hl7v2StoreGetIamPolicy.

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

Aggregations

Policy (com.google.api.services.cloudresourcemanager.v3.model.Policy)15 IOException (java.io.IOException)13 Binding (com.google.api.services.cloudresourcemanager.v3.model.Binding)11 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 CloudKMS (com.google.api.services.cloudkms.v1.CloudKMS)6 Policy (com.google.api.services.cloudkms.v1.model.Policy)6 Policy (com.google.api.services.iam.v1.model.Policy)6 Test (org.junit.jupiter.api.Test)6 FlightMap (bio.terra.stairway.FlightMap)5 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest)4 Binding (com.google.api.services.healthcare.v1.model.Binding)4 SetIamPolicyRequest (com.google.api.services.healthcare.v1.model.SetIamPolicyRequest)4 InternalServerErrorException (bio.terra.common.exception.InternalServerErrorException)3 Binding (com.google.api.services.cloudkms.v1.model.Binding)3 SetIamPolicyRequest (com.google.api.services.cloudkms.v1.model.SetIamPolicyRequest)3 CloudResourceManager (com.google.api.services.cloudresourcemanager.v3.CloudResourceManager)3 Binding (com.google.api.services.iam.v1.model.Binding)3 SetIamPolicyRequest (com.google.api.services.iam.v1.model.SetIamPolicyRequest)3