/** * * * <pre> * Representation of the actual Cloud IAM policy set on a cloud resource. For each * resource, there must be at most one Cloud IAM policy set on it. * </pre> * * <code>.google.iam.v1.Policy iam_policy = 4;</code> */ public Builder mergeIamPolicy(com.google.iam.v1.Policy value) { if (iamPolicyBuilder_ == null) { if (iamPolicy_ != null) { iamPolicy_ = com.google.iam.v1.Policy.newBuilder(iamPolicy_).mergeFrom(value).buildPartial(); } else { iamPolicy_ = value; } onChanged(); } else { iamPolicyBuilder_.mergeFrom(value); } return this; } /**
/** * * * <pre> * Representation of the actual Cloud IAM policy set on a cloud resource. For each * resource, there must be at most one Cloud IAM policy set on it. * </pre> * * <code>.google.iam.v1.Policy iam_policy = 4;</code> */ public com.google.iam.v1.Policy getIamPolicy() { return iamPolicy_ == null ? com.google.iam.v1.Policy.getDefaultInstance() : iamPolicy_; } /**
@Override protected Policy fromPb(com.google.iam.v1.Policy policyPb) { Map<Role, Set<Identity>> bindings = new HashMap<>(); for (com.google.iam.v1.Binding bindingPb : policyPb.getBindingsList()) { bindings.put( Role.of(bindingPb.getRole()), ImmutableSet.copyOf( Lists.transform( bindingPb.getMembersList(), new Function<String, Identity>() { @Override public Identity apply(String s) { return IDENTITY_VALUE_OF_FUNCTION.apply(s); } }))); } return newBuilder() .setBindings(bindings) .setEtag( policyPb.getEtag().isEmpty() ? null : BaseEncoding.base64().encode(policyPb.getEtag().toByteArray())) .setVersion(policyPb.getVersion()) .build(); }
for (Binding binding : iamPolicy.getBindingsList()) { if (!binding.getRole().equals(role)) { newBindings.add(binding); Policy newIamPolicy = Policy.newBuilder() .addAllBindings(newBindings) .build();
@Test public void testTopicPolicy() { ProjectTopicName topicName = ProjectTopicName.of(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()); assertThat(newPolicy.getBindingsList()).contains(binding); String permissionName = "pubsub.topics.get"; List<String> permissions = topicAdminClient .testIamPermissions(topicName.toString(), Collections.singletonList(permissionName)) .getPermissionsList(); assertThat(permissions).contains(permissionName); topicAdminClient.deleteTopic(topicName); }
/** Example of replacing a subscription policy. */ public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception { // [START pubsub_set_subscription_policy] try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId); Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString()); // Create a role => members binding Binding binding = Binding.newBuilder() .setRole(Role.viewer().toString()) .addMembers(Identity.allAuthenticatedUsers().toString()) .build(); // Update policy Policy updatedPolicy = policy.toBuilder().addBindings(binding).build(); updatedPolicy = subscriptionAdminClient.setIamPolicy(subscriptionName.toString(), updatedPolicy); return updatedPolicy; } // [END pubsub_set_subscription_policy] }
for (Binding binding : iamPolicy.getBindingsList()) { if (!binding.getRole().equals(role)) { newBindings.add(binding); Policy newIamPolicy = Policy.newBuilder() .addAllBindings(newBindings) .build();
Policy newPolicy = topicAdminClient.setIamPolicy( topic.toString(), policy.toBuilder().addBindings(binding).build()); assertTrue(newPolicy.getBindingsList().contains(binding)); String permissionName = "pubsub.topics.get"; List<String> permissions =
@Override protected com.google.iam.v1.Policy toPb(Policy policy) { com.google.iam.v1.Policy.Builder policyBuilder = com.google.iam.v1.Policy.newBuilder(); List<com.google.iam.v1.Binding> bindingPbList = new LinkedList<>(); for (Map.Entry<Role, Set<Identity>> binding : policy.getBindings().entrySet()) { com.google.iam.v1.Binding.Builder bindingBuilder = com.google.iam.v1.Binding.newBuilder(); bindingBuilder.setRole(binding.getKey().getValue()); bindingBuilder.addAllMembers( Lists.transform( new ArrayList<>(binding.getValue()), new Function<Identity, String>() { @Override public String apply(Identity identity) { return IDENTITY_STR_VALUE_FUNCTION.apply(identity); } })); bindingPbList.add(bindingBuilder.build()); } policyBuilder.addAllBindings(bindingPbList); if (policy.etag != null) { policyBuilder.setEtag(ByteString.copyFrom(BaseEncoding.base64().decode(policy.etag))); } policyBuilder.setVersion(policy.version); return policyBuilder.build(); } }
/** * * * <pre> * Representation of the actual Cloud IAM policy set on a cloud resource. For each * resource, there must be at most one Cloud IAM policy set on it. * </pre> * * <code>.google.iam.v1.Policy iam_policy = 4;</code> */ public com.google.iam.v1.Policy getIamPolicy() { if (iamPolicyBuilder_ == null) { return iamPolicy_ == null ? com.google.iam.v1.Policy.getDefaultInstance() : iamPolicy_; } else { return iamPolicyBuilder_.getMessage(); } } /**
@Override protected Policy fromPb(com.google.iam.v1.Policy policyPb) { Map<Role, Set<Identity>> bindings = new HashMap<>(); for (com.google.iam.v1.Binding bindingPb : policyPb.getBindingsList()) { bindings.put( Role.of(bindingPb.getRole()), ImmutableSet.copyOf( Lists.transform( bindingPb.getMembersList(), new Function<String, Identity>() { @Override public Identity apply(String s) { return IDENTITY_VALUE_OF_FUNCTION.apply(s); } }))); } return newBuilder() .setBindings(bindings) .setEtag( policyPb.getEtag().isEmpty() ? null : BaseEncoding.base64().encode(policyPb.getEtag().toByteArray())) .setVersion(policyPb.getVersion()) .build(); }
@Test @SuppressWarnings("all") public void setIamPolicyTest() { int version = 351608024; ByteString etag = ByteString.copyFromUtf8("21"); Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); mockCloudTasks.addResponse(expectedResponse); QueueName resource = QueueName.of("[PROJECT]", "[LOCATION]", "[QUEUE]"); Policy policy = Policy.newBuilder().build(); Policy actualResponse = client.setIamPolicy(resource, policy); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockCloudTasks.getRequests(); Assert.assertEquals(1, actualRequests.size()); SetIamPolicyRequest actualRequest = (SetIamPolicyRequest) actualRequests.get(0); Assert.assertEquals(Objects.toString(resource), Objects.toString(actualRequest.getResource())); Assert.assertEquals(policy, actualRequest.getPolicy()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * * * <pre> * Representation of the actual Cloud IAM policy set on a cloud resource. For each * resource, there must be at most one Cloud IAM policy set on it. * </pre> * * <code>.google.iam.v1.Policy iam_policy = 4;</code> */ public com.google.iam.v1.PolicyOrBuilder getIamPolicyOrBuilder() { if (iamPolicyBuilder_ != null) { return iamPolicyBuilder_.getMessageOrBuilder(); } else { return iamPolicy_ == null ? com.google.iam.v1.Policy.getDefaultInstance() : iamPolicy_; } } /**
@Test @SuppressWarnings("all") public void setIamPolicyTest() { int version = 351608024; ByteString etag = ByteString.copyFromUtf8("21"); Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); mockDeviceManager.addResponse(expectedResponse); RegistryName resource = RegistryName.of("[PROJECT]", "[LOCATION]", "[REGISTRY]"); Policy policy = Policy.newBuilder().build(); Policy actualResponse = client.setIamPolicy(resource, policy); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockDeviceManager.getRequests(); Assert.assertEquals(1, actualRequests.size()); SetIamPolicyRequest actualRequest = (SetIamPolicyRequest) actualRequests.get(0); Assert.assertEquals(Objects.toString(resource), Objects.toString(actualRequest.getResource())); Assert.assertEquals(policy, actualRequest.getPolicy()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
private static io.grpc.MethodDescriptor< com.google.iam.v1.SetIamPolicyRequest, com.google.iam.v1.Policy> getSetIamPolicyMethodHelper() { io.grpc.MethodDescriptor<com.google.iam.v1.SetIamPolicyRequest, com.google.iam.v1.Policy> getSetIamPolicyMethod; if ((getSetIamPolicyMethod = CloudTasksGrpc.getSetIamPolicyMethod) == null) { synchronized (CloudTasksGrpc.class) { if ((getSetIamPolicyMethod = CloudTasksGrpc.getSetIamPolicyMethod) == null) { CloudTasksGrpc.getSetIamPolicyMethod = getSetIamPolicyMethod = io.grpc.MethodDescriptor .<com.google.iam.v1.SetIamPolicyRequest, com.google.iam.v1.Policy>newBuilder() .setType(io.grpc.MethodDescriptor.MethodType.UNARY) .setFullMethodName( generateFullMethodName( "google.cloud.tasks.v2beta3.CloudTasks", "SetIamPolicy")) .setSampledToLocalTracing(true) .setRequestMarshaller( io.grpc.protobuf.ProtoUtils.marshaller( com.google.iam.v1.SetIamPolicyRequest.getDefaultInstance())) .setResponseMarshaller( io.grpc.protobuf.ProtoUtils.marshaller( com.google.iam.v1.Policy.getDefaultInstance())) .setSchemaDescriptor(new CloudTasksMethodDescriptorSupplier("SetIamPolicy")) .build(); } } } return getSetIamPolicyMethod; }
@Test @SuppressWarnings("all") public void setIamPolicyTest() { int version = 351608024; ByteString etag = ByteString.copyFromUtf8("21"); Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); mockCloudTasks.addResponse(expectedResponse); QueueName resource = QueueName.of("[PROJECT]", "[LOCATION]", "[QUEUE]"); Policy policy = Policy.newBuilder().build(); Policy actualResponse = client.setIamPolicy(resource, policy); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockCloudTasks.getRequests(); Assert.assertEquals(1, actualRequests.size()); SetIamPolicyRequest actualRequest = (SetIamPolicyRequest) actualRequests.get(0); Assert.assertEquals(Objects.toString(resource), Objects.toString(actualRequest.getResource())); Assert.assertEquals(policy, actualRequest.getPolicy()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
.setResponseMarshaller( io.grpc.protobuf.ProtoUtils.marshaller( com.google.iam.v1.Policy.getDefaultInstance())) .setSchemaDescriptor( new BigtableInstanceAdminMethodDescriptorSupplier("SetIamPolicy"))
@Test @SuppressWarnings("all") public void setIamPolicyTest() { int version = 351608024; ByteString etag = ByteString.copyFromUtf8("21"); Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); mockIAMPolicy.addResponse(expectedResponse); KeyName resource = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); Policy policy = Policy.newBuilder().build(); Policy actualResponse = client.setIamPolicy(resource, policy); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests(); Assert.assertEquals(1, actualRequests.size()); SetIamPolicyRequest actualRequest = (SetIamPolicyRequest) actualRequests.get(0); Assert.assertEquals(Objects.toString(resource), Objects.toString(actualRequest.getResource())); Assert.assertEquals(policy, actualRequest.getPolicy()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
.setResponseMarshaller( io.grpc.protobuf.ProtoUtils.marshaller( com.google.iam.v1.Policy.getDefaultInstance())) .setSchemaDescriptor( new InstanceAdminMethodDescriptorSupplier("SetIamPolicy"))