public async Task CreateAndGetWebNotificationHookWithOptionalMembers()
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

            string hookName    = Recording.GenerateAlphaNumericId("hook");
            var    endpoint    = new Uri("http://contoso.com/");
            var    description = "This hook was created to test the .NET client.";
            var    headers     = new Dictionary <string, string>()
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            var hookToCreate = new WebNotificationHook()
            {
                Name         = hookName,
                Endpoint     = endpoint,
                Description  = description,
                ExternalLink = new Uri("http://fake.endpoint.com/"),
                // TODO: add CertificateKey validation (https://github.com/Azure/azure-sdk-for-net/issues/17485)
                CertificatePassword = "******",
                Username            = "******",
                Password            = "******"
            };

            foreach (var header in headers)
            {
                hookToCreate.Headers.Add(header);
            }

            await using var disposableHook = await DisposableNotificationHook.CreateHookAsync(adminClient, hookToCreate);

            NotificationHook createdHook = disposableHook.Hook;

            Assert.That(createdHook.Id, Is.Not.Null.And.Not.Empty);
            Assert.That(createdHook.Name, Is.EqualTo(hookName));
            Assert.That(createdHook.Description, Is.EqualTo(description));
            Assert.That(createdHook.ExternalLink.AbsoluteUri, Is.EqualTo("http://fake.endpoint.com/"));
            Assert.That(createdHook.AdministratorsEmails, Is.Not.Null);
            Assert.That(createdHook.AdministratorsEmails.Single(), Is.Not.Null.And.Not.Empty);

            var createdWebHook = createdHook as WebNotificationHook;

            Assert.That(createdWebHook, Is.Not.Null);
            Assert.That(createdWebHook.Endpoint, Is.EqualTo(endpoint));
            // TODO: add CertificateKey validation (https://github.com/Azure/azure-sdk-for-net/issues/17485)
            Assert.That(createdWebHook.CertificatePassword, Is.EqualTo("certPassword"));
            Assert.That(createdWebHook.Username, Is.EqualTo("fakeUsername"));
            Assert.That(createdWebHook.Password, Is.EqualTo("fakePassword"));
            Assert.That(createdWebHook.Headers, Is.EquivalentTo(headers));
        }
예제 #2
0
        public void CreateHookValidatesArguments()
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

            var name     = "hookName";
            var endpoint = new Uri("http://fakeendpoint.com");

            var genericHook = new NotificationHook()
            {
                Name = name
            };
            var emailHook = new EmailNotificationHook()
            {
                Name = null, EmailsToAlert = { "*****@*****.**" }
            };
            var webHook = new WebNotificationHook()
            {
                Name = null, Endpoint = endpoint
            };

            Assert.That(() => adminClient.CreateHookAsync(null), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateHook(null), Throws.InstanceOf <ArgumentNullException>());

            Assert.That(() => adminClient.CreateHookAsync(emailHook), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateHook(emailHook), Throws.InstanceOf <ArgumentNullException>());

            emailHook.Name = "";
            Assert.That(() => adminClient.CreateHookAsync(emailHook), Throws.InstanceOf <ArgumentException>());
            Assert.That(() => adminClient.CreateHook(emailHook), Throws.InstanceOf <ArgumentException>());

            emailHook.Name = name;
            emailHook.EmailsToAlert.Clear();
            Assert.That(() => adminClient.CreateHookAsync(emailHook), Throws.InstanceOf <ArgumentException>());
            Assert.That(() => adminClient.CreateHook(emailHook), Throws.InstanceOf <ArgumentException>());

            Assert.That(() => adminClient.CreateHookAsync(webHook), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateHook(webHook), Throws.InstanceOf <ArgumentNullException>());

            webHook.Name = "";
            Assert.That(() => adminClient.CreateHookAsync(webHook), Throws.InstanceOf <ArgumentException>());
            Assert.That(() => adminClient.CreateHook(webHook), Throws.InstanceOf <ArgumentException>());

            webHook.Name     = name;
            webHook.Endpoint = null;
            Assert.That(() => adminClient.CreateHookAsync(webHook), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateHook(webHook), Throws.InstanceOf <ArgumentNullException>());

            Assert.That(() => adminClient.CreateHookAsync(genericHook), Throws.InstanceOf <ArgumentException>());
            Assert.That(() => adminClient.CreateHook(genericHook), Throws.InstanceOf <ArgumentException>());
        }
        public async Task NotificationHookGetsUnknownHook()
        {
            MockResponse getResponse = new MockResponse(200);

            getResponse.SetContent(UnknownHookContent);

            MetricsAdvisorAdministrationClient adminClient = CreateInstrumentedAdministrationClient(getResponse);
            NotificationHook hook = await adminClient.GetHookAsync(FakeGuid);

            Assert.That(hook.Id, Is.EqualTo(FakeGuid));
            Assert.That(hook.Name, Is.EqualTo("unknownHookName"));
            Assert.That(hook.ExternalUri.AbsoluteUri, Is.EqualTo("https://fakeuri.com/"));
            Assert.That(hook.Description, Is.EqualTo("unknown hook description"));
            Assert.That(hook.Administrators.Single(), Is.EqualTo("*****@*****.**"));
        }
