public Dictionary <string, TemplateParameterProperties> CreateMasterTemplateParameters(CreatorConfig creatorConfig)
        {
            // used to create the parameter metatadata, etc (not value) for use in file with resources
            // add parameters with metadata properties
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                Metadata = new TemplateParameterMetadata()
                {
                    Description = "Name of the API Management"
                },
                Type = "string"
            };

            parameters.Add(ParameterNames.ApimServiceName, apimServiceNameProperties);
            // add remote location of template files for linked option
            if (creatorConfig.linked == true)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    Metadata = new TemplateParameterMetadata()
                    {
                        Description = "Base URL of the repository"
                    },
                    Type = "string"
                };
                parameters.Add(ParameterNames.LinkedTemplatesBaseUrl, linkedTemplatesBaseUrlProperties);
                if (creatorConfig.linkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        Metadata = new TemplateParameterMetadata()
                        {
                            Description = "Query string for the URL of the repository"
                        },
                        Type = "string"
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesUrlQueryString, linkedTemplatesUrlQueryStringProperties);
                }
            }

            // add serviceUrl parameter for linked option
            if (creatorConfig.serviceUrlParameters != null && creatorConfig.serviceUrlParameters.Count > 0)
            {
                foreach (var serviceUrlProperty in creatorConfig.serviceUrlParameters)
                {
                    TemplateParameterProperties serviceUrlParamProperty = new TemplateParameterProperties()
                    {
                        Metadata = new TemplateParameterMetadata()
                        {
                            Description = "ServiceUrl parameter for API: " + serviceUrlProperty.ApiName
                        },
                        Type = "string"
                    };
                    parameters.Add(serviceUrlProperty.ApiName + "-ServiceUrl", serviceUrlParamProperty);
                }
            }

            return(parameters);
        }
        public Template CreateMasterTemplateParameterValues(string apimServiceName, string linkedTemplatesBaseUrl, string policyXMLBaseUrl)
        {
            // used to create the parameter values for use in parameters file
            // create empty template
            Template masterTemplate = GenerateEmptyTemplate();

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                value = apimServiceName
            };

            parameters.Add("ApimServiceName", apimServiceNameProperties);
            if (linkedTemplatesBaseUrl != null)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = linkedTemplatesBaseUrl
                };
                parameters.Add("LinkedTemplatesBaseUrl", linkedTemplatesBaseUrlProperties);
            }
            if (policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = policyXMLBaseUrl
                };
                parameters.Add("PolicyXMLBaseUrl", policyTemplateBaseUrlProperties);
            }
            masterTemplate.parameters = parameters;
            return(masterTemplate);
        }
        public Template CreateMasterTemplateParameterValues(CreatorConfig creatorConfig)
        {
            // used to create the parameter values for use in parameters file
            // create empty template
            Template masterTemplate = CreateEmptyParameters();

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                value = creatorConfig.apimServiceName
            };

            parameters.Add(ParameterNames.ApimServiceName, apimServiceNameProperties);
            if (creatorConfig.linked == true)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = creatorConfig.linkedTemplatesBaseUrl
                };
                parameters.Add(ParameterNames.LinkedTemplatesBaseUrl, linkedTemplatesBaseUrlProperties);
                if (creatorConfig.linkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        value = creatorConfig.linkedTemplatesUrlQueryString
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesUrlQueryString, linkedTemplatesUrlQueryStringProperties);
                }
            }
            masterTemplate.parameters = parameters;
            return(masterTemplate);
        }
        public Template GenerateEmptyTemplateWithParameters(string policyXMLBaseUrl, string policyXMLSasToken)
        {
            Template armTemplate = GenerateEmptyTemplate();

            armTemplate.parameters = new Dictionary <string, TemplateParameterProperties> {
                { "ApimServiceName", new TemplateParameterProperties()
                  {
                      type = "string"
                  } }
            };
            if (policyXMLBaseUrl != null && policyXMLSasToken != null)
            {
                TemplateParameterProperties policyTemplateSasTokenParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add("PolicyXMLSasToken", policyTemplateSasTokenParameterProperties);
            }
            if (policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add("PolicyXMLBaseUrl", policyTemplateBaseUrlParameterProperties);
            }
            return(armTemplate);
        }
        public Template CreateMasterTemplateParameterValues(string apimServiceName, string linkedTemplatesBaseUrl, string linkedTemplatesSasToken, string linkedTemplatesUrlQueryString, string policyXMLBaseUrl, string policyXMLSasToken)
        {
            // used to create the parameter values for use in parameters file
            // create empty template
            Template masterTemplate = GenerateEmptyTemplate();

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                value = apimServiceName
            };

            parameters.Add("ApimServiceName", apimServiceNameProperties);
            if (linkedTemplatesBaseUrl != null)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = linkedTemplatesBaseUrl
                };
                parameters.Add("LinkedTemplatesBaseUrl", linkedTemplatesBaseUrlProperties);
                // add linkedTemplatesSasToken parameter if provided and if the user has provided a linkedTemplatesBaseUrl
                if (linkedTemplatesSasToken != null)
                {
                    TemplateParameterProperties linkedTemplatesSasTokenProperties = new TemplateParameterProperties()
                    {
                        value = linkedTemplatesSasToken
                    };
                    parameters.Add("LinkedTemplatesSasToken", linkedTemplatesSasTokenProperties);
                }
                // add linkedTemplatesUrlQueryString parameter if provided and if the user has provided a linkedTemplatesBaseUrl
                if (linkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        value = linkedTemplatesUrlQueryString
                    };
                    parameters.Add("LinkedTemplatesUrlQueryString", linkedTemplatesUrlQueryStringProperties);
                }
            }
            if (policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = policyXMLBaseUrl
                };
                parameters.Add("PolicyXMLBaseUrl", policyTemplateBaseUrlProperties);
                // add policyXMLSasToken parameter if provided and if the user has provided a policyXMLBaseUrl
                if (policyXMLSasToken != null)
                {
                    TemplateParameterProperties policyTemplateSasTokenProperties = new TemplateParameterProperties()
                    {
                        value = policyXMLSasToken
                    };
                    parameters.Add("PolicyXMLSasToken", policyTemplateSasTokenProperties);
                }
            }
            masterTemplate.parameters = parameters;
            return(masterTemplate);
        }
        public Template CreateMasterTemplateParameterValues(DeploymentDefinition creatorConfig)
        {
            var masterTemplate = EmptyTemplate;

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                value = creatorConfig.ApimServiceName
            };

            parameters.Add("ApimServiceName", apimServiceNameProperties);

            TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
            {
                value = creatorConfig.LinkedTemplatesBaseUrl
            };

            parameters.Add("LinkedTemplatesBaseUrl", linkedTemplatesBaseUrlProperties);

            if (creatorConfig.LinkedTemplatesUrlQueryString != null)
            {
                TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                {
                    value = creatorConfig.LinkedTemplatesUrlQueryString
                };
                parameters.Add("LinkedTemplatesUrlQueryString", linkedTemplatesUrlQueryStringProperties);
            }


            masterTemplate.Parameters = parameters;
            return(masterTemplate);
        }
Пример #7
0
        public Template CreateMasterTemplateParameterValues(CreatorConfig creatorConfig)
        {
            // used to create the parameter values for use in parameters file
            // create empty template
            Template masterTemplate = this.templateCreator.CreateEmptyTemplate();

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                value = creatorConfig.apimServiceName
            };

            parameters.Add("ApimServiceName", apimServiceNameProperties);
            if (creatorConfig.linked == true)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = creatorConfig.linkedTemplatesBaseUrl
                };
                parameters.Add("LinkedTemplatesBaseUrl", linkedTemplatesBaseUrlProperties);
            }
            masterTemplate.parameters = parameters;
            return(masterTemplate);
        }
Пример #8
0
        public Dictionary <string, TemplateParameterProperties> CreateMasterTemplateParameters(bool linked)
        {
            // used to create the parameter metatadata, etc (not value) for use in file with resources
            // add parameters with metatdata properties
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                metadata = new TemplateParameterMetadata()
                {
                    description = "Name of the API Management"
                },
                type = "string"
            };

            parameters.Add("ApimServiceName", apimServiceNameProperties);
            if (linked == true)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "Base URL of the repository"
                    },
                    type = "string"
                };
                parameters.Add("LinkedTemplatesBaseUrl", linkedTemplatesBaseUrlProperties);
            }
            return(parameters);
        }
Пример #9
0
        public Dictionary <string, TemplateParameterProperties> CreateMasterTemplateParameters(bool linked, string linkedTemplatesUrlQueryString, string policyXMLBaseUrl)
        {
            // used to create the parameter metatadata, etc (not value) for use in file with resources
            // add parameters with metatdata properties
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                metadata = new TemplateParameterMetadata()
                {
                    description = "Name of the API Management"
                },
                type = "string"
            };

            parameters.Add("ApimServiceName", apimServiceNameProperties);
            // add remote location of template files for linked option
            if (linked == true)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "Base URL of the repository that contains the generated templates"
                    },
                    type = "string"
                };
                parameters.Add("LinkedTemplatesBaseUrl", linkedTemplatesBaseUrlProperties);
                // add linkedTemplatesUrlQueryString parameter if provided and if the templates are linked
                if (linkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        metadata = new TemplateParameterMetadata()
                        {
                            description = "Query string for the URL of the repository"
                        },
                        type = "string"
                    };
                    parameters.Add("LinkedTemplatesUrlQueryString", linkedTemplatesUrlQueryStringProperties);
                }
            }
            if (policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "Base URL of the repository that contains the generated policy files"
                    },
                    type = "string"
                };
                parameters.Add("PolicyXMLBaseUrl", policyTemplateBaseUrlProperties);
            }
            return(parameters);
        }
Пример #10
0
        public TemplateBuilder AddParameterizeBackendProperty(ExtractorParameters extractorParameters)
        {
            if (extractorParameters.ParameterizeBackend)
            {
                TemplateParameterProperties extractBackendParametersProperties = new TemplateParameterProperties()
                {
                    Type = "object"
                };
                this.template.Parameters.Add(ParameterNames.BackendSettings, extractBackendParametersProperties);
            }

            return(this);
        }
Пример #11
0
        public TemplateBuilder AddParameterizeLogResourceIdProperty(ExtractorParameters extractorParameters)
        {
            if (extractorParameters.ParameterizeLogResourceId)
            {
                TemplateParameterProperties loggerResourceIdParameterProperties = new TemplateParameterProperties()
                {
                    Type = "object"
                };
                this.template.Parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdParameterProperties);
            }

            return(this);
        }
Пример #12
0
        public TemplateBuilder AddParameterizeNamedValuesKeyVaultSecretParameters(ExtractorParameters extractorParameters)
        {
            if (extractorParameters.ParamNamedValuesKeyVaultSecrets)
            {
                TemplateParameterProperties keyVaultNamedValueParameterProperties = new TemplateParameterProperties()
                {
                    Type = "object"
                };
                this.template.Parameters.Add(ParameterNames.NamedValueKeyVaultSecrets, keyVaultNamedValueParameterProperties);
            }

            return(this);
        }
Пример #13
0
        public TemplateBuilder AddParameterizeNamedValueParameters(ExtractorParameters extractorParameters)
        {
            if (extractorParameters.ParameterizeNamedValue)
            {
                TemplateParameterProperties namedValueParameterProperties = new TemplateParameterProperties()
                {
                    Type = "object"
                };
                this.template.Parameters.Add(ParameterNames.NamedValues, namedValueParameterProperties);
            }

            return(this);
        }
        public TemplateBuilder AddParameterizeApiLoggerIdProperty(ExtractorParameters extractorParameters)
        {
            if (extractorParameters.ParameterizeApiLoggerId)
            {
                TemplateParameterProperties apiLoggerProperty = new TemplateParameterProperties()
                {
                    type = "object"
                };
                this.template.Parameters.Add(ParameterNames.ApiLoggerId, apiLoggerProperty);
            }

            return(this);
        }
        public TemplateBuilder AddParameterizeServiceUrlProperty(ExtractorParameters extractorParameters)
        {
            if (extractorParameters.ParameterizeServiceUrl || extractorParameters.ServiceUrlParameters != null && extractorParameters.ServiceUrlParameters.Length > 0)
            {
                TemplateParameterProperties serviceUrlParamProperty = new TemplateParameterProperties()
                {
                    type = "object"
                };
                this.template.Parameters.Add(ParameterNames.ServiceUrl, serviceUrlParamProperty);
            }

            return(this);
        }
        public Template CreateMasterTemplateParameterValues(CreatorConfig creatorConfig)
        {
            // used to create the parameter values for use in parameters file
            // create empty template
            Template masterTemplate = this.templateBuilder.GenerateEmptyTemplate().Build();

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                Value = creatorConfig.apimServiceName
            };

            parameters.Add(ParameterNames.ApimServiceName, apimServiceNameProperties);
            if (creatorConfig.linked == true)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    Value = creatorConfig.linkedTemplatesBaseUrl
                };
                parameters.Add(ParameterNames.LinkedTemplatesBaseUrl, linkedTemplatesBaseUrlProperties);
                if (creatorConfig.linkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        Value = creatorConfig.linkedTemplatesUrlQueryString
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesUrlQueryString, linkedTemplatesUrlQueryStringProperties);
                }
            }

            if (creatorConfig.serviceUrlParameters != null && creatorConfig.serviceUrlParameters.Count > 0)
            {
                foreach (var serviceUrlProperty in creatorConfig.serviceUrlParameters)
                {
                    TemplateParameterProperties serviceUrlParamProperty = new TemplateParameterProperties()
                    {
                        Value = serviceUrlProperty.ServiceUrl
                    };
                    parameters.Add(serviceUrlProperty.ApiName + "-ServiceUrl", serviceUrlParamProperty);
                }
            }

            masterTemplate.Parameters = parameters;
            return(masterTemplate);
        }
Пример #17
0
        public Template GenerateEmptyApiTemplateWithParameters(Extractor exc)
        {
            Template armTemplate = GenerateEmptyTemplate();

            armTemplate.parameters = new Dictionary <string, TemplateParameterProperties> {
                { ParameterNames.ApimServiceName, new TemplateParameterProperties()
                  {
                      type = "string"
                  } }
            };
            if (exc.policyXMLBaseUrl != null && exc.policyXMLSasToken != null)
            {
                TemplateParameterProperties policyTemplateSasTokenParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenParameterProperties);
            }
            if (exc.policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlParameterProperties);
            }
            if (exc.paramServiceUrl || (exc.serviceUrlParameters != null && exc.serviceUrlParameters.Length > 0))
            {
                TemplateParameterProperties serviceUrlParamProperty = new TemplateParameterProperties()
                {
                    type = "object"
                };
                armTemplate.parameters.Add(ParameterNames.ServiceUrl, serviceUrlParamProperty);
            }
            if (exc.paramApiLoggerId)
            {
                TemplateParameterProperties apiLoggerProperty = new TemplateParameterProperties()
                {
                    type = "object"
                };
                armTemplate.parameters.Add(ParameterNames.ApiLoggerId, apiLoggerProperty);
            }
            return(armTemplate);
        }
