protected PSWhatIfOperationResult ExecuteWhatIf()
        {
            const string statusMessage = "Getting the latest status of all resources...";
            var          clearMessage  = new string(' ', statusMessage.Length);
            var          information   = new HostInformationMessage {
                Message = statusMessage, NoNewLine = true
            };
            var clearInformation = new HostInformationMessage {
                Message = $"\r{clearMessage}\r", NoNewLine = true
            };
            var tags = new[] { "PSHOST" };

            try
            {
                // Write status message.
                this.WriteInformation(information, tags);

                PSWhatIfOperationResult whatIfResult = ResourceManagerSdkClient.ExecuteDeploymentWhatIf(this.WhatIfParameters);

                // Clear status before returning result.
                this.WriteInformation(clearInformation, tags);

                return(whatIfResult);
            }
            catch (Exception)
            {
                // Clear status before on exception.
                this.WriteInformation(clearInformation, tags);
                throw;
            }
        }
Esempio n. 2
0
        public static string Format(PSWhatIfOperationResult result)
        {
            var builder   = new ColoredStringBuilder();
            var formatter = new WhatIfOperationResultFormatter(builder);

            formatter.FormatPreviewNotice();
            formatter.FormatLegend(result.Changes);
            formatter.FormatResourceChanges(result.Changes);
            formatter.FormatStats(result.Changes);

            return(builder.ToString());
        }
Esempio n. 3
0
        public void Format_EmptyResourceChanges_ReturnsNoChangeInfo()
        {
            // Arrange.
            var    psWhatIfOperationResult = new PSWhatIfOperationResult(new WhatIfOperationResult());
            string noChangeInfo            = new ColoredStringBuilder()
                                             .AppendLine()
                                             .Append("Resource changes: no change.")
                                             .ToString();

            // Act.
            string result = WhatIfOperationResultFormatter.Format(psWhatIfOperationResult);

            // Assert.
            Assert.Contains(noChangeInfo, result);
        }
Esempio n. 4
0
        public void Format_OnSuccess_FormatPreviewNotice()
        {
            // Arrange.
            var    psWhatIfOperationResult = new PSWhatIfOperationResult(new WhatIfOperationResult());
            string previewNotice           = new ColoredStringBuilder()
                                             .AppendLine("Note: As What-If is currently in preview, the result may contain false positive predictions (noise).")
                                             .AppendLine("You can help us improve the accuracy of the result by opening an issue here: https://aka.ms/WhatIfIssues.")
                                             .ToString();

            // Act.
            string result = WhatIfOperationResultFormatter.Format(psWhatIfOperationResult);

            // Assert.
            Assert.StartsWith(previewNotice, result);
        }
Esempio n. 5
0
        private string ExecuteWhatIf()
        {
            const string statusMessage = "Getting the latest status of all resources...";
            var          clearMessage  = new string(' ', statusMessage.Length);
            var          information   = new HostInformationMessage {
                Message = statusMessage, NoNewLine = true
            };
            var clearInformation = new HostInformationMessage {
                Message = $"\r{clearMessage}\r", NoNewLine = true
            };
            var tags = new[] { "PSHOST" };

            try
            {
                // Write status message.
                this.WriteInformation(information, tags);


                var parameters = new PSDeploymentWhatIfCmdletParameters
                {
                    DeploymentName           = this.Name,
                    Location                 = this.Location,
                    Mode                     = DeploymentMode.Incremental,
                    TemplateUri              = TemplateUri ?? this.TryResolvePath(TemplateFile),
                    TemplateObject           = this.TemplateObject,
                    TemplateParametersUri    = this.TemplateParameterUri,
                    TemplateParametersObject = GetTemplateParameterObject(this.TemplateParameterObject),
                    ResultFormat             = this.WhatIfResultFormat
                };

                PSWhatIfOperationResult whatIfResult = ResourceManagerSdkClient.ExecuteDeploymentWhatIf(parameters, this.WhatIfExcludeChangeType);
                string whatIfMessage = WhatIfOperationResultFormatter.Format(whatIfResult);

                // Clear status before returning result.
                this.WriteInformation(clearInformation, tags);

                // Use \r to override the built-in "What if:" in output.
                return($"\r        \r{Environment.NewLine}{whatIfMessage}{Environment.NewLine}");
            }
            catch (Exception)
            {
                // Clear status before handling exception.
                this.WriteInformation(clearInformation, tags);
                throw;
            }
        }