예제 #4
0
        public async Task GetHookAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential);

            string hookId = HookId;

            Response <NotificationHook> response = await adminClient.GetHookAsync(hookId);

            NotificationHook hook = response.Value;

            Console.WriteLine($"Hook name: {hook.Name}");
            Console.WriteLine($"Hook description: {hook.Description}");

            Console.WriteLine($"Hook administrators emails:");
            foreach (string admin in hook.AdministratorsEmails)
            {
                Console.WriteLine($" - {admin}");
            }

            if (hook.HookType == HookType.Email)
            {
                EmailNotificationHook emailHook = hook as EmailNotificationHook;

                Console.WriteLine("Emails to alert:");
                foreach (string email in emailHook.EmailsToAlert)
                {
                    Console.WriteLine($" - {email}");
                }
            }
            else if (hook.HookType == HookType.Webhook)
            {
                WebNotificationHook webHook = hook as WebNotificationHook;

                Console.WriteLine($"Username: {webHook.Username}");
            }
        }
예제 #5
0
        public async Task CreateAndDeleteHookAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential);

            #region Snippet:CreateHookAsync
#if SNIPPET
            string hookName = "<hookName>";
#else
            string hookName = GetUniqueName();
#endif

            var emailHook = new EmailNotificationHook()
            {
                Name = hookName
            };

            emailHook.EmailsToAlert.Add("*****@*****.**");
            emailHook.EmailsToAlert.Add("*****@*****.**");

            Response <NotificationHook> response = await adminClient.CreateHookAsync(emailHook);

            NotificationHook createdHook = response.Value;

            Console.WriteLine($"Hook ID: {createdHook.Id}");
            #endregion

            // Delete the created hook to clean up the Metrics Advisor resource. Do not perform this
            // step if you intend to keep using the hook.

            await adminClient.DeleteHookAsync(createdHook.Id);
        }
예제 #6
0
        /// <summary>
        /// Creates a hook using the specified <see cref="MetricsAdvisorAdministrationClient"/>. A
        /// <see cref="DisposableNotificationHook"/> instance is returned, from which the created
        /// hook can be obtained. Upon disposal, the associated hook will be deleted.
        /// </summary>
        /// <param name="adminClient">The client to use for creating and for deleting the hook.</param>
        /// <param name="hook">Specifies how the created <see cref="NotificationHook"/> should be configured.</param>
        /// <returns>A <see cref="DisposableNotificationHook"/> instance from which the created hook can be obtained.</returns>
        public static async Task <DisposableNotificationHook> CreateHookAsync(MetricsAdvisorAdministrationClient adminClient, NotificationHook hook)
        {
            NotificationHook createdHook = await adminClient.CreateHookAsync(hook);

            return(new DisposableNotificationHook(adminClient, createdHook));
        }
예제 #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DisposableNotificationHook"/> class.
 /// </summary>
 /// <param name="adminClient">The client to use for deleting the hook upon disposal.</param>
 /// <param name="hook">The hook this instance is associated with.</param>
 private DisposableNotificationHook(MetricsAdvisorAdministrationClient adminClient, NotificationHook hook)
 {
     _adminClient = adminClient;
     Hook         = hook;
 }
        /// <summary>
        /// Creates a hook using the specified <see cref="MetricsAdvisorAdministrationClient"/>. A
        /// <see cref="DisposableNotificationHook"/> instance is returned, from which the ID of the
        /// created hook can be obtained. Upon disposal, the associated hook will be deleted.
        /// </summary>
        /// <param name="adminClient">The client to use for creating and for deleting the hook.</param>
        /// <param name="hook">Specifies how the created <see cref="NotificationHook"/> should be configured.</param>
        /// <returns>A <see cref="DisposableNotificationHook"/> instance from which the ID of the created hook can be obtained.</returns>
        public static async Task <DisposableNotificationHook> CreateHookAsync(MetricsAdvisorAdministrationClient adminClient, NotificationHook hook)
        {
            string hookId = await adminClient.CreateHookAsync(hook);

            Assert.That(hookId, Is.Not.Null.And.Not.Empty);

            return(new DisposableNotificationHook(adminClient, hookId));
        }