|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | + |
| 16 | +using Microsoft.Azure.Commands.Network.Models; |
| 17 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 18 | +using Microsoft.Azure.Commands.ResourceManager.Common.Tags; |
| 19 | +using Microsoft.Azure.Management.Network; |
| 20 | +using System; |
| 21 | +using System.Collections; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.Management.Automation; |
| 24 | +using System.Management.Automation.Remoting; |
| 25 | +using System.Text; |
| 26 | +using MNM = Microsoft.Azure.Management.Network.Models; |
| 27 | + |
| 28 | +namespace Microsoft.Azure.Commands.Network |
| 29 | +{ |
| 30 | + [Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SaaSNetworkVirtualAppliance", SupportsShouldProcess = true, DefaultParameterSetName = ResourceNameParameterSet), OutputType(typeof(PSNetworkVirtualAppliance))] |
| 31 | + public class NewSaaSNetworkVirtualApplianceCommand : NetworkVirtualApplianceBaseCmdlet |
| 32 | + { |
| 33 | + private const string ResourceNameParameterSet = "ResourceNameParameterSet"; |
| 34 | + private const string ResourceIdParameterSet = "ResourceIdParameterSet"; |
| 35 | + |
| 36 | + [Alias("ResourceName")] |
| 37 | + [Parameter( |
| 38 | + Mandatory = true, |
| 39 | + ValueFromPipelineByPropertyName = true, |
| 40 | + ParameterSetName = ResourceNameParameterSet, |
| 41 | + HelpMessage = "The resource name.")] |
| 42 | + [ValidateNotNullOrEmpty] |
| 43 | + public virtual string Name { get; set; } |
| 44 | + |
| 45 | + [Parameter( |
| 46 | + Mandatory = true, |
| 47 | + ValueFromPipelineByPropertyName = true, |
| 48 | + ParameterSetName = ResourceNameParameterSet, |
| 49 | + HelpMessage = "The resource group name.")] |
| 50 | + [ResourceGroupCompleter] |
| 51 | + [ValidateNotNullOrEmpty] |
| 52 | + public virtual string ResourceGroupName { get; set; } |
| 53 | + |
| 54 | + [Parameter( |
| 55 | + Mandatory = true, |
| 56 | + ValueFromPipelineByPropertyName = true, |
| 57 | + ParameterSetName = ResourceIdParameterSet, |
| 58 | + HelpMessage = "The resource Id.")] |
| 59 | + [ValidateNotNullOrEmpty] |
| 60 | + public virtual string ResourceId { get; set; } |
| 61 | + |
| 62 | + [Parameter( |
| 63 | + Mandatory = true, |
| 64 | + ValueFromPipelineByPropertyName = true, |
| 65 | + HelpMessage = "The SaaS NVA location.")] |
| 66 | + [ValidateNotNullOrEmpty] |
| 67 | + public string Location { get; set; } |
| 68 | + |
| 69 | + [Parameter( |
| 70 | + Mandatory = true, |
| 71 | + ValueFromPipelineByPropertyName = true, |
| 72 | + HelpMessage = "The Resource Id of the Virtual Hub.")] |
| 73 | + [ValidateNotNullOrEmpty] |
| 74 | + public string VirtualHubId { get; set; } |
| 75 | + |
| 76 | + [Parameter( |
| 77 | + Mandatory = false, |
| 78 | + ValueFromPipelineByPropertyName = true, |
| 79 | + HelpMessage = "A hashtable which represents resource tags.")] |
| 80 | + public Hashtable Tag { get; set; } |
| 81 | + |
| 82 | + [Parameter( |
| 83 | + Mandatory = false, |
| 84 | + HelpMessage = "Do not ask for confirmation if you want to overwrite a resource")] |
| 85 | + public SwitchParameter Force { get; set; } |
| 86 | + |
| 87 | + [Parameter( |
| 88 | + Mandatory = false, |
| 89 | + HelpMessage = "Run cmdlet in the background")] |
| 90 | + public SwitchParameter AsJob { get; set; } |
| 91 | + |
| 92 | + [Parameter( |
| 93 | + Mandatory = true, |
| 94 | + ValueFromPipelineByPropertyName = true, |
| 95 | + HelpMessage = "The delegated service name")] |
| 96 | + [ValidateNotNullOrEmpty] |
| 97 | + public string DelegatedServiceName { get; set; } |
| 98 | + |
| 99 | + |
| 100 | + public override void Execute() |
| 101 | + { |
| 102 | + base.Execute(); |
| 103 | + if (ParameterSetName.Equals(ResourceIdParameterSet)) |
| 104 | + { |
| 105 | + this.ResourceGroupName = GetResourceGroup(this.ResourceId); |
| 106 | + this.Name = GetResourceName(this.ResourceId, "Microsoft.Network/networkVirtualAppliances"); |
| 107 | + } |
| 108 | + var present = this.IsNetworkVirtualAppliancePresent(this.ResourceGroupName, this.Name); |
| 109 | + ConfirmAction( |
| 110 | + Force.IsPresent, |
| 111 | + string.Format(Properties.Resources.OverwritingResource, Name), |
| 112 | + Properties.Resources.CreatingResourceMessage, |
| 113 | + Name, |
| 114 | + () => |
| 115 | + { |
| 116 | + var nva = CreateNetworkVirtualAppliance(); |
| 117 | + if (present) |
| 118 | + { |
| 119 | + nva = this.GetNetworkVirtualAppliance(this.ResourceGroupName, this.Name); |
| 120 | + } |
| 121 | + WriteObject(nva); |
| 122 | + }, |
| 123 | + () => present); |
| 124 | + } |
| 125 | + |
| 126 | + private PSNetworkVirtualAppliance CreateNetworkVirtualAppliance() |
| 127 | + { |
| 128 | + var networkVirtualAppliance = new PSNetworkVirtualAppliance(); |
| 129 | + networkVirtualAppliance.Name = this.Name; |
| 130 | + networkVirtualAppliance.Location = this.Location; |
| 131 | + networkVirtualAppliance.VirtualHub = new PSResourceId(); |
| 132 | + networkVirtualAppliance.VirtualHub.Id = this.VirtualHubId; |
| 133 | + networkVirtualAppliance.Delegation = new PSNetworkVirtualApplianceDelegationProperties(this.DelegatedServiceName); |
| 134 | + var networkVirtualApplianceModel = NetworkResourceManagerProfile.Mapper.Map<MNM.NetworkVirtualAppliance>(networkVirtualAppliance); |
| 135 | + networkVirtualApplianceModel.Tags = TagsConversionHelper.CreateTagDictionary(this.Tag, validate: true); |
| 136 | + this.NetworkVirtualAppliancesClient.CreateOrUpdate(this.ResourceGroupName, this.Name, networkVirtualApplianceModel); |
| 137 | + var getNetworkVirtualAppliance = this.GetNetworkVirtualAppliance(this.ResourceGroupName, this.Name); |
| 138 | + return getNetworkVirtualAppliance; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments