Beispiel #1
0
    public void DeleteNotificationChannel(
        string channelName = "projects/your-project-id/notificationChannels/123")
    {
        var client = NotificationChannelServiceClient.Create();

        client.DeleteNotificationChannel(
            name: NotificationChannelName.Parse(channelName),
            force: true);
        Console.WriteLine("Deleted {0}.", channelName);
    }
Beispiel #2
0
 /// <summary>Snippet for GetNotificationChannelDescriptor</summary>
 public void GetNotificationChannelDescriptor()
 {
     // Snippet: GetNotificationChannelDescriptor(NotificationChannelDescriptorName,CallSettings)
     // Create client
     NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
     // Initialize request argument(s)
     NotificationChannelDescriptorName name = new NotificationChannelDescriptorName("[PROJECT]", "[CHANNEL_DESCRIPTOR]");
     // Make the request
     NotificationChannelDescriptor response = notificationChannelServiceClient.GetNotificationChannelDescriptor(name);
     // End snippet
 }
Beispiel #3
0
 /// <summary>Snippet for GetNotificationChannelVerificationCode</summary>
 public void GetNotificationChannelVerificationCode()
 {
     // Snippet: GetNotificationChannelVerificationCode(NotificationChannelName,CallSettings)
     // Create client
     NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
     // Initialize request argument(s)
     NotificationChannelName name = new NotificationChannelName("[PROJECT]", "[NOTIFICATION_CHANNEL]");
     // Make the request
     GetNotificationChannelVerificationCodeResponse response = notificationChannelServiceClient.GetNotificationChannelVerificationCode(name);
     // End snippet
 }
Beispiel #4
0
 /// <summary>Snippet for CreateNotificationChannel</summary>
 public void CreateNotificationChannel()
 {
     // Snippet: CreateNotificationChannel(ProjectName,NotificationChannel,CallSettings)
     // Create client
     NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
     // Initialize request argument(s)
     ProjectName         name = new ProjectName("[PROJECT]");
     NotificationChannel notificationChannel = new NotificationChannel();
     // Make the request
     NotificationChannel response = notificationChannelServiceClient.CreateNotificationChannel(name, notificationChannel);
     // End snippet
 }
Beispiel #5
0
 /// <summary>Snippet for VerifyNotificationChannel</summary>
 public void VerifyNotificationChannel()
 {
     // Snippet: VerifyNotificationChannel(NotificationChannelName,string,CallSettings)
     // Create client
     NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
     // Initialize request argument(s)
     NotificationChannelName name = new NotificationChannelName("[PROJECT]", "[NOTIFICATION_CHANNEL]");
     string code = "";
     // Make the request
     NotificationChannel response = notificationChannelServiceClient.VerifyNotificationChannel(name, code);
     // End snippet
 }
Beispiel #6
0
 /// <summary>Snippet for UpdateNotificationChannel</summary>
 public void UpdateNotificationChannel()
 {
     // Snippet: UpdateNotificationChannel(FieldMask,NotificationChannel,CallSettings)
     // Create client
     NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
     // Initialize request argument(s)
     FieldMask           updateMask          = new FieldMask();
     NotificationChannel notificationChannel = new NotificationChannel();
     // Make the request
     NotificationChannel response = notificationChannelServiceClient.UpdateNotificationChannel(updateMask, notificationChannel);
     // End snippet
 }
Beispiel #7
0
        /// <summary>Snippet for DeleteNotificationChannel</summary>
        public void DeleteNotificationChannel()
        {
            // Snippet: DeleteNotificationChannel(NotificationChannelName,bool?,CallSettings)
            // Create client
            NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
            // Initialize request argument(s)
            NotificationChannelName name = new NotificationChannelName("[PROJECT]", "[NOTIFICATION_CHANNEL]");
            bool force = false;

            // Make the request
            notificationChannelServiceClient.DeleteNotificationChannel(name, force);
            // End snippet
        }
Beispiel #8
0
 /// <summary>Snippet for GetNotificationChannel</summary>
 public void GetNotificationChannel_RequestObject()
 {
     // Snippet: GetNotificationChannel(GetNotificationChannelRequest,CallSettings)
     // Create client
     NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
     // Initialize request argument(s)
     GetNotificationChannelRequest request = new GetNotificationChannelRequest
     {
         NotificationChannelName = new NotificationChannelName("[PROJECT]", "[NOTIFICATION_CHANNEL]"),
     };
     // Make the request
     NotificationChannel response = notificationChannelServiceClient.GetNotificationChannel(request);
     // End snippet
 }
