public List <ProductAPITemplateResource> CreateProductAPITemplateResources(APIConfig api, string[] dependsOn) { // create a products/apis association resource for each product provided in the config file List <ProductAPITemplateResource> productAPITemplates = new List <ProductAPITemplateResource>(); string[] productIDs = api.products.Split(", "); foreach (string productID in productIDs) { ProductAPITemplateResource productAPITemplate = this.CreateProductAPITemplateResource(productID, api.name, dependsOn); productAPITemplates.Add(productAPITemplate); } return(productAPITemplates); }
public ProductAPITemplateResource CreateProductAPITemplateResource(string productID, string apiName, string[] dependsOn) { // create products/apis resource with properties ProductAPITemplateResource productAPITemplateResource = new ProductAPITemplateResource() { name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productID}/{apiName}')]", type = ResourceTypeConstants.ProductAPI, apiVersion = GlobalConstants.APIVersion, dependsOn = dependsOn }; return(productAPITemplateResource); }
public ProductAPITemplateResource CreateProductAPITemplateResource(string productID, string apiName, string[] dependsOn) { // create products/apis resource with properties ProductAPITemplateResource productAPITemplateResource = new ProductAPITemplateResource() { name = $"[concat(parameters('ApimServiceName'), '/{productID}/{apiName}')]", type = ResourceTypeConstants.ProductAPI, apiVersion = "2018-06-01-preview", properties = new ProductAPITemplateProperties(), dependsOn = dependsOn }; return(productAPITemplateResource); }
public ProductAPITemplateResource CreateProductAPITemplateResource(string productID, string apiName, string[] dependsOn) { // create products/apis resource with properties ProductAPITemplateResource productAPITemplateResource = new ProductAPITemplateResource() { name = $"[concat(parameters('ApimServiceName'), '/{productID}/{apiName}')]", type = "Microsoft.ApiManagement/service/products/apis", apiVersion = "2018-01-01", properties = new ProductAPITemplateProperties(), dependsOn = dependsOn }; return(productAPITemplateResource); }
public void ShouldCreateProductAPITemplateResourceFromValues() { // arrange ProductAPITemplateCreator productAPITemplateCreator = new ProductAPITemplateCreator(); string productId = "productId"; string apiName = "apiName"; string[] dependsOn = new string[] { "dependsOn" }; // act ProductAPITemplateResource productAPITemplateResource = productAPITemplateCreator.CreateProductAPITemplateResource(productId, apiName, dependsOn); // assert Assert.Equal($"[concat(parameters('ApimServiceName'), '/{productId}/{apiName}')]", productAPITemplateResource.name); Assert.Equal(dependsOn, productAPITemplateResource.dependsOn); }
public async Task <List <TemplateResource> > GenerateSingleProductAPIResourceAsync(string apiName, Extractor exc, string[] dependsOn) { List <TemplateResource> templateResources = new List <TemplateResource>(); string apimname = exc.sourceApimName, resourceGroup = exc.resourceGroup, fileFolder = exc.fileFolder, policyXMLBaseUrl = exc.policyXMLBaseUrl, policyXMLSasToken = exc.policyXMLSasToken; Console.WriteLine("------------------------------------------"); Console.WriteLine("Extracting products from {0} API:", apiName); // add product api associations to template #region API Products try { // pull product api associations string apiProducts = await GetAPIProductsAsync(apimname, resourceGroup, apiName); JObject oApiProducts = JObject.Parse(apiProducts); string lastProductAPIName = null; foreach (var item in oApiProducts["value"]) { string apiProductName = ((JValue)item["name"]).Value.ToString(); Console.WriteLine("'{0}' Product association found", apiProductName); // convert returned api product associations to template resource class ProductAPITemplateResource productAPIResource = JsonConvert.DeserializeObject <ProductAPITemplateResource>(item.ToString()); productAPIResource.type = ResourceTypeConstants.ProductAPI; productAPIResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiProductName}/{apiName}')]"; productAPIResource.apiVersion = GlobalConstants.APIVersion; productAPIResource.scale = null; productAPIResource.dependsOn = (lastProductAPIName != null) ? new string[] { lastProductAPIName } : dependsOn; templateResources.Add(productAPIResource); lastProductAPIName = $"[resourceId('Microsoft.ApiManagement/service/products/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiProductName}', '{apiName}')]"; } } catch (Exception e) { throw new Exception(e.Message); } #endregion return(templateResources); }
public List <ProductAPITemplateResource> CreateProductAPITemplateResources(APIConfig api, string[] dependsOn) { // create a products/apis association resource for each product provided in the config file List <ProductAPITemplateResource> productAPITemplates = new List <ProductAPITemplateResource>(); // products is comma separated list of productIds string[] productIDs = api.products.Split(", "); string[] allDependsOn = dependsOn; foreach (string productID in productIDs) { ProductAPITemplateResource productAPITemplate = this.CreateProductAPITemplateResource(productID, api.name, allDependsOn); // Add previous product/API resource as a dependency for next product/API resource allDependsOn = new string[dependsOn.Length + 1]; dependsOn.CopyTo(allDependsOn, 1); allDependsOn[0] = $"[resourceId('Microsoft.ApiManagement/service/products/apis', parameters('{ParameterNames.ApimServiceName}'), '{productID}', '{api.name}')]"; productAPITemplates.Add(productAPITemplate); } return(productAPITemplates); }
// this function will get the current revision of this api and will remove "isCurrent" paramter public async Task <List <TemplateResource> > GenerateCurrentRevisionAPIResourceAsync(string apiName, string apimname, string resourceGroup, string fileFolder, string policyXMLBaseUrl, string policyXMLSasToken) { List <TemplateResource> templateResources = new List <TemplateResource>(); string apiDetails = await GetAPIDetailsAsync(apimname, resourceGroup, apiName); Console.WriteLine("------------------------------------------"); Console.WriteLine("Extracting resources from {0} API:", apiName); // convert returned api to template resource class JObject oApiDetails = JObject.Parse(apiDetails); APITemplateResource apiResource = JsonConvert.DeserializeObject <APITemplateResource>(apiDetails); apiResource.type = ((JValue)oApiDetails["type"]).Value.ToString(); apiResource.name = $"[concat(parameters('ApimServiceName'), '/{apiName}')]"; apiResource.apiVersion = GlobalConstants.APIVersion; apiResource.scale = null; apiResource.properties.isCurrent = null; if (apiResource.properties.apiVersionSetId != null) { apiResource.dependsOn = new string[] { }; string versionSetName = apiResource.properties.apiVersionSetId; int versionSetPosition = versionSetName.IndexOf("apiVersionSets/"); versionSetName = versionSetName.Substring(versionSetPosition, (versionSetName.Length - versionSetPosition)); apiResource.properties.apiVersionSetId = $"[concat(resourceId('Microsoft.ApiManagement/service', parameters('ApimServiceName')), '/{versionSetName}')]"; } else { apiResource.dependsOn = new string[] { }; } templateResources.Add(apiResource); #region Schemas // add schema resources to api template List <TemplateResource> schemaResources = await GenerateSchemasARMTemplate(apimname, apiName, resourceGroup, fileFolder); templateResources.AddRange(schemaResources); #endregion #region Operations // pull api operations for service string[] operationNames = await GetAllOperationNames(apimname, resourceGroup, apiName); foreach (string operationName in operationNames) { string operationDetails = await GetAPIOperationDetailsAsync(apimname, resourceGroup, apiName, operationName); Console.WriteLine("'{0}' Operation found", operationName); // convert returned operation to template resource class OperationTemplateResource operationResource = JsonConvert.DeserializeObject <OperationTemplateResource>(operationDetails); string operationResourceName = operationResource.name; operationResource.name = $"[concat(parameters('ApimServiceName'), '/{apiName}/{operationResourceName}')]"; operationResource.apiVersion = GlobalConstants.APIVersion; operationResource.scale = null; // add operation dependencies and fix sample value if necessary List <string> operationDependsOn = new List <string>() { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{apiName}')]" }; foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationResource.properties.request.representations) { AddSchemaDependencyToOperationIfNecessary(apiName, operationDependsOn, operationTemplateRepresentation); ArmEscapeSampleValueIfNecessary(operationTemplateRepresentation); } foreach (OperationsTemplateResponse operationTemplateResponse in operationResource.properties.responses) { foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationTemplateResponse.representations) { AddSchemaDependencyToOperationIfNecessary(apiName, operationDependsOn, operationTemplateRepresentation); ArmEscapeSampleValueIfNecessary(operationTemplateRepresentation); } } operationResource.dependsOn = operationDependsOn.ToArray(); templateResources.Add(operationResource); // add operation policy resource to api template try { string operationPolicy = await GetOperationPolicyAsync(apimname, resourceGroup, apiName, operationName); Console.WriteLine($" - Operation policy found for {operationName} operation"); PolicyTemplateResource operationPolicyResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(operationPolicy); operationPolicyResource.name = $"[concat(parameters('ApimServiceName'), '/{apiName}/{operationResourceName}/policy')]"; operationPolicyResource.apiVersion = GlobalConstants.APIVersion; operationPolicyResource.scale = null; operationPolicyResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('ApimServiceName'), '{apiName}', '{operationResourceName}')]" }; // write policy xml content to file and point to it if policyXMLBaseUrl is provided if (policyXMLBaseUrl != null) { string policyXMLContent = operationPolicyResource.properties.value; string policyFolder = String.Concat(fileFolder, $@"/policies"); string operationPolicyFileName = $@"/{operationName}-operationPolicy.xml"; this.fileWriter.CreateFolderIfNotExists(policyFolder); this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, operationPolicyFileName)); operationPolicyResource.properties.format = "rawxml-link"; if (policyXMLSasToken != null) { operationPolicyResource.properties.value = $"[concat(parameters('PolicyXMLBaseUrl'), '{operationPolicyFileName}', parameters('PolicyXMLSasToken'))]"; } else { operationPolicyResource.properties.value = $"[concat(parameters('PolicyXMLBaseUrl'), '{operationPolicyFileName}')]"; } } templateResources.Add(operationPolicyResource); } catch (Exception) { } // add tags associated with the operation to template try { // pull tags associated with the operation string apiOperationTags = await GetOperationTagsAsync(apimname, resourceGroup, apiName, operationName); JObject oApiOperationTags = JObject.Parse(apiOperationTags); foreach (var tag in oApiOperationTags["value"]) { string apiOperationTagName = ((JValue)tag["name"]).Value.ToString(); Console.WriteLine(" - '{0}' Tag association found for {1} operation", apiOperationTagName, operationResourceName); // convert operation tag association to template resource class TagTemplateResource operationTagResource = JsonConvert.DeserializeObject <TagTemplateResource>(tag.ToString()); operationTagResource.name = $"[concat(parameters('ApimServiceName'), '/{apiName}/{operationResourceName}/{apiOperationTagName}')]"; operationTagResource.apiVersion = GlobalConstants.APIVersion; operationTagResource.scale = null; operationTagResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('ApimServiceName'), '{apiName}', '{operationResourceName}')]" }; templateResources.Add(operationTagResource); } } catch (Exception) { } } #endregion #region API Policies // add api policy resource to api template try { string apiPolicies = await GetAPIPolicyAsync(apimname, resourceGroup, apiName); Console.WriteLine("API policy found"); PolicyTemplateResource apiPoliciesResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(apiPolicies); apiPoliciesResource.apiVersion = GlobalConstants.APIVersion; apiPoliciesResource.name = $"[concat(parameters('ApimServiceName'), '/{apiName}/{apiPoliciesResource.name}')]"; apiPoliciesResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{apiName}')]" }; // write policy xml content to file and point to it if policyXMLBaseUrl is provided if (policyXMLBaseUrl != null) { string policyXMLContent = apiPoliciesResource.properties.value; string policyFolder = String.Concat(fileFolder, $@"/policies"); string apiPolicyFileName = $@"/{apiName}-apiPolicy.xml"; this.fileWriter.CreateFolderIfNotExists(policyFolder); this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, apiPolicyFileName)); apiPoliciesResource.properties.format = "rawxml-link"; if (policyXMLSasToken != null) { apiPoliciesResource.properties.value = $"[concat(parameters('PolicyXMLBaseUrl'), '{apiPolicyFileName}', parameters('PolicyXMLSasToken'))]"; } else { apiPoliciesResource.properties.value = $"[concat(parameters('PolicyXMLBaseUrl'), '{apiPolicyFileName}')]"; } } templateResources.Add(apiPoliciesResource); } catch (Exception) { } #endregion // add tags associated with the api to template try { // pull tags associated with the api string apiTags = await GetAPITagsAsync(apimname, resourceGroup, apiName); JObject oApiTags = JObject.Parse(apiTags); foreach (var tag in oApiTags["value"]) { string apiTagName = ((JValue)tag["name"]).Value.ToString(); Console.WriteLine("'{0}' Tag association found", apiTagName); // convert associations between api and tags to template resource class TagTemplateResource apiTagResource = JsonConvert.DeserializeObject <TagTemplateResource>(tag.ToString()); apiTagResource.name = $"[concat(parameters('ApimServiceName'), '/{apiName}/{apiTagName}')]"; apiTagResource.apiVersion = GlobalConstants.APIVersion; apiTagResource.scale = null; apiTagResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{apiName}')]" }; templateResources.Add(apiTagResource); } } catch (Exception) { } // add product api associations to template #region API Products try { // pull product api associations string apiProducts = await GetAPIProductsAsync(apimname, resourceGroup, apiName); JObject oApiProducts = JObject.Parse(apiProducts); foreach (var item in oApiProducts["value"]) { string apiProductName = ((JValue)item["name"]).Value.ToString(); Console.WriteLine("'{0}' Product association found", apiProductName); // convert returned api product associations to template resource class ProductAPITemplateResource productAPIResource = JsonConvert.DeserializeObject <ProductAPITemplateResource>(item.ToString()); productAPIResource.type = ResourceTypeConstants.ProductAPI; productAPIResource.name = $"[concat(parameters('ApimServiceName'), '/{apiProductName}/{apiName}')]"; productAPIResource.apiVersion = GlobalConstants.APIVersion; productAPIResource.scale = null; productAPIResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{apiName}')]" }; templateResources.Add(productAPIResource); } } catch (Exception) { } #endregion #region Diagnostics // add diagnostics to template // pull diagnostics for api string diagnostics = await GetAPIDiagnosticsAsync(apimname, resourceGroup, apiName); JObject oDiagnostics = JObject.Parse(diagnostics); foreach (var diagnostic in oDiagnostics["value"]) { string diagnosticName = ((JValue)diagnostic["name"]).Value.ToString(); Console.WriteLine("'{0}' Diagnostic found", diagnosticName); // convert returned diagnostic to template resource class DiagnosticTemplateResource diagnosticResource = diagnostic.ToObject <DiagnosticTemplateResource>(); diagnosticResource.name = $"[concat(parameters('ApimServiceName'), '/{apiName}/{diagnosticName}')]"; diagnosticResource.type = ResourceTypeConstants.APIDiagnostic; diagnosticResource.apiVersion = GlobalConstants.APIVersion; diagnosticResource.scale = null; diagnosticResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{apiName}')]" }; if (!diagnosticName.Contains("applicationinsights")) { // enableHttpCorrelationHeaders only works for application insights, causes errors otherwise diagnosticResource.properties.enableHttpCorrelationHeaders = null; } templateResources.Add(diagnosticResource); } #endregion return(templateResources); }
public async Task <Template> GenerateAPIsARMTemplate(string apimname, string resourceGroup, string fileFolder, string singleApiName) { // pull all apis from service string apis = await GetAPIs(apimname, resourceGroup); // initialize arm template Template armTemplate = GenerateEmptyTemplateWithParameters(); JObject oApi = JObject.Parse(apis); oApi = FormatoApi(singleApiName, oApi); Console.WriteLine("{0} API's found ...", ((JContainer)oApi["value"]).Count.ToString()); List <TemplateResource> templateResources = new List <TemplateResource>(); for (int i = 0; i < ((JContainer)oApi["value"]).Count; i++) { string apiName = ((JValue)oApi["value"][i]["name"]).Value.ToString(); string apiDetails = await GetAPIDetails(apimname, resourceGroup, apiName); Console.WriteLine("------------------------------------------"); Console.WriteLine("Extracting resources from {0} API:", apiName); // convert returned api to template resource class JObject oApiDetails = JObject.Parse(apiDetails); APITemplateResource apiResource = JsonConvert.DeserializeObject <APITemplateResource>(apiDetails); string oApiName = ((JValue)oApiDetails["name"]).Value.ToString(); apiResource.type = ((JValue)oApiDetails["type"]).Value.ToString(); apiResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}')]"; apiResource.apiVersion = GlobalConstants.APIVersion; apiResource.scale = null; if (apiResource.properties.apiVersionSetId != null) { apiResource.dependsOn = new string[] { }; string versionSetName = apiResource.properties.apiVersionSetId; int versionSetPosition = versionSetName.IndexOf("apiVersionSets/"); versionSetName = versionSetName.Substring(versionSetPosition, (versionSetName.Length - versionSetPosition)); apiResource.properties.apiVersionSetId = $"[concat(resourceId('Microsoft.ApiManagement/service', parameters('ApimServiceName')), '/{versionSetName}')]"; } else { apiResource.dependsOn = new string[] { }; } templateResources.Add(apiResource); #region Schemas // add schema resources to api template List <TemplateResource> schemaResources = await GenerateSchemasARMTemplate(apimname, apiName, resourceGroup, fileFolder); templateResources.AddRange(schemaResources); #endregion #region Operations // pull api operations for service string operations = await GetAPIOperations(apimname, resourceGroup, apiName); JObject oOperations = JObject.Parse(operations); foreach (var item in oOperations["value"]) { string operationName = ((JValue)item["name"]).Value.ToString(); string operationDetails = await GetAPIOperationDetail(apimname, resourceGroup, apiName, operationName); Console.WriteLine("'{0}' Operation found", operationName); // convert returned operation to template resource class OperationTemplateResource operationResource = JsonConvert.DeserializeObject <OperationTemplateResource>(operationDetails); string operationResourceName = operationResource.name; operationResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}/{operationResourceName}')]"; operationResource.apiVersion = GlobalConstants.APIVersion; operationResource.scale = null; // add api and schemas to operation dependsOn, if necessary List <string> operationDependsOn = new List <string>() { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{oApiName}')]" }; foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationResource.properties.request.representations) { if (operationTemplateRepresentation.schemaId != null) { string dependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/schemas', parameters('ApimServiceName'), '{oApiName}', '{operationTemplateRepresentation.schemaId}')]"; // add value to list if schema has not already been added if (!operationDependsOn.Exists(o => o == dependsOn)) { operationDependsOn.Add(dependsOn); } } } foreach (OperationsTemplateResponse operationTemplateResponse in operationResource.properties.responses) { foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationTemplateResponse.representations) { if (operationTemplateRepresentation.schemaId != null) { string dependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/schemas', parameters('ApimServiceName'), '{oApiName}', '{operationTemplateRepresentation.schemaId}')]"; // add value to list if schema has not already been added if (!operationDependsOn.Exists(o => o == dependsOn)) { operationDependsOn.Add(dependsOn); } } } } operationResource.dependsOn = operationDependsOn.ToArray(); templateResources.Add(operationResource); // add operation policy resource to api template try { string operationPolicy = await GetOperationPolicy(apimname, resourceGroup, oApiName, operationName); Console.WriteLine($" - Operation policy found for {operationName} operation"); PolicyTemplateResource operationPolicyResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(operationPolicy); operationPolicyResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}/{operationResourceName}/policy')]"; operationPolicyResource.apiVersion = GlobalConstants.APIVersion; operationPolicyResource.scale = null; operationPolicyResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('ApimServiceName'), '{oApiName}', '{operationResourceName}')]" }; templateResources.Add(operationPolicyResource); } catch (Exception) { } } #endregion #region API Policies // add api policy resource to api template try { string apiPolicies = await GetAPIPolicies(apimname, resourceGroup, apiName); Console.WriteLine("API policy found"); PolicyTemplateResource apiPoliciesResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(apiPolicies); apiPoliciesResource.apiVersion = GlobalConstants.APIVersion; apiPoliciesResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}/{apiPoliciesResource.name}')]"; apiPoliciesResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{apiName}')]" }; templateResources.Add(apiPoliciesResource); } catch (Exception) { } #endregion // add product api associations to template #region API Products try { // pull product api associations string apiProducts = await GetAPIProducts(apimname, resourceGroup, apiName); JObject oApiProducts = JObject.Parse(apiProducts); foreach (var item in oApiProducts["value"]) { string apiProductName = ((JValue)item["name"]).Value.ToString(); Console.WriteLine("'{0}' Product association found", apiProductName); // convert returned api product associations to template resource class ProductAPITemplateResource productAPIResource = JsonConvert.DeserializeObject <ProductAPITemplateResource>(apiProducts); productAPIResource.type = ResourceTypeConstants.ProductAPI; productAPIResource.name = $"[concat(parameters('ApimServiceName'), '/{apiProductName}/{oApiName}')]"; productAPIResource.apiVersion = GlobalConstants.APIVersion; productAPIResource.scale = null; productAPIResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{oApiName}')]" }; templateResources.Add(productAPIResource); } } catch (Exception) { } #endregion #region Diagnostics // add diagnostics to template // pull diagnostics for api string diagnostics = await GetAPIDiagnostics(apimname, resourceGroup, apiName); JObject oDiagnostics = JObject.Parse(diagnostics); foreach (var diagnostic in oDiagnostics["value"]) { string diagnosticName = ((JValue)diagnostic["name"]).Value.ToString(); Console.WriteLine("'{0}' Diagnostic found", diagnosticName); // convert returned diagnostic to template resource class DiagnosticTemplateResource diagnosticResource = diagnostic.ToObject <DiagnosticTemplateResource>(); diagnosticResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}/{diagnosticName}')]"; diagnosticResource.type = ResourceTypeConstants.APIDiagnostic; diagnosticResource.apiVersion = GlobalConstants.APIVersion; diagnosticResource.scale = null; diagnosticResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{oApiName}')]" }; if (!diagnosticName.Contains("applicationinsights")) { // enableHttpCorrelationHeaders only works for application insights, causes errors otherwise diagnosticResource.properties.enableHttpCorrelationHeaders = null; } templateResources.Add(diagnosticResource); } #endregion } armTemplate.resources = templateResources.ToArray(); return(armTemplate); }
public void ShouldCreateProductAPIFromCreatorConfig() { // arrange ProductAPITemplateCreator productAPITemplateCreator = new ProductAPITemplateCreator(); CreatorConfig creatorConfig = new CreatorConfig() { products = new List <ProductConfig>(), apis = new List <APIConfig>() }; ProductConfig product = new ProductConfig() { name = "productName", displayName = "display name", description = "description", terms = "terms", subscriptionRequired = true, approvalRequired = true, subscriptionsLimit = 1, state = "state" }; creatorConfig.products.Add(product); APIConfig api = new APIConfig() { name = "apiName", apiVersion = "apiVersion", apiVersionDescription = "apiVersionDescription", apiVersionSetId = "apiVersionSetId", apiRevision = "revision", apiRevisionDescription = "revisionDescription", suffix = "suffix", products = "productName", subscriptionRequired = true, authenticationSettings = new APITemplateAuthenticationSettings() { oAuth2 = new APITemplateOAuth2() { authorizationServerId = "", scope = "" }, openid = new APITemplateOpenID() { openidProviderId = "", bearerTokenSendingMethods = new string[] { } }, subscriptionKeyRequired = true }, openApiSpec = "https://petstore.swagger.io/v2/swagger.json", protocols = "https", isCurrent = true, type = "http" }; creatorConfig.apis.Add(api); // act Template productAPITemplate = productAPITemplateCreator.CreateProductAPITemplate(creatorConfig); ProductAPITemplateResource productAPITemplateResource = (ProductAPITemplateResource)productAPITemplate.resources[0]; // assert Assert.Equal($"[concat(parameters('ApimServiceName'), '/{product.name}/{api.name}')]", productAPITemplateResource.name); }
/// <summary> /// Adds related API Template resources like schemas, operations, products, tags etc. /// </summary> /// <param name="apiName">The name of the API.</param> /// <param name="exc">The extractor.</param> /// <returns></returns> private async Task <IEnumerable <TemplateResource> > GetRelatedTemplateResourcesAsync(string apiName, Extractor exc) { List <TemplateResource> templateResources = new List <TemplateResource>(); string apimname = exc.sourceApimName, resourceGroup = exc.resourceGroup, fileFolder = exc.fileFolder, policyXMLBaseUrl = exc.policyXMLBaseUrl, policyXMLSasToken = exc.policyXMLSasToken; #region Schemas // add schema resources to api template List <TemplateResource> schemaResources = await GenerateSchemasARMTemplate(apimname, apiName, resourceGroup, fileFolder); templateResources.AddRange(schemaResources); #endregion #region Operations // pull api operations for service string[] operationNames = await GetAllOperationNames(apimname, resourceGroup, apiName); int numBatches = 0; // create empty array for the batch operation owners List <string> batchOwners = new List <string>(); // if a batch size is specified if (exc.operationBatchSize > 0) { // store the number of batches required based on exc.operationBatchSize numBatches = (int)Math.Ceiling((double)operationNames.Length / (double)exc.operationBatchSize); //Console.WriteLine ("Number of batches: {0}", numBatches); } foreach (string operationName in operationNames) { int opIndex = Array.IndexOf(operationNames, operationName); //add batch owners into array // ensure each owner is linked to the one before if (exc.operationBatchSize > 0 && opIndex < numBatches) { batchOwners.Add(operationName); //Console.WriteLine("Adding operation {0} to owner list", operationName); } string operationDetails = await GetAPIOperationDetailsAsync(apimname, resourceGroup, apiName, operationName); Console.WriteLine("'{0}' Operation found", operationName); // convert returned operation to template resource class OperationTemplateResource operationResource = JsonConvert.DeserializeObject <OperationTemplateResource>(operationDetails); string operationResourceName = operationResource.name; operationResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{operationResourceName}')]"; operationResource.apiVersion = GlobalConstants.APIVersion; operationResource.scale = null; // add operation dependencies and fix sample value if necessary List <string> operationDependsOn = new List <string>() { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" }; foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationResource.properties.request.representations) { AddSchemaDependencyToOperationIfNecessary(apiName, operationDependsOn, operationTemplateRepresentation); ArmEscapeSampleValueIfNecessary(operationTemplateRepresentation); } foreach (OperationsTemplateResponse operationTemplateResponse in operationResource.properties.responses) { foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationTemplateResponse.representations) { AddSchemaDependencyToOperationIfNecessary(apiName, operationDependsOn, operationTemplateRepresentation); ArmEscapeSampleValueIfNecessary(operationTemplateRepresentation); } } // add to batch if flagged string batchdependsOn; if (exc.operationBatchSize > 0 && opIndex > 0) { if (opIndex >= 1 && opIndex <= numBatches - 1) { // chain the owners to each other batchdependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{batchOwners[opIndex - 1]}')]"; //Console.WriteLine("Owner chaining: this request {0} to previous {1}", operationName, batchOwners[opIndex-1]); } else { // chain the operation to respective owner int ownerIndex = (int)Math.Floor((opIndex - numBatches) / ((double)exc.operationBatchSize - 1)); batchdependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{batchOwners[ownerIndex]}')]"; //Console.WriteLine("Operation {0} chained to owner {1}", operationName, batchOwners[ownerIndex]); } operationDependsOn.Add(batchdependsOn); } operationResource.dependsOn = operationDependsOn.ToArray(); templateResources.Add(operationResource); // add operation policy resource to api template try { string operationPolicy = await GetOperationPolicyAsync(apimname, resourceGroup, apiName, operationName); Console.WriteLine($" - Operation policy found for {operationName} operation"); PolicyTemplateResource operationPolicyResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(operationPolicy); operationPolicyResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{operationResourceName}/policy')]"; operationPolicyResource.apiVersion = GlobalConstants.APIVersion; operationPolicyResource.scale = null; operationPolicyResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{operationResourceName}')]" }; // write policy xml content to file and point to it if policyXMLBaseUrl is provided if (policyXMLBaseUrl != null) { string policyXMLContent = operationPolicyResource.properties.value; string policyFolder = String.Concat(fileFolder, $@"/policies"); string operationPolicyFileName = $@"/{apiName}-{operationName}-operationPolicy.xml"; this.fileWriter.CreateFolderIfNotExists(policyFolder); this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, operationPolicyFileName)); operationPolicyResource.properties.format = "rawxml-link"; if (policyXMLSasToken != null) { operationPolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{operationPolicyFileName}', parameters('{ParameterNames.PolicyXMLSasToken}'))]"; } else { operationPolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{operationPolicyFileName}')]"; } } templateResources.Add(operationPolicyResource); } catch (Exception) { } // add tags associated with the operation to template try { // pull tags associated with the operation string apiOperationTags = await GetOperationTagsAsync(apimname, resourceGroup, apiName, operationName); JObject oApiOperationTags = JObject.Parse(apiOperationTags); foreach (var tag in oApiOperationTags["value"]) { string apiOperationTagName = ((JValue)tag["name"]).Value.ToString(); Console.WriteLine(" - '{0}' Tag association found for {1} operation", apiOperationTagName, operationResourceName); // convert operation tag association to template resource class TagTemplateResource operationTagResource = JsonConvert.DeserializeObject <TagTemplateResource>(tag.ToString()); operationTagResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{operationResourceName}/{apiOperationTagName}')]"; operationTagResource.apiVersion = GlobalConstants.APIVersion; operationTagResource.scale = null; operationTagResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{operationResourceName}')]" }; templateResources.Add(operationTagResource); } } catch (Exception) { } } #endregion #region API Policies // add api policy resource to api template try { string apiPolicies = await GetAPIPolicyAsync(apimname, resourceGroup, apiName); Console.WriteLine("API policy found"); PolicyTemplateResource apiPoliciesResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(apiPolicies); apiPoliciesResource.apiVersion = GlobalConstants.APIVersion; apiPoliciesResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{apiPoliciesResource.name}')]"; apiPoliciesResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" }; // write policy xml content to file and point to it if policyXMLBaseUrl is provided if (policyXMLBaseUrl != null) { string policyXMLContent = apiPoliciesResource.properties.value; string policyFolder = String.Concat(fileFolder, $@"/policies"); string apiPolicyFileName = $@"/{apiName}-apiPolicy.xml"; this.fileWriter.CreateFolderIfNotExists(policyFolder); this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, apiPolicyFileName)); apiPoliciesResource.properties.format = "rawxml-link"; if (policyXMLSasToken != null) { apiPoliciesResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{apiPolicyFileName}', parameters('{ParameterNames.PolicyXMLSasToken}'))]"; } else { apiPoliciesResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{apiPolicyFileName}')]"; } } templateResources.Add(apiPoliciesResource); } catch (Exception) { } #endregion #region API Tags // add tags associated with the api to template try { // pull tags associated with the api string apiTags = await GetAPITagsAsync(apimname, resourceGroup, apiName); JObject oApiTags = JObject.Parse(apiTags); foreach (var tag in oApiTags["value"]) { string apiTagName = ((JValue)tag["name"]).Value.ToString(); Console.WriteLine("'{0}' Tag association found", apiTagName); // convert associations between api and tags to template resource class TagTemplateResource apiTagResource = JsonConvert.DeserializeObject <TagTemplateResource>(tag.ToString()); apiTagResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{apiTagName}')]"; apiTagResource.apiVersion = GlobalConstants.APIVersion; apiTagResource.scale = null; apiTagResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" }; templateResources.Add(apiTagResource); } } catch (Exception) { } #endregion // add product api associations to template #region API Products try { // pull product api associations string apiProducts = await GetAPIProductsAsync(apimname, resourceGroup, apiName); JObject oApiProducts = JObject.Parse(apiProducts); foreach (var item in oApiProducts["value"]) { string apiProductName = ((JValue)item["name"]).Value.ToString(); Console.WriteLine("'{0}' Product association found", apiProductName); // convert returned api product associations to template resource class ProductAPITemplateResource productAPIResource = JsonConvert.DeserializeObject <ProductAPITemplateResource>(item.ToString()); productAPIResource.type = ResourceTypeConstants.ProductAPI; productAPIResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiProductName}/{apiName}')]"; productAPIResource.apiVersion = GlobalConstants.APIVersion; productAPIResource.scale = null; productAPIResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" }; templateResources.Add(productAPIResource); } } catch (Exception) { } #endregion #region Diagnostics // add diagnostics to template // pull diagnostics for api string diagnostics = await GetAPIDiagnosticsAsync(apimname, resourceGroup, apiName); JObject oDiagnostics = JObject.Parse(diagnostics); foreach (var diagnostic in oDiagnostics["value"]) { string diagnosticName = ((JValue)diagnostic["name"]).Value.ToString(); Console.WriteLine("'{0}' Diagnostic found", diagnosticName); // convert returned diagnostic to template resource class DiagnosticTemplateResource diagnosticResource = diagnostic.ToObject <DiagnosticTemplateResource>(); diagnosticResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{diagnosticName}')]"; diagnosticResource.type = ResourceTypeConstants.APIDiagnostic; diagnosticResource.apiVersion = GlobalConstants.APIVersion; diagnosticResource.scale = null; diagnosticResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" }; if (exc.paramApiLoggerId) { diagnosticResource.properties.loggerId = $"[parameters('{ParameterNames.ApiLoggerId}').{ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api)}.{ExtractorUtils.GenValidParamName(diagnosticName, ParameterPrefix.Diagnostic)}]"; } if (!diagnosticName.Contains("applicationinsights")) { // enableHttpCorrelationHeaders only works for application insights, causes errors otherwise diagnosticResource.properties.enableHttpCorrelationHeaders = null; } templateResources.Add(diagnosticResource); } #endregion return(templateResources); }
private void GenerateARMTemplate(string apimname, string resourceGroup, string fileFolder, string singleApiName) { #region API's FileWriter fileWriter; APIExtractor apiExtractor = new APIExtractor(); string apis = apiExtractor.GetAPIs(apimname, resourceGroup).Result; Template armTemplate = GenerateEmptyTemplateWithParameters(); JObject oApi = JObject.Parse(apis); oApi = FormatoApi(singleApiName, oApi); Console.WriteLine("{0} API's found ...", ((JContainer)oApi["value"]).Count.ToString()); List <TemplateResource> templateResources = new List <TemplateResource>(); for (int i = 0; i < ((JContainer)oApi["value"]).Count; i++) { string apiName = ((JValue)oApi["value"][i]["name"]).Value.ToString(); string apiDetails = apiExtractor.GetAPIDetails(apimname, resourceGroup, apiName).Result; Console.WriteLine("------------------------------------------"); Console.WriteLine("Getting operations from {0} API:", apiName); JObject oApiDetails = JObject.Parse(apiDetails); APITemplateResource apiResource = JsonConvert.DeserializeObject <APITemplateResource>(apiDetails); string oApiName = ((JValue)oApiDetails["name"]).Value.ToString(); apiResource.type = ((JValue)oApiDetails["type"]).Value.ToString(); apiResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}')]"; apiResource.apiVersion = "2018-06-01-preview"; apiResource.scale = null; if (apiResource.properties.apiVersionSetId != null) { apiResource.dependsOn = new string[] { }; string versionSetName = apiResource.properties.apiVersionSetId; int versionSetPosition = versionSetName.IndexOf("api-version-sets/"); versionSetName = versionSetName.Substring(versionSetPosition, (versionSetName.Length - versionSetPosition)); apiResource.properties.apiVersionSetId = $"[concat(resourceId('Microsoft.ApiManagement/service', parameters('ApimServiceName')), '/{versionSetName}')]"; GenerateVersionSetARMTemplate(apimname, resourceGroup, versionSetName, fileFolder); } else { apiResource.dependsOn = new string[] { }; } templateResources.Add(apiResource); #region Schemas List <TemplateResource> schemaResources = GenerateSchemasARMTemplate(apimname, apiName, resourceGroup, fileFolder); templateResources.AddRange(schemaResources); #endregion #region Operations string operations = apiExtractor.GetAPIOperations(apimname, resourceGroup, apiName).Result; JObject oOperations = JObject.Parse(operations); foreach (var item in oOperations["value"]) { string operationName = ((JValue)item["name"]).Value.ToString(); string operationDetails = apiExtractor.GetAPIOperationDetail(apimname, resourceGroup, apiName, operationName).Result; Console.WriteLine("'{0}' Operation found", operationName); OperationTemplateResource operationResource = JsonConvert.DeserializeObject <OperationTemplateResource>(operationDetails); string operationResourceName = operationResource.name; operationResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}/{operationResourceName}')]"; operationResource.apiVersion = "2018-06-01-preview"; operationResource.scale = null; // depend on api and schemas if necessary List <string> operationDependsOn = new List <string>() { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{oApiName}')]" }; foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationResource.properties.request.representations) { if (operationTemplateRepresentation.schemaId != null) { string dependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/schemas', parameters('ApimServiceName'), '{oApiName}', '{operationTemplateRepresentation.schemaId}')]"; // add value to list if schema has not already been added if (!operationDependsOn.Exists(o => o == dependsOn)) { operationDependsOn.Add(dependsOn); } } } foreach (OperationsTemplateResponse operationTemplateResponse in operationResource.properties.responses) { foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationTemplateResponse.representations) { if (operationTemplateRepresentation.schemaId != null) { string dependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/schemas', parameters('ApimServiceName'), '{oApiName}', '{operationTemplateRepresentation.schemaId}')]"; // add value to list if schema has not already been added if (!operationDependsOn.Exists(o => o == dependsOn)) { operationDependsOn.Add(dependsOn); } } } } operationResource.dependsOn = operationDependsOn.ToArray(); templateResources.Add(operationResource); try { string operationPolicy = apiExtractor.GetOperationPolicy(apimname, resourceGroup, oApiName, operationName).Result; Console.WriteLine($" - Policy found to {operationName} operation"); PolicyTemplateResource operationPolicyResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(operationPolicy); operationPolicyResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}/{operationResourceName}/policy')]"; operationPolicyResource.apiVersion = "2018-06-01-preview"; operationPolicyResource.scale = null; operationPolicyResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('ApimServiceName'), '{oApiName}', '{operationResourceName}')]" }; templateResources.Add(operationPolicyResource); } catch (Exception) { Console.WriteLine($" - No policy found for {operationName} operation"); } } #endregion #region API Policies try { Console.WriteLine("Getting API Policy from {0} API: ", apiName); string apiPolicies = apiExtractor.GetAPIPolicies(apimname, resourceGroup, apiName).Result; Console.WriteLine("API Policy found!"); PolicyTemplateResource apiPoliciesResource = JsonConvert.DeserializeObject <PolicyTemplateResource>(apiPolicies); apiPoliciesResource.apiVersion = "2018-06-01-preview"; apiPoliciesResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}/{apiPoliciesResource.name}')]"; apiPoliciesResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{apiName}')]" }; templateResources.Add(apiPoliciesResource); } catch (Exception) { Console.WriteLine("No API policy!"); } #endregion #region API Products try { Console.WriteLine("Getting API Products from {0} API: ", apiName); string apiProducts = apiExtractor.GetApiProducts(apimname, resourceGroup, apiName).Result; JObject oApiProducts = JObject.Parse(apiProducts); foreach (var item in oApiProducts["value"]) { string apiProductName = ((JValue)item["name"]).Value.ToString(); Console.WriteLine($" -- {apiProductName} Product found to {oApiName} API"); ProductAPITemplateResource productAPIResource = JsonConvert.DeserializeObject <ProductAPITemplateResource>(apiProducts); productAPIResource.type = ResourceTypeConstants.ProductAPI; productAPIResource.name = $"[concat(parameters('ApimServiceName'), '/{apiProductName}/{oApiName}')]"; productAPIResource.apiVersion = "2018-06-01-preview"; productAPIResource.scale = null; productAPIResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{oApiName}')]" }; templateResources.Add(productAPIResource); } } catch (Exception) { Console.WriteLine("No API products!"); } #endregion #region Diagnostics Console.WriteLine("------------------------------------------"); Console.WriteLine("Getting diagnostics from {0} API:", apiName); string diagnostics = apiExtractor.GetAPIDiagnostics(apimname, resourceGroup, apiName).Result; JObject oDiagnostics = JObject.Parse(diagnostics); foreach (var diagnostic in oDiagnostics["value"]) { string diagnosticName = ((JValue)diagnostic["name"]).Value.ToString(); Console.WriteLine("'{0}' Diagnostic found", diagnosticName); DiagnosticTemplateResource diagnosticResource = diagnostic.ToObject <DiagnosticTemplateResource>(); diagnosticResource.name = $"[concat(parameters('ApimServiceName'), '/{oApiName}/{diagnosticName}')]"; diagnosticResource.type = ResourceTypeConstants.APIDiagnostic; diagnosticResource.apiVersion = "2018-06-01-preview"; diagnosticResource.scale = null; diagnosticResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{oApiName}')]" }; if (!diagnosticName.Contains("applicationinsights")) { // enableHttpCorrelationHeaders only works for application insights, causes errors otherwise diagnosticResource.properties.enableHttpCorrelationHeaders = null; } templateResources.Add(diagnosticResource); } #endregion armTemplate.resources = templateResources.ToArray(); if (singleApiName != null) { fileWriter = new FileWriter(); fileWriter.WriteJSONToFile(armTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-" + oApiName + "-template.json"); } } // extract resources that do not fall under api. Pass in the single api name and associated resources for the single api case GenerateProductsARMTemplate(apimname, resourceGroup, fileFolder, singleApiName, templateResources); GenerateNamedValuesTemplate(resourceGroup, apimname, fileFolder, singleApiName, templateResources); GenerateLoggerTemplate(resourceGroup, apimname, fileFolder, singleApiName, templateResources); if (singleApiName == null) { fileWriter = new FileWriter(); fileWriter.WriteJSONToFile(armTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-apis-template.json"); } #endregion }