Exemple #1
0
        public async Task Delete()
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

            var credentialName     = Recording.GenerateAlphaNumericId("credential");
            var credentialToCreate = new ServicePrincipalCredentialEntity(credentialName, "clientId", "clientSecret", "tenantId");

            string credentialId = null;

            try
            {
                DataSourceCredentialEntity createdCredential = await adminClient.CreateDataSourceCredentialAsync(credentialToCreate);

                credentialId = createdCredential.Id;

                Assert.That(credentialId, Is.Not.Null.And.Not.Empty);
            }
            finally
            {
                if (credentialId != null)
                {
                    await adminClient.DeleteDataSourceCredentialAsync(credentialId);

                    var errorCause = "credentialId is invalid";
                    Assert.That(async() => await adminClient.GetDataSourceCredentialAsync(credentialId), Throws.InstanceOf <RequestFailedException>().With.Message.Contains(errorCause));
                }
            }
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        public void CreateDataSourceCredentialValidatesArguments()
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

            Assert.That(() => adminClient.CreateDataSourceCredentialAsync(null), Throws.InstanceOf <ArgumentNullException>());
            Assert.That(() => adminClient.CreateDataSourceCredential(null), Throws.InstanceOf <ArgumentNullException>());
        }
Exemple #4
0
        public void CreateDataSourceCredentialRespectsTheCancellationToken()
        {
            MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();

            var credential = new ServicePrincipalDataSourceCredential("credentialName", "clientId", "clientSecret", "tenantId");

            using var cancellationSource = new CancellationTokenSource();
            cancellationSource.Cancel();

            Assert.That(() => adminClient.CreateDataSourceCredentialAsync(credential, cancellationSource.Token), Throws.InstanceOf <OperationCanceledException>());
            Assert.That(() => adminClient.CreateDataSourceCredential(credential, cancellationSource.Token), Throws.InstanceOf <OperationCanceledException>());
        }
Exemple #5
0
        public async Task DataSourceCredentialEntitySendsSecretDuringCreation(DataSourceCredentialEntity credential, string expectedSubstring)
        {
            MockResponse createResponse = new MockResponse(201);

            createResponse.AddHeader(new HttpHeader("Location", $"https://fakeresource.cognitiveservices.azure.com/metricsadvisor/v1.0/credentials/{FakeGuid}"));

            MockResponse getResponse = new MockResponse(200);

            getResponse.SetContent(DataSourceCredentialEntityResponseContent);

            MockTransport mockTransport = new MockTransport(createResponse, getResponse);
            MetricsAdvisorAdministrationClient adminClient = CreateInstrumentedAdministrationClient(mockTransport);

            await adminClient.CreateDataSourceCredentialAsync(credential);

            MockRequest request = mockTransport.Requests.First();
            string      content = ReadContent(request);

            Assert.That(content, Contains.Substring(expectedSubstring));
        }
Exemple #6
0
        /// <summary>
        /// Creates a data source credential using the specified <see cref="MetricsAdvisorAdministrationClient"/>.
        /// A <see cref="DisposableDataSourceCredential"/> instance is returned, from which the created credential
        /// can be obtained. Upon disposal, the created credential will be deleted.
        /// </summary>
        /// <param name="adminClient">The client to use for creating and for deleting the data source credential.</param>
        /// <param name="credential">Specifies how the created <see cref="DataSourceCredential"/> should be configured.</param>
        /// <returns>A <see cref="DisposableDataSourceCredential"/> instance from which the created credential can be obtained.</returns>
        public static async Task <DisposableDataSourceCredential> CreateDataSourceCredentialAsync(MetricsAdvisorAdministrationClient adminClient, DataSourceCredential credential)
        {
            DataSourceCredential createdCredential = await adminClient.CreateDataSourceCredentialAsync(credential);

            return(new DisposableDataSourceCredential(adminClient, createdCredential));
        }