/// <summary>
 /// Creates or updates schema configuration for the API.
 /// </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='apiId'>
 /// API revision identifier. Must be unique in the current API Management
 /// service instance. Non-current revision has ;rev=n as a suffix where n is
 /// the revision number.
 /// </param>
 /// <param name='schemaId'>
 /// Schema id identifier. Must be unique in the current API Management service
 /// instance.
 /// </param>
 /// <param name='parameters'>
 /// The schema contents to apply.
 /// </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<SchemaContract> BeginCreateOrUpdateAsync(this IApiSchemaOperations operations, string resourceGroupName, string serviceName, string apiId, string schemaId, SchemaContract parameters, string ifMatch = default(string), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.BeginCreateOrUpdateWithHttpMessagesAsync(resourceGroupName, serviceName, apiId, schemaId, parameters, ifMatch, null, cancellationToken).ConfigureAwait(false))
     {
         return _result.Body;
     }
 }
Example #2
0
        public async Task CreateListUpdateDeleteSwaggerSchema()
        {
            Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var testBase = new ApiManagementTestBase(context);
                testBase.TryCreateApiManagementService();

                // add new api
                string newApiId    = TestUtilities.GenerateName("apiid");
                string newSchemaId = TestUtilities.GenerateName("schemaid");

                try
                {
                    string newApiName        = TestUtilities.GenerateName("apiname");
                    string newApiDescription = TestUtilities.GenerateName("apidescription");
                    string newApiPath        = "newapiPath";
                    string newApiServiceUrl  = "http://newechoapi.cloudapp.net/api";
                    string subscriptionKeyParametersHeader     = TestUtilities.GenerateName("header");
                    string subscriptionKeyQueryStringParamName = TestUtilities.GenerateName("query");

                    var createdApiContract = testBase.client.Api.CreateOrUpdate(
                        testBase.rgName,
                        testBase.serviceName,
                        newApiId,
                        new ApiCreateOrUpdateParameter
                    {
                        DisplayName = newApiName,
                        Description = newApiDescription,
                        Path        = newApiPath,
                        ServiceUrl  = newApiServiceUrl,
                        Protocols   = new List <string> {
                            Protocol.Https, Protocol.Http
                        },
                        SubscriptionKeyParameterNames = new SubscriptionKeyParameterNamesContract
                        {
                            Header = subscriptionKeyParametersHeader,
                            Query  = subscriptionKeyQueryStringParamName
                        }
                    });

                    // get new api to check it was added
                    var apiGetResponse = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId);

                    Assert.NotNull(apiGetResponse);
                    Assert.Equal(newApiId, apiGetResponse.Name);
                    Assert.Equal(newApiName, apiGetResponse.DisplayName);
                    Assert.Equal(newApiDescription, apiGetResponse.Description);
                    Assert.Equal(newApiPath, apiGetResponse.Path);
                    Assert.Equal(newApiServiceUrl, apiGetResponse.ServiceUrl);
                    Assert.Equal(subscriptionKeyParametersHeader, apiGetResponse.SubscriptionKeyParameterNames.Header);
                    Assert.Equal(subscriptionKeyQueryStringParamName, apiGetResponse.SubscriptionKeyParameterNames.Query);
                    Assert.Equal(2, apiGetResponse.Protocols.Count);
                    Assert.True(apiGetResponse.Protocols.Contains(Protocol.Http));
                    Assert.True(apiGetResponse.Protocols.Contains(Protocol.Https));

                    var schemaContractParams = new SchemaContract()
                    {
                        ContentType = "application/vnd.ms-azure-apim.swagger.definitions+json",
                        Value       = JsonSchemaString1
                    };

                    var schemaContract = await testBase.client.ApiSchema.CreateOrUpdateAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newApiId,
                        newSchemaId,
                        schemaContractParams);

                    Assert.NotNull(schemaContract);
                    Assert.Equal(schemaContractParams.ContentType, schemaContract.ContentType);
                    Assert.NotNull(schemaContract.Definitions);
                    Assert.Null(schemaContract.Value);

                    // list the schemas attached to the api
                    var schemasList = await testBase.client.ApiSchema.ListByApiAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newApiId);

                    Assert.NotNull(schemasList);
                    Assert.Single(schemasList);
                    Assert.Equal(schemaContractParams.ContentType, schemasList.First().ContentType);

                    // get the schema tag
                    var schemaTag = await testBase.client.ApiSchema.GetEntityTagAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newApiId,
                        newSchemaId);

                    Assert.NotNull(schemaTag);
                    Assert.NotNull(schemaTag.ETag);

                    // delete the schema
                    await testBase.client.ApiSchema.DeleteAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newApiId,
                        newSchemaId,
                        schemaTag.ETag);

                    Assert.NotNull(schemaContract);
                    Assert.Equal(schemaContractParams.ContentType, schemaContract.ContentType);

                    // check the entity is deleted
                    Assert.Throws <ErrorResponseException>(()
                                                           => testBase.client.ApiSchema.Get(testBase.rgName, testBase.serviceName, newApiId, newSchemaId));

                    // delete the api
                    testBase.client.Api.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        newApiId,
                        "*");

                    // get the deleted api to make sure it was deleted
                    try
                    {
                        testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId);
                        throw new Exception("This code should not have been executed.");
                    }
                    catch (ErrorResponseException ex)
                    {
                        Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode);
                    }
                }
                finally
                {
                    // delete the apischema
                    await testBase.client.ApiSchema.DeleteAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newApiId,
                        newSchemaId,
                        "*");


                    // delete the api
                    testBase.client.Api.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        newApiId,
                        "*");
                }
            }
        }
 /// <summary>
 /// Creates or updates schema configuration for the API.
 /// </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='apiId'>
 /// API revision identifier. Must be unique in the current API Management
 /// service instance. Non-current revision has ;rev=n as a suffix where n is
 /// the revision number.
 /// </param>
 /// <param name='schemaId'>
 /// Schema id identifier. Must be unique in the current API Management service
 /// instance.
 /// </param>
 /// <param name='parameters'>
 /// The schema contents to apply.
 /// </param>
 /// <param name='ifMatch'>
 /// ETag of the Entity. Not required when creating an entity, but required when
 /// updating an entity.
 /// </param>
 public static SchemaContract BeginCreateOrUpdate(this IApiSchemaOperations operations, string resourceGroupName, string serviceName, string apiId, string schemaId, SchemaContract parameters, string ifMatch = default(string))
 {
     return operations.BeginCreateOrUpdateAsync(resourceGroupName, serviceName, apiId, schemaId, parameters, ifMatch).GetAwaiter().GetResult();
 }