Skip to content

Commit 315809f

Browse files
committed
working on implementing commands
1 parent 6aff57e commit 315809f

10 files changed

+553
-89
lines changed

NuGet.Config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<packageSources>
4-
<clear/>
4+
<clear />
55
<!-- Do not add any additional feeds if new packages are needed they need to come from nuget.org or our azure-sdk-for-net DevOps feed -->
66
<add key="local-feed" value="tools/LocalFeed" />
77
<add key="azure-powershell" value="https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-powershell/nuget/v3/index.json" />

src/Resources/ResourceManager/Implementation/DeploymentStacks/ExportAzManagmentGroupDeploymentStackTemplate.cs

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,84 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
1615
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1716
{
17+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
18+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
1819
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
20+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22+
using Newtonsoft.Json.Linq;
1923
using System;
20-
using System.Collections.Generic;
2124
using System.Management.Automation;
22-
using System.Text;
2325

2426
[Cmdlet("Export", Common.AzureRMConstants.AzureRMPrefix + "ManagementGroupDeploymentStackTemplate",
25-
DefaultParameterSetName = ExportAzResourceGroupDeploymentStackTemplate.ExportByDeploymentStackName)]
26-
internal class ExportAzManagmentGroupDeploymentStackTemplate
27+
DefaultParameterSetName = ExportAzManagementGroupDeploymentStackTemplate.ExportByName)]
28+
public class ExportAzManagementGroupDeploymentStackTemplate : DeploymentStacksCmdletBase
2729
{
30+
#region Cmdlet Parameters and Parameter Set Definitions
31+
32+
internal const string ExportByResourceId = "ExportByResourceId";
33+
internal const string ExportByName = "ExportByName";
34+
35+
[Alias("Id")]
36+
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ExportByResourceId)]
37+
[ValidateNotNullOrEmpty]
38+
public string ResourceId { get; set; }
39+
40+
[Alias("StackName")]
41+
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ExportByName)]
42+
[ValidateNotNullOrEmpty]
43+
public string Name { get; set; }
44+
45+
[Alias("ManagementGroupId")]
46+
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ExportByName)]
47+
[ValidateNotNullOrEmpty]
48+
public string ManagementGroupId { get; set; }
49+
50+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The path to the file where the deployment stack template will be output to.")]
51+
[ValidateNotNullOrEmpty]
52+
public string OutputFile { get; set; }
53+
54+
#endregion
55+
56+
#region Cmdlet Overrides
57+
protected override void OnProcessRecord()
58+
{
59+
try
60+
{
61+
JObject template;
62+
switch (ParameterSetName)
63+
{
64+
case ExportByResourceId:
65+
template = DeploymentStacksSdkClient.ExportManagementGroupDeploymentStack(ResourceIdUtility.GetManagementGroupId(ResourceId), ResourceIdUtility.GetDeploymentName(ResourceId));
66+
break;
67+
case ExportByName:
68+
template = DeploymentStacksSdkClient.ExportManagementGroupDeploymentStack(ManagementGroupId, Name);
69+
break;
70+
default:
71+
throw new PSInvalidOperationException();
72+
}
73+
74+
// Ensure our output path is resolved based on the current powershell working
75+
// directory instead of the current process directory:
76+
OutputFile = ResolveUserPath(OutputFile);
77+
78+
string path = FileUtility.SaveTemplateFile(
79+
templateName: this.Name,
80+
contents: template.ToString(),
81+
outputPath: this.OutputFile,
82+
overwrite: true,
83+
shouldContinue: ShouldContinue);
84+
85+
WriteObject(PowerShellUtilities.ConstructPSObject(null, "Path", OutputFile));
86+
}
87+
catch (Exception ex)
88+
{
89+
WriteExceptionError(ex);
90+
}
91+
}
92+
93+
#endregion
2894
}
2995
}

src/Resources/ResourceManager/Implementation/DeploymentStacks/ExportAzResourceGroupDeploymentStackTemplate.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1717
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
1919
using Microsoft.WindowsAzure.Commands.Utilities.Common;
20+
using Newtonsoft.Json.Linq;
2021
using System;
2122
using System.Management.Automation;
2223

@@ -44,9 +45,9 @@ public class ExportAzResourceGroupDeploymentStackTemplate : DeploymentStacksCmdl
4445
public string Name { get; set; }
4546

4647

47-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The path to the folder where the deployment stack template will be output to.")]
48+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The path to the file where the deployment stack template will be output to.")]
4849
[ValidateNotNullOrEmpty]
49-
public string OutputFolder { get; set; }
50+
public string OutputFile { get; set; }
5051

5152
#endregion
5253

@@ -56,14 +57,14 @@ protected override void OnProcessRecord()
5657
{
5758
try
5859
{
59-
SdkModels.PSDeploymentStack stack;
60+
JObject template;
6061
switch (ParameterSetName)
6162
{
6263
case ExportByResourceIdParameterSetName:
63-
stack = DeploymentStacksSdkClient.GetResourceGroupDeploymentStack(ResourceIdUtility.GetResourceGroupName(ResourceId), ResourceIdUtility.GetDeploymentName(ResourceId));
64+
template = DeploymentStacksSdkClient.ExportResourceGroupDeploymentStack(ResourceIdUtility.GetResourceGroupName(ResourceId), ResourceIdUtility.GetDeploymentName(ResourceId));
6465
break;
6566
case ExportByDeploymentStackName:
66-
stack = DeploymentStacksSdkClient.GetResourceGroupDeploymentStack(ResourceGroupName, Name);
67+
template = DeploymentStacksSdkClient.ExportResourceGroupDeploymentStack(ResourceGroupName, Name);
6768
break;
6869
default:
6970
throw new PSInvalidOperationException();
@@ -72,22 +73,23 @@ protected override void OnProcessRecord()
7273

7374
// Ensure our output path is resolved based on the current powershell working
7475
// directory instead of the current process directory:
75-
OutputFolder = ResolveUserPath(OutputFolder);
76+
OutputFile = ResolveUserPath(OutputFile);
7677

7778
string path = FileUtility.SaveTemplateFile(
7879
templateName: this.Name,
79-
contents: stack.template.ToString(),
80-
outputPath: fullRootTemplateFilePath,
80+
contents: template.ToString(),
81+
outputPath: OutputFile,
8182
overwrite: true,
8283
shouldContinue: ShouldContinue);
8384

84-
WriteObject(PowerShellUtilities.ConstructPSObject(null, "Path", fullRootTemplateFilePath));
85+
WriteObject(PowerShellUtilities.ConstructPSObject(null, "Path", OutputFile));
8586
}
8687
catch (Exception ex)
8788
{
8889
WriteExceptionError(ex);
8990
}
9091
}
92+
9193
#endregion
9294
}
9395
}

src/Resources/ResourceManager/Implementation/DeploymentStacks/ExportAzSubscriptionDeploymentStackTemplate.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
2121
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22+
using Newtonsoft.Json;
23+
using Newtonsoft.Json.Linq;
2224
using System;
2325
using System.Management.Automation;
2426

@@ -42,7 +44,7 @@ public class ExportAzSubscriptionDeploymentStackTemplate : DeploymentStacksCmdle
4244
public string Name { get; set; }
4345

4446

45-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The path to the folder where the deployment stack template will be output to.")]
47+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The path to the file where the deployment stack template will be output to.")]
4648
[ValidateNotNullOrEmpty]
4749
public string OutputFile { get; set; }
4850

@@ -53,14 +55,14 @@ protected override void OnProcessRecord()
5355
{
5456
try
5557
{
56-
SdkModels.PSDeploymentStack stack;
58+
JObject template;
5759
switch (ParameterSetName)
5860
{
5961
case ExportByResourceIdParameterSetName:
60-
stack = DeploymentStacksSdkClient.GetSubscriptionDeploymentStack(ResourceIdUtility.GetResourceGroupName(ResourceId));
62+
template = DeploymentStacksSdkClient.ExportSubscriptionDeploymentStack(ResourceIdUtility.GetResourceGroupName(ResourceId));
6163
break;
6264
case ExportByDeploymentStackName:
63-
stack = DeploymentStacksSdkClient.GetSubscriptionDeploymentStack(Name);
65+
template = DeploymentStacksSdkClient.ExportSubscriptionDeploymentStack(Name);
6466
break;
6567
default:
6668
throw new PSInvalidOperationException();
@@ -70,11 +72,9 @@ protected override void OnProcessRecord()
7072
// directory instead of the current process directory:
7173
OutputFile = ResolveUserPath(OutputFile);
7274

73-
var contents = stack.ToString();
74-
7575
string path = FileUtility.SaveTemplateFile(
7676
templateName: this.Name,
77-
contents: contents,
77+
contents: template.ToString(),
7878
outputPath: this.OutputFile,
7979
overwrite: true,
8080
shouldContinue: ShouldContinue);
@@ -87,7 +87,6 @@ protected override void OnProcessRecord()
8787
}
8888
}
8989

90-
9190
#endregion
9291
}
9392
}

