/// <summary>
 /// Adds an API to the specified Gateway.
 /// </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='gatewayId'>
 /// Gateway entity identifier. Must be unique in the current API Management
 /// service instance. Must not have value 'managed'
 /// </param>
 /// <param name='apiId'>
 /// API identifier. Must be unique in the current API Management service
 /// instance.
 /// </param>
 /// <param name='parameters'>
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <ApiContract> CreateOrUpdateAsync(this IGatewayApiOperations operations, string resourceGroupName, string serviceName, string gatewayId, string apiId, AssociationContract parameters = default(AssociationContract), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, serviceName, gatewayId, apiId, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Пример #2
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 gateways: there should be none
                var gatewayListResponse = testBase.client.Gateway.ListByService(
                    testBase.rgName,
                    testBase.serviceName,
                    null);

                Assert.NotNull(gatewayListResponse);
                Assert.Empty(gatewayListResponse);

                // list all the APIs
                var apisResponse = testBase.client.Api.ListByService(
                    testBase.rgName,
                    testBase.serviceName,
                    null);

                Assert.NotNull(apisResponse);
                Assert.Single(apisResponse);
                Assert.Null(apisResponse.NextPageLink);
                var echoApi = apisResponse.First();

                string gatewayId        = TestUtilities.GenerateName("gatewayid");;
                string certificateId    = TestUtilities.GenerateName("certificateId");
                string hostnameConfigId = TestUtilities.GenerateName("hostnameConfigId");

                try
                {
                    var gatewayContract = new GatewayContract()
                    {
                        LocationData = new ResourceLocationDataContract()
                        {
                            City            = "Seattle",
                            CountryOrRegion = "USA",
                            District        = "King County",
                            Name            = "Microsoft"
                        },
                        Description = TestUtilities.GenerateName()
                    };

                    var createResponse = await testBase.client.Gateway.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        gatewayContract);

                    Assert.NotNull(createResponse);
                    Assert.Equal(gatewayId, createResponse.Name);
                    Assert.Equal(gatewayContract.Description, createResponse.Description);
                    Assert.NotNull(createResponse.LocationData);
                    Assert.Equal(gatewayContract.LocationData.City, createResponse.LocationData.City);
                    Assert.Equal(gatewayContract.LocationData.CountryOrRegion, createResponse.LocationData.CountryOrRegion);
                    Assert.Equal(gatewayContract.LocationData.District, createResponse.LocationData.District);
                    Assert.Equal(gatewayContract.LocationData.Name, createResponse.LocationData.Name);

                    // get the gateway to check is was created
                    var getResponse = await testBase.client.Gateway.GetWithHttpMessagesAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId);

                    Assert.NotNull(getResponse);
                    Assert.Equal(gatewayId, getResponse.Body.Name);

                    // list gateways
                    gatewayListResponse = testBase.client.Gateway.ListByService(
                        testBase.rgName,
                        testBase.serviceName,
                        null);

                    Assert.NotNull(gatewayListResponse);
                    Assert.Single(gatewayListResponse);


                    var associationContract = new AssociationContract()
                    {
                        ProvisioningState = ProvisioningState.Created
                    };

                    // assign gateway to api
                    var assignResponse = await testBase.client.GatewayApi.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        echoApi.Name,
                        associationContract);

                    Assert.NotNull(assignResponse);
                    Assert.Equal(echoApi.Name, assignResponse.Name);

                    // list gateway apis
                    var apiGatewaysResponse = await testBase.client.GatewayApi.ListByServiceAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId);

                    Assert.NotNull(apiGatewaysResponse);
                    Assert.Single(apiGatewaysResponse);
                    Assert.Equal(echoApi.Name, apiGatewaysResponse.First().Name);

                    //certificate first:
                    var base64ArrayCertificate = Convert.FromBase64String(testBase.base64EncodedTestCertificateData);
                    var cert = new X509Certificate2(base64ArrayCertificate, testBase.testCertificatePassword);

                    var certCreateResponse = testBase.client.Certificate.CreateOrUpdate(
                        testBase.rgName,
                        testBase.serviceName,
                        certificateId,
                        new CertificateCreateOrUpdateParameters
                    {
                        Data     = testBase.base64EncodedTestCertificateData,
                        Password = testBase.testCertificatePassword
                    },
                        null);

                    //GatewayCertificateAuthority:
                    var gatewayCertificateAuthority = new GatewayCertificateAuthorityContract()
                    {
                        IsTrusted = true
                    };

                    var gatewayCertificateAuthorityCreateResponse = await testBase.client.GatewayCertificateAuthority.CreateOrUpdateWithHttpMessagesAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        certificateId,
                        gatewayCertificateAuthority
                        );

                    Assert.NotNull(gatewayCertificateAuthorityCreateResponse);
                    Assert.Equal(certificateId, gatewayCertificateAuthorityCreateResponse.Body.Name);
                    Assert.True(gatewayCertificateAuthorityCreateResponse.Body.IsTrusted);

                    var gatewayCertificateAuthorityResponse = await testBase.client.GatewayCertificateAuthority.GetAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        certificateId
                        );

                    Assert.NotNull(gatewayCertificateAuthorityResponse);
                    Assert.Equal(certificateId, gatewayCertificateAuthorityResponse.Name);
                    Assert.True(gatewayCertificateAuthorityResponse.IsTrusted);

                    //delete
                    testBase.client.GatewayCertificateAuthority.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        certificateId,
                        "*");

                    //hostnameConfiguration:
                    var hostnameConfig = new GatewayHostnameConfigurationContract()
                    {
                        CertificateId = certCreateResponse.Id,
                        Hostname      = "www.contoso.com",
                        NegotiateClientCertificate = false
                    };

                    var hostnameConfigCreateResponse = await testBase.client.GatewayHostnameConfiguration.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        hostnameConfigId,
                        hostnameConfig
                        );

                    Assert.NotNull(hostnameConfigCreateResponse);
                    Assert.Equal(hostnameConfigId, hostnameConfigCreateResponse.Name);
                    Assert.Equal(hostnameConfigCreateResponse.CertificateId, hostnameConfigCreateResponse.CertificateId);
                    Assert.Equal("www.contoso.com", hostnameConfigCreateResponse.Hostname);
                    Assert.False(hostnameConfigCreateResponse.NegotiateClientCertificate);

                    var hostnameConfigResponse = await testBase.client.GatewayHostnameConfiguration.GetAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        hostnameConfigId
                        );

                    Assert.NotNull(hostnameConfigCreateResponse);
                    Assert.Equal(hostnameConfigId, hostnameConfigCreateResponse.Name);
                    Assert.Equal(hostnameConfigCreateResponse.CertificateId, hostnameConfigCreateResponse.CertificateId);
                    Assert.Equal("www.contoso.com", hostnameConfigCreateResponse.Hostname);
                    Assert.False(hostnameConfigCreateResponse.NegotiateClientCertificate);

                    //delete hostname config
                    testBase.client.GatewayHostnameConfiguration.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        hostnameConfigId,
                        "*");

                    //get latest etag for delete
                    getResponse = await testBase.client.Gateway.GetWithHttpMessagesAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId);

                    // remove the gateway
                    testBase.client.Gateway.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        gatewayId,
                        getResponse.Headers.ETag);

                    // list again to see it was removed
                    gatewayListResponse = testBase.client.Gateway.ListByService(
                        testBase.rgName,
                        testBase.serviceName,
                        null);

                    Assert.NotNull(gatewayListResponse);
                    Assert.Empty(gatewayListResponse);
                }
                finally
                {
                    try
                    {
                        testBase.client.GatewayHostnameConfiguration.Delete(testBase.rgName, testBase.serviceName, gatewayId, hostnameConfigId, "*");
                    }
                    catch (ErrorResponseException) { }
                    testBase.client.Gateway.Delete(testBase.rgName, testBase.serviceName, gatewayId, "*");
                    testBase.client.Certificate.Delete(testBase.rgName, testBase.serviceName, certificateId, "*");
                }
            }
        }
 /// <summary>
 /// Adds an API to the specified Gateway.
 /// </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='gatewayId'>
 /// Gateway entity identifier. Must be unique in the current API Management
 /// service instance. Must not have value 'managed'
 /// </param>
 /// <param name='apiId'>
 /// API identifier. Must be unique in the current API Management service
 /// instance.
 /// </param>
 /// <param name='parameters'>
 /// </param>
 public static ApiContract CreateOrUpdate(this IGatewayApiOperations operations, string resourceGroupName, string serviceName, string gatewayId, string apiId, AssociationContract parameters = default(AssociationContract))
 {
     return(operations.CreateOrUpdateAsync(resourceGroupName, serviceName, gatewayId, apiId, parameters).GetAwaiter().GetResult());
 }