GeneratorCmdlet.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace APIManagementTemplate
{
    [Cmdlet(VerbsCommon.Get, "APIManagementTemplate", ConfirmImpact = ConfirmImpact.None)]
    public class GeneratorCmdlet : PSCmdlet
    {
        [Parameter(Mandatory = true,HelpMessage = "Name of the API Management instance")]
        public string APIManagement;
 
        [Parameter(Mandatory = true,HelpMessage = "The name of the Resource Group")]
        public string ResourceGroup;
 
        [Parameter(Mandatory = false,HelpMessage = "The Subscription id (guid)")]
        public string SubscriptionId;
 
        [Parameter(Mandatory = false,HelpMessage = "Name of the Tenant i.e. contoso.onmicrosoft.com")]
        public string TenantName = "";
 
        //see filter in https://docs.microsoft.com/en-us/rest/api/apimanagement/api/listbyservice
        [Parameter(Mandatory = false,HelpMessage = "Filter for what API's to exort i.e: path eq 'api/v1/currencyconverter' or endswith(path,'currencyconverter')")]
        public string APIFilters = null;
 
        [Parameter(Mandatory = false,HelpMessage = "Export AuthorizationServers")]
        public bool ExportAuthorizationServers = true;
 
        [Parameter(Mandatory = false,HelpMessage = "Export the API Management Instance")]
        public bool ExportPIManagementInstance = true;
 
        [Parameter(Mandatory = false,HelpMessage = "Export the API Management Groups, not builtin")]
        public bool ExportGroups = true;
 
        [Parameter(Mandatory = false,HelpMessage = "Export the API Management Products")]
        public bool ExportProducts = true;
 
        [Parameter(Mandatory = false,HelpMessage = "A Bearer token value")]
        public string Token = "";
 
        [Parameter(Mandatory = false,HelpMessage = "Set to 'true' when all environment-specific parameters are defined as properties")]
        public bool ParametrizePropertiesOnly = false;
 
        [Parameter(Mandatory = false,HelpMessage = "Set to 'true' to replace the base-url of <set-backend-service> with a property")]
        public bool ReplaceSetBackendServiceBaseUrlWithProperty = false;
 
        [Parameter(Mandatory = false,HelpMessage = "If the parameter for the service name always should be called apimServiceName or depend on the name of the service")]
        public bool FixedServiceNameParameter = false;
 
        [Parameter(Mandatory = false, HelpMessage = "If set, result from rest interface will be saved to this folder")]
        public string DebugOutPutFolder = "";
 
        [Parameter(Mandatory = false,HelpMessage = "Filter API version")]
        public string ApiVersion = "";
 
        [Parameter(Mandatory = false, HelpMessage = "Piped input from armclient", ValueFromPipeline = true)]
        public string ClaimsDump;
 
        protected override void ProcessRecord()
        {
            AzureResourceCollector resourceCollector = new AzureResourceCollector();
 
            if (!string.IsNullOrEmpty(DebugOutPutFolder))
                resourceCollector.DebugOutputFolder = DebugOutPutFolder;
 
            if (ClaimsDump == null)
            {
                resourceCollector.token = String.IsNullOrEmpty(Token) ? resourceCollector.Login(TenantName) : Token;
            }
            else if (ClaimsDump.Contains("Token copied"))
            {
                Token = Clipboard.GetText().Replace("Bearer ", "");
                resourceCollector.token = Token;
            }
            else
            {
                return;
            }
 
            try
            {
                TemplateGenerator generator = new TemplateGenerator(APIManagement, SubscriptionId, ResourceGroup, APIFilters, ExportGroups, ExportProducts, ExportPIManagementInstance, ParametrizePropertiesOnly, resourceCollector, ReplaceSetBackendServiceBaseUrlWithProperty, FixedServiceNameParameter,ApiVersion);
                JObject result = generator.GenerateTemplate().Result;
                WriteObject(result.ToString());
            }
            catch (Exception ex)
            {
                if (ex is AggregateException)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Aggregation exception thrown, se following exceptions for more information");
                    AggregateException ae = (AggregateException)ex;
                    foreach (var e in ae.InnerExceptions)
                    {
                        sb.AppendLine($"Exception: {e.Message}");
                        sb.AppendLine($"{e.StackTrace}");
                        sb.AppendLine("-------------------------------------------");
                    }
                    WriteObject(sb.ToString());
                    throw new Exception($"Aggregation Exception thrown, {ae.Message}, first Exception message is: {ae.InnerExceptions.First().Message}, for more information read the output file.");
                }
                else
                {
                    throw ex;
                }
            }
        }
    }
}