Search in sources :

Example 1 with TestIamPermissionsResponse

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

the class TopicAdminClientSnippets method testTopicPermissions.

/** Example of testing whether the caller has the provided permissions on a topic.
   * Only viewer, editor or admin/owner can view results of pubsub.topics.get  */
public TestIamPermissionsResponse testTopicPermissions(String topicId) throws Exception {
    // [START pubsub_test_topic_permissions]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        List<String> permissions = new LinkedList<>();
        permissions.add("pubsub.topics.get");
        TopicName topicName = TopicName.create(projectId, topicId);
        TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(topicName.toString(), permissions);
        return testedPermissions;
    }
// [END pubsub_test_topic_permissions]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) TestIamPermissionsResponse(com.google.iam.v1.TestIamPermissionsResponse) LinkedList(java.util.LinkedList) TopicName(com.google.pubsub.v1.TopicName)

Example 2 with TestIamPermissionsResponse

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

the class SubscriptionAdminClientTest method testIamPermissionsTest.

@Test
@SuppressWarnings("all")
public void testIamPermissionsTest() {
    TestIamPermissionsResponse expectedResponse = TestIamPermissionsResponse.newBuilder().build();
    mockIAMPolicy.addResponse(expectedResponse);
    String formattedResource = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]").toString();
    List<String> permissions = new ArrayList<>();
    TestIamPermissionsResponse actualResponse = client.testIamPermissions(formattedResource, permissions);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    TestIamPermissionsRequest actualRequest = (TestIamPermissionsRequest) actualRequests.get(0);
    Assert.assertEquals(formattedResource, actualRequest.getResource());
    Assert.assertEquals(permissions, actualRequest.getPermissionsList());
}
Also used : TestIamPermissionsResponse(com.google.iam.v1.TestIamPermissionsResponse) TestIamPermissionsRequest(com.google.iam.v1.TestIamPermissionsRequest) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 3 with TestIamPermissionsResponse

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

the class TopicAdminClientTest method testIamPermissionsTest.

@Test
@SuppressWarnings("all")
public void testIamPermissionsTest() {
    TestIamPermissionsResponse expectedResponse = TestIamPermissionsResponse.newBuilder().build();
    mockIAMPolicy.addResponse(expectedResponse);
    String formattedResource = TopicName.create("[PROJECT]", "[TOPIC]").toString();
    List<String> permissions = new ArrayList<>();
    TestIamPermissionsResponse actualResponse = client.testIamPermissions(formattedResource, permissions);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    TestIamPermissionsRequest actualRequest = (TestIamPermissionsRequest) actualRequests.get(0);
    Assert.assertEquals(formattedResource, actualRequest.getResource());
    Assert.assertEquals(permissions, actualRequest.getPermissionsList());
}
Also used : TestIamPermissionsResponse(com.google.iam.v1.TestIamPermissionsResponse) TestIamPermissionsRequest(com.google.iam.v1.TestIamPermissionsRequest) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 4 with TestIamPermissionsResponse

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

the class SubscriptionAdminClientSnippets method testSubscriptionPermissions.

/** Example of testing whether the caller has the provided permissions on a subscription. */
public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionId) throws Exception {
    // [START pubsub_test_subscription_permissions]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        List<String> permissions = new LinkedList<>();
        permissions.add("pubsub.subscriptions.get");
        SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
        TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(subscriptionName.toString(), permissions);
        return testedPermissions;
    }
// [END pubsub_test_subscription_permissions]
}
Also used : TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) TestIamPermissionsResponse(com.google.iam.v1.TestIamPermissionsResponse) SubscriptionName(com.google.pubsub.v1.SubscriptionName) LinkedList(java.util.LinkedList)

Example 5 with TestIamPermissionsResponse

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

the class ITTopicAdminClientSnippets method replaceTopicPolicyAndTestPermissionsIsSuccessful.

@Test
public void replaceTopicPolicyAndTestPermissionsIsSuccessful() throws Exception {
    String topicName = topics[0];
    topicAdminClientSnippets.createTopic(topicName);
    Policy policy = topicAdminClientSnippets.replaceTopicPolicy(topicName);
    assertNotNull(policy.getBindingsCount());
    assertTrue(policy.getBindings(0).getRole().equalsIgnoreCase(Role.viewer().toString()));
    assertTrue(policy.getBindings(0).getMembers(0).equalsIgnoreCase(Identity.allAuthenticatedUsers().toString()));
    TestIamPermissionsResponse response = topicAdminClientSnippets.testTopicPermissions(topicName);
    assertTrue(response.getPermissionsList().contains("pubsub.topics.get"));
}
Also used : Policy(com.google.iam.v1.Policy) TestIamPermissionsResponse(com.google.iam.v1.TestIamPermissionsResponse) Test(org.junit.Test)

Aggregations

TestIamPermissionsResponse (com.google.iam.v1.TestIamPermissionsResponse)6 Test (org.junit.Test)4 ByteString (com.google.protobuf.ByteString)3 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)2 Policy (com.google.iam.v1.Policy)2 TestIamPermissionsRequest (com.google.iam.v1.TestIamPermissionsRequest)2 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 SubscriptionName (com.google.pubsub.v1.SubscriptionName)1 TopicName (com.google.pubsub.v1.TopicName)1