|
| 1 | +namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation |
| 2 | +{ |
| 3 | + using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 4 | + using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 5 | + using System; |
| 6 | + using System.Collections; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.IO; |
| 9 | + using System.Management.Automation; |
| 10 | + using System.Text; |
| 11 | + using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources; |
| 12 | + |
| 13 | + [Cmdlet("New", Common.AzureRMConstants.AzureRMPrefix + "ResourceGroupDeploymentStack", |
| 14 | + SupportsShouldProcess = true, DefaultParameterSetName = NewAzSubscriptionDeploymentStack.ParameterlessTemplateFileParameterSetName), OutputType(typeof(PSDeploymentStack))] |
| 15 | + public class NewAzSubscriptionDeploymentStack : DeploymentStacksCmdletBase |
| 16 | + { |
| 17 | + |
| 18 | + #region Cmdlet Parameters and Parameter Set Definitions |
| 19 | + |
| 20 | + internal const string ParameterlessTemplateFileParameterSetName = "ByTemplateFileWithNoParameters"; |
| 21 | + internal const string ParameterlessTemplateUriParameterSetName = "ByTemplateUriWithNoParameters"; |
| 22 | + internal const string ParameterlessTemplateSpecParameterSetName = "ByTemplateSpecWithNoParameters"; |
| 23 | + |
| 24 | + internal const string ParameterFileTemplateFileParameterSetName = "ByTemplateFileWithParameterFile"; |
| 25 | + internal const string ParameterFileTemplateUriParameterSetName = "ByTemplateUriWithParameterFile"; |
| 26 | + internal const string ParameterFileTemplateSpecParameterSetName = "ByTemplateSpecWithParameterFile"; |
| 27 | + |
| 28 | + internal const string ParameterUriTemplateFileParameterSetName = "ByTemplateFileWithParameterUri"; |
| 29 | + internal const string ParameterUriTemplateUriParameterSetName = "ByTemplateUriWithParameterUri"; |
| 30 | + internal const string ParameterUriTemplateSpecParameterSetName = "ByTemplateSpecWithParameterUri"; |
| 31 | + |
| 32 | + [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, |
| 33 | + HelpMessage = "The name of the deploymentStack to create")] |
| 34 | + [ValidateNotNullOrEmpty] |
| 35 | + public string Name { get; set; } |
| 36 | + |
| 37 | + [Parameter(Position = 1, ParameterSetName = ParameterFileTemplateFileParameterSetName, |
| 38 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "TemplateFile to be used to create the stack")] |
| 39 | + [Parameter(Position = 1, ParameterSetName = ParameterUriTemplateFileParameterSetName, |
| 40 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "TemplateFile to be used to create the stack")] |
| 41 | + [Parameter(Position = 1, ParameterSetName = ParameterlessTemplateFileParameterSetName, |
| 42 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "TemplateFile to be used to create the stack")] |
| 43 | + public string TemplateFile { get; set; } |
| 44 | + |
| 45 | + [Parameter(Position = 1, ParameterSetName = ParameterFileTemplateUriParameterSetName, |
| 46 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Location of the Template to be used to create the stack")] |
| 47 | + [Parameter(Position = 1, ParameterSetName = ParameterUriTemplateUriParameterSetName, |
| 48 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Location of the Template to be used to create the stack")] |
| 49 | + [Parameter(Position = 1, ParameterSetName = ParameterlessTemplateUriParameterSetName, |
| 50 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Location of the Template to be used to create the stack")] |
| 51 | + public string TemplateUri { get; set; } |
| 52 | + |
| 53 | + [Parameter(Position = 1, ParameterSetName = ParameterFileTemplateSpecParameterSetName, |
| 54 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "ResourceId of the TemplateSpec to be used to create the stack")] |
| 55 | + [Parameter(Position = 1, ParameterSetName = ParameterUriTemplateSpecParameterSetName, |
| 56 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "ResourceId of the TemplateSpec to be used to create the stack")] |
| 57 | + [Parameter(Position = 1, ParameterSetName = ParameterlessTemplateSpecParameterSetName, |
| 58 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "ResourceId of the TemplateSpec to be used to create the stack")] |
| 59 | + public string TemplateSpec { get; set; } |
| 60 | + |
| 61 | + [Parameter(Position = 2, ParameterSetName = ParameterFileTemplateFileParameterSetName, |
| 62 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Parameter file to use for the template")] |
| 63 | + [Parameter(Position = 2, ParameterSetName = ParameterFileTemplateUriParameterSetName, |
| 64 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Parameter file to use for the template")] |
| 65 | + [Parameter(Position = 2, ParameterSetName = ParameterFileTemplateSpecParameterSetName, |
| 66 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Parameter file to use for the template")] |
| 67 | + public string ParameterFile { get; set; } |
| 68 | + |
| 69 | + [Parameter(Position = 2, ParameterSetName = ParameterUriTemplateFileParameterSetName, |
| 70 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Location of the Parameter file to use for the template")] |
| 71 | + [Parameter(Position = 2, ParameterSetName = ParameterUriTemplateUriParameterSetName, |
| 72 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Location of the Parameter file to use for the template")] |
| 73 | + [Parameter(Position = 2, ParameterSetName = ParameterUriTemplateSpecParameterSetName, |
| 74 | + Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Location of the Parameter file to use for the template")] |
| 75 | + public string ParameterUri { get; set; } |
| 76 | + |
| 77 | + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 78 | + HelpMessage = "Description for the stack")] |
| 79 | + public string Description { get; set; } |
| 80 | + |
| 81 | + #endregion |
| 82 | + |
| 83 | + #region Cmdlet Overrides |
| 84 | + |
| 85 | + public override void ExecuteCmdlet() |
| 86 | + { |
| 87 | + try |
| 88 | + { |
| 89 | + Hashtable parameters = new Hashtable(); |
| 90 | + switch (ParameterSetName) |
| 91 | + { |
| 92 | + case ParameterlessTemplateFileParameterSetName: |
| 93 | + case ParameterUriTemplateFileParameterSetName: |
| 94 | + string filePath = this.TryResolvePath(TemplateFile); |
| 95 | + if (!File.Exists(filePath)) |
| 96 | + { |
| 97 | + throw new PSInvalidOperationException( |
| 98 | + string.Format(ProjectResources.InvalidFilePath, TemplateFile)); |
| 99 | + } |
| 100 | + break; |
| 101 | + case ParameterFileTemplateSpecParameterSetName: |
| 102 | + case ParameterFileTemplateUriParameterSetName: |
| 103 | + parameters = this.GetParameterObject(ParameterFile); |
| 104 | + break; |
| 105 | + case ParameterFileTemplateFileParameterSetName: |
| 106 | + string templatePath = this.TryResolvePath(TemplateFile); |
| 107 | + if (!File.Exists(templatePath)) |
| 108 | + { |
| 109 | + throw new PSInvalidOperationException( |
| 110 | + string.Format(ProjectResources.InvalidFilePath, TemplateFile)); |
| 111 | + } |
| 112 | + parameters = this.GetParameterObject(ParameterFile); |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + var deploymentStack = DeploymentStacksSdkClient.SubscriptionCreateOrUpdateDeploymentStack( |
| 117 | + Name, |
| 118 | + TemplateUri, |
| 119 | + TemplateSpec, |
| 120 | + ParameterUri, |
| 121 | + parameters, |
| 122 | + Description |
| 123 | + ); |
| 124 | + WriteObject(deploymentStack); |
| 125 | + |
| 126 | + } |
| 127 | + catch (Exception ex) |
| 128 | + { |
| 129 | + WriteExceptionError(ex); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + #endregion |
| 134 | + |
| 135 | + } |
| 136 | +} |
0 commit comments