Beispiel #9
0
        // [END monitoring_alert_list_channels]

        // [START monitoring_alert_backup_policies]
        static void BackupPolicies(string projectId, string filePath)
        {
            var policyClient  = AlertPolicyServiceClient.Create();
            var channelClient = NotificationChannelServiceClient.Create();
            var projectName   = new ProjectName(projectId);

            File.WriteAllText(filePath, JsonConvert.SerializeObject(
                                  new BackupRecord()
            {
                ProjectId = projectId,
                Policies  = policyClient.ListAlertPolicies(projectName),
                Channels  = channelClient.ListNotificationChannels(projectName)
            }, new ProtoMessageConverter()));
        }
Beispiel #10
0
 /// <summary>Snippet for UpdateNotificationChannel</summary>
 public void UpdateNotificationChannel_RequestObject()
 {
     // Snippet: UpdateNotificationChannel(UpdateNotificationChannelRequest,CallSettings)
     // Create client
     NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
     // Initialize request argument(s)
     UpdateNotificationChannelRequest request = new UpdateNotificationChannelRequest
     {
         NotificationChannel = new NotificationChannel(),
     };
     // Make the request
     NotificationChannel response = notificationChannelServiceClient.UpdateNotificationChannel(request);
     // End snippet
 }
Beispiel #11
0
        // [END monitoring_alert_list_policies]

        // [START monitoring_alert_list_channels]
        static void ListNotificationChannels(string projectId)
        {
            var client   = NotificationChannelServiceClient.Create();
            var response = client.ListNotificationChannels(new ProjectName(projectId));

            foreach (NotificationChannel channel in response)
            {
                Console.WriteLine(channel.Name);
                if (channel.DisplayName != null)
                {
                    Console.WriteLine(channel.DisplayName);
                }
                Console.WriteLine();
            }
        }
Beispiel #12
0
        /// <summary>Snippet for SendNotificationChannelVerificationCode</summary>
        public void SendNotificationChannelVerificationCode_RequestObject()
        {
            // Snippet: SendNotificationChannelVerificationCode(SendNotificationChannelVerificationCodeRequest,CallSettings)
            // Create client
            NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
            // Initialize request argument(s)
            SendNotificationChannelVerificationCodeRequest request = new SendNotificationChannelVerificationCodeRequest
            {
                NotificationChannelName = new NotificationChannelName("[PROJECT]", "[NOTIFICATION_CHANNEL]"),
            };

            // Make the request
            notificationChannelServiceClient.SendNotificationChannelVerificationCode(request);
            // End snippet
        }
Beispiel #13
0
 /// <summary>Snippet for CreateNotificationChannel</summary>
 public void CreateNotificationChannel_RequestObject()
 {
     // Snippet: CreateNotificationChannel(CreateNotificationChannelRequest,CallSettings)
     // Create client
     NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
     // Initialize request argument(s)
     CreateNotificationChannelRequest request = new CreateNotificationChannelRequest
     {
         ProjectName         = new ProjectName("[PROJECT]"),
         NotificationChannel = new NotificationChannel(),
     };
     // Make the request
     NotificationChannel response = notificationChannelServiceClient.CreateNotificationChannel(request);
     // End snippet
 }
