Exemplo n.º 1
0
        public async Task CreateListUpdateDelete()
        {
            Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var testBase = new ApiManagementTestBase(context);
                testBase.TryCreateApiManagementService();

                // list diagnostics: there should be none
                var diagnostics = testBase.client.Diagnostic.ListByService(
                    testBase.rgName,
                    testBase.serviceName,
                    null);

                Assert.NotNull(diagnostics);
                Assert.Empty(diagnostics);

                // create new diagnostic, supported Ids are applicationinsights, azuremonitor
                string diagnosticId = "applicationinsights";
                string loggerId     = TestUtilities.GenerateName("appInsights");

                try
                {
                    // create a logger
                    Guid applicationInsightsGuid = TestUtilities.GenerateGuid("appInsights");
                    var  credentials             = new Dictionary <string, string>();
                    credentials.Add("instrumentationKey", applicationInsightsGuid.ToString());

                    var loggerCreateParameters = new LoggerContract(LoggerType.ApplicationInsights, credentials: credentials);
                    var loggerContract         = await testBase.client.Logger.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        loggerId,
                        loggerCreateParameters);

                    Assert.NotNull(loggerContract);
                    Assert.Equal(loggerId, loggerContract.Name);
                    Assert.Equal(LoggerType.ApplicationInsights, loggerContract.LoggerType);
                    Assert.NotNull(loggerContract.Credentials);
                    Assert.Equal(1, loggerContract.Credentials.Keys.Count);

                    // create a diagnostic entity with just loggerId
                    var diagnosticContractParams = new DiagnosticContract()
                    {
                        LoggerId = loggerContract.Id
                    };
                    var diagnosticContract = await testBase.client.Diagnostic.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId,
                        diagnosticContractParams);

                    Assert.NotNull(diagnosticContract);
                    Assert.Equal(diagnosticId, diagnosticContract.Name);

                    // check the diagnostic entity etag
                    var diagnosticTag = await testBase.client.Diagnostic.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId);

                    Assert.NotNull(diagnosticTag);
                    Assert.NotNull(diagnosticTag.ETag);

                    // now update the sampling and other settings of the diagnostic
                    diagnosticContractParams.AlwaysLog = "allErrors";
                    diagnosticContractParams.Sampling  = new SamplingSettings("fixed", 50);
                    var listOfHeaders = new List <string> {
                        "Content-type"
                    };
                    var bodyDiagnostic = new BodyDiagnosticSettings(512);
                    diagnosticContractParams.Frontend = new PipelineDiagnosticSettings
                    {
                        Request = new HttpMessageDiagnostic()
                        {
                            Body    = bodyDiagnostic,
                            Headers = listOfHeaders
                        },
                        Response = new HttpMessageDiagnostic()
                        {
                            Body    = bodyDiagnostic,
                            Headers = listOfHeaders
                        }
                    };
                    diagnosticContractParams.Backend = new PipelineDiagnosticSettings
                    {
                        Request = new HttpMessageDiagnostic()
                        {
                            Body    = bodyDiagnostic,
                            Headers = listOfHeaders
                        },
                        Response = new HttpMessageDiagnostic()
                        {
                            Body    = bodyDiagnostic,
                            Headers = listOfHeaders
                        }
                    };

                    var updatedDiagnostic = await testBase.client.Diagnostic.CreateOrUpdateWithHttpMessagesAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId,
                        diagnosticContractParams,
                        diagnosticTag.ETag);

                    Assert.NotNull(updatedDiagnostic);
                    Assert.Equal("allErrors", updatedDiagnostic.Body.AlwaysLog);
                    Assert.NotNull(updatedDiagnostic.Body.Sampling);
                    Assert.NotNull(updatedDiagnostic.Body.Frontend);
                    Assert.NotNull(updatedDiagnostic.Body.Backend);
                    Assert.NotNull(updatedDiagnostic.Body.HttpCorrelationProtocol);
                    Assert.Equal(HttpCorrelationProtocol.Legacy, updatedDiagnostic.Body.HttpCorrelationProtocol);

                    // delete the diagnostic entity
                    await testBase.client.Diagnostic.DeleteAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId,
                        updatedDiagnostic.Headers.ETag);

                    Assert.Throws <ErrorResponseException>(()
                                                           => testBase.client.Diagnostic.GetEntityTag(testBase.rgName, testBase.serviceName, diagnosticId));

                    // check the logger entity etag
                    var loggerTag = await testBase.client.Logger.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        loggerId);

                    Assert.NotNull(loggerTag);
                    Assert.NotNull(loggerTag.ETag);

                    // delete the logger entity
                    await testBase.client.Logger.DeleteAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        loggerId,
                        loggerTag.ETag);

                    Assert.Throws <ErrorResponseException>(()
                                                           => testBase.client.Logger.GetEntityTag(testBase.rgName, testBase.serviceName, loggerId));
                }
                finally
                {
                    testBase.client.Diagnostic.Delete(testBase.rgName, testBase.serviceName, diagnosticId, "*");
                    testBase.client.Logger.Delete(testBase.rgName, testBase.serviceName, loggerId, "*");

                    // clean up all properties
                    var listOfProperties = testBase.client.NamedValue.ListByService(
                        testBase.rgName,
                        testBase.serviceName);
                    foreach (var property in listOfProperties)
                    {
                        testBase.client.NamedValue.Delete(
                            testBase.rgName,
                            testBase.serviceName,
                            property.Name,
                            "*");
                    }
                }
            }
        }
 /// <summary>
 /// Creates or Updates a logger.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// The name of the API Management service.
 /// </param>
 /// <param name='loggerId'>
 /// Logger identifier. Must be unique in the API Management service instance.
 /// </param>
 /// <param name='parameters'>
 /// Create parameters.
 /// </param>
 /// <param name='ifMatch'>
 /// ETag of the Entity. Not required when creating an entity, but required when
 /// updating an entity.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <LoggerContract> CreateOrUpdateAsync(this ILoggerOperations operations, string resourceGroupName, string serviceName, string loggerId, LoggerContract parameters, string ifMatch = default(string), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, serviceName, loggerId, parameters, ifMatch, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
 /// <summary>
 /// Creates or Updates a logger.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// The name of the API Management service.
 /// </param>
 /// <param name='loggerId'>
 /// Logger identifier. Must be unique in the API Management service instance.
 /// </param>
 /// <param name='parameters'>
 /// Create parameters.
 /// </param>
 /// <param name='ifMatch'>
 /// ETag of the Entity. Not required when creating an entity, but required when
 /// updating an entity.
 /// </param>
 public static LoggerContract CreateOrUpdate(this ILoggerOperations operations, string resourceGroupName, string serviceName, string loggerId, LoggerContract parameters, string ifMatch = default(string))
 {
     return(operations.CreateOrUpdateAsync(resourceGroupName, serviceName, loggerId, parameters, ifMatch).GetAwaiter().GetResult());
 }
