Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit ca3a68a

Browse files
authored
created command options to pass appinsights name and instrumentation key as an parameter in cli (#415)
1 parent e16b127 commit ca3a68a

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/APIM_ARMTemplate/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,11 @@ Below are the steps to run the Creator from the source code:
286286
- Navigate to {path_to_folder}/src/APIM_ARMTemplate/apimtemplate directory
287287
- Run the following command:
288288
```dotnet run create --configFile CONFIG_YAML_FILE_LOCATION ```
289+
- Run the following command to pass AppinsightsName and Appinsights InstrumentationKey as an parameter:
290+
```dotnet run create --configFile CONFIG_YAML_FILE_LOCATION --appInsightsInstrumentationKey 45d4v88-fdfs-4b35-9232-731d82d4d1c6 --appInsightsName myAppInsights ```
289291
- Run the following command to pass BackendUrls as an json input file into the parameter(sample file available in the same path as below in this repository):
290292
```dotnet run create --configFile CONFIG_YAML_FILE_LOCATION --backendurlconfigFile .\apimtemplate\Creator\ExampleFiles\BackendUrlParameter\BackendUrlParameters.json```
291293

292-
293294
You can also run it directly from the [releases](https://github.com/Azure/azure-api-management-devops-resource-kit/releases).
294295

295296
Additionaly, the Creator can also be made available as a global [dotnet CLI tool](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools) in your Azure DevOps artifacts or private NuGet repository. Build the Creator, and run the following commands to package the Creator as a dotnet tool:

src/APIM_ARMTemplate/apimtemplate/Commands/Create.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ public CreateCommand()
1818
// list command options
1919
CommandOption configFile = this.Option("--configFile <configFile>", "Config YAML file location", CommandOptionType.SingleValue).IsRequired();
2020

21+
CommandOption appInsightsInstrumentationKey = this.Option("--appInsightsInstrumentationKey <appInsightsInstrumentationKey>", "AppInsights intrumentationkey", CommandOptionType.SingleValue);
22+
23+
CommandOption appInsightsName = this.Option("--appInsightsName <appInsightsName>", "AppInsights Name", CommandOptionType.SingleValue);
24+
2125
// list command options
2226
CommandOption backendurlconfigFile = this.Option("--backendurlconfigFile <backendurlconfigFile>", "backend url json file location", CommandOptionType.SingleValue);
2327

28+
2429
this.HelpOption();
2530

2631
this.OnExecute(async () =>
@@ -29,6 +34,9 @@ public CreateCommand()
2934
FileReader fileReader = new FileReader();
3035
CreatorConfig creatorConfig = await fileReader.ConvertConfigYAMLToCreatorConfigAsync(configFile.Value());
3136

37+
AppInsightsUpdater appInsightsUpdater = new AppInsightsUpdater();
38+
appInsightsUpdater.UpdateAppInsightNameAndInstrumentationKey(creatorConfig, appInsightsInstrumentationKey, appInsightsName);
39+
3240
// validate creator config
3341
CreatorConfigurationValidator creatorConfigurationValidator = new CreatorConfigurationValidator(this);
3442

src/APIM_ARMTemplate/apimtemplate/Creator/Models/CreatorConfiguration.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
55
{
66
public class CLICreatorArguments
77
{
8+
public string appInsightsInstrumentationKey { get; set; }
9+
public string appInsightsName { get; set; }
810
public string configFile { get; set; }
911

1012
public string backendurlconfigFile { get; set; }
@@ -97,5 +99,5 @@ public class PropertyConfig : PropertyResourceProperties
9799
{
98100

99101
}
100-
102+
101103
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using McMaster.Extensions.CommandLineUtils;
5+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create;
6+
7+
namespace apimtemplate.Creator.Utilities
8+
{
9+
public class AppInsightsUpdater
10+
{
11+
public void UpdateAppInsightNameAndInstrumentationKey(
12+
CreatorConfig creatorConfig, CommandOption appInsightsInstrumentationKey, CommandOption appInsightsName)
13+
{
14+
string appInsightNamePassed = string.Empty;
15+
16+
if(appInsightsName != null && !string.IsNullOrEmpty(appInsightsName.Value()))
17+
{
18+
appInsightNamePassed = appInsightsName.Value();
19+
foreach (APIConfig aPIConfig in creatorConfig.apis)
20+
{
21+
if(aPIConfig.diagnostic != null)
22+
{
23+
aPIConfig.diagnostic.loggerId = appInsightNamePassed;
24+
}
25+
}
26+
}
27+
28+
if (appInsightsInstrumentationKey != null && !string.IsNullOrEmpty(appInsightsInstrumentationKey.Value()))
29+
{
30+
string appInsightsInstrumentationKeyPassed = appInsightsInstrumentationKey.Value();
31+
if(creatorConfig.loggers != null && creatorConfig.loggers.Count > 0)
32+
{
33+
if (!string.IsNullOrEmpty(appInsightNamePassed))
34+
{
35+
creatorConfig.loggers[0].name = appInsightNamePassed;
36+
}
37+
if(creatorConfig.loggers[0].credentials != null)
38+
{
39+
creatorConfig.loggers[0].credentials.instrumentationKey = appInsightsInstrumentationKeyPassed;
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)