Exemplo n.º 1
0
        /// <summary>
        /// Converts the resource object collection to a filtered PsPolicySetDefinition array.
        /// </summary>
        /// <param name="filter">the filter</param>
        /// <param name="resources">The policy definition resource object.</param>
        protected PsPolicySetDefinition[] GetFilteredOutputPolicySetDefinitions(ListFilter filter, params JObject[] resources)
        {
            Func <PsPolicySetDefinition, bool> filterLambda = (result) =>
            {
                if (filter == ListFilter.None)
                {
                    return(true);
                }

                var policyType = result.Properties.PolicyType;
                return(string.Equals(policyType.ToString(), filter.ToString(), StringComparison.OrdinalIgnoreCase));
            };

            return(resources
                   .CoalesceEnumerable()
                   .Where(resource => resource != null)
                   .SelectArray(resource => new PsPolicySetDefinition(resource))
                   .Where(filterLambda).ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the resource object collection to a filtered PSObject array.
        /// </summary>
        /// <param name="resourceType">The resource type of the input objects</param>
        /// <param name="filter">the filter</param>
        /// <param name="resources">The policy definition resource object.</param>
        protected PSObject[] GetFilteredOutputObjects(string resourceType, ListFilter filter, params JObject[] resources)
        {
            Func <PSObject, bool> filterLambda = (result) =>
            {
                if (filter == ListFilter.None)
                {
                    return(true);
                }

                var policyType = ((PSObject)result.Properties["Properties"].Value).Properties["policyType"].Value;
                return(policyType == null || string.Equals(policyType.ToString(), filter.ToString(), StringComparison.OrdinalIgnoreCase));
            };

            return(resources
                   .CoalesceEnumerable()
                   .Where(resource => resource != null)
                   .SelectArray(resource =>
            {
                var psobject = resource.ToResource().ToPsObject();
                psobject.Properties.Add(new PSNoteProperty(resourceType, psobject.Properties["ResourceId"].Value));
                return psobject;
            })
                   .Where(filterLambda).ToArray());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Queries the ARM cache and returns the cached resource that match the query specified.
        /// </summary>
        /// <param name="policyTypeFilter">The policy type filter.</param>
        private async Task <ResponseWithContinuation <JObject[]> > GetResources(ListFilter policyTypeFilter)
        {
            string resourceId  = this.GetResourceId();
            var    odataFilter = policyTypeFilter != ListFilter.None ? string.Format(PolicyCmdletBase.PolicyTypeFilterFormat, policyTypeFilter.ToString()) : null;

            var apiVersion = string.IsNullOrWhiteSpace(this.ApiVersion) ? Constants.PolicySetDefintionApiVersion : this.ApiVersion;

            if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)))
            {
                JObject resource;
                try
                {
                    resource = await this
                               .GetResourcesClient()
                               .GetResource <JObject>(
                        resourceId: resourceId,
                        apiVersion: apiVersion,
                        cancellationToken: this.CancellationToken.Value)
                               .ConfigureAwait(continueOnCapturedContext: false);
                }
                catch (ErrorResponseMessageException ex)
                {
                    if (!ex.Message.StartsWith("PolicySetDefinitionNotFound", StringComparison.OrdinalIgnoreCase))
                    {
                        throw;
                    }

                    resourceId = this.GetBuiltinResourceId();
                    resource   = await this
                                 .GetResourcesClient()
                                 .GetResource <JObject>(
                        resourceId: resourceId,
                        apiVersion: apiVersion,
                        cancellationToken: this.CancellationToken.Value)
                                 .ConfigureAwait(continueOnCapturedContext: false);
                }

                return(resource.TryConvertTo(out ResponseWithContinuation <JObject[]> retVal) && retVal.Value != null
                    ? retVal
                    : new ResponseWithContinuation <JObject[]> {
                    Value = resource.AsArray()
                });
            }
            else
            {
                return(await this
                       .GetResourcesClient()
                       .ListObjectColleciton <JObject>(
                           resourceCollectionId: resourceId,
                           apiVersion: apiVersion,
                           cancellationToken: this.CancellationToken.Value,
                           odataQuery: odataFilter)
                       .ConfigureAwait(continueOnCapturedContext: false));
            }
        }