예제 #1
0
        public static object SetSubscriptionIamPolicy(string projectId,
                                                      string subscriptionId, string role, string member)
        {
            PublisherClient publisher             = PublisherClient.Create();
            string          roleToBeAddedToPolicy = $"roles/{role}";
            // [START pubsub_set_subscription_policy]
            Policy policy = new Policy
            {
                Bindings =
                {
                    new Binding {
                        Role    = roleToBeAddedToPolicy,
                        Members ={ member   }
                    }
                }
            };
            SetIamPolicyRequest request = new SetIamPolicyRequest
            {
                Resource = new SubscriptionName(projectId, subscriptionId).ToString(),
                Policy   = policy
            };
            Policy response = publisher.SetIamPolicy(request);

            Console.WriteLine($"Subscription IAM Policy updated: {response}");
            // [END pubsub_set_subscription_policy]
            return(0);
        }
예제 #2
0
        public Policy SetSubscriptionIamPolicy(string subscriptionId, PublisherClient publisher)
        {
            // [START pubsub_set_subscription_policy]
            Policy policy = new Policy
            {
                Bindings =
                {
                    new Binding {
                        Role    = "roles/pubsub.editor",
                        Members ={ "group:[email protected]"   }
                    },
                    new Binding {
                        Role    = "roles/pubsub.viewer",
                        Members ={ "allUsers"                      }
                    }
                }
            };
            SetIamPolicyRequest request = new SetIamPolicyRequest
            {
                Resource = new SubscriptionName(_projectId, subscriptionId).ToString(),
                Policy   = policy
            };
            Policy response = publisher.SetIamPolicy(request);

            return(response);
            // [END pubsub_set_subscription_policy]
        }
 public void SetIamPolicy()
 {
     // Snippet: SetIamPolicy(string,Policy,CallSettings)
     // Create client
     PublisherClient publisherClient = PublisherClient.Create();
     // Initialize request argument(s)
     string formattedResource = new TopicName("[PROJECT]", "[TOPIC]").ToString();
     Policy policy            = new Policy();
     // Make the request
     Policy response = publisherClient.SetIamPolicy(formattedResource, policy);
     // End snippet
 }
 public void SetIamPolicy_RequestObject()
 {
     // Snippet: SetIamPolicy(SetIamPolicyRequest,CallSettings)
     // Create client
     PublisherClient publisherClient = PublisherClient.Create();
     // Initialize request argument(s)
     SetIamPolicyRequest request = new SetIamPolicyRequest
     {
         Resource = new TopicName("[PROJECT]", "[TOPIC]").ToString(),
         Policy   = new Policy(),
     };
     // Make the request
     Policy response = publisherClient.SetIamPolicy(request);
     // End snippet
 }
예제 #5
0
        public void NotificationsOverview()
        {
            string projectId = _fixture.ProjectId;
            string bucket    = _fixture.BucketName;
            string topicId   = "topic-" + Guid.NewGuid().ToString().ToLowerInvariant();

            // Sample: NotificationsOverview
            // First create a Pub/Sub topic.
            PublisherClient publisherClient = PublisherClient.Create();
            TopicName       topicName       = new TopicName(projectId, topicId);

            publisherClient.CreateTopic(topicName);

            // Prepare the topic for Storage notifications. The Storage Service Account must have Publish permission
            // for the topic. The code below adds the service account into the "roles/pubsub.publisher" role for the topic.

            // Determine the Storage Service Account name to use in IAM operations.
            StorageClient storageClient         = StorageClient.Create();
            string        storageServiceAccount = $"serviceAccount:{storageClient.GetStorageServiceAccountEmail(projectId)}";

            // Fetch the IAM policy for the topic.
            Iam.V1.Policy policy = publisherClient.GetIamPolicy(topicName.ToString());
            var           role   = "roles/pubsub.publisher";

            // Ensure the Storage Service Account is in the publisher role, setting the IAM policy for the topic
            // on the server if necessary.
            if (policy.AddRoleMember(role, storageServiceAccount))
            {
                publisherClient.SetIamPolicy(topicName.ToString(), policy);
            }

            // Now that the topic is ready, we can create a notification configuration for Storage
            Notification notification = new Notification
            {
                Topic         = $"//pubsub.googleapis.com/{topicName}",
                PayloadFormat = "JSON_API_V1"
            };

            notification = storageClient.CreateNotification(bucket, notification);
            Console.WriteLine($"Created notification ID: {notification.Id}");

            // End sample

            _fixture.RegisterTopicToDelete(topicName);
        }