private static IEnumerable <PSResourceProviderOperation> GetPSOperationsFromProviderOperationsMetadata(ProviderOperationsMetadata providerOperationsMetadata)
        {
            var operationsList             = providerOperationsMetadata.Operations?.Select(op => ToPSResourceProviderOperation(op, providerOperationsMetadata.DisplayName)) ?? new PSResourceProviderOperation[0];
            var ResourceTypeOperationsList = providerOperationsMetadata.ResourceTypes?.SelectMany(rt => rt.Operations.Select(op => ToPSResourceProviderOperation(op, providerOperationsMetadata.DisplayName, rt.DisplayName))) ?? new PSResourceProviderOperation[0];

            return(operationsList.Concat(ResourceTypeOperationsList));
        }
        private static IEnumerable <PSResourceProviderOperation> GetPSOperationsFromProviderOperationsMetadata(ProviderOperationsMetadata providerOperationsMetadata)
        {
            IEnumerable <PSResourceProviderOperation> operations = providerOperationsMetadata.Operations.Where(op => GetAzureProviderOperationCommand.IsUserOperation(op))
                                                                   .Select(op => ToPSResourceProviderOperation(op, providerOperationsMetadata.DisplayName));

            if (providerOperationsMetadata.ResourceTypes != null)
            {
                operations = operations.Concat(providerOperationsMetadata.ResourceTypes.SelectMany(rt => rt.Operations.Where(op => GetAzureProviderOperationCommand.IsUserOperation(op))
                                                                                                   .Select(op => ToPSResourceProviderOperation(op, providerOperationsMetadata.DisplayName, rt.DisplayName))));
            }

            return(operations);
        }
示例#3
0
        /// <summary>
        /// Gets provider operations metadata
        /// </summary>
        /// <param name='resourceProviderNamespace'>
        /// Required. Namespace of the resource provider.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Provider operations metadata
        /// </returns>
        public async Task <ProviderOperationsMetadataGetResult> GetAsync(string resourceProviderNamespace, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceProviderNamespace == null)
            {
                throw new ArgumentNullException("resourceProviderNamespace");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceProviderNamespace", resourceProviderNamespace);
                TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/providers/Microsoft.Authorization/providerOperations/";
            url = url + Uri.EscapeDataString(resourceProviderNamespace);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-07-01");
            queryParameters.Add("$expand=resourceTypes");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    ProviderOperationsMetadataGetResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new ProviderOperationsMetadataGetResult();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            ProviderOperationsMetadata providerInstance = new ProviderOperationsMetadata();
                            result.Provider = providerInstance;

                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                providerInstance.Id = idInstance;
                            }

                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                providerInstance.Name = nameInstance;
                            }

                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                providerInstance.Type = typeInstance;
                            }

                            JToken displayNameValue = responseDoc["displayName"];
                            if (displayNameValue != null && displayNameValue.Type != JTokenType.Null)
                            {
                                string displayNameInstance = ((string)displayNameValue);
                                providerInstance.DisplayName = displayNameInstance;
                            }

                            JToken resourceTypesArray = responseDoc["resourceTypes"];
                            if (resourceTypesArray != null && resourceTypesArray.Type != JTokenType.Null)
                            {
                                foreach (JToken resourceTypesValue in ((JArray)resourceTypesArray))
                                {
                                    ResourceType resourceTypeInstance = new ResourceType();
                                    providerInstance.ResourceTypes.Add(resourceTypeInstance);

                                    JToken nameValue2 = resourceTypesValue["name"];
                                    if (nameValue2 != null && nameValue2.Type != JTokenType.Null)
                                    {
                                        string nameInstance2 = ((string)nameValue2);
                                        resourceTypeInstance.Name = nameInstance2;
                                    }

                                    JToken displayNameValue2 = resourceTypesValue["displayName"];
                                    if (displayNameValue2 != null && displayNameValue2.Type != JTokenType.Null)
                                    {
                                        string displayNameInstance2 = ((string)displayNameValue2);
                                        resourceTypeInstance.DisplayName = displayNameInstance2;
                                    }

                                    JToken operationsArray = resourceTypesValue["operations"];
                                    if (operationsArray != null && operationsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken operationsValue in ((JArray)operationsArray))
                                        {
                                            Operation operationInstance = new Operation();
                                            resourceTypeInstance.Operations.Add(operationInstance);

                                            JToken nameValue3 = operationsValue["name"];
                                            if (nameValue3 != null && nameValue3.Type != JTokenType.Null)
                                            {
                                                string nameInstance3 = ((string)nameValue3);
                                                operationInstance.Name = nameInstance3;
                                            }

                                            JToken displayNameValue3 = operationsValue["displayName"];
                                            if (displayNameValue3 != null && displayNameValue3.Type != JTokenType.Null)
                                            {
                                                string displayNameInstance3 = ((string)displayNameValue3);
                                                operationInstance.DisplayName = displayNameInstance3;
                                            }

                                            JToken descriptionValue = operationsValue["description"];
                                            if (descriptionValue != null && descriptionValue.Type != JTokenType.Null)
                                            {
                                                string descriptionInstance = ((string)descriptionValue);
                                                operationInstance.Description = descriptionInstance;
                                            }

                                            JToken originValue = operationsValue["origin"];
                                            if (originValue != null && originValue.Type != JTokenType.Null)
                                            {
                                                string originInstance = ((string)originValue);
                                                operationInstance.Origin = originInstance;
                                            }

                                            JToken propertiesValue = operationsValue["properties"];
                                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                            {
                                                object propertiesInstance = propertiesValue.ToString(Newtonsoft.Json.Formatting.Indented);
                                                operationInstance.Properties = propertiesInstance;
                                            }
                                        }
                                    }
                                }
                            }

                            JToken operationsArray2 = responseDoc["operations"];
                            if (operationsArray2 != null && operationsArray2.Type != JTokenType.Null)
                            {
                                foreach (JToken operationsValue2 in ((JArray)operationsArray2))
                                {
                                    Operation operationInstance2 = new Operation();
                                    providerInstance.Operations.Add(operationInstance2);

                                    JToken nameValue4 = operationsValue2["name"];
                                    if (nameValue4 != null && nameValue4.Type != JTokenType.Null)
                                    {
                                        string nameInstance4 = ((string)nameValue4);
                                        operationInstance2.Name = nameInstance4;
                                    }

                                    JToken displayNameValue4 = operationsValue2["displayName"];
                                    if (displayNameValue4 != null && displayNameValue4.Type != JTokenType.Null)
                                    {
                                        string displayNameInstance4 = ((string)displayNameValue4);
                                        operationInstance2.DisplayName = displayNameInstance4;
                                    }

                                    JToken descriptionValue2 = operationsValue2["description"];
                                    if (descriptionValue2 != null && descriptionValue2.Type != JTokenType.Null)
                                    {
                                        string descriptionInstance2 = ((string)descriptionValue2);
                                        operationInstance2.Description = descriptionInstance2;
                                    }

                                    JToken originValue2 = operationsValue2["origin"];
                                    if (originValue2 != null && originValue2.Type != JTokenType.Null)
                                    {
                                        string originInstance2 = ((string)originValue2);
                                        operationInstance2.Origin = originInstance2;
                                    }

                                    JToken propertiesValue2 = operationsValue2["properties"];
                                    if (propertiesValue2 != null && propertiesValue2.Type != JTokenType.Null)
                                    {
                                        object propertiesInstance2 = propertiesValue2.ToString(Newtonsoft.Json.Formatting.Indented);
                                        operationInstance2.Properties = propertiesInstance2;
                                    }
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }