Skip to content

Commit

Permalink
Implementing Test cmdlets for Deployment Stacks Validation. (Azure#25363
Browse files Browse the repository at this point in the history
)

* Implementing Test cmdlets for Deployment Stacks Validation.

Standalone Test cmdlets so validation can be run on deployment stacks
without having to actually deploy.

* update a session record.

* added help files

* Updated help files.

* Update help.

* Update SignatureIssues.csv

---------

Co-authored-by: Dante DG <test>
Co-authored-by: Yabo Hu <[email protected]>
  • Loading branch information
dantedallag and VeryEarly authored Jul 23, 2024
1 parent f2c1ce8 commit d1d1ac8
Show file tree
Hide file tree
Showing 19 changed files with 6,005 additions and 524 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentStacks;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using System;
using System.Collections;
using System.Management.Automation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentStacks;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using System;
using System.Collections;
using System.Management.Automation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentStacks;
using Microsoft.Azure.Management.Resources.Models;
using System;
using System.Management.Automation;

[Cmdlet("Test", Common.AzureRMConstants.AzureRMPrefix + "ManagementGroupDeploymentStack",
SupportsShouldProcess = true, DefaultParameterSetName = ParameterlessTemplateFileParameterSetName), OutputType(typeof(bool))]
public class TestAzManagementGroupDeploymentStack : NewAzManagementGroupDeploymentStack
{

[Parameter(Mandatory = false, HelpMessage = "If set, a boolean will be returned with value dependent on cmdlet success.")]
public SwitchParameter PassThru { get; set; }

protected override void OnProcessRecord()
{
try
{
var shouldDeleteResources = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll || ActionOnUnmanage is PSActionOnUnmanage.DeleteResources) ? true : false;
var shouldDeleteResourceGroups = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll) ? true : false;
var shouldDeleteManagementGroups = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll) ? true : false;

string deploymentScope = null;
if (DeploymentSubscriptionId != null)
{
deploymentScope = "/subscriptions/" + DeploymentSubscriptionId;
}

DeploymentStacksSdkClient.ManagementGroupValidateDeploymentStack(
deploymentStackName: Name,
managementGroupId: ManagementGroupId,
location: Location,
templateFile: TemplateFile,
templateUri: !string.IsNullOrEmpty(protectedTemplateUri) ? protectedTemplateUri : TemplateUri,
templateSpec: TemplateSpecId,
templateObject: TemplateObject,
parameterUri: TemplateParameterUri,
parameters: GetTemplateParameterObject(),
description: Description,
resourcesCleanupAction: shouldDeleteResources ? "delete" : "detach",
resourceGroupsCleanupAction: shouldDeleteResourceGroups ? "delete" : "detach",
managementGroupsCleanupAction: shouldDeleteManagementGroups ? "delete" : "detach",
deploymentScope: deploymentScope,
denySettingsMode: DenySettingsMode.ToString(),
denySettingsExcludedPrincipals: DenySettingsExcludedPrincipal,
denySettingsExcludedActions: DenySettingsExcludedAction,
denySettingsApplyToChildScopes: DenySettingsApplyToChildScopes.IsPresent,
tags: Tag,
bypassStackOutOfSyncError: BypassStackOutOfSyncError.IsPresent
);

