Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            string resourceUri      = string.Empty;
            string recommendationId = string.Empty;

            WriteVerbose(Resources.SuppressionRemove);

            AzureOperationResponse <IPage <SuppressionContract> > suppressionList = null;

            // This i used for the recommendation call, to collect supression ID data associated.
            // AzureOperationResponse<IPage<ResourceRecommendationBase>> recommendationList = null;
            IList <PsAzureAdvisorResourceRecommendationBase> responseRecommendationList = new List <PsAzureAdvisorResourceRecommendationBase>();

            List <PsAzureAdvisorResourceRecommendationBase>     responseRecommendation = new List <PsAzureAdvisorResourceRecommendationBase>();
            AzureOperationResponse <ResourceRecommendationBase> recommendation         = null;

            IEnumerable <PsAzureAdvisorSuppressionContract> psSuppressionContractList = null;
            var response = new List <AzureOperationResponse>();

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

                if (ShouldProcess(recommendationId, string.Format(Resources.EnableRecommendationWarningMessage, this.RecommendationName)))
                {
                    responseRecommendation.AddRange(this.SuppressionDelete(resourceUri, recommendationId));
                }
                break;

            case NameParameterSet:
                recommendation            = this.ResourceAdvisorClient.Recommendations.GetWithHttpMessagesAsync("subscriptions/" + this.ResourceAdvisorClient.SubscriptionId, this.RecommendationName).Result;
                resourceUri               = RecommendationHelper.GetFullResourceUriFromResourceID(recommendation.Body.Id);
                suppressionList           = this.ResourceAdvisorClient.Suppressions.ListWithHttpMessagesAsync().Result;
                psSuppressionContractList = PsAzureAdvisorSuppressionContract.FromSuppressionContractList(suppressionList.Body.AsEnumerable());

                if (ShouldProcess(this.RecommendationName, string.Format(Resources.EnableRecommendationWarningMessage, this.RecommendationName)))
                {
                    responseRecommendation.AddRange(this.SuppressionDelete(resourceUri, this.RecommendationName));
                }
                break;

            case InputObjectParameterSet:
                // Parse out the Subscription-ID, Recommendation-ID from the ResourceId parameter.
                resourceUri      = RecommendationHelper.GetFullResourceUriFromResourceID(this.InputObject.Id);
                recommendationId = RecommendationHelper.GetRecommendationIdFromResourceID(this.InputObject.Id);
                if (ShouldProcess(recommendationId, string.Format(Resources.EnableRecommendationWarningMessage, recommendationId)))
                {
                    responseRecommendation.AddRange(this.SuppressionDelete(resourceUri, recommendationId));
                }
                break;
            }

            this.WriteObject(responseRecommendation, true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            string resourceUri      = string.Empty;
            string recommendationId = string.Empty;
            SuppressionContract suppressionContract = null;

            WriteVerbose(Resources.SuppressionCreate);

            // This list contains all the response for the azure-operation
            List <AzureOperationResponse <SuppressionContract> > azureOperationResponseSuppression = new List <AzureOperationResponse <SuppressionContract> >();
            var returnSuppressionContract = new List <PsAzureAdvisorSuppressionContract>();

            // Create the suppression contract
            suppressionContract = new SuppressionContract(null, DefaultSuppressionName, null, null, this.Days == 0 ? string.Empty : this.Days.ToString());

            switch (this.ParameterSetName)
            {
            case IdParameterSet:
                // parse out the Subscription-ID, Recommendation-ID form the ResourceId parameter.
                resourceUri      = RecommendationHelper.GetFullResourceUriFromResourceID(this.ResourceId);
                recommendationId = RecommendationHelper.GetRecommendationIdFromResourceID(this.ResourceId);

                if (ShouldProcess(recommendationId, string.Format(Resources.DisableRecommendationWarningMessage, recommendationId)))
                {
                    azureOperationResponseSuppression.Add(this.ResourceAdvisorClient.Suppressions.CreateWithHttpMessagesAsync(resourceUri, recommendationId, DefaultSuppressionName, suppressionContract).Result);
                }

                break;

            case NameParameterSet:
                AzureOperationResponse <ResourceRecommendationBase> recommendation = this.ResourceAdvisorClient.Recommendations.GetWithHttpMessagesAsync("subscriptions/" + this.ResourceAdvisorClient.SubscriptionId, this.RecommendationName).Result;
                resourceUri = RecommendationHelper.GetFullResourceUriFromResourceID(recommendation.Body.Id);

                // Make a get recommendation for this Name and get the ID
                if (ShouldProcess(this.RecommendationName, string.Format(Resources.DisableRecommendationWarningMessage, this.RecommendationName)))
                {
                    azureOperationResponseSuppression.Add(this.ResourceAdvisorClient.Suppressions.CreateWithHttpMessagesAsync(resourceUri, this.RecommendationName, DefaultSuppressionName, suppressionContract).Result);
                }
                break;

            case InputObjectParameterSet:

                // Parse out the Subscription-ID, Recommendation-ID from the ResourceId parameter.
                resourceUri      = RecommendationHelper.GetFullResourceUriFromResourceID(this.InputObject.ResourceId);
                recommendationId = RecommendationHelper.GetRecommendationIdFromResourceID(this.InputObject.ResourceId);

                if (ShouldProcess(recommendationId, string.Format(Resources.DisableRecommendationWarningMessage, recommendationId)))
                {
                    azureOperationResponseSuppression.Add(this.ResourceAdvisorClient.Suppressions.CreateWithHttpMessagesAsync(resourceUri, recommendationId, DefaultSuppressionName, suppressionContract).Result);
                }
                break;
            }

            // Get the supresssion details from the suppression Get API, the response does not have the data for the suppression-name, resourceid.
            if (azureOperationResponseSuppression.Count > 0)
            {
                AzureOperationResponse <IPage <SuppressionContract> > suppressionList           = this.ResourceAdvisorClient.Suppressions.ListWithHttpMessagesAsync().Result;
                IEnumerable <PsAzureAdvisorSuppressionContract>       psSuppressionContractList = PsAzureAdvisorSuppressionContract.FromSuppressionContractList(suppressionList.Body.AsEnumerable());

                foreach (AzureOperationResponse <SuppressionContract> azureOperationResponse in azureOperationResponseSuppression)
                {
                    foreach (PsAzureAdvisorSuppressionContract contractItem in psSuppressionContractList)
                    {
                        // Match the supression-ID
                        if (azureOperationResponse.Body.SuppressionId == contractItem.SuppressionId)
                        {
                            returnSuppressionContract.Add(contractItem);
                        }
                    }
                }
            }

            this.WriteObject(returnSuppressionContract, true);
        }