Пример #18
0
        private Dictionary <string, TemplateParameterProperties> CreateMasterTemplateParameters(DeploymentDefinition creatorConfig)
        {
            // used to create the parameter metatadata, etc (not value) for use in file with resources
            // add parameters with metatdata properties
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                metadata = new TemplateParameterMetadata()
                {
                    description = "Name of the API Management"
                },
                type = "string"
            };

            parameters.Add("ApimServiceName", apimServiceNameProperties);
            // add remote location of template files for linked option

            TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
            {
                metadata = new TemplateParameterMetadata()
                {
                    description = "Base URL of the repository"
                },
                type = "string"
            };

            parameters.Add("LinkedTemplatesBaseUrl", linkedTemplatesBaseUrlProperties);

            if (creatorConfig.LinkedTemplatesUrlQueryString != null)
            {
                TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "Query string for the URL of the repository"
                    },
                    type = "string"
                };
                parameters.Add("LinkedTemplatesUrlQueryString", linkedTemplatesUrlQueryStringProperties);
            }

            return(parameters);
        }
        public MasterTemplateResource CreateLinkedMasterTemplateResource(string name, string uriLink, string[] dependsOn, string apiName, bool isServiceUrlParameterizeInApi)
        {
            // create deployment resource with provided arguments
            MasterTemplateResource masterTemplateResource = new MasterTemplateResource()
            {
                Name       = name,
                Type       = "Microsoft.Resources/deployments",
                ApiVersion = GlobalConstants.ArmApiVersion,
                Properties = new MasterTemplateProperties()
                {
                    Mode         = "Incremental",
                    TemplateLink = new MasterTemplateLink()
                    {
                        Uri            = uriLink,
                        ContentVersion = "1.0.0.0"
                    },
                    Parameters = new Dictionary <string, TemplateParameterProperties>
                    {
                        { ParameterNames.ApimServiceName, new TemplateParameterProperties()
                          {
                              Value = $"[parameters('{ParameterNames.ApimServiceName}')]"
                          } }
                    }
                },
                DependsOn = dependsOn
            };

            if (name.IndexOf("APITemplate") > 0 && isServiceUrlParameterizeInApi)
            {
                TemplateParameterProperties serviceUrlParamProperty = new TemplateParameterProperties()
                {
                    Value = $"[parameters('{apiName}-ServiceUrl')]"
                };
                masterTemplateResource.Properties.Parameters.Add(apiName + "-ServiceUrl", serviceUrlParamProperty);
            }

            return(masterTemplateResource);
        }
        public TemplateBuilder AddPolicyProperties(ExtractorParameters extractorParameters)
        {
            if (extractorParameters.PolicyXMLBaseUrl != null && extractorParameters.PolicyXMLSasToken != null)
            {
                TemplateParameterProperties policyTemplateSasTokenParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };

                this.template.Parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenParameterProperties);
            }

            if (extractorParameters.PolicyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                this.template.Parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlParameterProperties);
            }

            return(this);
        }
Пример #21
0
        public Template GenerateEmptyPropertyTemplateWithParameters(Extractor exc)
        {
            Template armTemplate = GenerateEmptyTemplate();

            armTemplate.parameters = new Dictionary <string, TemplateParameterProperties> {
                { ParameterNames.ApimServiceName, new TemplateParameterProperties()
                  {
                      type = "string"
                  } }
            };
            if (exc.policyXMLBaseUrl != null && exc.policyXMLSasToken != null)
            {
                TemplateParameterProperties policyTemplateSasTokenParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenParameterProperties);
            }
            if (exc.policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlParameterProperties);
            }
            if (exc.paramNamedValue)
            {
                TemplateParameterProperties namedValueParameterProperties = new TemplateParameterProperties()
                {
                    type = "object"
                };
                armTemplate.parameters.Add(ParameterNames.NamedValues, namedValueParameterProperties);
            }
            return(armTemplate);
        }
Пример #22
0
        public async Task <Template> GenerateLoggerTemplateAsync(ExtractorParameters extractorParameters, string singleApiName, List <TemplateResource> apiTemplateResources, Dictionary <string, object> apiLoggerId)
        {
            Console.WriteLine("------------------------------------------");
            Console.WriteLine("Extracting loggers from service");
            Template armTemplate = this.templateBuilder.GenerateTemplateWithApimServiceNameProperty().Build();

            if (extractorParameters.ParameterizeLogResourceId)
            {
                TemplateParameterProperties loggerResourceIdParameterProperties = new TemplateParameterProperties()
                {
                    type = "object"
                };
                armTemplate.Parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdParameterProperties);
            }

            // isolate product api associations in the case of a single api extraction
            var policyResources = apiTemplateResources.Where(resource => resource.Type == ResourceTypeConstants.APIPolicy || resource.Type == ResourceTypeConstants.APIOperationPolicy || resource.Type == ResourceTypeConstants.ProductPolicy);

            List <TemplateResource> templateResources = new List <TemplateResource>();

            // pull all loggers for service
            string loggers = await this.GetLoggersAsync(extractorParameters.SourceApimName, extractorParameters.ResourceGroup);

            JObject oLoggers = JObject.Parse(loggers);

            foreach (var extractedLogger in oLoggers["value"])
            {
                string loggerName         = ((JValue)extractedLogger["name"]).Value.ToString();
                string fullLoggerResource = await this.GetLoggerDetailsAsync(extractorParameters.SourceApimName, extractorParameters.ResourceGroup, loggerName);

                // convert returned logger to template resource class
                LoggerTemplateResource loggerResource = fullLoggerResource.Deserialize <LoggerTemplateResource>();
                loggerResource.Name       = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{loggerName}')]";
                loggerResource.Type       = ResourceTypeConstants.Logger;
                loggerResource.ApiVersion = GlobalConstants.ApiVersion;
                loggerResource.Scale      = null;

                if (singleApiName == null)
                {
                    // if the user is extracting all apis, extract all the loggers
                    Console.WriteLine("'{0}' Logger found", loggerName);
                    templateResources.Add(loggerResource);
                }
                else
                {
                    // if the user is extracting a single api, extract the loggers referenced by its diagnostics and api policies
                    bool isReferencedInPolicy     = false;
                    bool isReferencedInDiagnostic = false;
                    foreach (PolicyTemplateResource policyTemplateResource in policyResources)
                    {
                        if (policyTemplateResource.Properties.PolicyContent.Contains(loggerName))
                        {
                            isReferencedInPolicy = true;
                        }
                    }
                    string validApiName = ParameterNamingHelper.GenerateValidParameterName(singleApiName, ParameterPrefix.Api);
                    if (extractorParameters.ParameterizeApiLoggerId && apiLoggerId.ContainsKey(validApiName))
                    {
                        object diagnosticObj = apiLoggerId[validApiName];
                        if (diagnosticObj is Dictionary <string, string> )
                        {
                            Dictionary <string, string> curDiagnostic = (Dictionary <string, string>)diagnosticObj;
                            string validDName = ParameterNamingHelper.GenerateValidParameterName(loggerResource.Properties.loggerType, ParameterPrefix.Diagnostic).ToLower();
                            if (curDiagnostic.ContainsKey(validDName) && curDiagnostic[validDName].Contains(loggerName))
                            {
                                isReferencedInDiagnostic = true;
                            }
                        }
                    }
                    if (isReferencedInPolicy == true || isReferencedInDiagnostic == true)
                    {
                        // logger was used in policy or diagnostic, extract it
                        Console.WriteLine("'{0}' Logger found", loggerName);
                        templateResources.Add(loggerResource);
                    }
                };
            }

            armTemplate.Resources = templateResources.ToArray();
            return(armTemplate);
        }
