|
| 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 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 16 | +using Microsoft.Azure.Commands.Network.Models; |
| 17 | +using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; |
| 18 | +using Microsoft.Azure.Management.Network; |
| 19 | +using System; |
| 20 | +using System.Management.Automation; |
| 21 | +using System.IO; |
| 22 | +using Microsoft.Azure.Management.Network.Models; |
| 23 | +using System.ComponentModel; |
| 24 | +using System.Security.Cryptography; |
| 25 | + |
| 26 | +namespace Microsoft.Azure.Commands.Network |
| 27 | +{ |
| 28 | + [Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "ExpressRoutePortLOA", SupportsShouldProcess = false, DefaultParameterSetName = ResourceNameParameterSet), OutputType(typeof(bool))] |
| 29 | + public partial class NewAzureRmExpressRoutePortLOA : NetworkBaseCmdlet |
| 30 | + { |
| 31 | + private const string ResourceIdParameterSet = "ResourceIdParameterSet"; |
| 32 | + private const string ResourceNameParameterSet = "ResourceNameParameterSet"; |
| 33 | + private const string ResourceObjectParameterSet = "ResourceObjectParameterSet"; |
| 34 | + private const string DefaultFileName = "LOA.pdf"; |
| 35 | + |
| 36 | + [Parameter( |
| 37 | + ParameterSetName = ResourceNameParameterSet, |
| 38 | + Mandatory = true, |
| 39 | + HelpMessage = "The express route port name.", |
| 40 | + ValueFromPipelineByPropertyName = false)] |
| 41 | + [ValidateNotNullOrEmpty] |
| 42 | + public string PortName { get; set; } |
| 43 | + |
| 44 | + [Parameter( |
| 45 | + ParameterSetName = ResourceNameParameterSet, |
| 46 | + Mandatory = true, |
| 47 | + HelpMessage = "The resource group name of the express route port.", |
| 48 | + ValueFromPipelineByPropertyName = false)] |
| 49 | + [ResourceGroupCompleter] |
| 50 | + [ValidateNotNullOrEmpty] |
| 51 | + public string ResourceGroupName { get; set; } |
| 52 | + |
| 53 | + [Parameter( |
| 54 | + ParameterSetName = ResourceObjectParameterSet, |
| 55 | + Mandatory = true, |
| 56 | + HelpMessage = "The express route port resource.", |
| 57 | + ValueFromPipelineByPropertyName = false)] |
| 58 | + [ValidateNotNullOrEmpty] |
| 59 | + public PSExpressRoutePort ExpressRoutePort { get; set; } |
| 60 | + |
| 61 | + [Alias("ResourceId")] |
| 62 | + [Parameter( |
| 63 | + ParameterSetName = ResourceIdParameterSet, |
| 64 | + Mandatory = true, |
| 65 | + HelpMessage = "ResourceId of the express route port.", |
| 66 | + ValueFromPipelineByPropertyName = true)] |
| 67 | + [ValidateNotNullOrEmpty] |
| 68 | + public string Id { get; set; } |
| 69 | + |
| 70 | + [Alias("Name")] |
| 71 | + [Parameter( |
| 72 | + Mandatory = true, |
| 73 | + HelpMessage = "The customer name to whom this Express Route Port is assigned to.", |
| 74 | + ValueFromPipelineByPropertyName = false)] |
| 75 | + [ValidateNotNullOrEmpty] |
| 76 | + public string CustomerName { get; set; } |
| 77 | + |
| 78 | + [Parameter( |
| 79 | + Mandatory = false, |
| 80 | + HelpMessage = "The output filepath to store the Letter of Authorization to.", |
| 81 | + ValueFromPipelineByPropertyName = false)] |
| 82 | + public string Destination { get; set; } |
| 83 | + |
| 84 | + [Parameter(Mandatory = false)] |
| 85 | + public SwitchParameter PassThru { get; set; } |
| 86 | + |
| 87 | + [Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")] |
| 88 | + public SwitchParameter AsJob { get; set; } |
| 89 | + |
| 90 | + public override void Execute() |
| 91 | + { |
| 92 | + base.Execute(); |
| 93 | + |
| 94 | + if (string.Equals(this.ParameterSetName, ResourceObjectParameterSet, StringComparison.OrdinalIgnoreCase)) |
| 95 | + { |
| 96 | + ResourceGroupName = ExpressRoutePort.ResourceGroupName; |
| 97 | + PortName = ExpressRoutePort.Name; |
| 98 | + } |
| 99 | + if (string.Equals(this.ParameterSetName, ResourceIdParameterSet, StringComparison.OrdinalIgnoreCase)) |
| 100 | + { |
| 101 | + var resourceInfo = new ResourceIdentifier(Id); |
| 102 | + ResourceGroupName = resourceInfo.ResourceGroupName; |
| 103 | + PortName = resourceInfo.ResourceName; |
| 104 | + } |
| 105 | + if(this.ResourceGroupName == null || this.PortName == null){ |
| 106 | + Console.WriteLine("Empty resource group or port name."); |
| 107 | + return; |
| 108 | + } |
| 109 | + GenerateExpressRoutePortsLOARequest generateExpressRoutePortsLOARequest = new GenerateExpressRoutePortsLOARequest(CustomerName); |
| 110 | + var response = this.NetworkClient.NetworkManagementClient.ExpressRoutePorts.GenerateLOA(this.ResourceGroupName, this.PortName, generateExpressRoutePortsLOARequest); |
| 111 | + var decodedDocument = Convert.FromBase64String(response.EncodedContent); |
| 112 | + if (String.IsNullOrEmpty(Destination)) |
| 113 | + { |
| 114 | + Destination = DefaultFileName; |
| 115 | + } |
| 116 | + if (!(Path.IsPathRooted(Destination))) |
| 117 | + { |
| 118 | + Destination = Directory.GetCurrentDirectory() + "\\" + Destination; |
| 119 | + } |
| 120 | + File.WriteAllBytes(Destination, decodedDocument); |
| 121 | + Console.WriteLine("Written Letter of Authorization To: " + Destination); |
| 122 | + if (PassThru) |
| 123 | + { |
| 124 | + WriteObject(true); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments