/* three condistions to use this function:
         *  1. singleApiName is null, then generate one master template for the multipleAPIs in multipleApiNams
         *  2. multipleApiNams is null, then generate separate folder and master template for each API
         *  3. when both singleApiName and multipleApiNams is null, then generate one master template to link all apis in the sourceapim
         */
        public static async Task GenerateTemplates(
            Extractor exc,
            string singleApiName,
            List <string> multipleAPINames,
            FileNameGenerator fileNameGenerator,
            FileNames fileNames,
            FileWriter fileWriter,
            Template apiTemplate)
        {
            if (singleApiName != null && multipleAPINames != null)
            {
                throw new Exception("can't specify single API and multiple APIs to extract at the same time");
            }
            // initialize entity extractor classes
            APIExtractor                 apiExtractor                 = new APIExtractor(fileWriter);
            APIVersionSetExtractor       apiVersionSetExtractor       = new APIVersionSetExtractor();
            AuthorizationServerExtractor authorizationServerExtractor = new AuthorizationServerExtractor();
            BackendExtractor             backendExtractor             = new BackendExtractor();
            LoggerExtractor              loggerExtractor              = new LoggerExtractor();
            PolicyExtractor              policyExtractor              = new PolicyExtractor(fileWriter);
            PropertyExtractor            propertyExtractor            = new PropertyExtractor();
            TagExtractor                 tagExtractor                 = new TagExtractor();
            ProductAPIExtractor          productAPIExtractor          = new ProductAPIExtractor(fileWriter);
            APITagExtractor              apiTagExtractor              = new APITagExtractor(fileWriter);
            ProductExtractor             productExtractor             = new ProductExtractor(fileWriter);
            MasterTemplateExtractor      masterTemplateExtractor      = new MasterTemplateExtractor();

            // read parameters
            string        sourceApim           = exc.sourceApimName;
            string        resourceGroup        = exc.resourceGroup;
            string        destinationApim      = exc.destinationApimName;
            string        linkedBaseUrl        = exc.linkedTemplatesBaseUrl;
            string        linkedSasToken       = exc.linkedTemplatesSasToken;
            string        policyXMLBaseUrl     = exc.policyXMLBaseUrl;
            string        policyXMLSasToken    = exc.policyXMLSasToken;
            string        dirName              = exc.fileFolder;
            List <string> multipleApiNames     = multipleAPINames;
            string        linkedUrlQueryString = exc.linkedTemplatesUrlQueryString;

            // Get all Apis that will be extracted
            List <string> apisToExtract = new List <string>();

            if (singleApiName != null)
            {
                apisToExtract.Add(singleApiName);
            }
            else if (multipleApiNames != null)
            {
                apisToExtract.AddRange(multipleApiNames);
            }
            else
            {
                List <string> allApis = await apiExtractor.GetAllAPINamesAsync(exc.sourceApimName, exc.resourceGroup);

                apisToExtract.AddRange(allApis);
            }
            Dictionary <string, object> apiLoggerId = null;

            if (exc.paramApiLoggerId)
            {
                apiLoggerId = await GetAllReferencedLoggers(apisToExtract, exc);
            }

            // extract templates from apim service
            Template globalServicePolicyTemplate = await policyExtractor.GenerateGlobalServicePolicyTemplateAsync(sourceApim, resourceGroup, policyXMLBaseUrl, policyXMLSasToken, dirName);

            if (apiTemplate == null)
            {
                apiTemplate = await apiExtractor.GenerateAPIsARMTemplateAsync(singleApiName, multipleApiNames, exc);
            }
            List <TemplateResource> apiTemplateResources = apiTemplate.resources.ToList();
            Template apiVersionSetTemplate = await apiVersionSetExtractor.GenerateAPIVersionSetsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources);

            Template authorizationServerTemplate = await authorizationServerExtractor.GenerateAuthorizationServersARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources);

            Template loggerTemplate = await loggerExtractor.GenerateLoggerTemplateAsync(exc, singleApiName, apiTemplateResources, apiLoggerId);

            Template productTemplate = await productExtractor.GenerateProductsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, dirName, exc);

            Template productAPITemplate = await productAPIExtractor.GenerateAPIProductsARMTemplateAsync(singleApiName, multipleApiNames, exc);

            Template apiTagTemplate = await apiTagExtractor.GenerateAPITagsARMTemplateAsync(singleApiName, multipleApiNames, exc);

            List <TemplateResource> productTemplateResources = productTemplate.resources.ToList();
            List <TemplateResource> loggerResources          = loggerTemplate.resources.ToList();
            Template namedValueTemplate = await propertyExtractor.GenerateNamedValuesTemplateAsync(singleApiName, apiTemplateResources, exc, backendExtractor, loggerResources);

            Template tagTemplate = await tagExtractor.GenerateTagsTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, productTemplateResources, policyXMLBaseUrl, policyXMLSasToken);

            List <TemplateResource> namedValueResources = namedValueTemplate.resources.ToList();

            Tuple <Template, Dictionary <string, BackendApiParameters> > backendResult = await backendExtractor.GenerateBackendsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, namedValueResources, exc);

            Dictionary <string, string> loggerResourceIds = null;

            if (exc.paramLogResourceId)
            {
                loggerResourceIds = loggerExtractor.GetAllLoggerResourceIds(loggerResources);
                loggerTemplate    = loggerExtractor.SetLoggerResourceId(loggerTemplate);
            }

            // create parameters file
            Template templateParameters = await masterTemplateExtractor.CreateMasterTemplateParameterValues(apisToExtract, exc, apiLoggerId, loggerResourceIds, backendResult.Item2, namedValueResources);

            // write templates to output file location
            string apiFileName = fileNameGenerator.GenerateExtractorAPIFileName(singleApiName, fileNames.baseFileName);

            fileWriter.WriteJSONToFile(apiTemplate, String.Concat(@dirName, apiFileName));
            // won't generate template when there is no resources
            if (apiVersionSetTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(apiVersionSetTemplate, String.Concat(@dirName, fileNames.apiVersionSets));
            }
            if (backendResult.Item1.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(backendResult.Item1, String.Concat(@dirName, fileNames.backends));
            }
            if (loggerTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(loggerTemplate, String.Concat(@dirName, fileNames.loggers));
            }
            if (authorizationServerTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(authorizationServerTemplate, String.Concat(@dirName, fileNames.authorizationServers));
            }
            if (productTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(productTemplate, String.Concat(@dirName, fileNames.products));
            }
            if (productAPITemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(productAPITemplate, String.Concat(@dirName, fileNames.productAPIs));
            }
            if (apiTagTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(apiTagTemplate, String.Concat(@dirName, fileNames.apiTags));
            }
            if (tagTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(tagTemplate, String.Concat(@dirName, fileNames.tags));
            }
            if (namedValueTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(namedValueTemplate, String.Concat(@dirName, fileNames.namedValues));
            }
            if (globalServicePolicyTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(globalServicePolicyTemplate, String.Concat(@dirName, fileNames.globalServicePolicy));
            }
            if (linkedBaseUrl != null)
            {
                // create a master template that links to all other templates
                Template masterTemplate = masterTemplateExtractor.GenerateLinkedMasterTemplate(
                    apiTemplate, globalServicePolicyTemplate, apiVersionSetTemplate, productTemplate, productAPITemplate,
                    apiTagTemplate, loggerTemplate, backendResult.Item1, authorizationServerTemplate, namedValueTemplate,
                    tagTemplate, fileNames, apiFileName, exc);

                fileWriter.WriteJSONToFile(masterTemplate, String.Concat(@dirName, fileNames.linkedMaster));
            }

            // write parameters to outputLocation
            fileWriter.WriteJSONToFile(templateParameters, String.Concat(dirName, fileNames.parameters));
        }
