private GeneratedTemplate GenerateProduct(JToken product, JObject parsedTemplate, bool separatePolicyFile, bool apiStandalone, bool listApiInProduct) { var productId = GetProductId(product); GeneratedTemplate generatedTemplate = new GeneratedTemplate { Directory = $"product-{productId}", FileName = $"product-{productId}.template.json" }; DeploymentTemplate template = new DeploymentTemplate(true, true); if (separatePolicyFile) { ReplaceProductPolicyWithFileLink(product, productId); AddParametersForFileLink(parsedTemplate); } template.parameters = GetParameters(parsedTemplate["parameters"], product); template.resources.Add(JObject.FromObject(product)); generatedTemplate.Content = JObject.FromObject(template); if (listApiInProduct) { ListApiInProduct(generatedTemplate.Content, parsedTemplate); } if (!listApiInProduct && apiStandalone) { RemoveProductAPIs(generatedTemplate.Content); } return(generatedTemplate); }
private JArray GenerateDeploymentDependsOn(GeneratedTemplate template, List <GeneratedTemplate> generatedTemplates) { var dependsOn = new JArray(); foreach (string name in template.ExternalDependencies) { //check for ListApiInProduct, then skip the dependency to the api resources. if (_listApiInProduct && name.Contains("Microsoft.ApiManagement/service/apis")) { continue; } var matches = generatedTemplates.Where(t => IsLocalDependency(name, t)); if (matches.Any()) { var match = matches.First(); dependsOn.Add($"[resourceId('Microsoft.Resources/deployments', '{match.GetShortPath()}')]"); } else { var notFound = true; } } if (template.FileName.EndsWith(".swagger.template.json")) { dependsOn.Add($"[resourceId('Microsoft.Resources/deployments', '{template.FileName.Replace(".swagger.template.json", ".template.json")}')]"); } return(dependsOn); }
private void GenerateSwaggerTemplate(JObject parsedTemplate, bool separatePolicyFile, IEnumerable <JToken> apis, List <GeneratedTemplate> templates) { AddParametersForFileLink(parsedTemplate); var apisWithSwagger = apis.Where(x => x["properties"].Value <string>("contentFormat") == "swagger-json" && x["properties"].Value <string>("contentValue") != null); foreach (var apiWithSwagger in apisWithSwagger) { GeneratedTemplate generatedTemplate = new GeneratedTemplate(); DeploymentTemplate template = new DeploymentTemplate(true, true); SetFilenameAndDirectory(apiWithSwagger, parsedTemplate, generatedTemplate, true); if (separatePolicyFile) { ReplaceApiOperationPolicyWithFileLink(apiWithSwagger, parsedTemplate); AddParametersForFileLink(parsedTemplate); } var swaggerTemplate = CreateSwaggerTemplate(apiWithSwagger, parsedTemplate); template.resources.Add(JObject.FromObject(swaggerTemplate)); template.parameters = GetParameters(parsedTemplate["parameters"], swaggerTemplate); generatedTemplate.Content = JObject.FromObject(template); templates.Add(generatedTemplate); GeneratedTemplate generatedSwagger = new GeneratedTemplate(); SetFilenameAndDirectory(apiWithSwagger, parsedTemplate, generatedSwagger, true); generatedSwagger.FileName = generatedSwagger.FileName.Replace("swagger.template.json", "swagger.json"); generatedSwagger.Content = JObject.Parse(apiWithSwagger["properties"].Value <string>("contentValue")); templates.Add(generatedSwagger); } }
private GeneratedTemplate GenerateAPI(JToken api, JObject parsedTemplate, bool apiStandalone, bool separatePolicyFile, bool separateSwaggerFile) { var apiObject = JObject.FromObject(api); GeneratedTemplate generatedTemplate = new GeneratedTemplate(); DeploymentTemplate template = new DeploymentTemplate(true, true); if (separatePolicyFile) { ReplaceApiOperationPolicyWithFileLink(apiObject, parsedTemplate); AddParametersForFileLink(parsedTemplate); } if (separateSwaggerFile) { ((JObject)apiObject["properties"]).Property("contentFormat").Remove(); ((JObject)apiObject["properties"]).Property("contentValue").Remove(); apiObject["resources"].Where(x => _swaggerTemplateApiResourceTypes.Any(p => p == x.Value <string>("type"))) .ToList().ForEach(x => x.Remove()); } template.parameters = GetParameters(parsedTemplate["parameters"], apiObject); SetFilenameAndDirectory(apiObject, parsedTemplate, generatedTemplate, false); template.resources.Add(apiStandalone ? RemoveServiceDependencies(apiObject) : apiObject); if (apiStandalone) { AddProductAPI(apiObject, parsedTemplate, template.resources); } generatedTemplate.Content = JObject.FromObject(template); return(generatedTemplate); }
private static void SetFilenameAndDirectoryForVersionSet(JToken api, GeneratedTemplate generatedTemplate, JObject parsedTemplate) { string versionSetName = GetVersionSetName(api, parsedTemplate); generatedTemplate.FileName = $"api-{versionSetName}.version-set.template.json"; generatedTemplate.Directory = $@"api-{versionSetName}"; }
private static void SetFilenameAndDirectory(JToken api, JObject parsedTemplate, GeneratedTemplate generatedTemplate, bool swaggerFile = false) { var filenameAndDirectory = GetFileNameAndDirectory(api, parsedTemplate, swaggerFile); generatedTemplate.FileName = filenameAndDirectory.FileName; generatedTemplate.Directory = filenameAndDirectory.Directory; }
private static bool IsLocalDependency(string name, GeneratedTemplate template) { var resourceType = GetSplitPart(1, name); var nameParts = name.Split(',').Skip(1).Select(x => x.Trim().Replace("'))]", "')").Replace("')]", "')")); var localDependency = template.Content.SelectTokens($"$..resources[?(@.type=='{resourceType}')]") .Any(resource => nameParts.All(namePart => NameContainsPart(resource, namePart))); return(localDependency); }
private JObject GenerateDeploymentParameters(GeneratedTemplate template2) { var parameters = new JObject(); var parametersFromTemplate = template2.Content["parameters"]; foreach (JProperty token in parametersFromTemplate.Cast <JProperty>()) { var name = token.Name; parameters.Add(name, JObject.FromObject(new { value = $"[parameters('{name}')]" })); } return(parameters); }
private IEnumerable <GeneratedTemplate> GenerateService(JObject parsedTemplate, bool separatePolicyFile, bool alwaysAddPropertiesAndBackend) { List <GeneratedTemplate> templates = new List <GeneratedTemplate>(); List <string> wantedResources = new List <string> { ServiceResourceType, OperationalInsightsWorkspaceResourceType, AppInsightsResourceType, StorageAccountResourceType }; if (alwaysAddPropertiesAndBackend) { wantedResources.AddRange(new[] { PropertyResourceType, BackendResourceType }); } var generatedTemplate = new GeneratedTemplate { FileName = "service.template.json", Directory = String.Empty }; DeploymentTemplate template = new DeploymentTemplate(true, true); var resources = parsedTemplate.SelectTokens("$.resources[*]") .Where(r => wantedResources.Any(w => w == r.Value <string>("type"))); foreach (JToken resource in resources) { if (resource.Value <string>("type") == ServiceResourceType) { AddServiceResources(parsedTemplate, resource, PropertyResourceType); AddServiceResources(parsedTemplate, resource, BackendResourceType); AddServiceResources(parsedTemplate, resource, OpenIdConnectProviderResourceType); AddServiceResources(parsedTemplate, resource, CertificateResourceType); if (separatePolicyFile) { var policy = resource.SelectToken($"$..resources[?(@.type==\'{ServicePolicyResourceType}\')]"); if (policy != null) { templates.Add(GenerateServicePolicyFile(parsedTemplate, policy)); ReplacePolicyWithFileLink(policy, new FileInfo(ServicePolicyFileName, String.Empty)); } } } template.parameters.Merge(GetParameters(parsedTemplate["parameters"], resource)); template.variables.Merge(GetParameters(parsedTemplate["variables"], resource, "variables")); var variableParameters = GetParameters(parsedTemplate["parameters"], parsedTemplate["variables"]); foreach (var parameter in variableParameters) { if (template.parameters[parameter.Key] == null) { template.parameters[parameter.Key] = parameter.Value; } } template.resources.Add(JObject.FromObject(resource)); } generatedTemplate.Content = JObject.FromObject(template); templates.Add(generatedTemplate); return(templates); }
private GeneratedTemplate GenerateAPI(JToken api, JObject parsedTemplate, bool apiStandalone) { GeneratedTemplate generatedTemplate = new GeneratedTemplate(); DeploymentTemplate template = new DeploymentTemplate(true); template.parameters = GetParameters(parsedTemplate["parameters"], api); SetFilenameAndDirectory(api, parsedTemplate, generatedTemplate); template.resources.Add(apiStandalone ? RemoveServiceDependencies(api) : JObject.FromObject(api)); generatedTemplate.Content = JObject.FromObject(template); return(generatedTemplate); }
private static GeneratedTemplate GenerateServicePolicyFile(JObject parsedTemplate, JToken policy) { var content = policy["properties"].Value <string>("policyContent"); var template = new GeneratedTemplate { Type = ContentType.Xml, XmlContent = content, FileName = ServicePolicyFileName, Directory = String.Empty }; return(template); }
private GeneratedTemplate GeneratedMasterTemplate2(JObject parsedTemplate, bool separatePolicyFile, string fileName, string directory, IEnumerable <GeneratedTemplate> filteredTemplates, List <GeneratedTemplate> generatedTemplates) { var generatedTemplate = new GeneratedTemplate { Directory = directory, FileName = fileName }; DeploymentTemplate template = new DeploymentTemplate(true, true); foreach (GeneratedTemplate template2 in filteredTemplates) { template.resources.Add(GenerateDeployment(template2, generatedTemplates)); } template.parameters = GetParameters(parsedTemplate["parameters"], template.resources, separatePolicyFile); generatedTemplate.Content = JObject.FromObject(template); return(generatedTemplate); }
private static GeneratedTemplate GeneratePolicyFile(JObject parsedTemplate, JToken policy, JToken api, string operationId) { var content = policy["properties"].Value <string>("policyContent"); var template = new GeneratedTemplate { Type = ContentType.Xml, XmlContent = content }; var filenameAndDirectory = GetFilenameAndDirectoryForOperationPolicy(api, parsedTemplate, operationId); template.FileName = filenameAndDirectory.FileName; template.Directory = filenameAndDirectory.Directory; return(template); }
private static GeneratedTemplate GenerateTemplate(JObject parsedTemplate, string filename, string directory, params string[] wantedResources) { var generatedTemplate = new GeneratedTemplate { Directory = directory, FileName = filename }; DeploymentTemplate template = new DeploymentTemplate(true, true); var resources = parsedTemplate.SelectTokens("$.resources[*]") .Where(r => wantedResources.Any(w => w == r.Value <string>("type"))); foreach (JToken resource in resources) { template.parameters = GetParameters(parsedTemplate["parameters"], resource); template.resources.Add(JObject.FromObject(resource)); } generatedTemplate.Content = JObject.FromObject(template); return(generatedTemplate); }
private GeneratedTemplate GenerateVersionSet(JToken api, JObject parsedTemplate, bool apiStandalone) { GeneratedTemplate generatedTemplate = new GeneratedTemplate(); DeploymentTemplate template = new DeploymentTemplate(true); SetFilenameAndDirectoryForVersionSet(api, generatedTemplate, parsedTemplate); var versionSetId = GetParameterPart(api["properties"], "apiVersionSetId", -1); var versionSet = parsedTemplate .SelectTokens("$.resources[?(@.type==\'Microsoft.ApiManagement/service/api-version-sets\')]") .FirstOrDefault(x => x.Value <string>("name").Contains(versionSetId)); if (versionSet != null) { template.parameters = GetParameters(parsedTemplate["parameters"], versionSet); template.resources.Add(apiStandalone ? RemoveServiceDependencies(versionSet) : JObject.FromObject(versionSet)); } generatedTemplate.Content = JObject.FromObject(template); return(generatedTemplate); }
private JArray GenerateDeploymentDependsOn(GeneratedTemplate template, List <GeneratedTemplate> generatedTemplates) { var dependsOn = new JArray(); foreach (string name in template.ExternalDependencies) { var matches = generatedTemplates.Where(t => IsLocalDependency(name, t)); if (matches.Any()) { var match = matches.First(); dependsOn.Add($"[resourceId('Microsoft.Resources/deployments', '{match.GetShortPath()}')]"); } else { var notFound = true; } } return(dependsOn); }
private JObject GenerateDeployment(GeneratedTemplate template2, List <GeneratedTemplate> generatedTemplates) { var deployment = new { apiVersion = "2017-05-10", name = template2.GetShortPath(), type = "Microsoft.Resources/deployments", properties = new { mode = "Incremental", templateLink = new { uri = $"[concat(parameters('repoBaseUrl'), '{template2.GetUnixPath()}', parameters('{TemplatesStorageAccountSASToken}'))]", contentVersion = "1.0.0.0" }, parameters = GenerateDeploymentParameters(template2) }, dependsOn = GenerateDeploymentDependsOn(template2, generatedTemplates) }; return(JObject.FromObject(deployment)); }
private GeneratedTemplate GenerateAPI(JToken api, JObject parsedTemplate, bool apiStandalone, bool separatePolicyFile) { GeneratedTemplate generatedTemplate = new GeneratedTemplate(); DeploymentTemplate template = new DeploymentTemplate(true, true); if (separatePolicyFile) { ReplaceApiOperationPolictPolicyWithFileLink(api, parsedTemplate); AddParametersForFileLink(parsedTemplate); } template.parameters = GetParameters(parsedTemplate["parameters"], api); SetFilenameAndDirectory(api, parsedTemplate, generatedTemplate); template.resources.Add(apiStandalone ? RemoveServiceDependencies(api) : JObject.FromObject(api)); if (apiStandalone) { AddProductAPI(api, parsedTemplate, template.resources); } generatedTemplate.Content = JObject.FromObject(template); return(generatedTemplate); }
private static GeneratedTemplate GenerateService(JObject parsedTemplate) { var generatedTemplate = new GeneratedTemplate { Directory = String.Empty, FileName = "service.template.json" }; DeploymentTemplate template = new DeploymentTemplate(true); var wantedResources = new[] { "Microsoft.ApiManagement/service", "Microsoft.OperationalInsights/workspaces", "Microsoft.Insights/components", "Microsoft.Storage/storageAccounts" }; var resources = parsedTemplate.SelectTokens("$.resources[*]") .Where(r => wantedResources.Any(w => w == r.Value <string>("type"))); foreach (JToken resource in resources) { template.parameters = GetParameters(parsedTemplate["parameters"], resource); template.resources.Add(JObject.FromObject(resource)); } generatedTemplate.Content = JObject.FromObject(template); return(generatedTemplate); }
private GeneratedTemplate GenerateParameterFile(GeneratedTemplate masterTemplate, bool apiMasterParameterFile = false) { var generatedTemplate = new GeneratedTemplate { Directory = masterTemplate.Directory, FileName = masterTemplate.FileName.Replace(".template.json", ".parameters.json") }; DeploymentParameters template = new DeploymentParameters(); var parameters = masterTemplate.Content["parameters"]; var excludedParameters = new List <string> { TemplatesStorageAccountSASToken }; if (apiMasterParameterFile) { excludedParameters.Add("repoBaseUrl"); excludedParameters.Add("apimServiceName"); } foreach (JProperty parameter in parameters.Cast <JProperty>().Where(p => excludedParameters.All(e => e != p.Name))) { string name = parameter.Name; string type = parameter.Value["type"].Value <string>(); switch (type) { case "bool": bool boolValue = parameter.Value["defaultValue"]?.Value <bool>() ?? false; template.AddParameter(name, boolValue); break; default: string value = parameter.Value["defaultValue"]?.Value <string>() ?? String.Empty; template.AddParameter(name, value); break; } } generatedTemplate.Content = JObject.FromObject(template); return(generatedTemplate); }
private static void SetFilenameAndDirectory(JToken api, JObject parsedTemplate, GeneratedTemplate generatedTemplate) { if (api["properties"]["apiVersionSetId"] != null) { string versionSetName = GetVersionSetName(api, parsedTemplate); string version = GetApiVersion(api, parsedTemplate); generatedTemplate.FileName = $"api-{versionSetName}.{version}.template.json"; generatedTemplate.Directory = $@"api-{versionSetName}\{version}"; } else { string name = api["properties"].Value <string>("displayName").Replace(' ', '-'); generatedTemplate.FileName = $"api-{name}.template.json"; generatedTemplate.Directory = $"api-{name}"; } }