Пример #23
0
        public async Task <Template> CreateMasterTemplateParameterValues(List <string> apisToExtract, ExtractorParameters extractorParameters,
                                                                         Dictionary <string, object> apiLoggerId,
                                                                         Dictionary <string, string> loggerResourceIds,
                                                                         Dictionary <string, BackendApiParameters> backendParams,
                                                                         List <TemplateResource> propertyResources)
        {
            // used to create the parameter values for use in parameters file
            // create empty template
            Template masterTemplate = this.templateBuilder.GenerateEmptyTemplate().Build();

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                value = extractorParameters.DestinationApimName
            };

            parameters.Add(ParameterNames.ApimServiceName, apimServiceNameProperties);
            if (extractorParameters.LinkedTemplatesBaseUrl != null)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = extractorParameters.LinkedTemplatesBaseUrl
                };
                parameters.Add(ParameterNames.LinkedTemplatesBaseUrl, linkedTemplatesBaseUrlProperties);
                // add linkedTemplatesSasToken parameter if provided and if the user has provided a linkedTemplatesBaseUrl
                if (extractorParameters.LinkedTemplatesSasToken != null)
                {
                    TemplateParameterProperties linkedTemplatesSasTokenProperties = new TemplateParameterProperties()
                    {
                        value = extractorParameters.LinkedTemplatesSasToken
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesSasToken, linkedTemplatesSasTokenProperties);
                }
                // add linkedTemplatesUrlQueryString parameter if provided and if the user has provided a linkedTemplatesBaseUrl
                if (extractorParameters.LinkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        value = extractorParameters.LinkedTemplatesUrlQueryString
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesUrlQueryString, linkedTemplatesUrlQueryStringProperties);
                }
            }
            if (extractorParameters.PolicyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = extractorParameters.PolicyXMLBaseUrl
                };
                parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlProperties);
                // add policyXMLSasToken parameter if provided and if the user has provided a policyXMLBaseUrl
                if (extractorParameters.PolicyXMLSasToken != null)
                {
                    TemplateParameterProperties policyTemplateSasTokenProperties = new TemplateParameterProperties()
                    {
                        value = extractorParameters.PolicyXMLSasToken
                    };
                    parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenProperties);
                }
            }
            if (extractorParameters.ParameterizeServiceUrl)
            {
                Dictionary <string, string> serviceUrls = new Dictionary <string, string>();
                foreach (string apiName in apisToExtract)
                {
                    string validApiName = ParameterNamingHelper.GenerateValidParameterName(apiName, ParameterPrefix.Api);
                    string serviceUrl   = extractorParameters.ServiceUrlParameters != null?this.GetApiServiceUrlFromParameters(apiName, extractorParameters.ServiceUrlParameters) :
                                              await this.GetAPIServiceUrl(extractorParameters.SourceApimName, extractorParameters.ResourceGroup, apiName);

                    serviceUrls.Add(validApiName, serviceUrl);
                }
                TemplateObjectParameterProperties serviceUrlProperties = new TemplateObjectParameterProperties()
                {
                    value = serviceUrls
                };
                parameters.Add(ParameterNames.ServiceUrl, serviceUrlProperties);
            }
            if (extractorParameters.ParameterizeNamedValue)
            {
                Dictionary <string, string> namedValues = new Dictionary <string, string>();
                PropertyExtractor           pExc        = new PropertyExtractor(this.templateBuilder);
                string[] properties = await pExc.GetPropertiesAsync(extractorParameters.SourceApimName, extractorParameters.ResourceGroup);

                foreach (var extractedProperty in properties)
                {
                    JToken oProperty    = JObject.Parse(extractedProperty);
                    string propertyName = ((JValue)oProperty["name"]).Value.ToString();

                    // check if the property has been extracted as it is being used in a policy or backend
                    if (propertyResources.Count(item => item.Name.Contains(propertyName)) > 0)
                    {
                        string fullPropertyResource = await pExc.GetPropertyDetailsAsync(extractorParameters.SourceApimName, extractorParameters.ResourceGroup, propertyName);

                        PropertyTemplateResource propertyTemplateResource = fullPropertyResource.Deserialize <PropertyTemplateResource>();

                        //Only add the property if it is not controlled by keyvault
                        if (propertyTemplateResource?.Properties.keyVault == null)
                        {
                            string propertyValue = propertyTemplateResource.Properties.value;
                            string validPName    = ParameterNamingHelper.GenerateValidParameterName(propertyName, ParameterPrefix.Property);
                            namedValues.Add(validPName, propertyValue);
                        }
                    }
                }
                TemplateObjectParameterProperties namedValueProperties = new TemplateObjectParameterProperties()
                {
                    value = namedValues
                };
                parameters.Add(ParameterNames.NamedValues, namedValueProperties);
            }
            if (extractorParameters.ParamNamedValuesKeyVaultSecrets)
            {
                Dictionary <string, string> keyVaultNamedValues = new Dictionary <string, string>();
                PropertyExtractor           pExc = new PropertyExtractor(this.templateBuilder);
                string[] properties = await pExc.GetPropertiesAsync(extractorParameters.SourceApimName, extractorParameters.ResourceGroup);

                foreach (var extractedProperty in properties)
                {
                    JToken oProperty    = JObject.Parse(extractedProperty);
                    string propertyName = ((JValue)oProperty["name"]).Value.ToString();

                    // check if the property has been extracted as it is being used in a policy or backend
                    if (propertyResources.Count(item => item.Name.Contains(propertyName)) > 0)
                    {
                        string fullPropertyResource = await pExc.GetPropertyDetailsAsync(extractorParameters.SourceApimName, extractorParameters.ResourceGroup, propertyName);

                        PropertyTemplateResource propertyTemplateResource = fullPropertyResource.Deserialize <PropertyTemplateResource>();
                        if (propertyTemplateResource?.Properties.keyVault != null)
                        {
                            string propertyValue = propertyTemplateResource.Properties.keyVault.secretIdentifier;
                            string validPName    = ParameterNamingHelper.GenerateValidParameterName(propertyName, ParameterPrefix.Property);
                            keyVaultNamedValues.Add(validPName, propertyValue);
                        }
                    }
                }
                TemplateObjectParameterProperties keyVaultNamedValueProperties = new TemplateObjectParameterProperties()
                {
                    value = keyVaultNamedValues
                };
                parameters.Add(ParameterNames.NamedValueKeyVaultSecrets, keyVaultNamedValueProperties);
            }
            if (extractorParameters.ParameterizeApiLoggerId)
            {
                TemplateObjectParameterProperties loggerIdProperties = new TemplateObjectParameterProperties()
                {
                    value = apiLoggerId
                };
                parameters.Add(ParameterNames.ApiLoggerId, loggerIdProperties);
            }
            if (extractorParameters.ParameterizeLogResourceId)
            {
                TemplateObjectParameterProperties loggerResourceIdProperties = new TemplateObjectParameterProperties()
                {
                    value = loggerResourceIds
                };
                parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdProperties);
            }

            if (extractorParameters.ParameterizeBackend)
            {
                TemplateObjectParameterProperties backendProperties = new TemplateObjectParameterProperties()
                {
                    value = backendParams
                };
                parameters.Add(ParameterNames.BackendSettings, backendProperties);
            }
            masterTemplate.Parameters = parameters;
            return(masterTemplate);
        }
        public async Task <Template> GenerateGlobalServicePolicyTemplateAsync(string apimname, string resourceGroup, string policyXMLBaseUrl, string policyXMLSasToken, string fileFolder)
        {
            // extract global service policy in both full and single api extraction cases
            Console.WriteLine("------------------------------------------");
            Console.WriteLine("Extracting global service policy from service");
            Template armTemplate = GenerateEmptyPropertyTemplateWithParameters();

            if (policyXMLBaseUrl != null && policyXMLSasToken != null)
            {
                TemplateParameterProperties policyTemplateSasTokenParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenParameterProperties);
            }
            if (policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlParameterProperties);
            }

            List <TemplateResource> templateResources = new List <TemplateResource>();

            // add global service policy resource to template
            try
            {
                string globalServicePolicy = await GetGlobalServicePolicyAsync(apimname, resourceGroup);

                Console.WriteLine($" - Global policy found for {apimname} API Management service");
                PolicyTemplateResource globalServicePolicyResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(globalServicePolicy);
                // REST API will return format property as rawxml and value property as the xml by default
                globalServicePolicyResource.name       = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/policy')]";
                globalServicePolicyResource.apiVersion = GlobalConstants.APIVersion;
                globalServicePolicyResource.scale      = null;

                // write policy xml content to file and point to it if policyXMLBaseUrl is provided
                if (policyXMLBaseUrl != null)
                {
                    string policyXMLContent            = globalServicePolicyResource.properties.value;
                    string policyFolder                = String.Concat(fileFolder, $@"/policies");
                    string globalServicePolicyFileName = $@"/globalServicePolicy.xml";
                    this.fileWriter.CreateFolderIfNotExists(policyFolder);
                    this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, globalServicePolicyFileName));
                    globalServicePolicyResource.properties.format = "rawxml-link";
                    if (policyXMLSasToken != null)
                    {
                        globalServicePolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{globalServicePolicyFileName}', parameters('{ParameterNames.PolicyXMLSasToken}'))]";
                    }
                    else
                    {
                        globalServicePolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{globalServicePolicyFileName}')]";
                    }
                }

                templateResources.Add(globalServicePolicyResource);
            }
            catch (Exception) { }

            armTemplate.resources = templateResources.ToArray();
            return(armTemplate);
        }
        public async Task <Template> GenerateProductsARMTemplateAsync(string apimname, string resourceGroup, string singleApiName, List <TemplateResource> apiTemplateResources, string fileFolder, Extractor exc)
        {
            Console.WriteLine("------------------------------------------");
            Console.WriteLine("Extracting products from service");
            Template armTemplate = GenerateEmptyPropertyTemplateWithParameters();

            if (exc.policyXMLBaseUrl != null && exc.policyXMLSasToken != null)
            {
                TemplateParameterProperties policyTemplateSasTokenParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenParameterProperties);
            }
            if (exc.policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties()
                {
                    type = "string"
                };
                armTemplate.parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlParameterProperties);
            }

            // isolate product api associations in the case of a single api extraction
            var productAPIResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.ProductAPI);

            List <TemplateResource> templateResources = new List <TemplateResource>();

            // pull all products for service
            string products = await GetProductsAsync(apimname, resourceGroup);

            JObject oProducts = JObject.Parse(products);

            foreach (var item in oProducts["value"])
            {
                string productName    = ((JValue)item["name"]).Value.ToString();
                string productDetails = await GetProductDetailsAsync(apimname, resourceGroup, productName);

                // convert returned product to template resource class
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    NullValueHandling     = NullValueHandling.Ignore,
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };
                ProductsTemplateResource productsTemplateResource = JsonConvert.DeserializeObject <ProductsTemplateResource>(productDetails, settings);
                productsTemplateResource.name       = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productName}')]";
                productsTemplateResource.apiVersion = GlobalConstants.APIVersion;

                string productGroupDetails = await GetProductGroupsAsync(apimname, resourceGroup, productName);

                ProductGroupsTemplateResource productGroupsDetails = JsonConvert.DeserializeObject <ProductGroupsTemplateResource>(productGroupDetails, settings);

                // only extract the product if this is a full extraction, or in the case of a single api, if it is found in products associated with the api
                if (singleApiName == null || productAPIResources.SingleOrDefault(p => p.name.Contains($"/{productName}/")) != null)
                {
                    Console.WriteLine("'{0}' Product found", productName);
                    templateResources.Add(productsTemplateResource);

                    // add product policy resource to template
                    try
                    {
                        var productResourceId = new string[] { $"[resourceId('Microsoft.ApiManagement/service/products', parameters('{ParameterNames.ApimServiceName}'), '{productName}')]" };
                        foreach (ProductGroupsValue ProductGroup in productGroupsDetails.value)
                        {
                            ProductGroup.name       = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productName}/{ProductGroup.name}')]";
                            ProductGroup.apiVersion = GlobalConstants.APIVersion;
                            ProductGroup.dependsOn  = productResourceId;
                            templateResources.Add(ProductGroup);
                        }
                        string productPolicy = await GetProductPolicyAsync(apimname, resourceGroup, productName);

                        Console.WriteLine($" - Product policy found for {productName} product");
                        PolicyTemplateResource productPolicyResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(productPolicy);
                        productPolicyResource.name       = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productName}/policy')]";
                        productPolicyResource.apiVersion = GlobalConstants.APIVersion;
                        productPolicyResource.scale      = null;
                        productPolicyResource.dependsOn  = productResourceId;

                        // write policy xml content to file and point to it if policyXMLBaseUrl is provided
                        if (exc.policyXMLBaseUrl != null)
                        {
                            string policyXMLContent      = productPolicyResource.properties.value;
                            string policyFolder          = String.Concat(fileFolder, $@"/policies");
                            string productPolicyFileName = $@"/{productName}-productPolicy.xml";
                            this.fileWriter.CreateFolderIfNotExists(policyFolder);
                            this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, productPolicyFileName));
                            productPolicyResource.properties.format = "rawxml-link";
                            if (exc.policyXMLSasToken != null)
                            {
                                productPolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{productPolicyFileName}', parameters('{ParameterNames.PolicyXMLSasToken}'))]";
                            }
                            else
                            {
                                productPolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{productPolicyFileName}')]";
                            }
                        }

                        templateResources.Add(productPolicyResource);
                    }
                    catch (Exception) { }

                    // add tags associated with the product to template
                    try
                    {
                        // pull tags associated with the product
                        string productTags = await GetProductTagsAsync(apimname, resourceGroup, productName);

                        JObject oProductTags = JObject.Parse(productTags);

                        foreach (var tag in oProductTags["value"])
                        {
                            string productTagName = ((JValue)tag["name"]).Value.ToString();
                            Console.WriteLine(" - '{0}' Tag association found for {1} product", productTagName, productName);

                            // convert associations between product and tags to template resource class
                            TagTemplateResource productTagResource = JsonConvert.DeserializeObject <TagTemplateResource>(tag.ToString());
                            productTagResource.name       = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productName}/{productTagName}')]";
                            productTagResource.apiVersion = GlobalConstants.APIVersion;
                            productTagResource.scale      = null;
                            productTagResource.dependsOn  = new string[] { $"[resourceId('Microsoft.ApiManagement/service/products', parameters('{ParameterNames.ApimServiceName}'), '{productName}')]" };
                            templateResources.Add(productTagResource);
                        }
                    }
                    catch (Exception) { }
                }
            }

            armTemplate.resources = templateResources.ToArray();
            return(armTemplate);
        }
