示例#1
0
 /// <summary>
 /// Determines the API version.
 /// </summary>
 /// <param name="resourceId">The resource Id.</param>
 /// <param name="pre">When specified, indicates if pre-release API versions should be considered.</param>
 protected Task <string> DetermineApiVersion(string resourceId, bool?pre = null)
 {
     return(string.IsNullOrWhiteSpace(this.ApiVersion)
         ? ApiVersionHelper.DetermineApiVersion(
                profile: this.Profile,
                resourceId: resourceId,
                cancellationToken: this.CancellationToken.Value,
                pre: pre ?? this.Pre)
         : Task.FromResult(this.ApiVersion));
 }
 /// <summary>
 /// Determines the API version.
 /// </summary>
 /// <param name="resourceId">The resource Id.</param>
 /// <param name="pre">When specified, indicates if pre-release API versions should be considered.</param>
 protected Task <string> DetermineApiVersion(string resourceId, bool?pre = null)
 {
     return(string.IsNullOrWhiteSpace(this.ApiVersion)
         ? ApiVersionHelper.DetermineApiVersion(
                context: DefaultContext,
                resourceId: resourceId,
                cancellationToken: this.CancellationToken.Value,
                pre: pre ?? this.Pre,
                cmdletHeaderValues: this.GetCmdletHeaders())
         : Task.FromResult(this.ApiVersion));
 }
示例#3
0
        public async override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            try
            {
                // Just make sure api-version is in url
                // Let exception handler handle any exception
                ApiVersionHelper.GetApiVersion(actionContext.Request.RequestUri);
            }
            catch
            {
                var account        = RequestHelper.ParseAccount(actionContext.Request);
                var subscriptionId = await RequestHelper.GetSubscriptionId(account);

                MetricManager.Instance.LogRequestFailed4xx(1, account, subscriptionId, string.Empty);
                throw;
            }

            await base.OnActionExecutingAsync(actionContext, cancellationToken);
        }
        /// <summary>
        /// Populates the permissions on the resource.
        /// </summary>
        /// <param name="resource">The resource.</param>
        private async Task PopulatePermissions(PSObject resource)
        {
            try
            {
                var resourceId = resource.Properties["ResourceId"].Value.ToString();

                var resourceCollectionId = resourceId + ResourceIdUtility
                                           .GetResourceCollectionId(
                    subscriptionId: null,
                    resourceGroupName: null,
                    resourceType: null,
                    extensionResourceType: "Microsoft.Authorization/permissions");

                var apiVersion = await ApiVersionHelper
                                 .DetermineApiVersion(
                    profile : this.Profile,
                    providerNamespace : "Microsoft.Authorization",
                    resourceType : "permissions",
                    cancellationToken : this.CancellationToken.Value,
                    pre : this.Pre)
                                 .ConfigureAwait(continueOnCapturedContext: false);

                var permissions = PaginatedResponseHelper.Enumerate(
                    getFirstPage: () => this.GetPermissions(permissionCheckId: resourceCollectionId, apiVersion: apiVersion),
                    getNextPage: nextLink => this.GetNextLink <Permission>(nextLink),
                    cancellationToken: this.CancellationToken);

                resource.Properties.Add(new PSNoteProperty("Permissions", permissions));
            }
            catch (Exception ex)
            {
                if (ex.IsFatal())
                {
                    throw;
                }

                ex = (ex is AggregateException)
                    ? (ex as AggregateException).Flatten()
                    : ex;

                this.errors.Add(new ErrorRecord(ex, "ErrorExpandingPermissions", ErrorCategory.CloseError, resource));
            }
        }
        /// <summary>
        /// Populates the properties of a single resource.
        /// </summary>
        /// <param name="resource">The resource.</param>
        private async Task <Resource <JToken> > GetPopulatedResource(Resource <JToken> resource)
        {
            try
            {
                var apiVersion = await ApiVersionHelper
                                 .DetermineApiVersion(
                    profile : this.Profile,
                    resourceId : resource.Id,
                    cancellationToken : this.CancellationToken.Value,
                    pre : this.Pre)
                                 .ConfigureAwait(continueOnCapturedContext: false);

                return(await this
                       .GetResourcesClient()
                       .GetResource <Resource <JToken> >(
                           resourceId: resource.Id,
                           apiVersion: apiVersion,
                           cancellationToken: this.CancellationToken.Value)
                       .ConfigureAwait(continueOnCapturedContext: false));
            }
            catch (Exception ex)
            {
                if (ex.IsFatal())
                {
                    throw;
                }

                ex = (ex is AggregateException)
                    ? (ex as AggregateException).Flatten()
                    : ex;

                this.errors.Add(new ErrorRecord(ex, "ErrorExpandingProperties", ErrorCategory.CloseError, resource));
            }

            return(resource);
        }
示例#6
0
        protected async Task <HttpResponseMessage> OnRequestAsync(string providerType)
        {
            var account        = RequestHelper.ParseAccount(this.Request);
            var subscriptionId = await RequestHelper.GetSubscriptionId(account);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            try
            {
                // Get provider
                var provider = ProviderManager.GetServiceProvider(providerType);
                Validator.IsTrue <ArgumentException>(provider != null, nameof(providerType), "The provider type '{0}' is invalid.", providerType);

                var path = ParseServicePath(this.Request.RequestUri, providerType);

                // Build request
                var request = new ServiceProviderRequest
                {
                    HttpMethod          = this.Request.Method.Method,
                    Path                = path,
                    Content             = await this.Request.Content.ReadAsStringAsync(),
                    Headers             = this.Request.Headers.ToDictionary(pair => pair.Key, pair => pair.Value),
                    QueryNameValuePairs = this.Request.GetQueryNameValuePairs(),
                    // comment by jin: this is where apiVersion is validated
                    ApiVersion = ApiVersionHelper.GetApiVersion(this.Request.RequestUri)
                };

                // Dispatch
                var result = await provider.OnRequestAsync(request);

                // Build response
                var response = new HttpResponseMessage(result.StatusCode)
                {
                    Content = result.Content != null && result.MediaType != null
                        ? new StringContent(result.Content, Encoding.UTF8, result.MediaType)
                        : null
                };

                if (result.Headers != null && result.Headers.Count >= 0)
                {
                    foreach (var header in result.Headers)
                    {
                        response.Headers.Add(header.Key, header.Value);
                    }
                }

                if (response.IsSuccessStatusCode)
                {
                    MetricManager.Instance.LogRequestSuccess(1, account, subscriptionId, providerType);
                }

                return(response);
            }
            catch
            {
                throw;
            }
            finally
            {
                stopwatch.Stop();
                MetricManager.Instance.LogRequestLatency(stopwatch.ElapsedMilliseconds, account, subscriptionId, providerType);
            }
        }