public async Task <Template> GenerateBackendsARMTemplateAsync(string apimname, string resourceGroup, string singleApiName, List <TemplateResource> apiTemplateResources, List <TemplateResource> propertyResources, string policyXMLBaseUrl, string policyXMLSasToken) { Console.WriteLine("------------------------------------------"); Console.WriteLine("Extracting backends from service"); Template armTemplate = GenerateEmptyTemplateWithParameters(policyXMLBaseUrl, policyXMLSasToken); 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 string backends = await GetBackendsAsync(apimname, resourceGroup, singleApiName); JObject oBackends = JObject.Parse(backends); foreach (var item in oBackends["value"]) { string backendName = ((JValue)item["name"]).Value.ToString(); string backend = await GetBackendDetailsAsync(apimname, resourceGroup, backendName); // convert returned backend to template resource class BackendTemplateResource backendTemplateResource = JsonConvert.DeserializeObject <BackendTemplateResource>(backend); backendTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{backendName}')]"; backendTemplateResource.apiVersion = GlobalConstants.APIVersion; if (singleApiName != null) { // Extract Properties JArray namedValues = new JArray(); var propertyRegex = new Regex("{{(.*?)}}"); var matches = propertyRegex.Matches(item?.ToString()); foreach (Match match in matches) { var propertyName = match.Groups[1].Value; var propertyExtractor = new PropertyExtractor(); var fullPropertyResource = await propertyExtractor.GetPropertyDetailsAsync(apimname, resourceGroup, propertyName); PropertyTemplateResource propertyTemplateResource = propertyExtractor.GetPropertyTemplateResource(propertyName, fullPropertyResource); propertyResources.Add(propertyTemplateResource); } } ////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 // Console.WriteLine("'{0}' Backend found", backendName); // templateResources.Add(backendTemplateResource); //} //else //{ // bool isReferencedInPolicy = false; // foreach (PolicyTemplateResource policyTemplateResource in policyResources) // { // // the backend is used in a policy if the xml contains a set-backend-service policy, which will reference the backend's url or id // string policyContent = policyTemplateResource.properties.policyContent; // isReferencedInPolicy = DoesPolicyReferenceBackend(policyContent, namedValueResources, backendName, backendTemplateResource); // } // if (isReferencedInPolicy == true) // { // // backend was used in policy, extract it // Console.WriteLine("'{0}' Backend found", backendName); // templateResources.Add(backendTemplateResource); // } //} Console.WriteLine("'{0}' Backend found", backendName); templateResources.Add(backendTemplateResource); } armTemplate.resources = templateResources.ToArray(); return(armTemplate); }
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); }