public async Task <Template> Create(DeploymentDefinition creatorConfig) { var template = EmptyTemplate; template.Parameters.Add(ApiServiceNameParameter.Key, ApiServiceNameParameter.Value); var resources = new List <TemplateResource>(); foreach (LoggerDeploymentDefinition logger in creatorConfig.Loggers) { var loggerTemplateResource = new LoggerTemplateResource() { Name = $"[concat(parameters('ApimServiceName'), '/{logger.Name}')]", Properties = new LoggerProperties() { LoggerType = logger.LoggerType, Description = logger.Description, Credentials = logger.Credentials, IsBuffered = logger.IsBuffered, ResourceId = logger.ResourceId }, DependsOn = new string[] { } }; resources.Add(loggerTemplateResource); } template.Resources = resources.ToArray(); return(await Task.FromResult(template)); }
private async void GenerateLoggerTemplate(string resourceGroup, string apimname, string fileFolder) { Console.WriteLine("------------------------------------------"); Console.WriteLine("Getting loggers from service"); LoggerExtractor loggerExtractor = new LoggerExtractor(); Template armTemplate = GenerateEmptyTemplateWithParameters(); List <TemplateResource> templateResources = new List <TemplateResource>(); string loggers = loggerExtractor.GetLoggers(apimname, resourceGroup).Result; JObject oLoggers = JObject.Parse(loggers); foreach (var extractedLogger in oLoggers["value"]) { string loggerName = ((JValue)extractedLogger["name"]).Value.ToString(); Console.WriteLine("'{0}' Logger found", loggerName); string fullLoggerResource = await loggerExtractor.GetLogger(apimname, resourceGroup, loggerName); LoggerTemplateResource loggerResource = JsonConvert.DeserializeObject <LoggerTemplateResource>(fullLoggerResource); loggerResource.name = $"[concat(parameters('ApimServiceName'), '/{loggerName}')]"; loggerResource.type = "Microsoft.ApiManagement/service/loggers"; loggerResource.apiVersion = "2018-06-01-preview"; loggerResource.scale = null; templateResources.Add(loggerResource); } armTemplate.resources = templateResources.ToArray(); FileWriter fileWriter = new FileWriter(); fileWriter.WriteJSONToFile(armTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-loggers.json"); }
public Template CreateLoggerTemplate(CreatorConfig creatorConfig) { // create empty template Template loggerTemplate = this.templateBuilder.GenerateEmptyTemplate().Build(); // add parameters loggerTemplate.Parameters = new Dictionary <string, TemplateParameterProperties> { { ParameterNames.ApimServiceName, new TemplateParameterProperties() { type = "string" } } }; List <TemplateResource> resources = new List <TemplateResource>(); foreach (LoggerConfig logger in creatorConfig.loggers) { // create logger resource with properties LoggerTemplateResource loggerTemplateResource = new LoggerTemplateResource() { Name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{logger.name}')]", Type = ResourceTypeConstants.Logger, ApiVersion = GlobalConstants.ApiVersion, Properties = new LoggerTemplateProperties() { loggerType = logger.loggerType, description = logger.description, credentials = logger.credentials, isBuffered = logger.isBuffered, resourceId = logger.resourceId }, DependsOn = new string[] { } }; resources.Add(loggerTemplateResource); } loggerTemplate.Resources = resources.ToArray(); return(loggerTemplate); }
public Template CreateLoggerTemplate(CreatorConfig creatorConfig) { // create empty template Template loggerTemplate = this.templateCreator.CreateEmptyTemplate(); // add parameters loggerTemplate.parameters = new Dictionary <string, TemplateParameterProperties> { { "ApimServiceName", new TemplateParameterProperties() { type = "string" } } }; List <TemplateResource> resources = new List <TemplateResource>(); foreach (LoggerConfig logger in creatorConfig.loggers) { // create logger resource with properties LoggerTemplateResource loggerTemplateResource = new LoggerTemplateResource() { name = $"[concat(parameters('ApimServiceName'), '/{logger.name}')]", type = ResourceTypeConstants.Logger, apiVersion = "2018-06-01-preview", properties = new LoggerTemplateProperties() { loggerType = logger.loggerType, description = logger.description, credentials = logger.credentials, isBuffered = logger.isBuffered, resourceId = logger.resourceId }, dependsOn = new string[] { } }; resources.Add(loggerTemplateResource); } loggerTemplate.resources = resources.ToArray(); return(loggerTemplate); }
public void ShouldCreateLoggerTemplateFromCreatorConfig() { // arrange LoggerTemplateCreator loggerTemplateCreator = new LoggerTemplateCreator(new TemplateBuilder()); CreatorConfig creatorConfig = new CreatorConfig() { loggers = new List <LoggerConfig>() }; LoggerConfig logger = new LoggerConfig() { name = "name", LoggerType = "applicationinsights", Description = "description", IsBuffered = true, ResourceId = "resourceId", Credentials = new LoggerCredentials() { ConnectionString = "connString", InstrumentationKey = "iKey", Name = "credName" } }; creatorConfig.loggers.Add(logger); // act Template loggerTemplate = loggerTemplateCreator.CreateLoggerTemplate(creatorConfig); LoggerTemplateResource loggerTemplateResource = (LoggerTemplateResource)loggerTemplate.Resources[0]; // assert Assert.Equal($"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{logger.name}')]", loggerTemplateResource.Name); Assert.Equal(logger.LoggerType, loggerTemplateResource.Properties.LoggerType); Assert.Equal(logger.Description, loggerTemplateResource.Properties.Description); Assert.Equal(logger.IsBuffered, loggerTemplateResource.Properties.IsBuffered); Assert.Equal(logger.ResourceId, loggerTemplateResource.Properties.ResourceId); Assert.Equal(logger.Credentials.ConnectionString, loggerTemplateResource.Properties.Credentials.ConnectionString); Assert.Equal(logger.Credentials.InstrumentationKey, loggerTemplateResource.Properties.Credentials.InstrumentationKey); Assert.Equal(logger.Credentials.Name, loggerTemplateResource.Properties.Credentials.Name); }
public void ShouldCreateLoggerTemplateFromCreatorConfig() { // arrange LoggerTemplateCreator loggerTemplateCreator = new LoggerTemplateCreator(); CreatorConfig creatorConfig = new CreatorConfig() { loggers = new List <LoggerConfig>() }; LoggerConfig logger = new LoggerConfig() { name = "name", loggerType = "applicationinsights", description = "description", isBuffered = true, resourceId = "resourceId", credentials = new LoggerCredentials() { connectionString = "connString", instrumentationKey = "iKey", name = "credName" } }; creatorConfig.loggers.Add(logger); // act Template loggerTemplate = loggerTemplateCreator.CreateLoggerTemplate(creatorConfig); LoggerTemplateResource loggerTemplateResource = (LoggerTemplateResource)loggerTemplate.resources[0]; // assert Assert.Equal($"[concat(parameters('ApimServiceName'), '/{logger.name}')]", loggerTemplateResource.name); Assert.Equal(logger.loggerType, loggerTemplateResource.properties.loggerType); Assert.Equal(logger.description, loggerTemplateResource.properties.description); Assert.Equal(logger.isBuffered, loggerTemplateResource.properties.isBuffered); Assert.Equal(logger.resourceId, loggerTemplateResource.properties.resourceId); Assert.Equal(logger.credentials.connectionString, loggerTemplateResource.properties.credentials.connectionString); Assert.Equal(logger.credentials.instrumentationKey, loggerTemplateResource.properties.credentials.instrumentationKey); Assert.Equal(logger.credentials.name, loggerTemplateResource.properties.credentials.name); }
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); }
public async Task <Template> GenerateLoggerTemplateAsync(Extractor exc, string singleApiName, List <TemplateResource> apiTemplateResources, Dictionary <string, Dictionary <string, string> > apiLoggerId) { Console.WriteLine("------------------------------------------"); Console.WriteLine("Extracting loggers from service"); Template armTemplate = GenerateEmptyLoggerTemplateWithParameters(exc); // 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 GetLoggersAsync(exc.sourceApimName, exc.resourceGroup); JObject oLoggers = JObject.Parse(loggers); foreach (var extractedLogger in oLoggers["value"]) { string loggerName = ((JValue)extractedLogger["name"]).Value.ToString(); string fullLoggerResource = await GetLoggerDetailsAsync(exc.sourceApimName, exc.resourceGroup, loggerName); // convert returned logger to template resource class LoggerTemplateResource loggerResource = JsonConvert.DeserializeObject <LoggerTemplateResource>(fullLoggerResource); 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.value.Contains(loggerName)) { isReferencedInPolicy = true; } } string validApiName = ExtractorUtils.GenValidParamName(singleApiName, ParameterPrefix.Api); if (exc.paramApiLoggerId && apiLoggerId.ContainsKey(validApiName)) { Dictionary <string, string> curDiagnostic = apiLoggerId[validApiName]; string validDName = ExtractorUtils.GenValidParamName(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); }
public async Task <Template> GenerateLoggerTemplateAsync(string apimname, string resourceGroup, string singleApiName, List <TemplateResource> apiTemplateResources, string policyXMLBaseUrl, string policyXMLSasToken) { Console.WriteLine("------------------------------------------"); Console.WriteLine("Extracting loggers from service"); Template armTemplate = GenerateEmptyTemplateWithParameters(policyXMLBaseUrl, policyXMLSasToken); // isolate product api associations in the case of a single api extraction var diagnosticResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.APIDiagnostic); 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 GetLoggersAsync(apimname, resourceGroup); JObject oLoggers = JObject.Parse(loggers); foreach (var extractedLogger in oLoggers["value"]) { string loggerName = ((JValue)extractedLogger["name"]).Value.ToString(); string fullLoggerResource = await GetLoggerDetailsAsync(apimname, resourceGroup, loggerName); // convert returned logger to template resource class LoggerTemplateResource loggerResource = JsonConvert.DeserializeObject <LoggerTemplateResource>(fullLoggerResource); loggerResource.name = $"[concat(parameters('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.value.Contains(loggerName)) { isReferencedInPolicy = true; } } foreach (DiagnosticTemplateResource diagnosticTemplateResource in diagnosticResources) { if (diagnosticTemplateResource.properties.loggerId.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); }
private async void GenerateLoggerTemplate(string resourceGroup, string apimname, string fileFolder, string singleApiName, List <TemplateResource> armTemplateResources) { Console.WriteLine("------------------------------------------"); Console.WriteLine("Getting loggers from service"); LoggerExtractor loggerExtractor = new LoggerExtractor(); Template armTemplate = GenerateEmptyTemplateWithParameters(); // isolate product api associations in the case of a single api extraction var diagnosticResources = armTemplateResources.Where(resource => resource.type == ResourceTypeConstants.APIDiagnostic); var policyResources = armTemplateResources.Where(resource => (resource.type == ResourceTypeConstants.APIPolicy || resource.type == ResourceTypeConstants.APIOperationPolicy)); List <TemplateResource> templateResources = new List <TemplateResource>(); string loggers = loggerExtractor.GetLoggers(apimname, resourceGroup).Result; JObject oLoggers = JObject.Parse(loggers); foreach (var extractedLogger in oLoggers["value"]) { string loggerName = ((JValue)extractedLogger["name"]).Value.ToString(); string fullLoggerResource = await loggerExtractor.GetLogger(apimname, resourceGroup, loggerName); LoggerTemplateResource loggerResource = JsonConvert.DeserializeObject <LoggerTemplateResource>(fullLoggerResource); loggerResource.name = $"[concat(parameters('ApimServiceName'), '/{loggerName}')]"; loggerResource.type = ResourceTypeConstants.Logger; loggerResource.apiVersion = "2018-06-01-preview"; 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; } } foreach (DiagnosticTemplateResource diagnosticTemplateResource in diagnosticResources) { if (diagnosticTemplateResource.properties.loggerId.Contains(loggerName)) { isReferencedInPolicy = 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(); FileWriter fileWriter = new FileWriter(); fileWriter.WriteJSONToFile(armTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-loggers.json"); }
private async void GenerateLoggerTemplate(string resourceGroup, string apimname, string fileFolder) { Console.WriteLine("------------------------------------------"); Console.WriteLine("Geting loggers from service"); LoggerExtractor loggerExtractor = new LoggerExtractor(); PropertyExtractor propertyExtractor = new PropertyExtractor(); Template armTemplate = new Template() { schema = "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", contentVersion = "1.0.0.0", parameters = new Dictionary <string, TemplateParameterProperties> { { "ApimServiceName", new TemplateParameterProperties() { type = "string" } } }, variables = { }, resources = { }, outputs = { } }; List <TemplateResource> templateResources = new List <TemplateResource>(); // pull named values for later credential reference string properties = propertyExtractor.GetProperties(apimname, resourceGroup).Result; JObject oProperties = JObject.Parse(properties); List <PropertyTemplateResource> propertyResources = oProperties["value"].ToObject <List <PropertyTemplateResource> >(); string loggers = loggerExtractor.GetLoggers(apimname, resourceGroup).Result; JObject oLoggers = JObject.Parse(loggers); foreach (var extractedLogger in oLoggers["value"]) { string loggerName = ((JValue)extractedLogger["name"]).Value.ToString(); Console.WriteLine("'{0}' Logger found", loggerName); string fullLoggerResource = await loggerExtractor.GetLogger(apimname, resourceGroup, loggerName); LoggerTemplateResource loggerResource = JsonConvert.DeserializeObject <LoggerTemplateResource>(fullLoggerResource); loggerResource.name = $"[concat(parameters('ApimServiceName'), '/{loggerName}')]"; loggerResource.type = "Microsoft.ApiManagement/service/loggers"; loggerResource.apiVersion = "2018-06-01-preview"; loggerResource.scale = null; // swap credentials for their hidden values, taken from named values if (loggerResource.properties.credentials != null) { if (loggerResource.properties.credentials.instrumentationKey != null) { string hiddenKey = loggerResource.properties.credentials.instrumentationKey.Substring(2, loggerResource.properties.credentials.instrumentationKey.Length - 4); loggerResource.properties.credentials.instrumentationKey = propertyResources.Find(p => p.properties.displayName == hiddenKey).properties.value; } else if (loggerResource.properties.credentials.connectionString != null) { string hiddenKey = loggerResource.properties.credentials.connectionString.Substring(2, loggerResource.properties.credentials.connectionString.Length - 4); loggerResource.properties.credentials.connectionString = propertyResources.Find(p => p.properties.displayName == hiddenKey).properties.value; } } templateResources.Add(loggerResource); } armTemplate.resources = templateResources.ToArray(); FileWriter fileWriter = new FileWriter(); string filePath = fileFolder + Path.DirectorySeparatorChar + string.Format("loggers", "/", "-") + ".json"; fileWriter.WriteJSONToFile(armTemplate, filePath); }