src/Resources/ResourceManager/Implementation/DeploymentStacks/GetAzManagementGroupDeploymentStack.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,33 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2222
using System.Text;
2323

2424
[Cmdlet("Get", Common.AzureRMConstants.AzureRMPrefix + "ManagementGroupDeploymentStack",
25-
DefaultParameterSetName = GetAzManagementGroupDeploymentStack.ListParameterSetname), OutputType(typeof(PSDeploymentStack))]
25+
DefaultParameterSetName = GetAzManagementGroupDeploymentStack.ListDeploymentStacksByManagementGroupId), OutputType(typeof(PSDeploymentStack))]
2626
public class GetAzManagementGroupDeploymentStack : DeploymentStacksCmdletBase
2727
{
2828
#region Cmdlet Parameters and Parameter Set Definitions
2929

30-
internal const string GetByStackNameParameterSetname = "GetIndividualDeploymentStack";
31-
internal const string GetByResourceIdParameterSetName = "GetDeploymentStackByResourceId";
32-
internal const string ListParameterSetname = "ListDeploymentStacks";
30+
internal const string GetDeploymentStackByResourceId = "GetDeploymentStackByResourceId";
31+
internal const string GetDeploymentStackByManagementGroupIdAndStackName = " GetDeploymentStackByManagementGroupIdAndStackName";
32+
internal const string ListDeploymentStacksByManagementGroupId = "ListDeploymentStacksByManagmentGroupId";
3333

3434
[Alias("StackName")]
35-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = GetByStackNameParameterSetname,
35+
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = GetDeploymentStackByManagementGroupIdAndStackName,
3636
HelpMessage = "The name of the deploymentStack to get")]
3737
[ValidateNotNullOrEmpty]
3838
public string Name { get; set; }
3939

4040
[Alias("Id")]
41-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = GetByResourceIdParameterSetName,
41+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = GetDeploymentStackByResourceId,
4242
HelpMessage = "ResourceId of the stack to get")]
4343
[ValidateNotNullOrEmpty]
4444
public string ResourceId { get; set; }
4545

46+
[Alias("Id")]
47+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = ListDeploymentStacksByManagementGroupId)]
48+
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = GetDeploymentStackByManagementGroupIdAndStackName)]
49+
[ValidateNotNullOrEmpty]
50+
public string ManagementGroupId { get; set; }
51+
4652
#endregion
4753

4854
#region Cmdlet Overrides
@@ -54,14 +60,15 @@ protected override void OnProcessRecord()
5460
this.GetResourcesClient();
5561
switch (ParameterSetName)
5662
{
57-
case GetByStackNameParameterSetname:
58-
WriteObject(DeploymentStacksSdkClient.GetManagementGroupDeploymentStack(Name), true);
63+
case GetDeploymentStackByResourceId:
64+
WriteObject(DeploymentStacksSdkClient.GetManagementGroupDeploymentStack(ResourceIdUtility.GetManagementGroupId(ResourceId),
65+
ResourceIdUtility.GetResourceName(ResourceId)), true);
5966
break;
60-
case GetByResourceIdParameterSetName:
61-
WriteObject(DeploymentStacksSdkClient.GetManagementGroupDeploymentStack(ResourceIdUtility.GetResourceName(ResourceId)), true);
67+
case GetDeploymentStackByManagementGroupIdAndStackName:
68+
WriteObject(DeploymentStacksSdkClient.GetManagementGroupDeploymentStack(ManagementGroupId, Name, true));
6269
break;
63-
case ListParameterSetname:
64-
WriteObject(DeploymentStacksSdkClient.ListManagementGroupDeploymentStack(), true);
70+
case ListDeploymentStacksByManagementGroupId:
71+
WriteObject(DeploymentStacksSdkClient.ListManagementGroupDeploymentStack(ManagementGroupId), true);
6572
break;
6673
default:
6774
throw new PSInvalidOperationException();

0 commit comments

Comments
 (0)