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);
        }
コード例 #2
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);
        }