Exemplo n.º 4
0
        public async Task CreateListUpdateDeleteEventHub()
        {
            Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var testBase = new ApiManagementTestBase(context);
                testBase.TryCreateApiManagementService();

                string newloggerId           = TestUtilities.GenerateName("newlogger");
                string eventHubNameSpaceName = TestUtilities.GenerateName("eventHubNamespace");
                string eventHubName          = TestUtilities.GenerateName("eventhubname");

                try
                {
                    // first create the event hub namespace
                    var eventHubNamespace = testBase.eventHubClient.Namespaces.CreateOrUpdate(
                        testBase.rgName,
                        eventHubNameSpaceName,
                        new NamespaceCreateOrUpdateParameters(testBase.location));
                    Assert.NotNull(eventHubNamespace);
                    Assert.NotNull(eventHubNamespace.Name);

                    // then create eventhub
                    var eventHub = testBase.eventHubClient.EventHubs.CreateOrUpdate(
                        testBase.rgName,
                        eventHubNameSpaceName,
                        eventHubName,
                        new EventHubCreateOrUpdateParameters(testBase.location));
                    Assert.NotNull(eventHub);

                    // create send policy auth rule
                    string sendPolicy       = TestUtilities.GenerateName("sendPolicy");
                    var    eventHubAuthRule = testBase.eventHubClient.EventHubs.CreateOrUpdateAuthorizationRule(
                        testBase.rgName,
                        eventHubNameSpaceName,
                        eventHubName,
                        sendPolicy,
                        new SharedAccessAuthorizationRuleCreateOrUpdateParameters()
                    {
                        Rights = new List <AccessRights?>()
                        {
                            AccessRights.Send
                        }
                    });

                    // get the keys
                    var eventHubKeys = testBase.eventHubClient.EventHubs.ListKeys(
                        testBase.rgName,
                        eventHubNameSpaceName,
                        eventHubName,
                        sendPolicy);

                    // now create logger using the eventhub
                    var credentials = new Dictionary <string, string>();
                    credentials.Add("name", eventHubName);
                    credentials.Add("connectionString", eventHubKeys.PrimaryConnectionString);

                    var loggerCreateParameters = new LoggerContract(LoggerType.AzureEventHub, credentials);
                    // create new group with default parameters
                    string loggerDescription = TestUtilities.GenerateName("newloggerDescription");
                    loggerCreateParameters.Description = loggerDescription;

                    var loggerContract = testBase.client.Logger.CreateOrUpdate(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId,
                        loggerCreateParameters);

                    Assert.NotNull(loggerContract);
                    Assert.Equal(newloggerId, loggerContract.Name);
                    Assert.True(loggerContract.IsBuffered);
                    Assert.Equal(LoggerType.AzureEventHub, loggerContract.LoggerType);
                    Assert.NotNull(loggerContract.Credentials);
                    Assert.Equal(2, loggerContract.Credentials.Keys.Count);

                    var listLoggers = testBase.client.Logger.ListByService(
                        testBase.rgName,
                        testBase.serviceName,
                        null);

                    Assert.NotNull(listLoggers);
                    // there should be one user
                    Assert.True(listLoggers.Count() >= 1);

                    // get the logger tag
                    var loggerTag = await testBase.client.Logger.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId);

                    Assert.NotNull(loggerTag);
                    Assert.NotNull(loggerTag.ETag);

                    // patch logger
                    string patchedDescription = TestUtilities.GenerateName("patchedDescription");
                    testBase.client.Logger.Update(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId,
                        new LoggerUpdateContract(LoggerType.AzureEventHub)
                    {
                        Description = patchedDescription
                    },
                        loggerTag.ETag);

                    // get to check it was patched
                    loggerContract = await testBase.client.Logger.GetAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId);

                    Assert.NotNull(loggerContract);
                    Assert.Equal(newloggerId, loggerContract.Name);
                    Assert.Equal(patchedDescription, loggerContract.Description);
                    Assert.NotNull(loggerContract.Credentials);
                    Assert.NotNull(loggerContract.CredentialsPropertyName);

                    // get the logger tag
                    loggerTag = await testBase.client.Logger.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId);

                    Assert.NotNull(loggerTag);
                    Assert.NotNull(loggerTag.ETag);

                    // delete the logger
                    testBase.client.Logger.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId,
                        loggerTag.ETag);

                    // get the deleted logger to make sure it was deleted
                    try
                    {
                        testBase.client.Logger.Get(testBase.rgName, testBase.serviceName, newloggerId);
                        throw new Exception("This code should not have been executed.");
                    }
                    catch (ErrorResponseException ex)
                    {
                        Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode);
                    }
                }
                finally
                {
                    testBase.client.Logger.Delete(testBase.rgName, testBase.serviceName, newloggerId, "*");
                    // clean up all properties
                    var listOfProperties = testBase.client.Property.ListByService(
                        testBase.rgName,
                        testBase.serviceName);
                    foreach (var property in listOfProperties)
                    {
                        testBase.client.Property.Delete(
                            testBase.rgName,
                            testBase.serviceName,
                            property.Name,
                            "*");
                    }
                    testBase.eventHubClient.EventHubs.Delete(testBase.rgName, eventHubNameSpaceName, eventHubName);
                    testBase.eventHubClient.Namespaces.Delete(testBase.rgName, eventHubNameSpaceName);
                }
            }
        }