Beispiel #14
0
        /// <summary>Snippet for ListNotificationChannels</summary>
        public void ListNotificationChannels_RequestObject()
        {
            // Snippet: ListNotificationChannels(ListNotificationChannelsRequest,CallSettings)
            // Create client
            NotificationChannelServiceClient notificationChannelServiceClient = NotificationChannelServiceClient.Create();
            // Initialize request argument(s)
            ListNotificationChannelsRequest request = new ListNotificationChannelsRequest
            {
                ProjectName = new ProjectName("[PROJECT]"),
            };
            // Make the request
            PagedEnumerable <ListNotificationChannelsResponse, NotificationChannel> response =
                notificationChannelServiceClient.ListNotificationChannels(request);

            // Iterate over all response items, lazily performing RPCs as required
            foreach (NotificationChannel item in response)
            {
                // Do something with each item
                Console.WriteLine(item);
            }

            // Or iterate over pages (of server-defined size), performing one RPC per page
            foreach (ListNotificationChannelsResponse page in response.AsRawResponses())
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (NotificationChannel item in page)
                {
                    Console.WriteLine(item);
                }
            }

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int pageSize = 10;
            Page <NotificationChannel> singlePage = response.ReadPage(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (NotificationChannel item in singlePage)
            {
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
Beispiel #15
0
    public NotificationChannel EnableNotificationChannel(
        string channelName = "projects/your-project-id/notificationChannels/123")
    {
        var client = NotificationChannelServiceClient.Create();
        NotificationChannel channel = new NotificationChannel();

        channel.Enabled = true;
        channel.Name    = channelName;
        var fieldMask = new FieldMask {
            Paths = { "enabled" }
        };

        channel = client.UpdateNotificationChannel(
            updateMask: fieldMask,
            notificationChannel: channel);
        Console.WriteLine("Enabled {0}.", channel.Name);
        return(channel);
    }
Beispiel #16
0
        static void DeleteNotificationChannel(string channelNameString,
                                              bool force)
        {
            var client = NotificationChannelServiceClient.Create();
            NotificationChannelName channelName;

            if (!NotificationChannelName.TryParse(channelNameString,
                                                  out channelName))
            {
                string message = string.Format(
                    @"{0} is not a valid notification channel name.
                    Channel names look like this: {1}.",
                    channelNameString,
                    new NotificationChannelName("project-id",
                                                "notification-channel-id"));
                throw new Exception(message);
            }
            client.DeleteNotificationChannel(channelName, force);
            Console.WriteLine($"Deleted {channelName}.");
        }
Beispiel #17
0
        // [END monitoring_alert_backup_policies]

        // [START monitoring_alert_restore_policies]
        // [START monitoring_alert_create_policy]
        // [START monitoring_alert_create_channel]
        // [START monitoring_alert_update_channel]
        static void RestorePolicies(string projectId, string filePath)
        {
            var policyClient            = AlertPolicyServiceClient.Create();
            var channelClient           = NotificationChannelServiceClient.Create();
            List <Exception> exceptions = new List <Exception>();
            var backup = JsonConvert.DeserializeObject <BackupRecord>(
                File.ReadAllText(filePath), new ProtoMessageConverter());
            var  projectName   = new ProjectName(projectId);
            bool isSameProject = projectId == backup.ProjectId;
            // When a channel is recreated, rather than updated, it will get
            // a new name.  We have to update the AlertPolicy with the new
            // name.  Track the names in this map.
            var channelNameMap = new Dictionary <string, string>();

            foreach (NotificationChannel channel in backup.Channels)
            {
                // [END monitoring_alert_create_policy]
                try
                {
                    bool updated = false;
                    Console.WriteLine("Updating channel.\n{0}",
                                      channel.DisplayName);
                    // This field is immutable and it is illegal to specify a
                    // non-default value (UNVERIFIED or VERIFIED) in the
                    // Create() or Update() operations.
                    channel.VerificationStatus = NotificationChannel.Types
                                                 .VerificationStatus.Unspecified;
                    if (isSameProject)
                    {
                        try
                        {
                            channelClient.UpdateNotificationChannel(
                                null, channel);
                            updated = true;
                        }
                        catch (Grpc.Core.RpcException e)
                            when(e.Status.StatusCode == StatusCode.NotFound)
                            {
                            }
                    }
                    if (!updated)
                    {
                        // The channel no longer exists.  Recreate it.
                        string oldName = channel.Name;
                        channel.Name = null;
                        var response = channelClient.CreateNotificationChannel(
                            projectName, channel);
                        channelNameMap.Add(oldName, response.Name);
                    }
                }
                catch (Exception e)
                {
                    // If one failed, continue trying to update the others.
                    exceptions.Add(e);
                }
                // [START monitoring_alert_create_policy]
            }
            foreach (AlertPolicy policy in backup.Policies)
            {
                // [END monitoring_alert_create_channel]
                // [END monitoring_alert_update_channel]
                string policyName = policy.Name;
                // These two fields cannot be set directly, so clear them.
                policy.CreationRecord = null;
                policy.MutationRecord = null;
                // Update channel names if the channel was recreated with
                // another name.
                for (int i = 0; i < policy.NotificationChannels.Count; ++i)
                {
                    if (channelNameMap.ContainsKey(policy.NotificationChannels[i]))
                    {
                        policy.NotificationChannels[i] =
                            channelNameMap[policy.NotificationChannels[i]];
                    }
                }
                try
                {
                    Console.WriteLine("Updating policy.\n{0}",
                                      policy.DisplayName);
                    bool updated = false;
                    if (isSameProject)
                    {
                        try
                        {
                            policyClient.UpdateAlertPolicy(null, policy);
                            updated = true;
                        }
                        catch (Grpc.Core.RpcException e)
                            when(e.Status.StatusCode == StatusCode.NotFound)
                            {
                            }
                    }
                    if (!updated)
                    {
                        // The policy no longer exists.  Recreate it.
                        policy.Name = null;
                        foreach (var condition in policy.Conditions)
                        {
                            condition.Name = null;
                        }
                        policyClient.CreateAlertPolicy(projectName, policy);
                    }
                    Console.WriteLine("Restored {0}.", policyName);
                }
                catch (Exception e)
                {
                    // If one failed, continue trying to update the others.
                    exceptions.Add(e);
                }
                // [START monitoring_alert_create_channel]
                // [START monitoring_alert_update_channel]
            }
            if (exceptions.Count > 0)
            {
                throw new AggregateException(exceptions);
            }
        }