Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MetricsAdvisor] Made NotificationHook constructors parameterless #18475

Merged
merged 5 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ Page<DataFeed> NextPageFunc(string nextLink, int? pageSizeHint)
/// <exception cref="ArgumentException"><paramref name="dataFeed"/>.Name is empty.</exception>
public virtual async Task<Response<string>> CreateDataFeedAsync(DataFeed dataFeed, CancellationToken cancellationToken = default)
{
ValidateDataFeedToCreate(dataFeed);
ValidateDataFeedToCreate(dataFeed, nameof(dataFeed));

using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsAdvisorAdministrationClient)}.{nameof(CreateDataFeed)}");
scope.Start();
Expand Down Expand Up @@ -311,7 +311,7 @@ public virtual async Task<Response<string>> CreateDataFeedAsync(DataFeed dataFee
/// <exception cref="ArgumentException"><paramref name="dataFeed"/>.Name is empty.</exception>
public virtual Response<string> CreateDataFeed(DataFeed dataFeed, CancellationToken cancellationToken = default)
{
ValidateDataFeedToCreate(dataFeed);
ValidateDataFeedToCreate(dataFeed, nameof(dataFeed));

using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsAdvisorAdministrationClient)}.{nameof(CreateDataFeed)}");
scope.Start();
Expand Down Expand Up @@ -680,15 +680,15 @@ private static IReadOnlyList<DataFeed> ConvertToDataFeeds(IReadOnlyList<DataFeed
return dataFeeds;
}

private static void ValidateDataFeedToCreate(DataFeed dataFeed)
private static void ValidateDataFeedToCreate(DataFeed dataFeed, string paramName)
{
Argument.AssertNotNull(dataFeed, nameof(dataFeed));
Argument.AssertNotNullOrEmpty(dataFeed.Name, $"{nameof(dataFeed)}.{nameof(dataFeed.Name)}");
Argument.AssertNotNull(dataFeed.DataSource, $"{nameof(dataFeed)}.{nameof(dataFeed.DataSource)}");
Argument.AssertNotNull(dataFeed.Granularity, $"{nameof(dataFeed)}.{nameof(dataFeed.Granularity)}");
Argument.AssertNotNull(dataFeed.Schema, $"{nameof(dataFeed)}.{nameof(dataFeed.Schema)}");
Argument.AssertNotNull(dataFeed.IngestionSettings, $"{nameof(dataFeed)}.{nameof(dataFeed.IngestionSettings)}");
Argument.AssertNotNull(dataFeed.IngestionSettings.IngestionStartTime, $"{nameof(dataFeed)}.{nameof(dataFeed.IngestionSettings)}.{nameof(dataFeed.IngestionSettings.IngestionStartTime)}");
Argument.AssertNotNull(dataFeed, paramName);
Argument.AssertNotNullOrEmpty(dataFeed.Name, $"{paramName}.{nameof(dataFeed.Name)}");
Argument.AssertNotNull(dataFeed.DataSource, $"{paramName}.{nameof(dataFeed.DataSource)}");
Argument.AssertNotNull(dataFeed.Granularity, $"{paramName}.{nameof(dataFeed.Granularity)}");
Argument.AssertNotNull(dataFeed.Schema, $"{paramName}.{nameof(dataFeed.Schema)}");
Argument.AssertNotNull(dataFeed.IngestionSettings, $"{paramName}.{nameof(dataFeed.IngestionSettings)}");
Argument.AssertNotNull(dataFeed.IngestionSettings.IngestionStartTime, $"{paramName}.{nameof(dataFeed.IngestionSettings)}.{nameof(dataFeed.IngestionSettings.IngestionStartTime)}");
}