Esempio n. 6
0
        public void Format_NonEmptyResourceChanges_AddsLegendAtTheBeginning()
        {
            // Arrange.
            var resourceChanges = new List <WhatIfChange>
            {
                new WhatIfChange
                {
                    ResourceId = "",
                    ChangeType = ChangeType.Deploy
                },
                new WhatIfChange
                {
                    ResourceId = "",
                    ChangeType = ChangeType.Create
                },
                new WhatIfChange
                {
                    ResourceId = "",
                    ChangeType = ChangeType.Ignore
                },
                new WhatIfChange
                {
                    ResourceId = "",
                    ChangeType = ChangeType.Create
                }
            };

            var psWhatIfOperationResult =
                new PSWhatIfOperationResult(new WhatIfOperationResult(changes: resourceChanges));

            string legend = @"Resource and property changes are indicated with these symbols:
  + Create
  ! Deploy
  * Ignore
"
                            .Replace("+", new ColoredStringBuilder().Append("+", Color.Green).ToString())
                            .Replace("!", new ColoredStringBuilder().Append("!", Color.Blue).ToString())
                            .Replace("*", new ColoredStringBuilder().Append("*", Color.Gray).ToString())
                            .Replace("\r\n", Environment.NewLine);

            // Act.
            string result = WhatIfOperationResultFormatter.Format(psWhatIfOperationResult);

            // Assert.
            Assert.Contains(legend, result);
        }
Esempio n. 7
0
        protected override void OnProcessRecord()
        {
            const string statusMessage = "Getting the latest status of all resources...";
            var          clearMessage  = new string(' ', statusMessage.Length);
            var          information   = new HostInformationMessage {
                Message = statusMessage, NoNewLine = true
            };
            var clearInformation = new HostInformationMessage {
                Message = $"\r{clearMessage}\r", NoNewLine = true
            };
            var tags = new[] { "PSHOST" };

            try
            {
                // Write status message.
                this.WriteInformation(information, tags);

                var parameters = new PSDeploymentWhatIfCmdletParameters
                {
                    DeploymentName           = this.Name,
                    Location                 = this.Location,
                    Mode                     = DeploymentMode.Incremental,
                    TemplateUri              = TemplateUri ?? this.TryResolvePath(TemplateFile),
                    TemplateObject           = this.TemplateObject,
                    TemplateParametersUri    = this.TemplateParameterUri,
                    TemplateParametersObject = GetTemplateParameterObject(this.TemplateParameterObject),
                    ResultFormat             = this.ResultFormat
                };

                PSWhatIfOperationResult whatIfResult = ResourceManagerSdkClient.ExecuteDeploymentWhatIf(parameters);

                // Clear status before returning result.
                this.WriteInformation(clearInformation, tags);
                this.WriteObject(whatIfResult);
            }
            catch (Exception)
            {
                // Clear status on exception.
                this.WriteInformation(clearInformation, tags);
                throw;
            }
        }
Esempio n. 8
0
        protected override void OnProcessRecord()
        {
            string whatIfMessage  = null;
            string warningMessage = null;
            string captionMessage = null;

            if (this.ShouldExecuteWhatIf())
            {
                PSWhatIfOperationResult whatIfResult = this.ExecuteWhatIf();
                string whatIfFormattedOutput         = WhatIfOperationResultFormatter.Format(whatIfResult);

                if (this.ShouldProcessGivenCurrentConfirmFlagAndPreference() &&
                    this.ShouldSkipConfirmationIfNoChange() &&
                    whatIfResult.Changes.All(x => x.ChangeType == ChangeType.NoChange || x.ChangeType == ChangeType.Ignore))
                {
                    var whatIfInformation = new HostInformationMessage {
                        Message = whatIfFormattedOutput
                    };
                    var tags = new[] { "PSHOST" };

                    this.WriteInformation(whatIfInformation, tags);
                    this.ExecuteDeployment();

                    return;
                }

                string cursorUp = $"{(char)27}[1A";

                // Use \r to override the built-in "What if:" in output.
                whatIfMessage  = $"\r        \r{Environment.NewLine}{whatIfFormattedOutput}{Environment.NewLine}";
                warningMessage = $"{Environment.NewLine}{Resources.ConfirmDeploymentMessage}";
                captionMessage = $"{cursorUp}{Color.Reset}{whatIfMessage}";
            }

            if (this.ShouldProcess(whatIfMessage, warningMessage, captionMessage))
            {
                this.ExecuteDeployment();
            }
        }
Esempio n. 9
0
        protected override void OnProcessRecord()
        {
            string whatIfMessage  = null;
            string warningMessage = null;
            string captionMessage = null;

            if (this.ShouldExecuteWhatIf())
            {
                PSWhatIfOperationResult whatIfResult = this.ExecuteWhatIf();
                string whatIfFormattedOutput         = WhatIfOperationResultFormatter.Format(whatIfResult);
                string cursorUp = $"{(char)27}[1A";

                // Use \r to override the built-in "What if:" in output.
                whatIfMessage  = $"\r        \r{Environment.NewLine}{whatIfFormattedOutput}{Environment.NewLine}";
                warningMessage = $"{Environment.NewLine}{Resources.ConfirmDeploymentMessage}";
                captionMessage = $"{cursorUp}{Color.Reset}{whatIfMessage}";
            }

            if (this.ShouldProcess(whatIfMessage, warningMessage, captionMessage))
            {
                this.ExecuteDeployment();
            }
        }
        protected override void OnProcessRecord()
        {
            PSWhatIfOperationResult whatIfResult = this.ExecuteWhatIf();

            this.WriteObject(whatIfResult);
        }