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

Allow backend.proxy parameterization #697

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public class ExtractorConsoleAppConfiguration
[Option(longName: "paramBackend", HelpText = "Parameterize environment specific values from backend")]
public string ParamBackend { get; set; }

[Option(longName: "paramBackendProxy", HelpText = "Parameterize backend proxy section")]
public string ParameterizeBackendProxySection { get; set; }

[Option(longName: "extractGateways", HelpText = "Attempt to extract information about Self-Hosted gateways")]
public string ExtractGateways { get; set; }
}
Expand Down
6 changes: 6 additions & 0 deletions src/ArmTemplates/Common/Constants/GlobalConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public static class ParameterNames
public const string ApimServiceName = "apimServiceName";
public const string LinkedTemplatesBaseUrl = "linkedTemplatesBaseUrl";
public const string NamedValueKeyVaultSecrets = "namedValueKeyVaultSecrets";

#region Backend

public const string BackendSettings = "backendSettings";
public const string BackendProxy = "backendProxy";

#endregion
}

public static class ParameterPrefix
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// --------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// --------------------------------------------------------------------------

using Newtonsoft.Json;

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions
{
public class TemplateParameterProperties<TParameter> : TemplateParameterProperties
{
public new TParameter[] AllowedValues { get; set; }

[JsonProperty("defaultValue")]
public new TParameter Value { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates
public class TemplateParameterProperties
{
public string Type { get; set; }

public TemplateParameterMetadata Metadata { get; set; }

public string[] AllowedValues { get; set; }
public string DefaultValue { get; set; }

public string Value { get; set; }

public TemplateParameterProperties()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// --------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// --------------------------------------------------------------------------

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend.Parameters
{
class BackendProxySectionParameter
{
public string Url { get; set; }

public string Username { get; set; }

public string Password { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public interface ITemplateBuilder
public Template<TTemplateResources> Build<TTemplateResources>()
where TTemplateResources : ITemplateResources, new();

/// <summary>
/// Builds generic template using pre-defined template resources
/// </summary>
/// <typeparam name="TTemplateResources">type of resources of template, which has parameterless constructor</typeparam>
public Template<TTemplateResources> Build<TTemplateResources>(TTemplateResources templateResources)
where TTemplateResources : ITemplateResources, new();

TemplateBuilder GenerateTemplateWithApimServiceNameProperty();

TemplateBuilder GenerateTemplateWithPresetProperties(ExtractorParameters extractorParameters);
Expand All @@ -36,7 +43,7 @@ public Template<TTemplateResources> Build<TTemplateResources>()

TemplateBuilder AddParameterizeApiLoggerIdProperty(ExtractorParameters extractorParameters);

TemplateBuilder AddParameterizeBackendProperty(ExtractorParameters extractorParameters);
TemplateBuilder AddParameterizeBackendSettings(ExtractorParameters extractorParameters);

TemplateBuilder AddParameterizeLogResourceIdProperty(ExtractorParameters extractorParameters);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// --------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// --------------------------------------------------------------------------

using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend.Parameters;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders
{
public partial class TemplateBuilder : ITemplateBuilder
{
public TemplateBuilder AddParameterizeBackendSettings(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParameterizeBackend)
{
TemplateParameterProperties extractBackendParametersProperties = new TemplateParameterProperties()
{
Type = "object"
};
this.template.Parameters.Add(ParameterNames.BackendSettings, extractBackendParametersProperties);
}

return this;
}

public TemplateBuilder AddParameterizeBackendProxySection(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParameterizeBackendProxySection)
{
var backendProxySectionParameterObject = new TemplateParameterProperties<BackendProxySectionParameter>
{
Type = "object",
Metadata = new() { Description = "proxy settings for backend resource (view https://docs.microsoft.com/en-us/rest/api/apimanagement/current-ga/backend/create-or-update)" },
Value = new BackendProxySectionParameter
{
Url = "preset-proxy-url-value",
Username = "preset-proxy-username-value",
Password = "preset-proxy-password-value"
}
};

this.template.Parameters.Add(ParameterNames.BackendProxy, backendProxySectionParameterObject);
}

return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// --------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// --------------------------------------------------------------------------

using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders
{
public partial class TemplateBuilder : ITemplateBuilder
{
public TemplateBuilder AddParameterizeLogResourceIdProperty(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParameterizeLogResourceId)
{
TemplateParameterProperties loggerResourceIdParameterProperties = new TemplateParameterProperties()
{
Type = "object"
};
this.template.Parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdParameterProperties);
}

return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// --------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// --------------------------------------------------------------------------

using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders
{
public partial class TemplateBuilder : ITemplateBuilder
{
public TemplateBuilder AddParameterizeNamedValueParameters(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParameterizeNamedValue)
{
TemplateParameterProperties namedValueParameterProperties = new TemplateParameterProperties()
{
Type = "object"
};
this.template.Parameters.Add(ParameterNames.NamedValues, namedValueParameterProperties);
}

return this;
}

public TemplateBuilder AddParameterizeNamedValuesKeyVaultSecretParameters(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParamNamedValuesKeyVaultSecrets)
{
TemplateParameterProperties keyVaultNamedValueParameterProperties = new TemplateParameterProperties()
{
Type = "object"
};
this.template.Parameters.Add(ParameterNames.NamedValueKeyVaultSecrets, keyVaultNamedValueParameterProperties);
}

return this;
}
}
}
67 changes: 9 additions & 58 deletions src/ArmTemplates/Common/Templates/Builders/TemplateBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders
{
public class TemplateBuilder : ITemplateBuilder
public partial class TemplateBuilder : ITemplateBuilder
{
Template template;

Expand All @@ -21,6 +21,13 @@ public class TemplateBuilder : ITemplateBuilder
/// <inheritdoc/>
public Template<TTemplateResources> Build<TTemplateResources>()
where TTemplateResources : ITemplateResources, new()
{
return this.Build<TTemplateResources>(new());
}

/// <inheritdoc/>
public Template<TTemplateResources> Build<TTemplateResources>(TTemplateResources templateResources)
where TTemplateResources : ITemplateResources, new()
{
// left Template.NotGeneric variant for backward compatibility
// for refactored code please use Template.Generic and use explicit TTemplateResources type
Expand All @@ -30,7 +37,7 @@ public Template<TTemplateResources> Build<TTemplateResources>()
genericTemplate.ContentVersion = this.template.ContentVersion;
genericTemplate.Parameters = this.template.Parameters;
genericTemplate.Variables = this.template.Variables;
genericTemplate.TypedResources = new TTemplateResources(); // creating empty resources
genericTemplate.TypedResources = templateResources;
genericTemplate.Outputs = this.template.Outputs;

return genericTemplate;
Expand Down Expand Up @@ -122,61 +129,5 @@ public TemplateBuilder AddParameterizeApiLoggerIdProperty(ExtractorParameters ex

return this;
}

public TemplateBuilder AddParameterizeNamedValueParameters(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParameterizeNamedValue)
{
TemplateParameterProperties namedValueParameterProperties = new TemplateParameterProperties()
{
Type = "object"
};
this.template.Parameters.Add(ParameterNames.NamedValues, namedValueParameterProperties);
}

return this;
}

public TemplateBuilder AddParameterizeNamedValuesKeyVaultSecretParameters(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParamNamedValuesKeyVaultSecrets)
{
TemplateParameterProperties keyVaultNamedValueParameterProperties = new TemplateParameterProperties()
{
Type = "object"
};
this.template.Parameters.Add(ParameterNames.NamedValueKeyVaultSecrets, keyVaultNamedValueParameterProperties);
}

return this;
}

public TemplateBuilder AddParameterizeLogResourceIdProperty(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParameterizeLogResourceId)
{
TemplateParameterProperties loggerResourceIdParameterProperties = new TemplateParameterProperties()
{
Type = "object"
};
this.template.Parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdParameterProperties);
}

return this;
}

public TemplateBuilder AddParameterizeBackendProperty(ExtractorParameters extractorParameters)
{
if (extractorParameters.ParameterizeBackend)
{
TemplateParameterProperties extractBackendParametersProperties = new TemplateParameterProperties()
{
Type = "object"
};
this.template.Parameters.Add(ParameterNames.BackendSettings, extractBackendParametersProperties);
}

return this;
}
}
}
14 changes: 13 additions & 1 deletion src/ArmTemplates/Extractor/EntityExtractors/BackendExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public async Task<Template<BackendTemplateResources>> GenerateBackendsTemplateAs
{
var backendTemplate = this.templateBuilder
.GenerateTemplateWithApimServiceNameProperty()
.AddParameterizeBackendProperty(extractorParameters)
.AddParameterizeBackendSettings(extractorParameters)
.AddParameterizeBackendProxySection(extractorParameters)
.Build<BackendTemplateResources>();

var backends = await this.backendClient.GetAllAsync(extractorParameters);
Expand All @@ -72,6 +73,17 @@ public async Task<Template<BackendTemplateResources>> GenerateBackendsTemplateAs
backendResource.Type = ResourceTypeConstants.Backend;
backendResource.ApiVersion = GlobalConstants.ApiVersion;

// set backend proxy section values according to parameter
if (extractorParameters.ParameterizeBackendProxySection)
{
backendResource.Properties.Proxy = new BackendProxy
{
Url = $"[parameters('{ParameterNames.BackendProxy}').url]",
Username = $"[parameters('{ParameterNames.BackendProxy}').username]",
Password = $"[parameters('{ParameterNames.BackendProxy}').password]"
};
}

if (string.IsNullOrEmpty(singleApiName))
{
// if the user is extracting all apis, extract all the backends
Expand Down
4 changes: 4 additions & 0 deletions src/ArmTemplates/Extractor/Models/ExtractorParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public record ExtractorParameters

public bool ParameterizeBackend { get; private set; }

public bool ParameterizeBackendProxySection { get; private set; }

public bool ExtractGateways { get; set; }

public ExtractorParameters(ExtractorConsoleAppConfiguration extractorConfig)
Expand All @@ -104,6 +106,7 @@ public ExtractorParameters(ExtractorConsoleAppConfiguration extractorConfig)
this.OperationBatchSize = extractorConfig.OperationBatchSize ?? default;
this.ParamNamedValuesKeyVaultSecrets = extractorConfig.ParamNamedValuesKeyVaultSecrets != null && extractorConfig.ParamNamedValuesKeyVaultSecrets.Equals("true", StringComparison.OrdinalIgnoreCase);
this.ParameterizeBackend = extractorConfig.ParamBackend != null && extractorConfig.ParamBackend.Equals("true", StringComparison.OrdinalIgnoreCase);
this.ParameterizeBackendProxySection = extractorConfig.ParameterizeBackendProxySection != null && extractorConfig.ParameterizeBackendProxySection.Equals("true", StringComparison.OrdinalIgnoreCase);
this.SplitApis = !string.IsNullOrEmpty(extractorConfig.SplitAPIs) && extractorConfig.SplitAPIs.Equals("true", StringComparison.OrdinalIgnoreCase);
this.IncludeAllRevisions = !string.IsNullOrEmpty(extractorConfig.IncludeAllRevisions) && extractorConfig.IncludeAllRevisions.Equals("true", StringComparison.OrdinalIgnoreCase);
this.FileNames = this.GenerateFileNames(extractorConfig.BaseFileName, extractorConfig.SourceApimName);
Expand Down Expand Up @@ -138,6 +141,7 @@ public ExtractorParameters OverrideConfiguration(ExtractorConsoleAppConfiguratio
this.NotIncludeNamedValue = !string.IsNullOrEmpty(overridingConfig.NotIncludeNamedValue) ? overridingConfig.NotIncludeNamedValue.Equals("true", StringComparison.OrdinalIgnoreCase) : this.NotIncludeNamedValue;
this.ParamNamedValuesKeyVaultSecrets = !string.IsNullOrEmpty(overridingConfig.ParamNamedValuesKeyVaultSecrets) ? overridingConfig.ParamNamedValuesKeyVaultSecrets.Equals("true", StringComparison.OrdinalIgnoreCase) : this.ParamNamedValuesKeyVaultSecrets;
this.ParameterizeBackend = !string.IsNullOrEmpty(overridingConfig.ParamBackend) ? overridingConfig.ParamBackend.Equals("true", StringComparison.OrdinalIgnoreCase) : this.ParameterizeBackend;
this.ParameterizeBackendProxySection = !string.IsNullOrEmpty(overridingConfig.ParameterizeBackendProxySection) ? overridingConfig.ParameterizeBackendProxySection.Equals("true", StringComparison.OrdinalIgnoreCase) : this.ParameterizeBackendProxySection;
this.SplitApis = !string.IsNullOrEmpty(overridingConfig.SplitAPIs) ? overridingConfig.SplitAPIs.Equals("true", StringComparison.OrdinalIgnoreCase) : this.SplitApis;
this.IncludeAllRevisions = !string.IsNullOrEmpty(overridingConfig.IncludeAllRevisions) ? overridingConfig.IncludeAllRevisions.Equals("true", StringComparison.OrdinalIgnoreCase) : this.IncludeAllRevisions;
this.ExtractGateways = !string.IsNullOrEmpty(overridingConfig.ExtractGateways) ? overridingConfig.ExtractGateways.Equals("true", StringComparison.OrdinalIgnoreCase) : this.ExtractGateways;
Expand Down
3 changes: 2 additions & 1 deletion src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ You have two choices when specifying your settings:
| serviceBaseUrl | No | Specify the base url where you want to run your extractor |
| notIncludeNamedValue | No | Set to "true" will not generate Named Value Templates|
| paramNamedValuesKeyVaultSecrets | No | Set to true will parameterize all named values where the value is from a key vault secret |
| paramBackend | No | Set to true will parameterize sepcific backend values (limited to resourceId, url and protocol) |
| paramBackend | No | Set to true will parameterize specific backend values (limited to resourceId, url and protocol) |
| paramBackendProxy | No | Set to true will include `backendProxy` parameter in the output configuration file. Inner properties specification will set the same `proxy` settings to each backend resource |
| extractGateways | No | Set to true will attempt to extract the Self Hosted Gateways. |

#### Note
Expand Down
Loading