#endregion DataFeed
Expand All @@ -708,7 +708,7 @@ private static void ValidateDataFeedToCreate(DataFeed dataFeed)
/// <exception cref="ArgumentException"><paramref name="detectionConfiguration"/>.MetricId or <paramref name="detectionConfiguration"/>.Name is empty.</exception>
public virtual async Task<Response<string>> CreateDetectionConfigurationAsync(AnomalyDetectionConfiguration detectionConfiguration, CancellationToken cancellationToken = default)
{
ValidateDetectionConfigurationToCreate(detectionConfiguration);
ValidateDetectionConfigurationToCreate(detectionConfiguration, nameof(detectionConfiguration));

using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsAdvisorAdministrationClient)}.{nameof(CreateDetectionConfiguration)}");
scope.Start();
Expand Down Expand Up @@ -740,7 +740,7 @@ public virtual async Task<Response<string>> CreateDetectionConfigurationAsync(An
/// <exception cref="ArgumentException"><paramref name="detectionConfiguration"/>.MetricId or <paramref name="detectionConfiguration"/>.Name is empty.</exception>
public virtual Response<string> CreateDetectionConfiguration(AnomalyDetectionConfiguration detectionConfiguration, CancellationToken cancellationToken = default)
{
ValidateDetectionConfigurationToCreate(detectionConfiguration);
ValidateDetectionConfigurationToCreate(detectionConfiguration, nameof(detectionConfiguration));

using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsAdvisorAdministrationClient)}.{nameof(CreateDetectionConfiguration)}");
scope.Start();
Expand Down Expand Up @@ -1009,12 +1009,12 @@ public virtual Response DeleteDetectionConfiguration(string detectionConfigurati
}
}

private static void ValidateDetectionConfigurationToCreate(AnomalyDetectionConfiguration configuration)
private static void ValidateDetectionConfigurationToCreate(AnomalyDetectionConfiguration configuration, string paramName)
{
Argument.AssertNotNull(configuration, nameof(configuration));
Argument.AssertNotNullOrEmpty(configuration.MetricId, $"{nameof(configuration)}.{nameof(AnomalyDetectionConfiguration.MetricId)}");
Argument.AssertNotNullOrEmpty(configuration.Name, $"{nameof(configuration)}.{nameof(AnomalyDetectionConfiguration.Name)}");
Argument.AssertNotNull(configuration.WholeSeriesDetectionConditions, $"{nameof(configuration)}.{nameof(AnomalyDetectionConfiguration.WholeSeriesDetectionConditions)}");
Argument.AssertNotNull(configuration, paramName);
Argument.AssertNotNullOrEmpty(configuration.MetricId, $"{paramName}.{nameof(AnomalyDetectionConfiguration.MetricId)}");
Argument.AssertNotNullOrEmpty(configuration.Name, $"{paramName}.{nameof(AnomalyDetectionConfiguration.Name)}");
Argument.AssertNotNull(configuration.WholeSeriesDetectionConditions, $"{paramName}.{nameof(AnomalyDetectionConfiguration.WholeSeriesDetectionConditions)}");
}

#endregion AnomalyDetectionConfiguration
Expand Down Expand Up @@ -1354,12 +1354,7 @@ public virtual Response DeleteAlertConfiguration(string alertConfigurationId, Ca
/// <exception cref="ArgumentException"><paramref name="hook"/> is an <see cref="EmailNotificationHook"/> and <paramref name="hook"/>.EmailsToAlert is empty.</exception>
public virtual async Task<Response<string>> CreateHookAsync(NotificationHook hook, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(hook, nameof(hook));

if (hook is EmailNotificationHook emailHook)
{
Argument.AssertNotNullOrEmpty(emailHook.EmailsToAlert, nameof(EmailNotificationHook.EmailsToAlert));
}
ValidateHookToCreate(hook, nameof(hook));

using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsAdvisorAdministrationClient)}.{nameof(CreateHook)}");
scope.Start();
Expand Down Expand Up @@ -1391,12 +1386,7 @@ public virtual async Task<Response<string>> CreateHookAsync(NotificationHook hoo
/// <exception cref="ArgumentException"><paramref name="hook"/> is an <see cref="EmailNotificationHook"/> and <paramref name="hook"/>.EmailsToAlert is empty.</exception>
public virtual Response<string> CreateHook(NotificationHook hook, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(hook, nameof(hook));

if (hook is EmailNotificationHook emailHook)
{
Argument.AssertNotNullOrEmpty(emailHook.EmailsToAlert, nameof(EmailNotificationHook.EmailsToAlert));
}
ValidateHookToCreate(hook, nameof(hook));

using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsAdvisorAdministrationClient)}.{nameof(CreateHook)}");
scope.Start();
Expand Down Expand Up @@ -1431,11 +1421,6 @@ public virtual async Task<Response> UpdateHookAsync(string hookId, NotificationH
Guid hookGuid = ClientCommon.ValidateGuid(hookId, nameof(hookId));
Argument.AssertNotNull(hook, nameof(hook));

if (hook is EmailNotificationHook emailHook)
{
Argument.AssertNotNullOrEmpty(emailHook.EmailsToAlert, nameof(EmailNotificationHook.EmailsToAlert));
}

using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsAdvisorAdministrationClient)}.{nameof(UpdateHook)}");
scope.Start();

