コード例 #1
0
        /// <summary>
        /// Suppression delete utility for a given recommendation resource Id.
        /// </summary>
        /// <param name="resourceUri">ResourceID of the recommendation</param>
        /// <param name="recommendationId">RecommendationId of the recommendation</param>
        /// <returns>List of PsAzureAdvisorResourceRecommendationBase objects</returns>
        public List <PsAzureAdvisorResourceRecommendationBase> SuppressionDelete(string resourceUri, string recommendationId)
        {
            AzureOperationResponse <IPage <SuppressionContract> >        suppressionList    = null;
            AzureOperationResponse <IPage <ResourceRecommendationBase> > recommendationList = null;
            IEnumerable <PsAzureAdvisorSuppressionContract> psSuppressionContractList       = null;

            IList <PsAzureAdvisorResourceRecommendationBase> responseRecommendationList = new List <PsAzureAdvisorResourceRecommendationBase>();
            List <PsAzureAdvisorResourceRecommendationBase>  responseRecommendation     = new List <PsAzureAdvisorResourceRecommendationBase>();
            List <AzureOperationResponse> response = new List <AzureOperationResponse>();

            // Get the list of all suppressions
            suppressionList           = this.ResourceAdvisorClient.Suppressions.ListWithHttpMessagesAsync().Result;
            psSuppressionContractList = PsAzureAdvisorSuppressionContract.FromSuppressionContractList(suppressionList.Body.AsEnumerable());

            // Get all the suppression for this recommendationId
            foreach (PsAzureAdvisorSuppressionContract contract in psSuppressionContractList)
            {
                // Delete only if the supression belongs to provided RecommendationName
                if (contract.Id.Contains(recommendationId))
                {
                    response.Add(this.ResourceAdvisorClient.Suppressions.DeleteWithHttpMessagesAsync(resourceUri, recommendationId, contract.Name).Result);
                }
            }

            // Get all the recommendation and convert to its corresponding psobject
            recommendationList         = this.ResourceAdvisorClient.Recommendations.ListWithHttpMessagesAsync().Result;
            responseRecommendationList = PsAzureAdvisorResourceRecommendationBase.GetFromResourceRecommendationBase(recommendationList.Body);

            // Add the particular recommendation to the response of cmdlet
            responseRecommendation.Add(RecommendationHelper.RecommendationFilterByRecommendation(responseRecommendationList, recommendationId));
            return(responseRecommendation);
        }
コード例 #2
0
        /// <summary>
        /// Gets the list of recommendations associated with the given resourceId. Default subscriptionId will be used to gather recommendation(s).
        /// </summary>
        /// <param name="advisorClient">Advisor Client</param>
        /// <param name="resourceId">ResourceId of recommendations</param>
        /// <returns>List of PsAzureAdvisorResourceRecommendationBase</returns>
        public List <PsAzureAdvisorResourceRecommendationBase> GetAllRecommendationsFromClient(IAdvisorManagementClient advisorClient, string resourceId)
        {
            AzureOperationResponse <IPage <ResourceRecommendationBase> > operationResponseRecommendation = null;
            List <ResourceRecommendationBase> entirePageLinkRecommendationData = new List <ResourceRecommendationBase>();
            string nextPageLink = string.Empty;

            do
            {
                if (string.IsNullOrEmpty(nextPageLink))
                {
                    operationResponseRecommendation = advisorClient.Recommendations.ListWithHttpMessagesAsync().Result;
                }
                else
                {
                    operationResponseRecommendation = advisorClient.Recommendations.ListNextWithHttpMessagesAsync(nextPageLink).Result;
                }
                nextPageLink = operationResponseRecommendation.Body.NextPageLink;

                // Add current page items to the List
                entirePageLinkRecommendationData.AddRange(operationResponseRecommendation.Body.ToList());
            }while (!string.IsNullOrEmpty(nextPageLink));

            // Convert to PsAzureAdvisorResourceRecommendationBase list and return
            return(RecommendationHelper.RecommendationFilterByResourceId(
                       PsAzureAdvisorResourceRecommendationBase.GetFromResourceRecommendationBase(entirePageLinkRecommendationData),
                       resourceId));
        }