if (PassThru.IsPresent)
{
WriteObject(true);
}
}
catch (Exception ex)
{
if (ex is DeploymentStacksErrorException dex)
throw new PSArgumentException(dex.Message + " : " + dex.Body.Error.Code + " : " + dex.Body.Error.Message);
else
WriteExceptionError(ex);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentStacks;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.Resources.Models;
using System;
using System.Management.Automation;

[Cmdlet("Test", Common.AzureRMConstants.AzureRMPrefix + "ResourceGroupDeploymentStack",
SupportsShouldProcess = true, DefaultParameterSetName = ParameterlessTemplateFileParameterSetName), OutputType(typeof(bool))]
public class TestAzResourceGroupDeploymentStack : NewAzResourceGroupDeploymentStack
{
[Parameter(Mandatory = false, HelpMessage = "If set, a boolean will be returned with value dependent on cmdlet success.")]
public SwitchParameter PassThru { get; set; }

protected override void OnProcessRecord()
{
try
{
var shouldDeleteResources = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll || ActionOnUnmanage is PSActionOnUnmanage.DeleteResources) ? true : false;
var shouldDeleteResourceGroups = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll) ? true : false;
var shouldDeleteManagementGroups = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll) ? true : false;

var currentStack = DeploymentStacksSdkClient.GetResourceGroupDeploymentStack(ResourceGroupName, Name, throwIfNotExists: false);
if (currentStack != null && Tag == null)
{
Tag = TagsConversionHelper.CreateTagHashtable(currentStack.tags);
}

DeploymentStacksSdkClient.ResourceGroupValidateDeploymentStack(
deploymentStackName: Name,
resourceGroupName: ResourceGroupName,
templateFile: TemplateFile,
templateUri: !string.IsNullOrEmpty(protectedTemplateUri) ? protectedTemplateUri : TemplateUri,
templateSpec: TemplateSpecId,
templateObject: TemplateObject,
parameterUri: TemplateParameterUri,
parameters: GetTemplateParameterObject(),
description: Description,
resourcesCleanupAction: shouldDeleteResources ? "delete" : "detach",
resourceGroupsCleanupAction: shouldDeleteResourceGroups ? "delete" : "detach",
managementGroupsCleanupAction: shouldDeleteManagementGroups ? "delete" : "detach",
denySettingsMode: DenySettingsMode.ToString(),
denySettingsExcludedPrincipals: DenySettingsExcludedPrincipal,
denySettingsExcludedActions: DenySettingsExcludedAction,
denySettingsApplyToChildScopes: DenySettingsApplyToChildScopes.IsPresent,
tags: Tag,
bypassStackOutOfSyncError: BypassStackOutOfSyncError.IsPresent
);

if (PassThru.IsPresent)
{
WriteObject(true);
}
}
catch (Exception ex)
{
if (ex is DeploymentStacksErrorException dex)
throw new PSArgumentException(dex.Message + " : " + dex.Body.Error.Code + " : " + dex.Body.Error.Message);
else
WriteExceptionError(ex);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.DeploymentStacks;
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
using Microsoft.Azure.Management.Resources.Models;
using System;
using System.Management.Automation;

[Cmdlet("Test", Common.AzureRMConstants.AzureRMPrefix + "SubscriptionDeploymentStack",
SupportsShouldProcess = true, DefaultParameterSetName = ParameterlessTemplateFileParameterSetName), OutputType(typeof(bool))]
public class TestAzSubscriptionDeploymentStack : NewAzSubscriptionDeploymentStack
{

[Parameter(Mandatory = false, HelpMessage = "If set, a boolean will be returned with value dependent on cmdlet success.")]
public SwitchParameter PassThru { get; set; }

protected override void OnProcessRecord()
{
try
{
var shouldDeleteResources = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll || ActionOnUnmanage is PSActionOnUnmanage.DeleteResources) ? true : false;
var shouldDeleteResourceGroups = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll) ? true : false;
var shouldDeleteManagementGroups = (ActionOnUnmanage is PSActionOnUnmanage.DeleteAll) ? true : false;

// construct deploymentScope if ResourceGroup was provided
var deploymentScope = DeploymentResourceGroupName != null ? "/subscriptions/" + DeploymentStacksSdkClient.DeploymentStacksClient.SubscriptionId
+ "/resourceGroups/" + DeploymentResourceGroupName : null;

var currentStack = DeploymentStacksSdkClient.GetSubscriptionDeploymentStack(Name, throwIfNotExists: false);
if (currentStack != null && Tag == null)
{
Tag = TagsConversionHelper.CreateTagHashtable(currentStack.tags);
}

DeploymentStacksSdkClient.SubscriptionValidateDeploymentStack(
deploymentStackName: Name,
location: Location,
templateFile: TemplateFile,
templateUri: !string.IsNullOrEmpty(protectedTemplateUri) ? protectedTemplateUri : TemplateUri,
templateSpec: TemplateSpecId,
templateObject: TemplateObject,
parameterUri: TemplateParameterUri,
parameters: GetTemplateParameterObject(),
description: Description,
resourcesCleanupAction: shouldDeleteResources ? "delete" : "detach",
resourceGroupsCleanupAction: shouldDeleteResourceGroups ? "delete" : "detach",
managementGroupsCleanupAction: shouldDeleteManagementGroups ? "delete" : "detach",
deploymentScope: deploymentScope,
denySettingsMode: DenySettingsMode.ToString(),
denySettingsExcludedPrincipals: DenySettingsExcludedPrincipal,
denySettingsExcludedActions: DenySettingsExcludedAction,
denySettingsApplyToChildScopes: DenySettingsApplyToChildScopes.IsPresent,
tags: Tag,
bypassStackOutOfSyncError: BypassStackOutOfSyncError.IsPresent
);

if (PassThru.IsPresent)
{
WriteObject(true);
}
}
catch (Exception ex)
{
if (ex is DeploymentStacksErrorException dex)
throw new PSArgumentException(dex.Message + " : " + dex.Body.Error.Code + " : " + dex.Body.Error.Message);
else
WriteExceptionError(ex);
}
}
}
}
Loading

0 comments on commit d1d1ac8

Please sign in to comment.