예제 #1
0
        internal List <PSPermission> GetResourceGroupPermissions(string resourceGroup)
        {
            PermissionGetResult permissionsResult = AuthorizationManagementClient.Permissions.ListForResourceGroup(resourceGroup);

            if (permissionsResult != null)
            {
                return(permissionsResult.Permissions.Select(p => p.ToPSPermission()).ToList());
            }

            return(null);
        }
예제 #2
0
        internal List <PSPermission> GetResourcePermissions(ResourceIdentifier identity)
        {
            PermissionGetResult permissionsResult = AuthorizationManagementClient.Permissions.ListForResource(
                identity.ResourceGroupName,
                identity.ToResourceIdentity());

            if (permissionsResult != null)
            {
                return(permissionsResult.Permissions.Select(p => p.ToPSPermission()).ToList());
            }

            return(null);
        }
        /// <summary>
        /// Gets a resource permissions.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. The name of the resource group. The name is case
        /// insensitive.
        /// </param>
        /// <param name='identity'>
        /// Required. Resource
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Permissions information.
        /// </returns>
        public async Task <PermissionGetResult> ListForResourceAsync(string resourceGroupName, ResourceIdentity identity, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (identity == null)
            {
                throw new ArgumentNullException("identity");
            }
            if (identity.ResourceName == null)
            {
                throw new ArgumentNullException("identity.");
            }
            if (identity.ResourceProviderNamespace == null)
            {
                throw new ArgumentNullException("identity.");
            }
            if (identity.ResourceType == null)
            {
                throw new ArgumentNullException("identity.");
            }

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

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

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourcegroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            url = url + Uri.EscapeDataString(identity.ResourceProviderNamespace);
            url = url + "/";
            if (identity.ParentResourcePath != null)
            {
                url = url + identity.ParentResourcePath;
            }
            url = url + "/";
            url = url + identity.ResourceType;
            url = url + "/";
            url = url + Uri.EscapeDataString(identity.ResourceName);
            url = url + "/providers/Microsoft.Authorization/permissions";
            List <string> queryParameters = new List <string>();

            queryParameters.Add(APIVersion);
            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
                    PermissionGetResult result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    Permission permissionInstance = new Permission();
                                    result.Permissions.Add(permissionInstance);

                                    JToken actionsArray = valueValue["actions"];
                                    if (actionsArray != null && actionsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken actionsValue in ((JArray)actionsArray))
                                        {
                                            permissionInstance.Actions.Add(((string)actionsValue));
                                        }
                                    }

                                    JToken notActionsArray = valueValue["notActions"];
                                    if (notActionsArray != null && notActionsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken notActionsValue in ((JArray)notActionsArray))
                                        {
                                            permissionInstance.NotActions.Add(((string)notActionsValue));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }