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

Commit e16b127

Browse files
authored
Parameterizing backend Urls for the apis (#413)
* Parameterizing backend Urls for the apis Parameterizing backend Urls for the apis. Key value pair like - apiName and the value will be apiUrl. This apiUrl will be mapped to the apiName which matches with the same name. * fixed the unit test fails * fixed the command option name * updated the project version to 1.0.1 * Reverted the version update changes * Fixed the code review comments Moved the logic to separate file CreatorApiBackendUrlUpdater When using the Creator cli tool in the pipeline we need to specify the backend url which will be changing for each environment, so want to pass it as an json input so that we can call different json files for eaach environment. * Updated the Readme.md file with instructions to pass the backendurls json file as an parameter
1 parent 84f71be commit e16b127

File tree

7 files changed

+80
-1
lines changed

7 files changed

+80
-1
lines changed

src/APIM_ARMTemplate/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ 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 BackendUrls as an json input file into the parameter(sample file available in the same path as below in this repository):
290+
```dotnet run create --configFile CONFIG_YAML_FILE_LOCATION --backendurlconfigFile .\apimtemplate\Creator\ExampleFiles\BackendUrlParameter\BackendUrlParameters.json```
291+
289292

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

src/APIM_ARMTemplate/apimtemplate/Commands/Create.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using Newtonsoft.Json;
7+
using apimtemplate.Creator.Utilities;
68

79
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
810
{
@@ -16,6 +18,9 @@ public CreateCommand()
1618
// list command options
1719
CommandOption configFile = this.Option("--configFile <configFile>", "Config YAML file location", CommandOptionType.SingleValue).IsRequired();
1820

21+
// list command options
22+
CommandOption backendurlconfigFile = this.Option("--backendurlconfigFile <backendurlconfigFile>", "backend url json file location", CommandOptionType.SingleValue);
23+
1924
this.HelpOption();
2025

2126
this.OnExecute(async () =>
@@ -26,6 +31,14 @@ public CreateCommand()
2631

2732
// validate creator config
2833
CreatorConfigurationValidator creatorConfigurationValidator = new CreatorConfigurationValidator(this);
34+
35+
//if backendurlfile passed as parameter
36+
if (backendurlconfigFile != null && !string.IsNullOrEmpty(backendurlconfigFile.Value()))
37+
{
38+
CreatorApiBackendUrlUpdater creatorApiBackendUrlUpdater = new CreatorApiBackendUrlUpdater();
39+
creatorConfig = creatorApiBackendUrlUpdater.UpdateBackendServiceUrl(backendurlconfigFile.Value(), creatorConfig);
40+
}
41+
2942
bool isValidCreatorConfig = creatorConfigurationValidator.ValidateCreatorConfig(creatorConfig);
3043
if (isValidCreatorConfig == true)
3144
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[{
2+
"apiName": "myAPI",
3+
"apiUrl": "http://myApiBackendUrl.com"
4+
},
5+
{
6+
"apiName": "myAPI2",
7+
"apiUrl": "http://myApiBackendUrl.com"
8+
}
9+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
4+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
5+
{
6+
public class BackendUrlsConfig
7+
{
8+
public string apiName { get; set; }
9+
public string apiUrl { get; set; }
10+
}
11+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
66
public class CLICreatorArguments
77
{
88
public string configFile { get; set; }
9+
10+
public string backendurlconfigFile { get; set; }
911
}
1012

1113
public class CreatorConfig
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
5+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create;
6+
using Newtonsoft.Json;
7+
8+
namespace apimtemplate.Creator.Utilities
9+
{
10+
public class CreatorApiBackendUrlUpdater
11+
{
12+
public CreatorConfig UpdateBackendServiceUrl(string backendServiceUrlFile, CreatorConfig creatorConfig)
13+
{
14+
// convert config file to CreatorConfig class
15+
FileReader fileReader = new FileReader();
16+
17+
string backendurlConfigContent = fileReader.RetrieveLocalFileContents(backendServiceUrlFile);
18+
19+
//if the file is json file
20+
if (fileReader.isJSON(backendurlConfigContent))
21+
{
22+
List<BackendUrlsConfig> backendUrls = JsonConvert.DeserializeObject<List<BackendUrlsConfig>>(backendurlConfigContent);
23+
24+
foreach (APIConfig aPIConfig in creatorConfig.apis)
25+
{
26+
//if the apiname matches with the one in valid yaml file
27+
BackendUrlsConfig backendUrlsConfig = backendUrls.Find(f => f.apiName == aPIConfig.name);
28+
29+
//update the backendurl as per the input json file
30+
if (backendUrlsConfig != null && !string.IsNullOrEmpty(backendUrlsConfig.apiUrl))
31+
{
32+
aPIConfig.serviceUrl = backendUrlsConfig.apiUrl;
33+
}
34+
}
35+
}
36+
37+
return creatorConfig;
38+
}
39+
}
40+
}

src/APIM_ARMTemplate/apimtemplate/apimtemplate.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -7,6 +7,7 @@
77
<LangVersion>preview</LangVersion>
88
<PackAsTool>true</PackAsTool>
99
<ToolCommandName>apim-templates</ToolCommandName>
10+
<Version>1.0.0</Version>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

0 commit comments

Comments
 (0)