Search in sources :

Example 11 with Binding

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

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

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

use of com.google.api.services.notebooks.v1.model.Binding 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)

Example 15 with Binding

use of com.google.api.services.notebooks.v1.model.Binding in project terra-cloud-resource-lib by DataBiosphere.

the class IamCowTest method getAndSetAndTestIamOnServiceAccount.

@Test
public void getAndSetAndTestIamOnServiceAccount() throws Exception {
    IamCow.Projects.ServiceAccounts serviceAccounts = defaultIam().projects().serviceAccounts();
    String projectName = "projects/" + reusableProject.getProjectId();
    ServiceAccount serviceAccount = serviceAccounts.create(projectName, new CreateServiceAccountRequest().setAccountId(randomServiceAccountId())).execute();
    ServiceAccountName serviceAccountName = ServiceAccountName.builder().projectId(serviceAccount.getProjectId()).email(serviceAccount.getEmail()).build();
    Policy policy = serviceAccounts.getIamPolicy(serviceAccountName).execute();
    assertNotNull(policy);
    List<Binding> bindingList = new ArrayList<>();
    String member = String.format("serviceAccount:%s", IntegrationCredentials.getUserGoogleCredentialsOrDie().getClientEmail());
    Binding newBinding = new Binding().setRole("roles/iam.serviceAccountUser").setMembers(Collections.singletonList(member));
    bindingList.add(newBinding);
    policy.setBindings(bindingList);
    Policy updatedPolicy = serviceAccounts.setIamPolicy(serviceAccountName, new SetIamPolicyRequest().setPolicy(policy)).execute();
    assertThat(updatedPolicy.getBindings(), hasItem(newBinding));
    assertEquals(updatedPolicy, serviceAccounts.getIamPolicy(serviceAccountName).execute());
    // Test the permissions of the user for which the IAM policy was set.
    IamCow.Projects.ServiceAccounts userIamServiceAccounts = IamCow.create(IntegrationUtils.DEFAULT_CLIENT_CONFIG, IntegrationCredentials.getUserGoogleCredentialsOrDie()).projects().serviceAccounts();
    // The "actAs" permission associated with "roles/iam.serviceAccountUser".
    String actAsPermission = "iam.serviceAccounts.actAs";
    TestIamPermissionsResponse iamResponse = userIamServiceAccounts.testIamPermissions(serviceAccountName, new TestIamPermissionsRequest().setPermissions(ImmutableList.of(actAsPermission))).execute();
    assertThat(iamResponse.getPermissions(), Matchers.contains(actAsPermission));
}
Also used : Policy(com.google.api.services.iam.v1.model.Policy) Binding(com.google.api.services.iam.v1.model.Binding) ServiceAccount(com.google.api.services.iam.v1.model.ServiceAccount) SetIamPolicyRequest(com.google.api.services.iam.v1.model.SetIamPolicyRequest) TestIamPermissionsRequest(com.google.api.services.iam.v1.model.TestIamPermissionsRequest) ArrayList(java.util.ArrayList) CreateServiceAccountRequest(com.google.api.services.iam.v1.model.CreateServiceAccountRequest) TestIamPermissionsResponse(com.google.api.services.iam.v1.model.TestIamPermissionsResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Binding (com.google.api.services.cloudresourcemanager.v3.model.Binding)12 Policy (com.google.api.services.cloudresourcemanager.v3.model.Policy)9 IOException (java.io.IOException)9 Binding (com.google.api.services.iam.v1.model.Binding)6 Policy (com.google.api.services.iam.v1.model.Policy)5 SetIamPolicyRequest (com.google.api.services.iam.v1.model.SetIamPolicyRequest)5 ArrayList (java.util.ArrayList)5 CloudKMS (com.google.api.services.cloudkms.v1.CloudKMS)4 Binding (com.google.api.services.cloudkms.v1.model.Binding)4 Policy (com.google.api.services.cloudkms.v1.model.Policy)4 SetIamPolicyRequest (com.google.api.services.cloudkms.v1.model.SetIamPolicyRequest)4 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)4 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest)4 CloudHealthcare (com.google.api.services.healthcare.v1.CloudHealthcare)4 Binding (com.google.api.services.healthcare.v1.model.Binding)4 Policy (com.google.api.services.healthcare.v1.model.Policy)4 SetIamPolicyRequest (com.google.api.services.healthcare.v1.model.SetIamPolicyRequest)4 ServiceAccountName (bio.terra.cloudres.google.iam.ServiceAccountName)3 CreateServiceAccountRequest (com.google.api.services.iam.v1.model.CreateServiceAccountRequest)3 ServiceAccount (com.google.api.services.iam.v1.model.ServiceAccount)3