Expand Down Expand Up @@ -1468,11 +1453,6 @@ public virtual Response UpdateHook(string hookId, NotificationHook hook, Cancell
Guid hookGuid = ClientCommon.ValidateGuid(hookId, nameof(hookId));
Argument.AssertNotNull(hook, nameof(hook));

if (hook is EmailNotificationHook emailHook)
{
Argument.AssertNotNullOrEmpty(emailHook.EmailsToAlert, nameof(EmailNotificationHook.EmailsToAlert));
}

using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MetricsAdvisorAdministrationClient)}.{nameof(UpdateHook)}");
scope.Start();

Expand Down Expand Up @@ -1693,6 +1673,25 @@ Page<NotificationHook> NextPageFunc(string nextLink, int? pageSizeHint)
return PageableHelpers.CreateEnumerable(FirstPageFunc, NextPageFunc);
}

private static void ValidateHookToCreate(NotificationHook hook, string paramName)
{
Argument.AssertNotNull(hook, paramName);
Argument.AssertNotNullOrEmpty(hook.Name, $"{paramName}.{nameof(hook.Name)}");

if (hook is EmailNotificationHook emailHook)
{
Argument.AssertNotNullOrEmpty(emailHook.EmailsToAlert, $"{paramName}.{nameof(EmailNotificationHook.EmailsToAlert)}");
}
else if (hook is WebNotificationHook webHook)
{
Argument.AssertNotNullOrEmpty(webHook.Endpoint, $"{paramName}.{nameof(WebNotificationHook.Endpoint)}");
}
else
{
throw new ArgumentException($"Invalid hook type. A hook must be created from an ${nameof(EmailNotificationHook)} or a {nameof(WebNotificationHook)} instance.");
}
}

#endregion NotificationHook
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Azure.Core;

