Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to be able to customize env name #41947

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
13 changes: 12 additions & 1 deletion sdk/provisioning/Azure.Provisioning/src/Construct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public abstract class Construct : IConstruct, IPersistableModel<Construct>
private List<IConstruct> _constructs;
private List<Output> _outputs;
private List<Resource> _existingResources;
private string _environmentName;

/// <inheritdoc/>
public string Name { get; }
/// <inheritdoc/>
public string EnvironmentName => GetEnvironmentName();
/// <inheritdoc/>
public IConstruct? Scope { get; }
/// <inheritdoc/>
public ResourceGroup? ResourceGroup { get; protected set; }
Expand All @@ -44,8 +47,9 @@ public abstract class Construct : IConstruct, IPersistableModel<Construct>
/// <param name="constructScope">The <see cref="ConstructScope"/> the construct is.</param>
/// <param name="tenantId">The tenant id to use. If not passed in will try to load from AZURE_TENANT_ID environment variable.</param>
/// <param name="subscriptionId">The subscription id to use. If not passed in will try to load from AZURE_SUBSCRIPTION_ID environment variable.</param>
/// <param name="envName">The environment name to use. If not passed in will try to load from AZURE_ENV_NAME environment variable.</param>
/// <exception cref="ArgumentException"><paramref name="constructScope"/> is <see cref="ConstructScope.ResourceGroup"/> and <paramref name="scope"/> is null.</exception>
protected Construct(IConstruct? scope, string name, ConstructScope constructScope = ConstructScope.ResourceGroup, Guid? tenantId = null, Guid? subscriptionId = null)
protected Construct(IConstruct? scope, string name, ConstructScope constructScope = ConstructScope.ResourceGroup, Guid? tenantId = null, Guid? subscriptionId = null, string? envName = null)
{
if (scope is null && constructScope == ConstructScope.ResourceGroup)
{
Expand All @@ -70,6 +74,13 @@ protected Construct(IConstruct? scope, string name, ConstructScope constructScop
{
Subscription = scope is null ? this.GetOrCreateSubscription(subscriptionId) : scope.Subscription ?? scope.GetOrCreateSubscription(subscriptionId);
}

_environmentName = envName ?? Environment.GetEnvironmentVariable("AZURE_ENV_NAME") ?? throw new Exception("No environment variable found named 'AZURE_ENV_NAME'");
}

private string GetEnvironmentName()
{
return _environmentName is null ? Scope!.EnvironmentName : _environmentName;
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions sdk/provisioning/Azure.Provisioning/src/IConstruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public interface IConstruct
/// </summary>
public string Name { get; }

/// <summary>
/// Gets the environment name of the construct.
/// </summary>
public string EnvironmentName { get; }

/// <summary>
/// Gets the <see cref="Provisioning.ConstructScope"/> scope.
/// </summary>
Expand Down
7 changes: 3 additions & 4 deletions sdk/provisioning/Azure.Provisioning/src/Infrastructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ namespace Azure.Provisioning
public abstract class Infrastructure : Construct
#pragma warning restore AZC0012 // Avoid single word type names
{
internal static readonly string Seed = Environment.GetEnvironmentVariable("AZURE_ENV_NAME") ?? throw new Exception("No environment variable found named 'AZURE_ENV_NAME'");

/// <summary>
/// Initializes a new instance of the <see cref="Infrastructure"/> class.
/// </summary>
/// <param name="constructScope">The <see cref="ConstructScope"/> to use for the root <see cref="IConstruct"/>.</param>
/// <param name="tenantId">The tenant id to use. If not passed in will try to load from AZURE_TENANT_ID environment variable.</param>
/// <param name="subscriptionId">The subscription id to use. If not passed in will try to load from AZURE_SUBSCRIPTION_ID environment variable.</param>
public Infrastructure(ConstructScope constructScope = ConstructScope.Subscription, Guid? tenantId = null, Guid? subscriptionId = null)
: base(null, "default", constructScope, tenantId, subscriptionId)
/// <param name="envName">The environment name to use. If not passed in will try to load from AZURE_ENV_NAME environment variable.</param>
public Infrastructure(ConstructScope constructScope = ConstructScope.Subscription, Guid? tenantId = null, Guid? subscriptionId = null, string? envName = null)
: base(null, "default", constructScope, tenantId, subscriptionId, envName)
{
}

Expand Down
8 changes: 4 additions & 4 deletions sdk/provisioning/Azure.Provisioning/src/keyvault/KeyVault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class KeyVault : Resource<KeyVaultData>
/// <param name="version">The version.</param>
/// <param name="location">The location.</param>
public KeyVault(IConstruct scope, string? name = null, string version = "2023-02-01", AzureLocation? location = default)
: base(scope, null, GetName(name), ResourceTypeName, version, ArmKeyVaultModelFactory.KeyVaultData(
name: GetName(name),
: base(scope, null, GetName(scope, name), ResourceTypeName, version, ArmKeyVaultModelFactory.KeyVaultData(
name: GetName(scope, name),
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
properties: ArmKeyVaultModelFactory.KeyVaultProperties(
Expand All @@ -46,9 +46,9 @@ public KeyVault(IConstruct scope, string? name = null, string version = "2023-02
{
}

private static string GetName(string? name)
private static string GetName(IConstruct scope, string? name)
{
return name is null ? $"kv-{Infrastructure.Seed}" : $"{name}-{Infrastructure.Seed}";
return name is null ? $"kv-{scope.EnvironmentName}" : $"{name}-{scope.EnvironmentName}";
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class KeyVaultSecret : Resource<KeyVaultSecretData>
/// <param name="name">The name.</param>
/// <param name="version">The version.</param>
public KeyVaultSecret(IConstruct scope, string name, string version = "2023-02-01")
: base(scope, null, GetName(name), ResourceTypeName, version, ArmKeyVaultModelFactory.KeyVaultSecretData(
name: GetName(name),
: base(scope, null, GetName(scope, name), ResourceTypeName, version, ArmKeyVaultModelFactory.KeyVaultSecretData(
name: GetName(scope, name),
resourceType: ResourceTypeName,
properties: ArmKeyVaultModelFactory.SecretProperties(
value: Guid.Empty.ToString())
Expand All @@ -39,16 +39,16 @@ public KeyVaultSecret(IConstruct scope, string name, string version = "2023-02-0
/// <param name="connectionString">The connection string.</param>
/// <param name="version">The version.</param>
public KeyVaultSecret(IConstruct scope, string name, ConnectionString connectionString, string version = "2023-02-01")
: base(scope, null, GetName(name), ResourceTypeName, version, ArmKeyVaultModelFactory.KeyVaultSecretData(
name: GetName(name),
: base(scope, null, GetName(scope, name), ResourceTypeName, version, ArmKeyVaultModelFactory.KeyVaultSecretData(
name: GetName(scope, name),
resourceType: ResourceTypeName,
properties: ArmKeyVaultModelFactory.SecretProperties(
value: connectionString.Value)
))
{
}

private static string GetName(string? name) => name is null ? $"kvs-{Infrastructure.Seed}" : name;
private static string GetName(IConstruct scope, string? name) => name is null ? $"kvs-{scope.EnvironmentName}" : name;

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class ResourceGroup : Resource<ResourceGroupData>
/// <param name="version">The version of the resourceGroup.</param>
/// <param name="location">The location of the resourceGroup.</param>
public ResourceGroup(IConstruct scope, string? name = default, string version = "2023-07-01", AzureLocation? location = default)
: base(scope, null, GetName(name), ResourceType, version, ResourceManagerModelFactory.ResourceGroupData(
name: GetName(name),
: base(scope, null, GetName(scope, name), ResourceType, version, ResourceManagerModelFactory.ResourceGroupData(
name: GetName(scope, name),
resourceType: ResourceType,
tags: new Dictionary<string, string> { { "azd-env-name", Environment.GetEnvironmentVariable("AZURE_ENV_NAME") ?? throw new Exception("No environment variable 'AZURE_ENV_NAME' was found") } },
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS))
{
}

private static string GetName(string? name) => name is null ? $"rg-{Infrastructure.Seed}" : $"{name}-{Infrastructure.Seed}";
private static string GetName(IConstruct scope, string? name) => name is null ? $"rg-{scope.EnvironmentName}" : $"{name}-{scope.EnvironmentName}";

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class DeploymentScript : Resource<AzureCliScript>
/// <param name="version">The resource version.</param>
/// <param name="location">The resource location.</param>
public DeploymentScript(IConstruct scope, string resourceName, IEnumerable<ScriptEnvironmentVariable> scriptEnvironmentVariables, string scriptContent, string version = _defaultVersion, AzureLocation? location = default)
: base(scope, null, GetName(resourceName), ResourceTypeName, version, ArmResourcesModelFactory.AzureCliScript(
name: GetName(resourceName),
: base(scope, null, GetName(scope, resourceName), ResourceTypeName, version, ArmResourcesModelFactory.AzureCliScript(
name: GetName(scope, resourceName),
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
azCliVersion: "2.37.0",
Expand All @@ -51,8 +51,8 @@ public DeploymentScript(IConstruct scope, string resourceName, IEnumerable<Scrip
/// <param name="version">The resource version.</param>
/// <param name="location">The resource location.</param>
public DeploymentScript(IConstruct scope, string resourceName, Resource database, Parameter appUserPasswordSecret, Parameter sqlAdminPasswordSecret, string version = _defaultVersion, AzureLocation? location = default)
: base(scope, null, GetName(resourceName), ResourceTypeName, version, ArmResourcesModelFactory.AzureCliScript(
name: GetName(resourceName),
: base(scope, null, GetName(scope, resourceName), ResourceTypeName, version, ArmResourcesModelFactory.AzureCliScript(
name: GetName(scope, resourceName),
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
azCliVersion: "2.37.0",
Expand Down Expand Up @@ -88,7 +88,7 @@ alter role db_owner add member ${APPUSERNAME}
Scope.AddParameter(sqlAdminPasswordSecret);
}

private static string GetName(string? name) => name is null ? $"deploymentScript-{Infrastructure.Seed}" : $"{name}-{Infrastructure.Seed}";
private static string GetName(IConstruct scope, string? name) => name is null ? $"deploymentScript-{scope.EnvironmentName}" : $"{name}-{scope.EnvironmentName}";

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class SqlDatabase : Resource<SqlDatabaseData>
/// <param name="version">The version.</param>
/// <param name="location">The location.</param>
public SqlDatabase(IConstruct scope, string? name = default, string version = "2022-08-01-preview", AzureLocation? location = default)
: base(scope, null, GetName(name), ResourceTypeName, version, ArmSqlModelFactory.SqlDatabaseData(
name: GetName(name),
: base(scope, null, GetName(scope, name), ResourceTypeName, version, ArmSqlModelFactory.SqlDatabaseData(
name: GetName(scope, name),
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS))
{
Expand All @@ -39,7 +39,7 @@ public SqlDatabase(IConstruct scope, string? name = default, string version = "2
public ConnectionString GetConnectionString(Parameter passwordSecret, string userName = "appUser")
=> new ConnectionString(this, passwordSecret, userName);

private static string GetName(string? name) => name is null ? $"db-{Infrastructure.Seed}" : name;
private static string GetName(IConstruct scope, string? name) => name is null ? $"db-{scope.EnvironmentName}" : name;

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public class SqlFirewallRule : Resource<SqlFirewallRuleData>
/// <param name="name">The name.</param>
/// <param name="version">The version.</param>
public SqlFirewallRule(IConstruct scope, string? name = default, string version = "2020-11-01-preview")
: base(scope, null, GetName(name), ResourceTypeName, version, ArmSqlModelFactory.SqlFirewallRuleData(
name: GetName(name),
: base(scope, null, GetName(scope, name), ResourceTypeName, version, ArmSqlModelFactory.SqlFirewallRuleData(
name: GetName(scope, name),
resourceType: ResourceTypeName,
startIPAddress: "0.0.0.1",
endIPAddress: "255.255.255.254"
))
{
}

private static string GetName(string? name) => name is null ? $"fw-{Infrastructure.Seed}" : $"{name}-{Infrastructure.Seed}";
private static string GetName(IConstruct scope, string? name) => name is null ? $"fw-{scope.EnvironmentName}" : $"{name}-{scope.EnvironmentName}";

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class SqlServer : Resource<SqlServerData>
/// <param name="version">The version.</param>
/// <param name="location">The location.</param>
public SqlServer(IConstruct scope, string name, string? version = default, AzureLocation? location = default)
: base(scope, null, GetName(name), ResourceTypeName, version ?? "2022-08-01-preview", ArmSqlModelFactory.SqlServerData(
name: GetName(name),
: base(scope, null, GetName(scope, name), ResourceTypeName, version ?? "2022-08-01-preview", ArmSqlModelFactory.SqlServerData(
name: GetName(scope, name),
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
resourceType: ResourceTypeName,
version: "12.0",
Expand All @@ -36,7 +36,7 @@ public SqlServer(IConstruct scope, string name, string? version = default, Azure
{
}

private static string GetName(string? name) => name is null ? $"sql-{Infrastructure.Seed}" : $"{name}-{Infrastructure.Seed}";
private static string GetName(IConstruct scope, string? name) => name is null ? $"sql-{scope.EnvironmentName}" : $"{name}-{scope.EnvironmentName}";

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AppServicePlan : Resource<AppServicePlanData>
{
private const string ResourceTypeName = "Microsoft.Web/serverfarms";

private static string GetName(string? name) => name is null ? $"appServicePlan-{Infrastructure.Seed}" : $"{name}-{Infrastructure.Seed}";
private static string GetName(IConstruct scope, string? name) => name is null ? $"appServicePlan-{scope.EnvironmentName}" : $"{name}-{scope.EnvironmentName}";

/// <summary>
/// Initializes a new instance of the <see cref="AppServicePlan"/>.
Expand All @@ -26,8 +26,8 @@ public class AppServicePlan : Resource<AppServicePlanData>
/// <param name="version">The version.</param>
/// <param name="location">The location.</param>
public AppServicePlan(IConstruct scope, string resourceName, string version = "2021-02-01", AzureLocation? location = default)
: base(scope, null, GetName(resourceName), ResourceTypeName, version, ArmAppServiceModelFactory.AppServicePlanData(
name: GetName(resourceName),
: base(scope, null, GetName(scope, resourceName), ResourceTypeName, version, ArmAppServiceModelFactory.AppServicePlanData(
name: GetName(scope, resourceName),
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
sku: new AppServiceSkuDescription() { Name = "B1" },
isReserved: true))
Expand Down
6 changes: 3 additions & 3 deletions sdk/provisioning/Azure.Provisioning/src/websites/WebSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum WebSiteRuntime
public class WebSite : Resource<WebSiteData>
{
private const string ResourceTypeName = "Microsoft.Web/sites";
private static string GetName(string? name) => name is null ? $"webSite-{Infrastructure.Seed}" : $"{name}-{Infrastructure.Seed}";
private static string GetName(IConstruct scope, string? name) => name is null ? $"webSite-{scope.EnvironmentName}" : $"{name}-{scope.EnvironmentName}";

private ApplicationSettingsResource AppSettings { get; }

Expand All @@ -47,8 +47,8 @@ public class WebSite : Resource<WebSiteData>
/// <param name="version">The version.</param>
/// <param name="location">The location.</param>
public WebSite(IConstruct scope, string resourceName, AppServicePlan appServicePlan, WebSiteRuntime runtime, string runtimeVersion, string version = "2021-02-01", AzureLocation? location = default)
: base(scope, null, GetName(resourceName), ResourceTypeName, version, ArmAppServiceModelFactory.WebSiteData(
name: GetName(resourceName),
: base(scope, null, GetName(scope, resourceName), ResourceTypeName, version, ArmAppServiceModelFactory.WebSiteData(
name: GetName(scope, resourceName),
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
resourceType: ResourceTypeName,
kind: "app,linux",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class WebSiteConfigLogs : Resource<SiteLogsConfigData>
{
private const string ResourceTypeName = "Microsoft.Web/sites/config";

private static string GetName(string? name) => name is null ? $"logs-{Infrastructure.Seed}" : $"{name}-{Infrastructure.Seed}";
private static string GetName(IConstruct scope, string? name) => name is null ? $"logs-{scope.EnvironmentName}" : $"{name}-{scope.EnvironmentName}";

/// <summary>
/// Initializes a new instance of the <see cref="WebSiteConfigLogs"/>.
Expand Down
Loading