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] README updates + credential code samples #22549

Merged
merged 7 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
120 changes: 90 additions & 30 deletions sdk/metricsadvisor/Azure.AI.MetricsAdvisor/README.md

Large diffs are not rendered by default.

38 changes: 20 additions & 18 deletions sdk/metricsadvisor/Azure.AI.MetricsAdvisor/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ Azure Cognitive Services Metrics Advisor is a cloud service that uses machine le
|**Sample**|**Description**|
|---|---|
|[Data feed CRUD operations][metricsadv-sample1]|Create, get, update, list, and delete data feeds|
|[Data feed ingestion operations][metricsadv-sample2]|Check and refresh a data feed's ingestion status|
|[Anomaly detection configuration CRUD operations][metricsadv-sample3]|Create, get, update, list, and delete anomaly detection configurations|
|[Hook CRUD operations][metricsadv-sample4]|Create, get, update, list, and delete hooks|
|[Anomaly alert configuration CRUD operations][metricsadv-sample5]|Create, get, update, list, and delete anomaly alert configurations|
|[Query triggered alerts][metricsadv-sample6]|Get the alerts triggered by the service|
|[Query detected anomalies][metricsadv-sample7]|Get the anomalies detected by the service|
|[Query incidents and their root causes][metricsadv-sample8]|Get the incidents created by the service and root cause analysis|
|[Query time series information][metricsadv-sample9]|Get information about the time series monitored by the service|
|[Feedback CRUD operations][metricsadv-sample10]|Create, get, and list feedback|
|[Credential entity CRUD operations][metricsadv-sample2]|Create, get, update, list, and delete credential entities|
kinelski marked this conversation as resolved.
Show resolved Hide resolved
|[Data feed ingestion operations][metricsadv-sample3]|Check and refresh a data feed's ingestion status|
|[Anomaly detection configuration CRUD operations][metricsadv-sample4]|Create, get, update, list, and delete anomaly detection configurations|
|[Hook CRUD operations][metricsadv-sample5]|Create, get, update, list, and delete hooks|
|[Anomaly alert configuration CRUD operations][metricsadv-sample6]|Create, get, update, list, and delete anomaly alert configurations|
|[Query triggered alerts][metricsadv-sample7]|Get the alerts triggered by the service|
|[Query detected anomalies][metricsadv-sample8]|Get the anomalies detected by the service|
|[Query incidents and their root causes][metricsadv-sample9]|Get the incidents created by the service and root cause analysis|
|[Query time series information][metricsadv-sample10]|Get information about the time series monitored by the service|
|[Feedback CRUD operations][metricsadv-sample11]|Create, get, and list feedback|

