use of oasis.names.tc.xacml._3_0.core.schema.wd_17.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]
}
use of oasis.names.tc.xacml._3_0.core.schema.wd_17.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]
}
use of oasis.names.tc.xacml._3_0.core.schema.wd_17.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);
}
use of oasis.names.tc.xacml._3_0.core.schema.wd_17.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());
}
use of oasis.names.tc.xacml._3_0.core.schema.wd_17.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());
}
}
Aggregations