Expand All @@ -17,35 +16,28 @@ public partial class EmailNotificationHook : NotificationHook
{
/// <summary>
/// Initializes a new instance of the <see cref="EmailNotificationHook"/> class.
/// <param name="name">The name to assign to the hook.</param>
/// <param name="emailsToAlert">The list of e-mail addresses to alert.</param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="emailsToAlert"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception>
/// </summary>
public EmailNotificationHook(string name, IList<string> emailsToAlert)
: base(name)
public EmailNotificationHook()
{
Argument.AssertNotNull(emailsToAlert, nameof(emailsToAlert));

HookParameter = new EmailHookParameter(emailsToAlert);
HookType = HookType.Email;
EmailsToAlert = new ChangeTrackingList<string>();
}

internal EmailNotificationHook(HookType hookType, string id, string name, string description, string externalLink, IReadOnlyList<string> administrators, EmailHookParameter hookParameter)
: base(hookType, id, name, description, externalLink, administrators)
{
HookParameter = hookParameter;
HookType = hookType;
EmailsToAlert = hookParameter.ToList;
}

/// <summary>
/// The list of e-mail addresses to alert.
/// </summary>
public IList<string> EmailsToAlert => HookParameter.ToList;
public IList<string> EmailsToAlert { get; }

/// <summary>
/// Used by CodeGen during serialization.
/// </summary>
internal EmailHookParameter HookParameter { get; }
internal EmailHookParameter HookParameter => new EmailHookParameter(EmailsToAlert);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ namespace Azure.AI.MetricsAdvisor.Models
[CodeGenModel("HookInfo")]
public partial class NotificationHook
{
internal NotificationHook(string name)
internal NotificationHook()
{
Argument.AssertNotNullOrEmpty(name, nameof(name));

Name = name;
}

/// <summary>
Expand All @@ -30,7 +27,7 @@ internal NotificationHook(string name)
/// The name of the hook.
/// </summary>
[CodeGenMember("HookName")]
public string Name { get; }
public string Name { get; set; }

/// <summary>
/// The list of user e-mails with administrative rights to manage this hook.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Azure.Core;

Expand All @@ -17,72 +16,65 @@ public partial class WebNotificationHook : NotificationHook
{
/// <summary>
/// Initializes a new instance of the <see cref="WebNotificationHook"/> class.
/// <param name="name">The name to assign to the hook.</param>
/// <param name="endpoint">The API address to be called when an alert is triggered.</param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="endpoint"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="name"/> or <paramref name="endpoint"/> is empty.</exception>
/// </summary>
public WebNotificationHook(string name, string endpoint)
: base(name)
public WebNotificationHook()
{
Argument.AssertNotNullOrEmpty(endpoint, nameof(endpoint));

HookParameter = new WebhookHookParameter(endpoint, default, default, new ChangeTrackingDictionary<string, string>(), default, default);
kinelski marked this conversation as resolved.
Show resolved Hide resolved

HookType = HookType.Webhook;
Headers = new ChangeTrackingDictionary<string, string>();
}

internal WebNotificationHook(HookType hookType, string id, string name, string description, string externalLink, IReadOnlyList<string> administrators, WebhookHookParameter hookParameter)
: base(hookType, id, name, description, externalLink, administrators)
{
HookParameter = hookParameter;
HookType = hookType;
Endpoint = hookParameter.Endpoint;
Username = hookParameter.Username;
Password = hookParameter.Password;
CertificateKey = hookParameter.CertificateKey;
CertificatePassword = hookParameter.CertificatePassword;
Headers = hookParameter.Headers;
}

/// <summary>
/// The API address to be called when an alert is triggered.
/// </summary>
public string Endpoint { get => HookParameter.Endpoint; }
public string Endpoint { get; set; }

/// <summary>
/// The username for authenticating to the API address. Leave this blank if authentication isn't needed.
/// </summary>
public string Username { get => HookParameter.Username; set => HookParameter.Username = value; }
public string Username { get; set; }

/// <summary>
/// The password for authenticating to the API address. Leave this blank if authentication isn't needed.
/// </summary>
public string Password { get => HookParameter.Password; set => HookParameter.Password = value; }
public string Password { get; set; }

/// <summary>
/// The certificate key for authenticating to the API address. Leave this blank if authentication isn't needed.
/// </summary>
public string CertificateKey { get => HookParameter.CertificateKey; set => HookParameter.CertificateKey = value; }
public string CertificateKey { get; set; }

/// <summary>
/// The certificate password for authenticating to the API address. Leave this blank if authentication isn't needed.
/// </summary>
public string CertificatePassword { get => HookParameter.CertificatePassword; set => HookParameter.CertificatePassword = value; }
public string CertificatePassword { get; set; }

/// <summary>
/// Custom headers to send in the API call.
/// </summary>
/// <exception cref="ArgumentNullException">The value assigned to <see cref="Headers"/> is null.</exception>
#pragma warning disable CA2227 // Collection properties should be readonly
public IDictionary<string, string> Headers
{
get => HookParameter.Headers;
set
{
Argument.AssertNotNull(value, nameof(Headers));
HookParameter.Headers = value;
}
}
#pragma warning restore CA2227 // Collection properties should be readonly
public IDictionary<string, string> Headers { get; }

/// <summary>
/// Used by CodeGen during serialization.
/// </summary>
internal WebhookHookParameter HookParameter { get; private set; }
internal WebhookHookParameter HookParameter => new WebhookHookParameter(Endpoint)
{
Username = Username,
Password = Password,
CertificateKey = CertificateKey,
CertificatePassword = CertificatePassword,
Headers = Headers
maririos marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
Loading