<!-- LINKS -->
[metricsadv-sample1]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample01_DataFeedCrudOperations.cs
[metricsadv-sample2]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_DataFeedIngestionOperations.cs
[metricsadv-sample3]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DetectionConfigurationCrudOperations.cs
[metricsadv-sample4]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_HookCrudOperations.cs
[metricsadv-sample5]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_AlertConfigurationCrudOperations.cs
[metricsadv-sample6]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_QueryTriggeredAlerts.cs
[metricsadv-sample7]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryDetectedAnomalies.cs
[metricsadv-sample8]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryIncidentsAndRootCauses.cs
[metricsadv-sample9]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryTimeSeriesInformation.cs
[metricsadv-sample10]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_FeedbackCrudOperations.cs
[metricsadv-sample2]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_CredentialEntityCrudOperations.cs
[metricsadv-sample3]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DataFeedIngestionOperations.cs
[metricsadv-sample4]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_DetectionConfigurationCrudOperations.cs
[metricsadv-sample5]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_HookCrudOperations.cs
[metricsadv-sample6]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_AlertConfigurationCrudOperations.cs
[metricsadv-sample7]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryTriggeredAlerts.cs
[metricsadv-sample8]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryDetectedAnomalies.cs
[metricsadv-sample9]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryIncidentsAndRootCauses.cs
[metricsadv-sample10]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_QueryTimeSeriesInformation.cs
[metricsadv-sample11]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample11_FeedbackCrudOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class MetricsAdvisorTestEnvironment : TestEnvironment
public string HookId => Environment.GetEnvironmentVariable("METRICSADVISOR_HOOK_ID");
public string DetectionConfigurationId => Environment.GetEnvironmentVariable("METRICSADVISOR_DETECTION_CONFIGURATION_ID");
public string AlertConfigurationId => Environment.GetEnvironmentVariable("METRICSADVISOR_ALERT_CONFIGURATION_ID");
public string CredentialEntityId => Environment.GetEnvironmentVariable("METRICSADVISOR_CREDENTIAL_ENTITY_ID");
public string AlertId => Environment.GetEnvironmentVariable("METRICSADVISOR_ALERT_ID");
public string IncidentId => Environment.GetEnvironmentVariable("METRICSADVISOR_INCIDENT_ID");
public string FeedbackId => Environment.GetEnvironmentVariable("METRICSADVISOR_FEEDBACK_ID");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Azure.AI.MetricsAdvisor.Administration;
using Azure.AI.MetricsAdvisor.Models;
using Azure.AI.MetricsAdvisor.Tests;
using NUnit.Framework;