Пример #26
0
        /// <summary>
        /// Generate the ARM assets for the backend resources
        /// </summary>
        /// <param name="apimname"></param>
        /// <param name="resourceGroup"></param>
        /// <param name="singleApiName"></param>
        /// <param name="apiTemplateResources"></param>
        /// <param name="propertyResources"></param>
        /// <param name="policyXMLBaseUrl"></param>
        /// <param name="policyXMLSasToken"></param>
        /// <param name="extractBackendParameters"></param>
        /// <returns>a combination of a Template and the value for the BackendSettings parameter</returns>
        public async Task <Tuple <Template, Dictionary <string, BackendApiParameters> > > GenerateBackendsARMTemplateAsync(string apimname, string resourceGroup, string singleApiName, List <TemplateResource> apiTemplateResources, List <TemplateResource> propertyResources, ExtractorParameters extractorParameters)
        {
            Console.WriteLine("------------------------------------------");
            Console.WriteLine("Extracting backends from service");
            Template armTemplate = this.templateBuilder.GenerateTemplateWithApimServiceNameProperty().Build();

            if (extractorParameters.ParameterizeBackend)
            {
                TemplateParameterProperties extractBackendParametersProperties = new TemplateParameterProperties()
                {
                    type = "object"
                };
                armTemplate.Parameters.Add(ParameterNames.BackendSettings, extractBackendParametersProperties);
            }

            List <TemplateResource> templateResources = new List <TemplateResource>();

            // isolate api and operation policy resources in the case of a single api extraction, as they may reference backends
            var policyResources     = apiTemplateResources.Where(resource => resource.Type == ResourceTypeConstants.APIPolicy || resource.Type == ResourceTypeConstants.APIOperationPolicy || resource.Type == ResourceTypeConstants.ProductPolicy);
            var namedValueResources = propertyResources.Where(resource => resource.Type == ResourceTypeConstants.Property);

            // pull all backends for service
            JObject oBackends            = new JObject();
            var     oBackendParameters   = new Dictionary <string, BackendApiParameters>();
            int     skipNumberOfBackends = 0;

            do
            {
                string backends = await this.GetBackendsAsync(apimname, resourceGroup, skipNumberOfBackends);

                oBackends = JObject.Parse(backends);

                foreach (var item in oBackends["value"])
                {
                    string backendName = ((JValue)item["name"]).Value.ToString();
                    string backend     = await this.GetBackendDetailsAsync(apimname, resourceGroup, backendName);

                    // convert returned backend to template resource class
                    BackendTemplateResource backendTemplateResource = backend.Deserialize <BackendTemplateResource>();
                    backendTemplateResource.Name       = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{backendName}')]";
                    backendTemplateResource.ApiVersion = GlobalConstants.ApiVersion;

                    bool includeBackend = false;
                    ////only extract the backend if this is a full extraction, or in the case of a single api, if it is referenced by one of the policies
                    if (singleApiName == null)
                    {
                        // if the user is extracting all apis, extract all the backends
                        includeBackend = true;
                    }
                    else
                    {
                        // check extracted policies to see if the backend is referenced.
                        foreach (PolicyTemplateResource policyTemplateResource in policyResources)
                        {
                            string policyContent = PolicyTemplateUtils.GetPolicyContent(extractorParameters, policyTemplateResource);

                            if (this.DoesPolicyReferenceBackend(policyContent, namedValueResources, backendName, backendTemplateResource))
                            {
                                // backend was used in policy, extract it
                                includeBackend = true;

                                // dont need to go through all policies if the back end has already been found
                                break;
                            }
                        }
                    }

                    if (includeBackend)
                    {
                        if (extractorParameters.ParameterizeBackend)
                        {
                            var    apiToken          = new BackendApiParameters();
                            string validApiParamName = ParameterNamingHelper.GenerateValidParameterName(backendName, ParameterPrefix.Diagnostic).ToLower();

                            if (!string.IsNullOrEmpty(backendTemplateResource.Properties.resourceId))
                            {
                                apiToken.resourceId = backendTemplateResource.Properties.resourceId;
                                backendTemplateResource.Properties.resourceId = $"[parameters('{ParameterNames.BackendSettings}').{validApiParamName}.resourceId]";
                            }

                            if (!string.IsNullOrEmpty(backendTemplateResource.Properties.url))
                            {
                                apiToken.url = backendTemplateResource.Properties.url;
                                backendTemplateResource.Properties.url = $"[parameters('{ParameterNames.BackendSettings}').{validApiParamName}.url]";
                            }

                            if (!string.IsNullOrEmpty(backendTemplateResource.Properties.protocol))
                            {
                                apiToken.protocol = backendTemplateResource.Properties.protocol;
                                backendTemplateResource.Properties.protocol = $"[parameters('{ParameterNames.BackendSettings}').{validApiParamName}.protocol]";
                            }
                            oBackendParameters.Add(validApiParamName, apiToken);
                        }

                        Console.WriteLine("'{0}' Backend found", backendName);
                        templateResources.Add(backendTemplateResource);
                    }
                }

                skipNumberOfBackends += GlobalConstants.NumOfRecords;
            }while (oBackends["nextLink"] != null);

            armTemplate.Resources = templateResources.ToArray();
            return(new Tuple <Template, Dictionary <string, BackendApiParameters> >(armTemplate, oBackendParameters));
        }