Exemple #2
0
        /* three condistions to use this function:
         *  1. singleApiName is null, then generate one master template for the multipleAPIs in multipleApiNams
         *  2. multipleApiNams is null, then generate separate folder and master template for each API
         *  3. when both singleApiName and multipleApiNams is null, then generate one master template to link all apis in the sourceapim
         */
        public static async Task GenerateTemplates(
            Extractor exc,
            string singleApiName,
            List <string> multipleAPINames,
            FileNameGenerator fileNameGenerator,
            FileNames fileNames,
            FileWriter fileWriter,
            Template apiTemplate)
        {
            if (singleApiName != null && multipleAPINames != null)
            {
                throw new Exception("can't specify single API and multiple APIs to extract at the same time");
            }
            // initialize entity extractor classes
            APIExtractor                 apiExtractor                 = new APIExtractor(fileWriter);
            APIVersionSetExtractor       apiVersionSetExtractor       = new APIVersionSetExtractor();
            AuthorizationServerExtractor authorizationServerExtractor = new AuthorizationServerExtractor();
            BackendExtractor             backendExtractor             = new BackendExtractor();
            LoggerExtractor              loggerExtractor              = new LoggerExtractor();
            PolicyExtractor              policyExtractor              = new PolicyExtractor(fileWriter);
            PropertyExtractor            propertyExtractor            = new PropertyExtractor();
            TagExtractor                 tagExtractor                 = new TagExtractor();
            ProductExtractor             productExtractor             = new ProductExtractor(fileWriter);
            MasterTemplateExtractor      masterTemplateExtractor      = new MasterTemplateExtractor();

            // read parameters
            string        sourceApim           = exc.sourceApimName;
            string        resourceGroup        = exc.resourceGroup;
            string        destinationApim      = exc.destinationApimName;
            string        linkedBaseUrl        = exc.linkedTemplatesBaseUrl;
            string        policyXMLBaseUrl     = exc.policyXMLBaseUrl;
            string        dirName              = exc.fileFolder;
            List <string> multipleApiNames     = multipleAPINames;
            string        linkedUrlQueryString = exc.linkedTemplatesUrlQueryString;

            // extract templates from apim service
            Template globalServicePolicyTemplate = await policyExtractor.GenerateGlobalServicePolicyTemplateAsync(sourceApim, resourceGroup, policyXMLBaseUrl, dirName);

            if (apiTemplate == null)
            {
                apiTemplate = await apiExtractor.GenerateAPIsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, multipleApiNames, policyXMLBaseUrl, dirName);
            }
            List <TemplateResource> apiTemplateResources = apiTemplate.resources.ToList();
            Template apiVersionSetTemplate = await apiVersionSetExtractor.GenerateAPIVersionSetsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);

            Template authorizationServerTemplate = await authorizationServerExtractor.GenerateAuthorizationServersARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);

            Template loggerTemplate = await loggerExtractor.GenerateLoggerTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);

            Template productTemplate = await productExtractor.GenerateProductsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl, dirName);

            List <TemplateResource> productTemplateResources = productTemplate.resources.ToList();
            Template namedValueTemplate = await propertyExtractor.GenerateNamedValuesTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);

            Template tagTemplate = await tagExtractor.GenerateTagsTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, productTemplateResources, policyXMLBaseUrl);

            List <TemplateResource> namedValueResources = namedValueTemplate.resources.ToList();
            Template backendTemplate = await backendExtractor.GenerateBackendsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, namedValueResources, policyXMLBaseUrl);

            // create parameters file
            Template templateParameters = masterTemplateExtractor.CreateMasterTemplateParameterValues(destinationApim, linkedBaseUrl, linkedUrlQueryString, policyXMLBaseUrl);

            // write templates to output file location
            string apiFileName = fileNameGenerator.GenerateExtractorAPIFileName(singleApiName, sourceApim);

            fileWriter.WriteJSONToFile(apiTemplate, String.Concat(@dirName, apiFileName));
            // won't generate template when there is no resources
            if (apiVersionSetTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(apiVersionSetTemplate, String.Concat(@dirName, fileNames.apiVersionSets));
            }
            if (backendTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(backendTemplate, String.Concat(@dirName, fileNames.backends));
            }
            if (authorizationServerTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(authorizationServerTemplate, String.Concat(@dirName, fileNames.authorizationServers));
            }
            if (productTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(productTemplate, String.Concat(@dirName, fileNames.products));
            }
            if (tagTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(tagTemplate, String.Concat(@dirName, fileNames.tags));
            }
            if (namedValueTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(namedValueTemplate, String.Concat(@dirName, fileNames.namedValues));
            }
            if (globalServicePolicyTemplate.resources.Count() != 0)
            {
                fileWriter.WriteJSONToFile(globalServicePolicyTemplate, String.Concat(@dirName, fileNames.globalServicePolicy));
            }
            if (linkedBaseUrl != null)
            {
                // create a master template that links to all other templates
                Template masterTemplate = masterTemplateExtractor.GenerateLinkedMasterTemplate(
                    apiTemplate, globalServicePolicyTemplate, apiVersionSetTemplate, productTemplate,
                    loggerTemplate, backendTemplate, authorizationServerTemplate, namedValueTemplate,
                    tagTemplate, fileNames, apiFileName, linkedUrlQueryString, policyXMLBaseUrl);

                fileWriter.WriteJSONToFile(masterTemplate, String.Concat(@dirName, fileNames.linkedMaster));
            }

            // write parameters to outputLocation
            fileWriter.WriteJSONToFile(templateParameters, String.Concat(dirName, fileNames.parameters));
        }
        public ExtractCommand()
        {
            this.Name        = GlobalConstants.ExtractName;
            this.Description = GlobalConstants.ExtractDescription;

            var sourceApimName = this.Option("--sourceApimName <sourceApimName>", "Source API Management name", CommandOptionType.SingleValue);
            var destinationAPIManagementName = this.Option("--destinationApimName <destinationApimName>", "Destination API Management name", CommandOptionType.SingleValue);
            var resourceGroupName            = this.Option("--resourceGroup <resourceGroup>", "Resource Group name", CommandOptionType.SingleValue);
            var fileFolderName                = this.Option("--fileFolder <filefolder>", "ARM Template files folder", CommandOptionType.SingleValue);
            var apiName                       = this.Option("--apiName <apiName>", "API name", CommandOptionType.SingleValue);
            var linkedTemplatesBaseUrlName    = this.Option("--linkedTemplatesBaseUrl <linkedTemplatesBaseUrl>", "Creates a master template with links", CommandOptionType.SingleValue);
            var linkedTemplatesUrlQueryString = this.Option("--linkedTemplatesUrlQueryString <linkedTemplatesUrlQueryString>", "Query string appended to linked templates uris that enables retrieval from private storage", CommandOptionType.SingleValue);
            var policyXMLBaseUrlName          = this.Option("--policyXMLBaseUrl <policyXMLBaseUrl>", "Writes policies to local XML files that require deployment to remote folder", CommandOptionType.SingleValue);

            this.HelpOption();

            this.OnExecute(async() =>
            {
                try
                {
                    if (!sourceApimName.HasValue())
                    {
                        throw new Exception("Missing parameter <sourceApimName>.");
                    }
                    if (!destinationAPIManagementName.HasValue())
                    {
                        throw new Exception("Missing parameter <destinationApimName>.");
                    }
                    if (!resourceGroupName.HasValue())
                    {
                        throw new Exception("Missing parameter <resourceGroup>.");
                    }
                    if (!fileFolderName.HasValue())
                    {
                        throw new Exception("Missing parameter <filefolder>.");
                    }

                    // isolate cli parameters
                    string resourceGroup        = resourceGroupName.Value().ToString();
                    string sourceApim           = sourceApimName.Value().ToString();
                    string destinationApim      = destinationAPIManagementName.Value().ToString();
                    string fileFolder           = fileFolderName.Value().ToString();
                    string linkedBaseUrl        = linkedTemplatesBaseUrlName.HasValue() ? linkedTemplatesBaseUrlName.Value().ToString() : null;
                    string linkedUrlQueryString = linkedTemplatesUrlQueryString.HasValue() ? linkedTemplatesUrlQueryString.Value().ToString() : null;
                    string policyXMLBaseUrl     = policyXMLBaseUrlName.HasValue() ? policyXMLBaseUrlName.Value().ToString() : null;
                    string singleApiName        = null;

                    if (apiName.Values.Count > 0)
                    {
                        singleApiName = apiName.Value().ToString();
                    }

                    Console.WriteLine("API Management Template");
                    Console.WriteLine();
                    Console.WriteLine("Connecting to {0} API Management Service on {1} Resource Group ...", sourceApim, resourceGroup);
                    if (singleApiName != null)
                    {
                        Console.WriteLine("Executing extraction for {0} API ...", singleApiName);
                    }
                    else
                    {
                        Console.WriteLine("Executing full extraction ...", singleApiName);
                    }

                    // initialize file helper classes
                    FileWriter fileWriter = new FileWriter();
                    FileNameGenerator fileNameGenerator = new FileNameGenerator();
                    FileNames fileNames = fileNameGenerator.GenerateFileNames(sourceApim);

                    // initialize entity extractor classes
                    APIExtractor apiExtractor = new APIExtractor(fileWriter);
                    APIVersionSetExtractor apiVersionSetExtractor             = new APIVersionSetExtractor();
                    AuthorizationServerExtractor authorizationServerExtractor = new AuthorizationServerExtractor();
                    BackendExtractor backendExtractor               = new BackendExtractor();
                    LoggerExtractor loggerExtractor                 = new LoggerExtractor();
                    PolicyExtractor policyExtractor                 = new PolicyExtractor(fileWriter);
                    PropertyExtractor propertyExtractor             = new PropertyExtractor();
                    TagExtractor tagExtractor                       = new TagExtractor();
                    ProductExtractor productExtractor               = new ProductExtractor(fileWriter);
                    MasterTemplateExtractor masterTemplateExtractor = new MasterTemplateExtractor();

                    // extract templates from apim service
                    Template globalServicePolicyTemplate = await policyExtractor.GenerateGlobalServicePolicyTemplateAsync(sourceApim, resourceGroup, policyXMLBaseUrl, fileFolder);
                    Template apiTemplate = await apiExtractor.GenerateAPIsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, policyXMLBaseUrl, fileFolder);
                    List <TemplateResource> apiTemplateResources = apiTemplate.resources.ToList();
                    Template apiVersionSetTemplate       = await apiVersionSetExtractor.GenerateAPIVersionSetsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);
                    Template authorizationServerTemplate = await authorizationServerExtractor.GenerateAuthorizationServersARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);
                    Template loggerTemplate  = await loggerExtractor.GenerateLoggerTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);
                    Template productTemplate = await productExtractor.GenerateProductsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl, fileFolder);
                    List <TemplateResource> productTemplateResources = productTemplate.resources.ToList();
                    Template namedValueTemplate = await propertyExtractor.GenerateNamedValuesTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);
                    Template tagTemplate        = await tagExtractor.GenerateTagsTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, productTemplateResources, policyXMLBaseUrl);
                    List <TemplateResource> namedValueResources = namedValueTemplate.resources.ToList();
                    Template backendTemplate = await backendExtractor.GenerateBackendsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, namedValueResources, policyXMLBaseUrl);

                    // create parameters file
                    Template templateParameters = masterTemplateExtractor.CreateMasterTemplateParameterValues(destinationApim, linkedBaseUrl, linkedUrlQueryString, policyXMLBaseUrl);

                    // write templates to output file location
                    string apiFileName = fileNameGenerator.GenerateExtractorAPIFileName(singleApiName, sourceApim);
                    fileWriter.WriteJSONToFile(apiTemplate, String.Concat(@fileFolder, apiFileName));
                    fileWriter.WriteJSONToFile(apiVersionSetTemplate, String.Concat(@fileFolder, fileNames.apiVersionSets));
                    fileWriter.WriteJSONToFile(authorizationServerTemplate, String.Concat(@fileFolder, fileNames.authorizationServers));
                    fileWriter.WriteJSONToFile(backendTemplate, String.Concat(@fileFolder, fileNames.backends));
                    fileWriter.WriteJSONToFile(loggerTemplate, String.Concat(@fileFolder, fileNames.loggers));
                    fileWriter.WriteJSONToFile(namedValueTemplate, String.Concat(@fileFolder, fileNames.namedValues));
                    fileWriter.WriteJSONToFile(tagTemplate, String.Concat(@fileFolder, fileNames.tags));
                    fileWriter.WriteJSONToFile(productTemplate, String.Concat(@fileFolder, fileNames.products));
                    fileWriter.WriteJSONToFile(globalServicePolicyTemplate, String.Concat(@fileFolder, fileNames.globalServicePolicy));

                    if (linkedBaseUrl != null)
                    {
                        // create a master template that links to all other templates
                        Template masterTemplate = masterTemplateExtractor.GenerateLinkedMasterTemplate(apiTemplate, globalServicePolicyTemplate, apiVersionSetTemplate, productTemplate, loggerTemplate, backendTemplate, authorizationServerTemplate, namedValueTemplate, fileNames, apiFileName, linkedUrlQueryString, policyXMLBaseUrl);
                        fileWriter.WriteJSONToFile(masterTemplate, String.Concat(@fileFolder, fileNames.linkedMaster));
                    }

                    // write parameters to outputLocation
                    fileWriter.WriteJSONToFile(templateParameters, String.Concat(fileFolder, fileNames.parameters));
                    Console.WriteLine("Templates written to output location");
                    Console.WriteLine("Press any key to exit process:");
#if DEBUG
                    Console.ReadKey();
#endif
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error occured: " + ex.Message);
                    throw;
                }
            });
        }