namespace Azure.AI.MetricsAdvisor.Samples
{
public partial class MetricsAdvisorSamples : MetricsAdvisorTestEnvironment
{
[Test]
public async Task CreateAndDeleteDataSourceCredentialAsync()
{
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:CreateDataSourceCredentialAsync
#if SNIPPET
string credentialName = "<credentialName>";
#else
string credentialName = GetUniqueName();
#endif

var credentialEntity = new ServicePrincipalCredentialEntity(credentialName, "<clientId>", "<clientSecret>", "<tenantId>");

Response<DataSourceCredentialEntity> response = await adminClient.CreateDataSourceCredentialAsync(credentialEntity);

DataSourceCredentialEntity createdCredentialEntity = response.Value;

Console.WriteLine($"Credential entity ID: {createdCredentialEntity.Id}");
#endregion

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

await adminClient.DeleteDataSourceCredentialAsync(createdCredentialEntity.Id);
}

[Test]
public async Task GetDataSourceCredentialAsync()
{
string endpoint = MetricsAdvisorUri;
string subscriptionKey = MetricsAdvisorSubscriptionKey;
string apiKey = MetricsAdvisorApiKey;
var credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

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

string credentialEntityId = CredentialEntityId;

Response<DataSourceCredentialEntity> response = await adminClient.GetDataSourceCredentialAsync(credentialEntityId);

DataSourceCredentialEntity credentialEntity = response.Value;

Console.WriteLine($"Credential name: {credentialEntity.Name}");
Console.WriteLine($"Credential description: {credentialEntity.Description}");

// You can access specific properties of your credential entity depending on its kind.

if (credentialEntity.CredentialKind == DataSourceCredentialKind.ServicePrincipal)
{
Console.WriteLine("Credential of kind Service Principal:");

var servicePrincipalEntity = credentialEntity as ServicePrincipalCredentialEntity;

Console.WriteLine($" Client ID: {servicePrincipalEntity.ClientId}");
Console.WriteLine($" Tenant ID: {servicePrincipalEntity.TenantId}");
}
else if (credentialEntity.CredentialKind == DataSourceCredentialKind.ServicePrincipalInKeyVault)
{
Console.WriteLine("Credential of kind Service Principal in Key Vault:");

var servicePrincipalEntity = credentialEntity as ServicePrincipalInKeyVaultCredentialEntity;

Console.WriteLine($"Name of secret for client ID: {servicePrincipalEntity.SecretNameForClientId}");
Console.WriteLine($"Name of secret for client secret: {servicePrincipalEntity.SecretNameForClientSecret}");
}
}

[Test]
public async Task UpdateDataSourceCredentialAsync()
{
string endpoint = MetricsAdvisorUri;
string subscriptionKey = MetricsAdvisorSubscriptionKey;
string apiKey = MetricsAdvisorApiKey;
var credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

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

string credentialEntityId = CredentialEntityId;

Response<DataSourceCredentialEntity> response = await adminClient.GetDataSourceCredentialAsync(credentialEntityId);
DataSourceCredentialEntity credentialEntity = response.Value;

credentialEntity.Description = "This description was generated by a sample.";

// You can update specific properties of your credential entity depending on its kind.

if (credentialEntity.CredentialKind == DataSourceCredentialKind.ServicePrincipal)
{
var servicePrincipalEntity = credentialEntity as ServicePrincipalCredentialEntity;

servicePrincipalEntity.ClientId = "<newClientId>";

// Secrets can't be read and must be updated through specific methods.

servicePrincipalEntity.UpdateClientSecret("<newClientSecret>");
}

response = await adminClient.UpdateDataSourceCredentialAsync(credentialEntity);
DataSourceCredentialEntity updatedCredentialEntity = response.Value;

Console.WriteLine($"Updated description: {updatedCredentialEntity.Description}");
}

[Test]
public async Task GetDataSourceCredentialsAsync()
{
string endpoint = MetricsAdvisorUri;
string subscriptionKey = MetricsAdvisorSubscriptionKey;
string apiKey = MetricsAdvisorApiKey;
var credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

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

var options = new GetDataSourceCredentialsOptions()
{
MaxPageSize = 5
};

int credentialCount = 0;

await foreach (DataSourceCredentialEntity credentialEntity in adminClient.GetDataSourceCredentialsAsync(options))
{
Console.WriteLine($"Credential ID: {credentialEntity.Id}");
Console.WriteLine($"Name: {credentialEntity.Name}");
Console.WriteLine($"Description: {credentialEntity.Description}");
Console.WriteLine($"Kind: {credentialEntity.CredentialKind}");
Console.WriteLine();

// Print at most 5 credentials.
if (++credentialCount >= 5)
{
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,30 @@ public async Task MetricsAdvisorNotFound()
}
#endregion
}

[Test]

public void SettingAuthentication()
{
#region Snippet:SettingAuthentication
var dataSoure = new SqlServerDataFeedSource("<connection-string>", "<query>")
{
Authentication = SqlServerDataFeedSource.AuthenticationType.ManagedIdentity
};
#endregion
}

[Test]

public void SettingCredentialAuthentication()
{
#region Snippet:SettingCredentialAuthentication
var dataSoure = new SqlServerDataFeedSource("<connection-string>", "<query>")
{
Authentication = SqlServerDataFeedSource.AuthenticationType.ServicePrincipal,
DataSourceCredentialId = "<credentialId>"
};
#endregion
}
}
}
1 change: 1 addition & 0 deletions sdk/metricsadvisor/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extends:
METRICSADVISOR_METRIC_ID: "27e3015f-04fd-44ba-a20b-bc529a0aebae"
METRICSADVISOR_DETECTION_CONFIGURATION_ID: "fb5a6ed6-2b9e-4b72-8b0c-0046ead1c15c"
METRICSADVISOR_ALERT_CONFIGURATION_ID: "204a211a-c5f4-45f3-a30e-512fb25d1d2c"
METRICSADVISOR_CREDENTIAL_ENTITY_ID: "c4b3985c-5112-4516-9417-d655dfcacf99"
METRICSADVISOR_HOOK_ID: "ffe4e97d-dbc4-4b60-a96a-03d22ca87f00"
METRICSADVISOR_ALERT_ID: "17548749000"
METRICSADVISOR_INCIDENT_ID: "2c8a72ba0e8eb41643cc0e5cff368412-17548749000"
Expand Down