Exemplo n.º 5
0
        public async Task CreateListUpdateDeleteApplicationInsights()
        {
            Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var testBase = new ApiManagementTestBase(context);
                testBase.TryCreateApiManagementService();

                string newloggerId = TestUtilities.GenerateName("applicationInsight");

                try
                {
                    // now create logger using the event
                    Guid applicationInsightsGuid = TestUtilities.GenerateGuid("appInsights");
                    var  credentials             = new Dictionary <string, string>();
                    credentials.Add("instrumentationKey", applicationInsightsGuid.ToString());

                    var loggerCreateParameters = new LoggerContract(LoggerType.ApplicationInsights, credentials);
                    // create new group with default parameters
                    string loggerDescription = TestUtilities.GenerateName("newloggerDescription");
                    loggerCreateParameters.Description = loggerDescription;

                    var loggerContract = testBase.client.Logger.CreateOrUpdate(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId,
                        loggerCreateParameters);

                    Assert.NotNull(loggerContract);
                    Assert.Equal(newloggerId, loggerContract.Name);
                    Assert.Equal(LoggerType.ApplicationInsights, loggerContract.LoggerType);
                    Assert.NotNull(loggerContract.Credentials);
                    Assert.Equal(1, loggerContract.Credentials.Keys.Count);

                    var listLoggers = testBase.client.Logger.ListByService(
                        testBase.rgName,
                        testBase.serviceName,
                        null);

                    Assert.NotNull(listLoggers);
                    // there should be atleast one logger
                    Assert.True(listLoggers.Count() >= 1);

                    // get the logger tag
                    var loggerTag = await testBase.client.Logger.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId);

                    Assert.NotNull(loggerTag);
                    Assert.NotNull(loggerTag.ETag);

                    // patch logger
                    string patchedDescription = TestUtilities.GenerateName("patchedDescription");
                    testBase.client.Logger.Update(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId,
                        new LoggerUpdateContract(LoggerType.ApplicationInsights)
                    {
                        Description = patchedDescription
                    },
                        loggerTag.ETag);

                    // get to check it was patched
                    loggerContract = await testBase.client.Logger.GetAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId);

                    Assert.NotNull(loggerContract);
                    Assert.Equal(newloggerId, loggerContract.Name);
                    Assert.Equal(patchedDescription, loggerContract.Description);
                    Assert.NotNull(loggerContract.Credentials);
                    Assert.NotNull(loggerContract.CredentialsPropertyName);

                    // get the logger tag
                    loggerTag = await testBase.client.Logger.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId);

                    Assert.NotNull(loggerTag);
                    Assert.NotNull(loggerTag.ETag);

                    // delete the logger
                    testBase.client.Logger.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        newloggerId,
                        loggerTag.ETag);

                    // get the deleted logger to make sure it was deleted
                    try
                    {
                        testBase.client.Logger.Get(testBase.rgName, testBase.serviceName, newloggerId);
                        throw new Exception("This code should not have been executed.");
                    }
                    catch (ErrorResponseException ex)
                    {
                        Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode);
                    }
                }
                finally
                {
                    testBase.client.Logger.Delete(testBase.rgName, testBase.serviceName, newloggerId, "*");
                    var listOfProperties = testBase.client.Property.ListByService(
                        testBase.rgName,
                        testBase.serviceName);

                    foreach (var property in listOfProperties)
                    {
                        testBase.client.Property.Delete(
                            testBase.rgName,
                            testBase.serviceName,
                            property.Name,
                            "*");
                    }
                }
            }
        }