Пример #27
0
        public async Task <Template> GenerateNamedValuesTemplateAsync(string singleApiName, List <TemplateResource> apiTemplateResources, ExtractorParameters extractorParameters, IBackendExtractor backendExtractor, List <TemplateResource> loggerTemplateResources)
        {
            Template armTemplate = this.templateBuilder.GenerateTemplateWithApimServiceNameProperty().Build();

            if (extractorParameters.ParameterizeNamedValue)
            {
                TemplateParameterProperties namedValueParameterProperties = new TemplateParameterProperties()
                {
                    type = "object"
                };
                armTemplate.Parameters.Add(ParameterNames.NamedValues, namedValueParameterProperties);
            }
            if (extractorParameters.ParamNamedValuesKeyVaultSecrets)
            {
                TemplateParameterProperties keyVaultNamedValueParameterProperties = new TemplateParameterProperties()
                {
                    type = "object"
                };
                armTemplate.Parameters.Add(ParameterNames.NamedValueKeyVaultSecrets, keyVaultNamedValueParameterProperties);
            }
            if (extractorParameters.NotIncludeNamedValue == true)
            {
                Console.WriteLine("------------------------------------------");
                Console.WriteLine("Skipping extracting named values from service");
                return(armTemplate);
            }

            Console.WriteLine("------------------------------------------");
            Console.WriteLine("Extracting named values from service");

            List <TemplateResource> templateResources = new List <TemplateResource>();

            // pull all named values (properties) for service
            string[] properties = await this.GetPropertiesAsync(extractorParameters.SourceApimName, extractorParameters.ResourceGroup);

            // isolate api and operation policy resources in the case of a single api extraction, as they may reference named value
            var policyResources = apiTemplateResources.Where(resource => resource.Type == ResourceTypeConstants.APIPolicy || resource.Type == ResourceTypeConstants.APIOperationPolicy || resource.Type == ResourceTypeConstants.ProductPolicy);

            foreach (var extractedProperty in properties)
            {
                JToken oProperty            = JObject.Parse(extractedProperty);
                string propertyName         = ((JValue)oProperty["name"]).Value.ToString();
                string fullPropertyResource = await this.GetPropertyDetailsAsync(extractorParameters.SourceApimName, extractorParameters.ResourceGroup, propertyName);

                // convert returned named value to template resource class
                PropertyTemplateResource propertyTemplateResource = fullPropertyResource.Deserialize <PropertyTemplateResource>();
                propertyTemplateResource.Name       = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{propertyName}')]";
                propertyTemplateResource.Type       = ResourceTypeConstants.Property;
                propertyTemplateResource.ApiVersion = GlobalConstants.ApiVersion;
                propertyTemplateResource.Scale      = null;

                if (extractorParameters.ParameterizeNamedValue)
                {
                    propertyTemplateResource.Properties.value = $"[parameters('{ParameterNames.NamedValues}').{ParameterNamingHelper.GenerateValidParameterName(propertyName, ParameterPrefix.Property)}]";
                }

                //Hide the value field if it is a keyvault named value
                if (propertyTemplateResource.Properties.keyVault != null)
                {
                    propertyTemplateResource.Properties.value = null;
                }

                if (propertyTemplateResource.Properties.keyVault != null && extractorParameters.ParamNamedValuesKeyVaultSecrets)
                {
                    propertyTemplateResource.Properties.keyVault.secretIdentifier = $"[parameters('{ParameterNames.NamedValueKeyVaultSecrets}').{ParameterNamingHelper.GenerateValidParameterName(propertyName, ParameterPrefix.Property)}]";
                }

                if (singleApiName == null)
                {
                    // if the user is executing a full extraction, extract all the loggers
                    Console.WriteLine("'{0}' Named value found", propertyName);
                    templateResources.Add(propertyTemplateResource);
                }
                else
                {
                    // if the user is executing a single api, extract all the named values used in the template resources
                    bool foundInPolicy  = this.DoesPolicyReferenceNamedValue(extractorParameters, policyResources, propertyName, propertyTemplateResource);
                    bool foundInBackEnd = await backendExtractor.IsNamedValueUsedInBackends(extractorParameters.SourceApimName, extractorParameters.ResourceGroup, singleApiName, apiTemplateResources, extractorParameters, propertyName, propertyTemplateResource.Properties.displayName);

                    bool foundInLogger = this.DoesLoggerReferenceNamedValue(loggerTemplateResources, propertyName, propertyTemplateResource);

                    // check if named value is referenced in a backend
                    if (foundInPolicy || foundInBackEnd || foundInLogger)
                    {
                        // named value was used in policy, extract it
                        Console.WriteLine("'{0}' Named value found", propertyName);
                        templateResources.Add(propertyTemplateResource);
                    }
                }
            }

            armTemplate.Resources = templateResources.ToArray();
            return(armTemplate);
        }
