예제 #1
0
        /// <summary>
        /// Completes the resource creation.
        /// </summary>
        /// <param name="subscriptionId">The subscription Id.</param>
        /// <param name="resourceGroup">The resource group.</param>
        /// <param name="providerNamespace">The provider namespace.</param>
        /// <param name="resourceType">The resource type.</param>
        /// <param name="resourceName">The resource name.</param>
        /// <param name="tenantId">The tenant Id.</param>
        private async Task CompleteResourceCreation(string subscriptionId, string resourceGroup, string providerNamespace, string resourceType, string resourceName, string tenantId)
        {
            // Call into ARM to get all resources of this resource type within the resource group.
            var resourceRequestUri = UriEngine.GetResourceGroupResourceRequestUri(
                endpoint: this.ArmEndpoint,
                subscriptionId: subscriptionId,
                resourceGroupName: resourceGroup,
                resourceProviderNamespace: providerNamespace,
                resourceType: resourceType,
                resourceName: resourceName,
                apiVersion: this.ApiVersion);

            var getResponseFromARM = await this.CallRPSaaSForMetadata(resourceRequestUri, tenantId, HttpMethod.Get).ConfigureAwait(false);

            Logger.LogMessage($"Response status from ARM for GET resource '{resourceRequestUri}': '{getResponseFromARM.StatusCode}'");

            if (getResponseFromARM.StatusCode == HttpStatusCode.OK)
            {
                var reader     = new StreamReader(await getResponseFromARM.Content.ReadAsStreamAsync().ConfigureAwait(false));
                var getContent = reader.ReadToEnd();
                var resource   = JsonConvert.DeserializeObject <Resource>(getContent);

                if (resource != null)
                {
                    // Add some tags on this resource.
                    // In order to do a PUT, ensure that all the properties specified in the swagger
                    // for PUT are present in the payload.
                    var updatedResource = new Resource
                    {
                        Tags = resource.Tags,
                    };

                    if (updatedResource.Tags.ContainsKey("contosoPrivateTag"))
                    {
                        updatedResource.Tags["contosoPrivateTag"] = $"Updated in OnResourceCreationCompleted at '{DateTime.UtcNow}'";
                    }
                    else
                    {
                        updatedResource.Tags.TryAdd("contosoPrivateTag", $"Updated in OnResourceCreationCompleted at '{DateTime.UtcNow}'");
                    }

                    var putResponseFromARM = await this.CallRPSaaSForMetadata(resourceRequestUri, tenantId, HttpMethod.Patch, updatedResource).ConfigureAwait(false);

                    Logger.LogMessage($"Response code from ARM for PUT: '{putResponseFromARM.StatusCode}'");
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Validates if the number of resources in the resource group is under the limit.
        /// </summary>
        /// <param name="subscriptionId">The subscription Id.</param>
        /// <param name="resourceGroup">The resource group.</param>
        /// <param name="providerNamespace">The provider namespace.</param>
        /// <param name="resourceType">The resource type.</param>
        /// <param name="tenantId">The tenant Id.</param>
        private async Task <IActionResult> ValidateMaximumResourceCounts(string subscriptionId, string resourceGroup, string providerNamespace, string resourceType, string tenantId)
        {
            ErrorResponse errorRespone = null;

            // Call into ARM to get all resources of this resource type within the resource group.
            var resourceCollectionUri = UriEngine.GetResourceGroupResourceRequestUri(
                endpoint: this.ArmEndpoint,
                subscriptionId: subscriptionId,
                resourceGroupName: resourceGroup,
                resourceProviderNamespace: providerNamespace,
                resourceType: resourceType,
                resourceName: null,
                apiVersion: this.ApiVersion);

            var responseFromARM = await this.CallRPSaaSForMetadata(resourceCollectionUri, tenantId, HttpMethod.Get).ConfigureAwait(false);

            Logger.LogMessage($"Response status from ARM: '{responseFromARM.StatusCode}'");

            if (responseFromARM.StatusCode == HttpStatusCode.OK)
            {
                var reader              = new StreamReader(await responseFromARM.Content.ReadAsStreamAsync().ConfigureAwait(false));
                var responseContent     = reader.ReadToEnd();
                var resourcesCollection = JsonConvert.DeserializeObject <ResponseWithContinuation <Resource[]> >(responseContent);

                if (resourcesCollection.Value != null)
                {
                    // Return error if we have more than max count resources of this type in the resource group.
                    if (resourcesCollection.Value.Count() >= this.MaxCount)
                    {
                        errorRespone = new ErrorResponse
                        {
                            Error = new Error
                            {
                                Code    = "SampleValidationErrorCode",
                                Message = "SampleValidationErrorMessage - Maximum number of resources per resource group count hit.",
                            },
                            Status = "Failed",
                        };
                    }
                }
            }

            // Required response format in case of validation failure.
            return(ContosoController.CreateResponse(
                       statusCode: HttpStatusCode.OK,
                       value: errorRespone));
        }