Exemplo n.º 6
0
        public async Task CreateListUpdateDelete()
        {
            Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var testBase = new ApiManagementTestBase(context);
                testBase.TryCreateApiManagementService();

                // list diagnostics: there should be none
                var diagnostics = testBase.client.Diagnostic.ListByService(
                    testBase.rgName,
                    testBase.serviceName,
                    null);

                Assert.NotNull(diagnostics);
                Assert.Empty(diagnostics);

                // create new diagnostic, supported Ids are applicationinsights, azuremonitor
                string diagnosticId = "applicationinsights";
                string loggerId     = TestUtilities.GenerateName("appInsights");

                try
                {
                    // create a diagnostic entity
                    var diagnosticContractParams = new DiagnosticContract()
                    {
                        Enabled = true
                    };
                    var diagnosticContract = await testBase.client.Diagnostic.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId,
                        diagnosticContractParams);

                    Assert.NotNull(diagnosticContract);
                    Assert.Equal(diagnosticId, diagnosticContract.Name);
                    Assert.Equal(diagnosticContractParams.Enabled, diagnosticContract.Enabled);

                    // create a logger
                    Guid applicationInsightsGuid = TestUtilities.GenerateGuid("appInsights");
                    var  credentials             = new Dictionary <string, string>();
                    credentials.Add("instrumentationKey", applicationInsightsGuid.ToString());

                    var loggerCreateParameters = new LoggerContract(LoggerType.ApplicationInsights, credentials);
                    var loggerContract         = await testBase.client.Logger.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        loggerId,
                        loggerCreateParameters);

                    Assert.NotNull(loggerContract);
                    Assert.Equal(loggerId, loggerContract.Name);
                    Assert.Equal(LoggerType.ApplicationInsights, loggerContract.LoggerType);
                    Assert.NotNull(loggerContract.Credentials);
                    Assert.Equal(1, loggerContract.Credentials.Keys.Count);

                    // create a diagnostic logger
                    loggerContract = await testBase.client.DiagnosticLogger.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId,
                        loggerId);

                    Assert.NotNull(loggerContract);
                    Assert.Equal(loggerId, loggerContract.Name);
                    Assert.Equal(LoggerType.ApplicationInsights, loggerContract.LoggerType);

                    //list diagnostic loggers
                    var loggerContractList = await testBase.client.DiagnosticLogger.ListByServiceAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId);

                    Assert.NotNull(loggerContractList);
                    Assert.Single(loggerContractList);
                    Assert.Equal(loggerId, loggerContractList.GetEnumerator().ToIEnumerable().First().Name);
                    Assert.Equal(LoggerType.ApplicationInsights, loggerContractList.GetEnumerator().ToIEnumerable().First().LoggerType);

                    // check the diagnostic entity etag
                    var diagnosticTag = await testBase.client.Diagnostic.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId);

                    Assert.NotNull(diagnosticTag);
                    Assert.NotNull(diagnosticTag.ETag);

                    // delete the diagnostic entity
                    await testBase.client.Diagnostic.DeleteAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        diagnosticId,
                        diagnosticTag.ETag);

                    Assert.Throws <ErrorResponseException>(()
                                                           => testBase.client.Diagnostic.GetEntityTag(testBase.rgName, testBase.serviceName, diagnosticId));

                    // check the logger entity etag
                    var loggerTag = await testBase.client.Logger.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        loggerId);

                    Assert.NotNull(loggerTag);
                    Assert.NotNull(loggerTag.ETag);

                    // delete the logger entity
                    await testBase.client.Logger.DeleteAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        loggerId,
                        loggerTag.ETag);

                    Assert.Throws <ErrorResponseException>(()
                                                           => testBase.client.Logger.GetEntityTag(testBase.rgName, testBase.serviceName, loggerId));
                }
                finally
                {
                    testBase.client.Diagnostic.Delete(testBase.rgName, testBase.serviceName, diagnosticId, "*");
                    testBase.client.Logger.Delete(testBase.rgName, testBase.serviceName, loggerId, "*");
                }
            }
        }