Beispiel #1
0
 /// <summary>
 /// Creates new product subscription.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.ISubscriptionsOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='sid'>
 /// Required. Identifier of the subscription.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create parameters.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task <AzureOperationResponse> CreateAsync(this ISubscriptionsOperations operations, string resourceGroupName, string serviceName, string sid, SubscriptionCreateParameters parameters)
 {
     return(operations.CreateAsync(resourceGroupName, serviceName, sid, parameters, CancellationToken.None));
 }
Beispiel #2
0
 /// <summary>
 /// Creates new product subscription.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.ISubscriptionsOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='sid'>
 /// Required. Identifier of the subscription.
 /// </param>
 /// <param name='parameters'>
 /// Required. Create parameters.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse Create(this ISubscriptionsOperations operations, string resourceGroupName, string serviceName, string sid, SubscriptionCreateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((ISubscriptionsOperations)s).CreateAsync(resourceGroupName, serviceName, sid, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
Beispiel #3
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 subscriptions: there should be two by default
                var listResponse = testBase.client.Subscription.List(
                    testBase.rgName,
                    testBase.serviceName,
                    null);

                Assert.NotNull(listResponse);
                Assert.True(listResponse.Count() >= 2);
                Assert.NotNull(listResponse.NextPageLink);

                // list paged
                listResponse = testBase.client.Subscription.List(
                    testBase.rgName,
                    testBase.serviceName,
                    new Microsoft.Rest.Azure.OData.ODataQuery <SubscriptionContract> {
                    Top = 1
                });

                Assert.NotNull(listResponse);
                Assert.Single(listResponse);
                Assert.NotNull(listResponse.NextPageLink);

                // get first subscription
                var firstSubscription = listResponse.First();

                var getResponse = testBase.client.Subscription.Get(
                    testBase.rgName,
                    testBase.serviceName,
                    firstSubscription.Name);

                Assert.NotNull(getResponse);
                Assert.Equal(firstSubscription.Name, getResponse.Name);
                Assert.Equal(firstSubscription.NotificationDate, getResponse.NotificationDate);
                Assert.Equal(firstSubscription.PrimaryKey, getResponse.PrimaryKey);
                Assert.Equal(firstSubscription.ProductId, getResponse.ProductId);
                Assert.Equal(firstSubscription.SecondaryKey, getResponse.SecondaryKey);
                Assert.Equal(firstSubscription.StartDate, getResponse.StartDate);
                Assert.Equal(firstSubscription.State, getResponse.State);
                Assert.Equal(firstSubscription.StateComment, getResponse.StateComment);
                Assert.Equal(firstSubscription.UserId, getResponse.UserId);
                Assert.Equal(firstSubscription.CreatedDate, getResponse.CreatedDate);
                Assert.Equal(firstSubscription.EndDate, getResponse.EndDate);
                Assert.Equal(firstSubscription.ExpirationDate, getResponse.ExpirationDate);

                // update product to accept unlimited number or subscriptions
                testBase.client.Product.Update(
                    testBase.rgName,
                    testBase.serviceName,
                    firstSubscription.ProductIdentifier,
                    new ProductUpdateParameters
                {
                    SubscriptionsLimit = int.MaxValue
                },
                    "*");

                // add new subscription
                string newSubscriptionId = TestUtilities.GenerateName("newSubscriptionId");

                try
                {
                    string newSubscriptionName  = TestUtilities.GenerateName("newSubscriptionName");
                    string newSubscriptionPk    = TestUtilities.GenerateName("newSubscriptionPK");
                    string newSubscriptionSk    = TestUtilities.GenerateName("newSubscriptionSK");
                    var    newSubscriptionState = SubscriptionState.Active;

                    var newSubscriptionCreate = new SubscriptionCreateParameters(
                        firstSubscription.UserId,
                        firstSubscription.ProductId,
                        newSubscriptionName)
                    {
                        PrimaryKey   = newSubscriptionPk,
                        SecondaryKey = newSubscriptionSk,
                        State        = newSubscriptionState,
                    };

                    var subscriptionContract = testBase.client.Subscription.CreateOrUpdate(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId,
                        newSubscriptionCreate);

                    Assert.NotNull(subscriptionContract);
                    Assert.Equal(firstSubscription.ProductId, subscriptionContract.ProductId);
                    Assert.Equal(firstSubscription.UserId, subscriptionContract.UserId);
                    Assert.Equal(newSubscriptionState, subscriptionContract.State);
                    Assert.Equal(newSubscriptionSk, subscriptionContract.SecondaryKey);
                    Assert.Equal(newSubscriptionPk, subscriptionContract.PrimaryKey);
                    Assert.Equal(newSubscriptionName, subscriptionContract.DisplayName);

                    var subscriptionResponse = await testBase.client.Subscription.GetWithHttpMessagesAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId);

                    Assert.NotNull(subscriptionResponse);
                    Assert.NotNull(subscriptionResponse.Headers.ETag);

                    // patch the subscription
                    string patchedName           = TestUtilities.GenerateName("patchedName");
                    string patchedPk             = TestUtilities.GenerateName("patchedPk");
                    string patchedSk             = TestUtilities.GenerateName("patchedSk");
                    var    patchedExpirationDate = new DateTime(2025, 5 + 2, 20);

                    testBase.client.Subscription.Update(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId,
                        new SubscriptionUpdateParameters
                    {
                        DisplayName    = patchedName,
                        PrimaryKey     = patchedPk,
                        SecondaryKey   = patchedSk,
                        ExpirationDate = patchedExpirationDate
                    },
                        subscriptionResponse.Headers.ETag);

                    // get patched subscription to check it was actually patched
                    getResponse = testBase.client.Subscription.Get(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId);

                    Assert.NotNull(getResponse);
                    Assert.Equal(newSubscriptionId, getResponse.Name);
                    Assert.Equal(patchedName, getResponse.DisplayName);
                    Assert.Equal(patchedPk, getResponse.PrimaryKey);
                    Assert.Equal(patchedSk, getResponse.SecondaryKey);
                    Assert.Equal(newSubscriptionState, getResponse.State);
                    Assert.Equal(patchedExpirationDate, getResponse.ExpirationDate);

                    // regenerate primary key
                    testBase.client.Subscription.RegeneratePrimaryKey(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId);

                    // get the subscription to check the key
                    getResponse = testBase.client.Subscription.Get(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId);

                    Assert.NotNull(getResponse);
                    Assert.NotEqual(patchedPk, getResponse.PrimaryKey);
                    Assert.Equal(patchedSk, getResponse.SecondaryKey);

                    // regenerate secondary key
                    testBase.client.Subscription.RegenerateSecondaryKey(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId);

                    // get the subscription to check the key
                    subscriptionResponse = await testBase.client.Subscription.GetWithHttpMessagesAsync(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId);

                    Assert.NotNull(subscriptionResponse);
                    Assert.NotNull(subscriptionResponse.Headers.ETag);
                    Assert.NotEqual(patchedPk, subscriptionResponse.Body.PrimaryKey);
                    Assert.NotEqual(patchedSk, subscriptionResponse.Body.SecondaryKey);

                    // delete the subscription
                    testBase.client.Subscription.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId,
                        subscriptionResponse.Headers.ETag);

                    // get the deleted subscription to make sure it was deleted
                    try
                    {
                        testBase.client.Subscription.Get(testBase.rgName, testBase.serviceName, newSubscriptionId);
                        throw new Exception("This code should not have been executed.");
                    }
                    catch (ErrorResponseException ex)
                    {
                        Assert.Equal((int)HttpStatusCode.NotFound, (int)ex.Response.StatusCode);
                    }
                }
                finally
                {
                    testBase.client.Subscription.Delete(
                        testBase.rgName,
                        testBase.serviceName,
                        newSubscriptionId,
                        "*");
                }
            }
        }
        public void SubscriptionsCreateListUpdateDelete()
        {
            TestUtilities.StartTest("SmapiFunctionalTests", "SubscriptionsCreateListUpdateDelete");

            try
            {
                // list subscriptions: there should be two by default
                var listResponse = ApiManagementClient.Subscriptions.List(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    null);

                Assert.NotNull(listResponse);
                Assert.NotNull(listResponse.Result.Values);
                Assert.True(listResponse.Result.TotalCount >= 2);
                Assert.True(listResponse.Result.Values.Count >= 2);
                Assert.Null(listResponse.Result.NextLink);

                // list paged
                listResponse = ApiManagementClient.Subscriptions.List(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    new QueryParameters {
                    Top = 1
                });

                Assert.NotNull(listResponse);
                Assert.NotNull(listResponse.Result.Values);
                Assert.True(listResponse.Result.TotalCount >= 2);
                Assert.Equal(1, listResponse.Result.Values.Count);
                Assert.NotNull(listResponse.Result.NextLink);

                // get first subscription
                var firstSubscription = listResponse.Result.Values.First();

                var getResponse = ApiManagementClient.Subscriptions.Get(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    firstSubscription.Id);

                Assert.NotNull(getResponse);
                Assert.Equal(firstSubscription.Id, getResponse.Value.Id);
                Assert.Equal(firstSubscription.Name, getResponse.Value.Name);
                Assert.Equal(firstSubscription.NotificationDate, getResponse.Value.NotificationDate);
                Assert.Equal(firstSubscription.PrimaryKey, getResponse.Value.PrimaryKey);
                Assert.Equal(firstSubscription.ProductId, getResponse.Value.ProductId);
                Assert.Equal(firstSubscription.SecondaryKey, getResponse.Value.SecondaryKey);
                Assert.Equal(firstSubscription.StartDate, getResponse.Value.StartDate);
                Assert.Equal(firstSubscription.State, getResponse.Value.State);
                Assert.Equal(firstSubscription.StateComment, getResponse.Value.StateComment);
                Assert.Equal(firstSubscription.UserId, getResponse.Value.UserId);
                Assert.Equal(firstSubscription.CreatedDate, getResponse.Value.CreatedDate);
                Assert.Equal(firstSubscription.EndDate, getResponse.Value.EndDate);
                Assert.Equal(firstSubscription.ExpirationDate, getResponse.Value.ExpirationDate);

                // update product to accept unlimited number or subscriptions
                ApiManagementClient.Products.Update(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    firstSubscription.ProductId,
                    new ProductUpdateParameters
                {
                    SubscriptionsLimit = int.MaxValue
                },
                    "*");

                // add new subscription
                string newSubscriptionId    = TestUtilities.GenerateName("newSubscriptionId");
                string newSubscriptionName  = TestUtilities.GenerateName("newSubscriptionName");
                string newSubscriptionPk    = TestUtilities.GenerateName("newSubscriptionPK");
                string newSubscriptionSk    = TestUtilities.GenerateName("newSubscriptionSK");
                var    newSubscriptionState = SubscriptionStateContract.Active;

                var newSubscriptionCreate = new SubscriptionCreateParameters(
                    firstSubscription.UserIdPath,
                    firstSubscription.ProductIdPath,
                    newSubscriptionName)
                {
                    PrimaryKey   = newSubscriptionPk,
                    SecondaryKey = newSubscriptionSk,
                    State        = newSubscriptionState,
                };

                var createResponse = ApiManagementClient.Subscriptions.Create(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId,
                    newSubscriptionCreate);

                Assert.NotNull(createResponse);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                // get created subscription to check it was actually created
                getResponse = ApiManagementClient.Subscriptions.Get(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId);

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);
                Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
                Assert.Equal(newSubscriptionId, getResponse.Value.Id);
                Assert.Equal(newSubscriptionName, getResponse.Value.Name);
                Assert.Equal(newSubscriptionPk, getResponse.Value.PrimaryKey);
                Assert.Equal(newSubscriptionSk, getResponse.Value.SecondaryKey);
                Assert.Equal(newSubscriptionState, getResponse.Value.State);

                // patch the subscription
                string patchedName           = TestUtilities.GenerateName("patchedName");
                string patchedPk             = TestUtilities.GenerateName("patchedPk");
                string patchedSk             = TestUtilities.GenerateName("patchedSk");
                var    patchedExpirationDate = new DateTime(2025, 5 + 2, 20);

                var patchResponse = ApiManagementClient.Subscriptions.Update(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId,
                    new SubscriptionUpdateParameters
                {
                    Name         = patchedName,
                    PrimaryKey   = patchedPk,
                    SecondaryKey = patchedSk,
                    ExpiresOn    = patchedExpirationDate
                },
                    getResponse.ETag);

                Assert.NotNull(patchResponse);
                Assert.Equal(HttpStatusCode.NoContent, patchResponse.StatusCode);

                // get patched subscription to check it was actually patched
                getResponse = ApiManagementClient.Subscriptions.Get(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId);

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);
                Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
                Assert.Equal(newSubscriptionId, getResponse.Value.Id);
                Assert.Equal(patchedName, getResponse.Value.Name);
                Assert.Equal(patchedPk, getResponse.Value.PrimaryKey);
                Assert.Equal(patchedSk, getResponse.Value.SecondaryKey);
                Assert.Equal(newSubscriptionState, getResponse.Value.State);
                Assert.Equal(patchedExpirationDate, getResponse.Value.ExpirationDate);

                // regenerate primary key
                var regenerateResponse = ApiManagementClient.Subscriptions.RegeneratePrimaryKey(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId);

                Assert.NotNull(regenerateResponse);
                Assert.Equal(HttpStatusCode.NoContent, regenerateResponse.StatusCode);

                // get the subscription to check the key
                getResponse = ApiManagementClient.Subscriptions.Get(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId);

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);
                Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
                Assert.NotEqual(patchedPk, getResponse.Value.PrimaryKey);
                Assert.Equal(patchedSk, getResponse.Value.SecondaryKey);

                // regenerate secondary key
                regenerateResponse = ApiManagementClient.Subscriptions.RegenerateSecondaryKey(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId);

                Assert.NotNull(regenerateResponse);
                Assert.Equal(HttpStatusCode.NoContent, regenerateResponse.StatusCode);

                // get the subscription to check the key
                getResponse = ApiManagementClient.Subscriptions.Get(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId);

                Assert.NotNull(getResponse);
                Assert.NotNull(getResponse.Value);
                Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
                Assert.NotEqual(patchedPk, getResponse.Value.PrimaryKey);
                Assert.NotEqual(patchedSk, getResponse.Value.SecondaryKey);

                // delete the subscription
                var deleteResponse = ApiManagementClient.Subscriptions.Delete(
                    ResourceGroupName,
                    ApiManagementServiceName,
                    newSubscriptionId,
                    getResponse.ETag);

                Assert.NotNull(deleteResponse);
                Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode);

                // get the deleted subscription to make sure it was deleted
                try
                {
                    ApiManagementClient.Subscriptions.Get(ResourceGroupName, ApiManagementServiceName, newSubscriptionId);
                    throw new Exception("This code should not have been executed.");
                }
                catch (CloudException ex)
                {
                    Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode);
                }
            }
            finally
            {
                TestUtilities.EndTest();
            }
        }
 /// <summary>
 /// Creates or updates the subscription of specified user to the specified
 /// product.
 /// </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='sid'>
 /// Subscription entity Identifier. The entity represents the association
 /// between a user and a product in API Management.
 /// </param>
 /// <param name='parameters'>
 /// Create parameters.
 /// </param>
 /// <param name='notify'>
 /// Notify change in Subscription State.
 /// - If false, do not send any email notification for change of state of
 /// subscription
 /// - If true, send email notification of change of state of subscription
 /// </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 <SubscriptionContract> CreateOrUpdateAsync(this ISubscriptionOperations operations, string resourceGroupName, string serviceName, string sid, SubscriptionCreateParameters parameters, bool?notify = default(bool?), string ifMatch = default(string), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, serviceName, sid, parameters, notify, ifMatch, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
 /// <summary>
 /// Creates or updates the subscription of specified user to the specified
 /// product.
 /// </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='sid'>
 /// Subscription entity Identifier. The entity represents the association
 /// between a user and a product in API Management.
 /// </param>
 /// <param name='parameters'>
 /// Create parameters.
 /// </param>
 /// <param name='notify'>
 /// Notify change in Subscription State.
 /// - If false, do not send any email notification for change of state of
 /// subscription
 /// - If true, send email notification of change of state of subscription
 /// </param>
 /// <param name='ifMatch'>
 /// ETag of the Entity. Not required when creating an entity, but required when
 /// updating an entity.
 /// </param>
 public static SubscriptionContract CreateOrUpdate(this ISubscriptionOperations operations, string resourceGroupName, string serviceName, string sid, SubscriptionCreateParameters parameters, bool?notify = default(bool?), string ifMatch = default(string))
 {
     return(operations.CreateOrUpdateAsync(resourceGroupName, serviceName, sid, parameters, notify, ifMatch).GetAwaiter().GetResult());
 }