Ejemplo n.º 1
0
        /// <summary>
        /// Called when resource is deleted.
        /// </summary>
        /// <remarks>
        /// A request context is not necessary to delete a resource.
        /// </remarks>
        /// <param name="deletedResource">The deleted resource.</param>
        Task <ResourceOperationStatus> IManagedResourceGroupWideResourceTypeRequestHandler.OnDeleteResource(IResourceEntity deletedResource)
        {
            ArgumentValidator.ValidateNotNull("deletedResource", deletedResource);

            // TODO: Add custom nested resource deletion logic here.

            return(Task.FromResult(ResourceOperationStatus.CompleteSynchronously(deletedResource)));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called before a resource is updated in the storage layer.
        /// </summary>
        /// <remarks>
        /// Use requestContext.ApiVersion to get the API version of the request.
        /// </remarks>
        /// <param name="requestContext">The request context.</param>
        /// <param name="subscription">The subscription.</param>
        /// <param name="existingResource">The existing resource.</param>
        /// <param name="updatedResourceDefinition">The updated resource definition.</param>
        Task <ResourceOperationStatus> IManagedResourceGroupWideResourceTypeRequestHandler.OnUpdateResource(
            RequestContext requestContext,
            SubscriptionNotificationDefinition subscription,
            IResourceEntity existingResource,
            ResourceDefinition updatedResourceDefinition)
        {
            ArgumentValidator.ValidateNotNull("requestContext", requestContext);
            ArgumentValidator.ValidateNotNull("subscription", subscription);
            ArgumentValidator.ValidateNotNull("existingResource", existingResource);
            ArgumentValidator.ValidateNotNull("updatedResourceDefinition", updatedResourceDefinition);

            // TODO: Implement custom logic to update the resource.

            return(Task.FromResult(ResourceOperationStatus.CompleteSynchronously(existingResource)));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Called after a resource is created in the storage layer.
        /// </summary>
        /// <remarks>
        /// Use requestContext.ApiVersion to get the API version of the request.
        /// </remarks>
        /// <param name="requestContext">The request context.</param>
        /// <param name="subscription">The subscription.</param>
        /// <param name="createdResource">The created resource.</param>
        async Task <ResourceOperationStatus> IManagedResourceGroupWideResourceTypeRequestHandler.OnAfterInsertResource(
            RequestContext requestContext,
            SubscriptionNotificationDefinition subscription,
            IResourceEntity createdResource)
        {
            ArgumentValidator.ValidateNotNull("requestContext", requestContext);
            ArgumentValidator.ValidateNotNull("subscription", subscription);
            ArgumentValidator.ValidateNotNull("createdResource", createdResource);

            // Enforce quotas for nested resources.
            ServiceQuota[] quotas = ServiceQuota.CreateSubscriptionQuotaFromSubscriptionNotification(
                createdResource.SubscriptionId,
                subscription);

            // Count aggregated quota for nested resources for the entire subscription.
            int nestedResourceCountLimit = 0;

            quotas.ToList().ForEach(quota => nestedResourceCountLimit += quota.NestedResourceCountLimit);

            int resourceCount = 0;
            DataContinuationToken continuationToken = null;

            do
            {
                var result = await this.resourceProviderStorage
                             .ResourceDataProvider
                             .FindResources(
                    createdResource.SubscriptionId,
                    createdResource.QualifiedTypeName,
                    continuationToken : continuationToken);

                resourceCount    += result.Entities.Length;
                continuationToken = result.ContinuationToken;
            } while (continuationToken != null);

            // TODO: Apply custom quota enforcement logic here.

            // Check that there is sufficient quota to create additional resources (enforce the resource count)
            if (nestedResourceCountLimit < resourceCount)
            {
                string errorMessage = string.Format(CultureInfo.CurrentCulture, ResponseErrorMessages.InsufficientResourceQuota, createdResource.Name);
                throw new ResourceProviderException(errorMessage, ResponseErrorCode.InsufficientQuota.ToString(), HttpStatusCode.BadRequest);
            }

            // TODO: Add custom nested resource creation logic here.

            return(ResourceOperationStatus.CompleteSynchronously(createdResource));
        }