Search in sources :

Example 1 with Policy

use of com.google.iam.v1.Policy in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientSnippets method replaceTopicPolicy.

/** Example of replacing a topic policy. */
public Policy replaceTopicPolicy(String topicId) throws Exception {
    // [START pubsub_set_topic_policy]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        String topicName = TopicName.create(projectId, topicId).toString();
        Policy policy = topicAdminClient.getIamPolicy(topicName);
        // add role -> members binding
        Binding binding = Binding.newBuilder().setRole(Role.viewer().toString()).addMembers(Identity.allAuthenticatedUsers().toString()).build();
        // create updated policy
        Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build();
        updatedPolicy = topicAdminClient.setIamPolicy(topicName, updatedPolicy);
        return updatedPolicy;
    }
// [END pubsub_set_topic_policy]
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient)

Example 2 with Policy

use of com.google.iam.v1.Policy in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientSnippets method getTopicPolicy.

/** Example of getting a topic policy. */
public Policy getTopicPolicy(String topicId) throws Exception {
    // [START pubsub_get_topic_policy]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        TopicName topicName = TopicName.create(projectId, topicId);
        Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
        if (policy == null) {
        // topic iam policy was not found
        }
        return policy;
    }
// [END pubsub_get_topic_policy]
}
Also used : Policy(com.google.iam.v1.Policy) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) TopicName(com.google.pubsub.v1.TopicName)

Example 3 with Policy

use of com.google.iam.v1.Policy in project google-cloud-java by GoogleCloudPlatform.

the class ITPubSubTest method testTopicPolicy.

@Test
public void testTopicPolicy() {
    TopicName topicName = TopicName.create(projectId, formatForTest("testing-topic-policy"));
    topicAdminClient.createTopic(topicName);
    Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
    Binding binding = Binding.newBuilder().setRole("roles/viewer").addMembers("allAuthenticatedUsers").build();
    Policy newPolicy = topicAdminClient.setIamPolicy(topicName.toString(), policy.toBuilder().addBindings(binding).build());
    assertTrue(newPolicy.getBindingsList().contains(binding));
    String permissionName = "pubsub.topics.get";
    List<String> permissions = topicAdminClient.testIamPermissions(topicName.toString(), Collections.singletonList(permissionName)).getPermissionsList();
    assertTrue(permissions.contains(permissionName));
    topicAdminClient.deleteTopic(topicName);
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) ByteString(com.google.protobuf.ByteString) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 4 with Policy

use of com.google.iam.v1.Policy in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientTest method getIamPolicyTest.

@Test
@SuppressWarnings("all")
public void getIamPolicyTest() {
    int version = 351608024;
    ByteString etag = ByteString.copyFromUtf8("21");
    Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build();
    mockIAMPolicy.addResponse(expectedResponse);
    String formattedResource = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]").toString();
    Policy actualResponse = client.getIamPolicy(formattedResource);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetIamPolicyRequest actualRequest = (GetIamPolicyRequest) actualRequests.get(0);
    Assert.assertEquals(formattedResource, actualRequest.getResource());
}
Also used : Policy(com.google.iam.v1.Policy) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) GetIamPolicyRequest(com.google.iam.v1.GetIamPolicyRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 5 with Policy

use of com.google.iam.v1.Policy in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientTest method setIamPolicyExceptionTest.

@Test
@SuppressWarnings("all")
public void setIamPolicyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockIAMPolicy.addException(exception);
    try {
        String formattedResource = TopicName.create("[PROJECT]", "[TOPIC]").toString();
        Policy policy = Policy.newBuilder().build();
        client.setIamPolicy(formattedResource, policy);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : Policy(com.google.iam.v1.Policy) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Aggregations

Policy (com.google.iam.v1.Policy)15 Test (org.junit.Test)11 ByteString (com.google.protobuf.ByteString)9 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)4 Binding (com.google.iam.v1.Binding)3 ApiException (com.google.api.gax.grpc.ApiException)2 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)2 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)2 GetIamPolicyRequest (com.google.iam.v1.GetIamPolicyRequest)2 SetIamPolicyRequest (com.google.iam.v1.SetIamPolicyRequest)2 TestIamPermissionsResponse (com.google.iam.v1.TestIamPermissionsResponse)2 SubscriptionName (com.google.pubsub.v1.SubscriptionName)2 TopicName (com.google.pubsub.v1.TopicName)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 Policy (org.openstack4j.model.identity.v3.Policy)1