Пример #28
0
        public async Task <Template> CreateMasterTemplateParameterValues(string singleApiName, List <string> multipleApiNames, Extractor exc)
        {
            // used to create the parameter values for use in parameters file
            // create empty template
            Template masterTemplate = GenerateEmptyTemplate();

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                value = exc.destinationApimName
            };

            parameters.Add("ApimServiceName", apimServiceNameProperties);
            if (exc.linkedTemplatesBaseUrl != null)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = exc.linkedTemplatesBaseUrl
                };
                parameters.Add("LinkedTemplatesBaseUrl", linkedTemplatesBaseUrlProperties);
                // add linkedTemplatesSasToken parameter if provided and if the user has provided a linkedTemplatesBaseUrl
                if (exc.linkedTemplatesSasToken != null)
                {
                    TemplateParameterProperties linkedTemplatesSasTokenProperties = new TemplateParameterProperties()
                    {
                        value = exc.linkedTemplatesSasToken
                    };
                    parameters.Add("LinkedTemplatesSasToken", linkedTemplatesSasTokenProperties);
                }
                // add linkedTemplatesUrlQueryString parameter if provided and if the user has provided a linkedTemplatesBaseUrl
                if (exc.linkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        value = exc.linkedTemplatesUrlQueryString
                    };
                    parameters.Add("LinkedTemplatesUrlQueryString", linkedTemplatesUrlQueryStringProperties);
                }
            }
            if (exc.policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = exc.policyXMLBaseUrl
                };
                parameters.Add("PolicyXMLBaseUrl", policyTemplateBaseUrlProperties);
                // add policyXMLSasToken parameter if provided and if the user has provided a policyXMLBaseUrl
                if (exc.policyXMLSasToken != null)
                {
                    TemplateParameterProperties policyTemplateSasTokenProperties = new TemplateParameterProperties()
                    {
                        value = exc.policyXMLSasToken
                    };
                    parameters.Add("PolicyXMLSasToken", policyTemplateSasTokenProperties);
                }
            }
            if (exc.paramServiceUrl)
            {
                Dictionary <string, string> serviceUrls = new Dictionary <string, string>();
                APIExtractor apiExc = new APIExtractor(new FileWriter());
                if (singleApiName != null)
                {
                    string validApiName = ExtractorUtils.GenValidApiParamName(singleApiName);
                    string serviceUrl   = GetApiServiceUrlFromParameters(singleApiName, exc.serviceUrlParameters);
                    if (serviceUrl == null)
                    {
                        serviceUrl = await apiExc.GetAPIServiceUrl(exc.sourceApimName, exc.resourceGroup, singleApiName);
                    }
                    serviceUrls.Add(validApiName, serviceUrl);
                }
                else if (multipleApiNames != null)
                {
                    foreach (string apiName in multipleApiNames)
                    {
                        string validApiName = ExtractorUtils.GenValidApiParamName(apiName);
                        string serviceUrl   = GetApiServiceUrlFromParameters(apiName, exc.serviceUrlParameters);
                        if (serviceUrl == null)
                        {
                            serviceUrl = await apiExc.GetAPIServiceUrl(exc.sourceApimName, exc.resourceGroup, apiName);
                        }
                        serviceUrls.Add(validApiName, serviceUrl);
                    }
                }
                else
                {
                    JToken[] oApis = await apiExc.GetAllAPIObjsAsync(exc.sourceApimName, exc.resourceGroup);

                    foreach (JToken oApi in oApis)
                    {
                        string apiName      = ((JValue)oApi["name"]).Value.ToString();
                        string validApiName = ExtractorUtils.GenValidApiParamName(apiName);
                        string serviceUrl   = GetApiServiceUrlFromParameters(apiName, exc.serviceUrlParameters);
                        if (serviceUrl == null)
                        {
                            serviceUrl = await apiExc.GetAPIServiceUrl(exc.sourceApimName, exc.resourceGroup, apiName);
                        }
                        serviceUrls.Add(validApiName, serviceUrl);
                    }
                }
                TemplateServiceUrlProperties serviceUrlProperties = new TemplateServiceUrlProperties()
                {
                    value = serviceUrls
                };
                parameters.Add("serviceUrl", serviceUrlProperties);
            }
            masterTemplate.parameters = parameters;
            return(masterTemplate);
        }
        public async Task <Template> CreateMasterTemplateParameterValues(List <string> apisToExtract, Extractor exc, Dictionary <string, Dictionary <string, string> > apiLoggerId, Dictionary <string, string> loggerResourceIds)
        {
            // used to create the parameter values for use in parameters file
            // create empty template
            Template masterTemplate = GenerateEmptyTemplate();

            // add parameters with value property
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                value = exc.destinationApimName
            };

            parameters.Add(ParameterNames.ApimServiceName, apimServiceNameProperties);
            if (exc.linkedTemplatesBaseUrl != null)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = exc.linkedTemplatesBaseUrl
                };
                parameters.Add(ParameterNames.LinkedTemplatesBaseUrl, linkedTemplatesBaseUrlProperties);
                // add linkedTemplatesSasToken parameter if provided and if the user has provided a linkedTemplatesBaseUrl
                if (exc.linkedTemplatesSasToken != null)
                {
                    TemplateParameterProperties linkedTemplatesSasTokenProperties = new TemplateParameterProperties()
                    {
                        value = exc.linkedTemplatesSasToken
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesSasToken, linkedTemplatesSasTokenProperties);
                }
                // add linkedTemplatesUrlQueryString parameter if provided and if the user has provided a linkedTemplatesBaseUrl
                if (exc.linkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        value = exc.linkedTemplatesUrlQueryString
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesUrlQueryString, linkedTemplatesUrlQueryStringProperties);
                }
            }
            if (exc.policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlProperties = new TemplateParameterProperties()
                {
                    value = exc.policyXMLBaseUrl
                };
                parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlProperties);
                // add policyXMLSasToken parameter if provided and if the user has provided a policyXMLBaseUrl
                if (exc.policyXMLSasToken != null)
                {
                    TemplateParameterProperties policyTemplateSasTokenProperties = new TemplateParameterProperties()
                    {
                        value = exc.policyXMLSasToken
                    };
                    parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenProperties);
                }
            }
            if (exc.paramServiceUrl)
            {
                Dictionary <string, string> serviceUrls = new Dictionary <string, string>();
                APIExtractor apiExc = new APIExtractor(new FileWriter());
                foreach (string apiName in apisToExtract)
                {
                    string validApiName = ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api);
                    string serviceUrl   = exc.serviceUrlParameters != null?GetApiServiceUrlFromParameters(apiName, exc.serviceUrlParameters) :
                                              await apiExc.GetAPIServiceUrl(exc.sourceApimName, exc.resourceGroup, apiName);

                    serviceUrls.Add(validApiName, serviceUrl);
                }
                TemplateObjectParameterProperties serviceUrlProperties = new TemplateObjectParameterProperties()
                {
                    value = serviceUrls
                };
                parameters.Add(ParameterNames.ServiceUrl, serviceUrlProperties);
            }
            if (exc.paramNamedValue)
            {
                Dictionary <string, string> namedValues = new Dictionary <string, string>();
                PropertyExtractor           pExc        = new PropertyExtractor();
                string[] properties = await pExc.GetPropertiesAsync(exc.sourceApimName, exc.resourceGroup);

                foreach (var extractedProperty in properties)
                {
                    JToken oProperty            = JObject.Parse(extractedProperty);
                    string propertyName         = ((JValue)oProperty["name"]).Value.ToString();
                    string fullPropertyResource = await pExc.GetPropertyDetailsAsync(exc.sourceApimName, exc.resourceGroup, propertyName);

                    PropertyTemplateResource propertyTemplateResource = JsonConvert.DeserializeObject <PropertyTemplateResource>(fullPropertyResource);
                    string propertyValue = propertyTemplateResource.properties.value;
                    string validPName    = ExtractorUtils.GenValidParamName(propertyName, ParameterPrefix.Property);
                    namedValues.Add(validPName, propertyValue);
                }
                TemplateObjectParameterProperties namedValueProperties = new TemplateObjectParameterProperties()
                {
                    value = namedValues
                };
                parameters.Add(ParameterNames.NamedValues, namedValueProperties);
            }
            if (exc.paramApiLoggerId)
            {
                TemplateObjectParameterProperties loggerIdProperties = new TemplateObjectParameterProperties()
                {
                    value = apiLoggerId
                };
                parameters.Add(ParameterNames.ApiLoggerId, loggerIdProperties);
            }
            if (exc.paramLogResourceId)
            {
                TemplateObjectParameterProperties loggerResourceIdProperties = new TemplateObjectParameterProperties()
                {
                    value = loggerResourceIds
                };
                parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdProperties);
            }
            masterTemplate.parameters = parameters;
            return(masterTemplate);
        }
        public Dictionary <string, TemplateParameterProperties> CreateMasterTemplateParameters(bool linked, Extractor exc)
        {
            // used to create the parameter metatadata, etc (not value) for use in file with resources
            // add parameters with metatdata properties
            Dictionary <string, TemplateParameterProperties> parameters = new Dictionary <string, TemplateParameterProperties>();
            TemplateParameterProperties apimServiceNameProperties       = new TemplateParameterProperties()
            {
                metadata = new TemplateParameterMetadata()
                {
                    description = "Name of the API Management"
                },
                type = "string"
            };

            parameters.Add(ParameterNames.ApimServiceName, apimServiceNameProperties);
            // add remote location of template files for linked option
            if (linked == true)
            {
                TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "Base URL of the repository that contains the generated templates"
                    },
                    type = "string"
                };
                parameters.Add(ParameterNames.LinkedTemplatesBaseUrl, linkedTemplatesBaseUrlProperties);
                // add linkedTemplatesSasToken parameter if provided and if the templates are linked
                if (exc.linkedTemplatesSasToken != null)
                {
                    TemplateParameterProperties linkedTemplatesSasTokenProperties = new TemplateParameterProperties()
                    {
                        metadata = new TemplateParameterMetadata()
                        {
                            description = "The Shared Access Signature for the URL of the repository"
                        },
                        type = "string"
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesSasToken, linkedTemplatesSasTokenProperties);
                }
                // add linkedTemplatesUrlQueryString parameter if provided and if the templates are linked
                if (exc.linkedTemplatesUrlQueryString != null)
                {
                    TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
                    {
                        metadata = new TemplateParameterMetadata()
                        {
                            description = "Query string for the URL of the repository"
                        },
                        type = "string"
                    };
                    parameters.Add(ParameterNames.LinkedTemplatesUrlQueryString, linkedTemplatesUrlQueryStringProperties);
                }
            }
            if (exc.policyXMLBaseUrl != null)
            {
                TemplateParameterProperties policyTemplateBaseUrlProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "Base URL of the repository that contains the generated policy files"
                    },
                    type = "string"
                };
                parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlProperties);
                if (exc.policyXMLSasToken != null)
                {
                    TemplateParameterProperties policyXMLSasTokenProperties = new TemplateParameterProperties()
                    {
                        metadata = new TemplateParameterMetadata()
                        {
                            description = "The SAS token for the URL of the policy container"
                        },
                        type = "string"
                    };
                    parameters.Add(ParameterNames.PolicyXMLSasToken, policyXMLSasTokenProperties);
                }
            }
            if (exc.paramServiceUrl)
            {
                TemplateParameterProperties paramServiceUrlProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "Service url for each Api"
                    },
                    type = "object"
                };
                parameters.Add(ParameterNames.ServiceUrl, paramServiceUrlProperties);
            }
            if (exc.paramNamedValue)
            {
                TemplateParameterProperties namedValueProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "Named values"
                    },
                    type = "object"
                };
                parameters.Add(ParameterNames.NamedValues, namedValueProperties);
            }
            if (exc.paramApiLoggerId)
            {
                TemplateParameterProperties loggerIdProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "LoggerId for this api"
                    },
                    type = "object"
                };
                parameters.Add(ParameterNames.ApiLoggerId, loggerIdProperties);
            }
            if (exc.paramLogResourceId)
            {
                TemplateParameterProperties loggerResourceIdProperties = new TemplateParameterProperties()
                {
                    metadata = new TemplateParameterMetadata()
                    {
                        description = "ResourceId for the logger"
                    },
                    type = "object"
                };
                parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdProperties);
            }
            return(parameters);
        }