コード例 #3
0
        /// <summary>
        /// Executes the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            RecommendationResource recommendationResourceUtil                  = new RecommendationResource();
            List <PsAzureAdvisorResourceRecommendationBase> results            = new List <PsAzureAdvisorResourceRecommendationBase>();
            List <ResourceRecommendationBase> entirePageLinkRecommendationData = new List <ResourceRecommendationBase>();
            AzureOperationResponse <ResourceRecommendationBase> recommendation = null;

            switch (this.ParameterSetName)
            {
            case IdParameterSet:
                string recommendationId = RecommendationHelper.GetRecommendationIdFromResourceID(this.ResourceId);

                recommendation = this.ResourceAdvisorClient.Recommendations.GetWithHttpMessagesAsync("subscriptions/" + this.ResourceAdvisorClient.SubscriptionId, recommendationId).Result;
                results.Add(PsAzureAdvisorResourceRecommendationBase.GetFromResourceRecommendationBase(recommendation.Body));
                break;

            case NameParameterSet:
                results = recommendationResourceUtil.GetAllRecommendationsFromClient(this.ResourceAdvisorClient);

                // Filter out the resourcegroupname recommendations
                if (!string.IsNullOrEmpty(this.ResourceGroupName))
                {
                    results = RecommendationHelper.RecommendationFilterByCategoryAndResource(results, string.Empty, this.ResourceGroupName);
                }
                break;
            }

            if (!string.IsNullOrEmpty(this.Category))
            {
                results = RecommendationHelper.RecommendationFilterByCategoryAndResource(results, this.Category, string.Empty);
            }

            this.WriteObject(results, true);
        }
コード例 #4
0
        /// <summary>
        /// Executes the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            List <PsAzureAdvisorResourceRecommendationBase> results = new List <PsAzureAdvisorResourceRecommendationBase>();

            AzureOperationResponse <IPage <ResourceRecommendationBase> > operationResponseRecommendation = null;
            List <ResourceRecommendationBase> entirePageLinkRecommendationData = new List <ResourceRecommendationBase>();
            AzureOperationResponse <ResourceRecommendationBase> recommendation = null;

            switch (this.ParameterSetName)
            {
            case IdParameterSet:
                string recommendationId = RecommendationHelper.GetRecommendationIdFromResourceID(this.ResourceId);

                recommendation = this.ResourceAdvisorClient.Recommendations.GetWithHttpMessagesAsync("subscriptions/" + this.ResourceAdvisorClient.SubscriptionId, recommendationId).Result;
                results.Add(PsAzureAdvisorResourceRecommendationBase.GetFromResourceRecommendationBase(recommendation.Body));
                break;

            case NameParameterSet:
                string nextPagelink = string.Empty;

                // Iterate the page-link if exists, if the first iteration retreives the data.
                do
                {
                    if (string.IsNullOrEmpty(nextPagelink))
                    {
                        operationResponseRecommendation = this.ResourceAdvisorClient.Recommendations.ListWithHttpMessagesAsync().Result;
                    }
                    else
                    {
                        operationResponseRecommendation = this.ResourceAdvisorClient.Recommendations.ListWithHttpMessagesAsync(nextPagelink).Result;
                    }
                    // Advisor .net SDK is broken for the paging capability. So we will not use the paging until SDK is fixed.
                    // nextPagelink = operationResponseRecommendation.Body.NextPageLink;

                    // Add current page items to the List
                    entirePageLinkRecommendationData.AddRange(operationResponseRecommendation.Body.ToList());
                }while (!string.IsNullOrEmpty(nextPagelink));

                // Convert to PsAzureAdvisorResourceRecommendationBase list
                results = PsAzureAdvisorResourceRecommendationBase.GetFromResourceRecommendationBase(entirePageLinkRecommendationData);

                // Filter out the resourcegroupname recommendations
                if (!string.IsNullOrEmpty(this.ResourceGroupName))
                {
                    results = RecommendationHelper.RecommendationFilterByCategoryAndResource(results, string.Empty, this.ResourceGroupName);
                }

                break;
            }

            if (!string.IsNullOrEmpty(this.Category))
            {
                results = RecommendationHelper.RecommendationFilterByCategoryAndResource(results, this.Category, string.Empty);
            }

            if (Refresh)
            {
                this.ResourceAdvisorClient.Recommendations.GenerateWithHttpMessagesAsync();
            }

            this.WriteObject(results, true);
        }