/// <summary>
        /// Lists resources in a type collection.
        /// </summary>
        private async Task <ResponseWithContinuation <JObject[]> > ListResourcesTypeCollection()
        {
            var resourceCollectionId = ResourceIdUtility.GetResourceId(
                subscriptionId: this.SubscriptionId.AsArray().CoalesceEnumerable().Cast <Guid?>().FirstOrDefault(),
                resourceGroupName: null,
                resourceType: this.ResourceType,
                resourceName: null,
                extensionResourceType: this.ExtensionResourceType,
                extensionResourceName: null);

            var odataQuery = QueryFilterBuilder.CreateFilter(
                subscriptionId: null,
                resourceGroup: this.ResourceGroupNameEquals,
                resourceType: null,
                resourceName: this.ResourceNameEquals,
                tagName: TagsHelper.GetTagNameFromParameters(this.Tag, this.TagName),
                tagValue: TagsHelper.GetTagValueFromParameters(this.Tag, this.TagValue),
                filter: this.ODataQuery,
                resourceGroupNameContains: this.ResourceGroupNameContains,
                nameContains: this.ResourceNameContains);

            return(await this
                   .GetResourcesClient()
                   .ListObjectColleciton <JObject>(
                       resourceCollectionId: resourceCollectionId,
                       apiVersion: this.DefaultApiVersion,
                       cancellationToken: this.CancellationToken.Value,
                       odataQuery: odataQuery)
                   .ConfigureAwait(continueOnCapturedContext: false));
        }
        /// <summary>
        /// Lists the resources in a resource group.
        /// </summary>
        private async Task <ResponseWithContinuation <JObject[]> > ListResourcesInResourceGroup()
        {
            var filterQuery = QueryFilterBuilder
                              .CreateFilter(
                subscriptionId: null,
                resourceGroup: this.ResourceGroupNameEquals,
                resourceType: this.ResourceType,
                resourceName: this.ResourceNameEquals,
                tagName: TagsHelper.GetTagNameFromParameters(this.Tag, this.TagName),
                tagValue: TagsHelper.GetTagValueFromParameters(this.Tag, this.TagValue),
                filter: this.ODataQuery,
                resourceGroupNameContains: this.ResourceGroupNameContains,
                nameContains: this.ResourceNameContains);

            return(await this
                   .GetResourcesClient()
                   .ListResources <JObject>(
                       subscriptionId: this.SubscriptionId.Value,
                       resourceGroupName: null,
                       apiVersion: this.DefaultApiVersion,
                       top: this.Top,
                       filter: filterQuery,
                       cancellationToken: this.CancellationToken.Value)
                   .ConfigureAwait(continueOnCapturedContext: false));
        }
 /// <summary>
 /// Returns true if this is a resource group level query.
 /// </summary>
 private bool IsResourceGroupLevelQuery()
 {
     return(this.SubscriptionId.HasValue &&
            this.ResourceGroupNameAvailable() &&
            (TagsHelper.GetTagNameFromParameters(this.Tag, this.TagName) != null ||
             TagsHelper.GetTagValueFromParameters(this.Tag, this.TagValue) != null ||
             this.ResourceType != null ||
             this.ExtensionResourceType != null));
 }
예제 #4
0
        private void RunSimpleCmdlet()
        {
            if (this.IsParameterBound(c => c.Tag))
            {
                this.TagName  = TagsHelper.GetTagNameFromParameters(this.Tag, null);
                this.TagValue = TagsHelper.GetTagValueFromParameters(this.Tag, null);
            }

            var expression = QueryFilterBuilder.CreateFilter(
                subscriptionId: null,
                resourceGroup: null,
                resourceType: this.ResourceType,
                resourceName: null,
                tagName: null,
                tagValue: null,
                filter: this.ODataQuery);

            var odataQuery = new Rest.Azure.OData.ODataQuery <GenericResourceFilter>(expression);
            var result     = Enumerable.Empty <PSResource>();

            if (!ShouldListBySubscription(ResourceGroupName, Name))
            {
                result = this.ResourceManagerSdkClient.ListByResourceGroup(this.ResourceGroupName, odataQuery);
            }
            else
            {
                result = this.ResourceManagerSdkClient.ListResources(odataQuery);
            }

            result = TopLevelWildcardFilter(ResourceGroupName, Name, result);

            if (!string.IsNullOrEmpty(this.TagName) && !string.IsNullOrEmpty(this.TagValue))
            {
                result = result.Where(r => r.Tags != null &&
                                      r.Tags.Keys != null &&
                                      r.Tags.Keys.Where(k => string.Equals(k, this.TagName, StringComparison.OrdinalIgnoreCase))
                                      .Any(k => string.Equals(r.Tags[k], this.TagValue, StringComparison.OrdinalIgnoreCase)));
            }
            else if (!string.IsNullOrEmpty(this.TagName))
            {
                result = result.Where(r => r.Tags != null &&
                                      r.Tags.Keys != null &&
                                      r.Tags.Keys.Where(k => string.Equals(k, this.TagName, StringComparison.OrdinalIgnoreCase))
                                      .Any());
            }
            else if (!string.IsNullOrEmpty(this.TagValue))
            {
                result = result.Where(r => r.Tags != null &&
                                      r.Tags.Values != null &&
                                      r.Tags.Values.Where(v => string.Equals(v, this.TagValue, StringComparison.OrdinalIgnoreCase))
                                      .Any());
            }

            WriteObject(result, true);
        }