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 FromExisting to always treat as expression #42434

Merged
merged 2 commits into from
Mar 6, 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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sdk/provisioning/Azure.Provisioning/src/ModuleConstruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void WriteExistingResources(MemoryStream stream)
{
stream.WriteLine();
stream.WriteLine($"resource {resource.Name} '{resource.Id.ResourceType}@{resource.Version}' existing = {{");
stream.WriteLine($" name: '{resource.Id.Name}'");
stream.WriteLine($" name: {resource.Id.Name}");
stream.WriteLine($"}}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Azure.Provisioning.AppConfiguration
public class AppConfigurationStore : Resource<AppConfigurationStoreData>
{
private const string ResourceTypeName = "Microsoft.AppConfiguration/configurationStores";
private static readonly Func<string, AppConfigurationStoreData> Empty = (name) => ArmAppConfigurationModelFactory.AppConfigurationStoreData();

/// <summary>
/// Initializes a new instance of the <see cref="AppConfigurationStore"/> class.
Expand All @@ -24,7 +25,7 @@ public class AppConfigurationStore : Resource<AppConfigurationStoreData>
/// <param name="version">The version.</param>
/// <param name="location">The location.</param>
public AppConfigurationStore(IConstruct scope, string name = "store", string version = "2023-03-01", AzureLocation? location = default)
: base(scope, null, name, ResourceTypeName, version, (name) => ArmAppConfigurationModelFactory.AppConfigurationStoreData(
: this(scope, name, version, location, null, false, (name) => ArmAppConfigurationModelFactory.AppConfigurationStoreData(
name: name,
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
Expand All @@ -33,6 +34,21 @@ public AppConfigurationStore(IConstruct scope, string name = "store", string ver
AddOutput(store => store.Endpoint, $"{Name}_endpoint");
}

private AppConfigurationStore(IConstruct scope, string name = "store", string version = "2023-03-01", AzureLocation? location = default, ResourceGroup? parent = null, bool isExisting = false, Func<string, AppConfigurationStoreData>? creator = null)
: base(scope, null, name, ResourceTypeName, version, creator ?? Empty, isExisting)
{
}

/// <summary>
/// Creates a new instance of the <see cref="AppConfigurationStore"/> class referencing an existing instance.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The resource name.</param>
/// <param name="parent">The resource group.</param>
/// <returns>The KeyVault instance.</returns>
public static AppConfigurationStore FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
=> new AppConfigurationStore(scope, name, parent: parent, isExisting: true);

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
Expand Down
29 changes: 13 additions & 16 deletions sdk/provisioning/Azure.Provisioning/src/keyvault/KeyVault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ namespace Azure.Provisioning.KeyVaults
public class KeyVault : Resource<KeyVaultData>
{
private const string ResourceTypeName = "Microsoft.KeyVault/vaults";
private static readonly Func<string, KeyVaultData> Empty = (name) => ArmKeyVaultModelFactory.KeyVaultData();

/// <summary>
/// Creates a new instance of the <see cref="KeyVault"/> class referencing an existing KeyVault.
/// Creates a new instance of the <see cref="KeyVault"/> class referencing an existing instance.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The resource name.</param>
Expand All @@ -36,12 +37,7 @@ public static KeyVault FromExisting(IConstruct scope, string name, ResourceGroup
/// <param name="location">The location.</param>
/// <param name="parent"></param>
public KeyVault(IConstruct scope, ResourceGroup? parent = default, string name = "kv", string version = "2023-02-01", AzureLocation? location = default)
: this(scope, parent, name, version, location, false)
{
}

private KeyVault(IConstruct scope, ResourceGroup? parent = default, string name = "kv", string version = "2023-02-01", AzureLocation? location = default, bool isExisting = false)
: base(scope, parent, name, ResourceTypeName, version, (name) => ArmKeyVaultModelFactory.KeyVaultData(
: this(scope, parent, name, version, location, false, (name) => ArmKeyVaultModelFactory.KeyVaultData(
name: name,
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
Expand All @@ -58,20 +54,21 @@ private KeyVault(IConstruct scope, ResourceGroup? parent = default, string name
}
})
} : default,
enableRbacAuthorization: true)),
isExisting: isExisting)
enableRbacAuthorization: true)))
{
if (!isExisting)
{
AssignProperty(data => data.Name, GetAzureName(scope, name));
AssignProperty(data => data.Name, GetAzureName(scope, name));

if (scope.Root.Properties.TenantId == Guid.Empty)
{
AssignProperty(kv => kv.Properties.TenantId, Tenant.TenantIdExpression);
}
if (scope.Root.Properties.TenantId == Guid.Empty)
{
AssignProperty(kv => kv.Properties.TenantId, Tenant.TenantIdExpression);
}
}

private KeyVault(IConstruct scope, ResourceGroup? parent = default, string name = "kv", string version = "2023-02-01", AzureLocation? location = default, bool isExisting = false, Func<string, KeyVaultData>? creator = null)
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting: isExisting)
{
}

/// <summary>
/// Adds an access policy to the <see cref="KeyVault"/>.
/// </summary>
Expand Down
21 changes: 18 additions & 3 deletions sdk/provisioning/Azure.Provisioning/src/keyvault/KeyVaultSecret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using Azure.Provisioning.Sql;
using Azure.ResourceManager.KeyVault;
using Azure.ResourceManager.KeyVault.Models;

Expand All @@ -14,6 +13,7 @@ namespace Azure.Provisioning.KeyVaults
public class KeyVaultSecret : Resource<KeyVaultSecretData>
{
private const string ResourceTypeName = "Microsoft.KeyVault/vaults/secrets";
private static readonly Func<string, KeyVaultSecretData> Empty = (name) => ArmKeyVaultModelFactory.KeyVaultSecretData();

/// <summary>
/// Initializes a new instance of the <see cref="KeyVaultSecret"/>.
Expand All @@ -23,7 +23,7 @@ public class KeyVaultSecret : Resource<KeyVaultSecretData>
/// <param name="name">The name.</param>
/// <param name="version">The version.</param>
public KeyVaultSecret(IConstruct scope, KeyVault? parent = null, string name = "kvs", string version = "2023-02-01")
: base(scope, parent, name, ResourceTypeName, version, (name) => ArmKeyVaultModelFactory.KeyVaultSecretData(
: this(scope, parent, name, version, false, (name) => ArmKeyVaultModelFactory.KeyVaultSecretData(
name: name,
resourceType: ResourceTypeName,
properties: ArmKeyVaultModelFactory.SecretProperties(
Expand All @@ -32,6 +32,11 @@ public KeyVaultSecret(IConstruct scope, KeyVault? parent = null, string name = "
{
}

private KeyVaultSecret(IConstruct scope, KeyVault? parent = null, string name = "kvs", string version = "2023-02-01", bool isExisting = false, Func<string, KeyVaultSecretData>? createProperties = null)
: base(scope, parent, name, ResourceTypeName, version, createProperties ?? Empty, isExisting)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="KeyVaultSecret"/>.
/// </summary>
Expand All @@ -41,7 +46,7 @@ public KeyVaultSecret(IConstruct scope, KeyVault? parent = null, string name = "
/// <param name="connectionString">The connection string.</param>
/// <param name="version">The version.</param>
public KeyVaultSecret(IConstruct scope, string name, ConnectionString connectionString, KeyVault? parent = null, string version = "2023-02-01")
: base(scope, parent, name, ResourceTypeName, version, (name) => ArmKeyVaultModelFactory.KeyVaultSecretData(
: this(scope, parent, name, version, false, (name) => ArmKeyVaultModelFactory.KeyVaultSecretData(
name: name,
resourceType: ResourceTypeName,
properties: ArmKeyVaultModelFactory.SecretProperties(
Expand All @@ -50,6 +55,16 @@ public KeyVaultSecret(IConstruct scope, string name, ConnectionString connection
{
}

/// <summary>
/// Creates a new instance of the <see cref="KeyVaultSecret"/> class referencing an existing instance.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The resource name.</param>
/// <param name="parent">The resource group.</param>
/// <returns>The KeyVault instance.</returns>
public static KeyVaultSecret FromExisting(IConstruct scope, string name, KeyVault? parent = null)
=> new KeyVaultSecret(scope, parent, name, isExisting: true);

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
using Azure.Core;
using Azure.Provisioning.Redis;
using Azure.Provisioning.ResourceManager;
using Azure.ResourceManager.PostgreSql;
using Azure.ResourceManager.PostgreSql.FlexibleServers;
using Azure.ResourceManager.PostgreSql.FlexibleServers.Models;
using Azure.ResourceManager.PostgreSql.Models;
using Azure.ResourceManager.Redis.Models;

namespace Azure.Provisioning.PostgreSql
{
Expand All @@ -19,6 +16,7 @@ namespace Azure.Provisioning.PostgreSql
public class PostgreSqlFlexibleServer : Resource<PostgreSqlFlexibleServerData>
{
private const string ResourceTypeName = "Microsoft.DBforPostgreSQL/flexibleServers";
private static readonly Func<string, PostgreSqlFlexibleServerData> Empty = (name) => ArmPostgreSqlFlexibleServersModelFactory.PostgreSqlFlexibleServerData();

/// <summary>
/// Creates a new instance of the <see cref="PostgreSqlFlexibleServer"/> class.
Expand Down Expand Up @@ -50,7 +48,7 @@ public PostgreSqlFlexibleServer(
string name = "postgres",
string version = "2020-06-01",
AzureLocation? location = default)
: base(scope, parent, name, ResourceTypeName, version, (name) => ArmPostgreSqlFlexibleServersModelFactory.PostgreSqlFlexibleServerData(
: this(scope, administratorLogin, administratorPassword, sku, highAvailability, storage, backup, network, availabilityZone, parent, name, version, location, false, (name) => ArmPostgreSqlFlexibleServersModelFactory.PostgreSqlFlexibleServerData(
name: name,
sku: sku,
// create new instances so the properties can be overriden by user if needed
Expand All @@ -66,6 +64,26 @@ public PostgreSqlFlexibleServer(
AssignProperty(data => data.AdministratorLoginPassword, administratorPassword);
}

private PostgreSqlFlexibleServer(
IConstruct scope,
Parameter administratorLogin = default,
Parameter administratorPassword = default,
PostgreSqlFlexibleServerSku? sku = default,
PostgreSqlFlexibleServerHighAvailability? highAvailability = default,
PostgreSqlFlexibleServerStorage? storage = default,
PostgreSqlFlexibleServerBackupProperties? backup = default,
PostgreSqlFlexibleServerNetwork? network = default,
string? availabilityZone = default,
ResourceGroup? parent = default,
string name = "postgres",
string version = "2020-06-01",
AzureLocation? location = default,
bool isExisting = false,
Func<string, PostgreSqlFlexibleServerData>? creator = null)
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting)
{
}

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
Expand All @@ -77,6 +95,16 @@ public PostgreSqlFlexibleServer(
return result;
}

/// <summary>
/// Creates a new instance of the <see cref="PostgreSqlFlexibleServer"/> class referencing an existing instance.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The resource name.</param>
/// <param name="parent">The resource group.</param>
/// <returns>The KeyVault instance.</returns>
public static PostgreSqlFlexibleServer FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
=> new PostgreSqlFlexibleServer(scope, parent: parent, name: name, isExisting: true);

/// <summary>
/// Gets the connection string for the <see cref="RedisCache"/>.
/// </summary>
Expand Down
18 changes: 17 additions & 1 deletion sdk/provisioning/Azure.Provisioning/src/redis/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Azure.Provisioning.Redis
public class RedisCache : Resource<RedisData>
{
private const string ResourceTypeName = "Microsoft.Cache/Redis";
private static readonly Func<string, RedisData> Empty = (name) => ArmRedisModelFactory.RedisData(updateChannel: null);

/// <summary>
/// Creates a new instance of the <see cref="RedisCache"/> class.
Expand All @@ -25,7 +26,7 @@ public class RedisCache : Resource<RedisData>
/// <param name="name"></param>
/// <param name="location"></param>
public RedisCache(IConstruct scope, RedisSku? sku = default, ResourceGroup? parent = default, string name = "redis", AzureLocation? location = default)
: base(scope, parent, name, ResourceTypeName, "2020-06-01", (name) => ArmRedisModelFactory.RedisData(
: this(scope, sku, parent, name, location, false, (name) => ArmRedisModelFactory.RedisData(
name: name,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
enableNonSslPort: false,
Expand All @@ -37,6 +38,11 @@ public RedisCache(IConstruct scope, RedisSku? sku = default, ResourceGroup? pare
AssignProperty(data => data.Name, GetAzureName(scope, name));
}

private RedisCache(IConstruct scope, RedisSku? sku = default, ResourceGroup? parent = default, string name = "redis", AzureLocation? location = default, bool isExisting = false, Func<string, RedisData>? creator = null)
: base(scope, parent, name, ResourceTypeName, "2020-06-01", creator ?? Empty, isExisting)
{
}

/// <summary>
/// Gets the connection string for the <see cref="RedisCache"/>.
/// </summary>
Expand All @@ -54,6 +60,16 @@ public RedisCacheConnectionString GetConnectionString(bool useSecondary = false)
return result;
}

/// <summary>
/// Creates a new instance of the <see cref="RedisCache"/> class referencing an existing instance.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The resource name.</param>
/// <param name="parent">The resource group.</param>
/// <returns>The KeyVault instance.</returns>
public static RedisCache FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
=> new RedisCache(scope, parent: parent, name: name, isExisting: true);

/// <inheritdoc/>
protected override string GetAzureName(IConstruct scope, string resourceName) => GetGloballyUniqueName(resourceName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DeploymentScript : Resource<AzureCliScript>
{
private const string ResourceTypeName = "Microsoft.Resources/deploymentScripts";
private const string _defaultVersion = "2020-10-01";
private static readonly Func<string, AzureCliScript> Empty = (name) => ArmResourcesModelFactory.AzureCliScript();

/// <summary>
/// Initializes a new instance of the <see cref="DeploymentScript"/>.
Expand All @@ -36,7 +37,8 @@ public DeploymentScript(IConstruct scope, string resourceName, IEnumerable<Scrip
timeout: TimeSpan.FromMinutes(5),
cleanupPreference: ScriptCleanupOptions.OnSuccess,
environmentVariables: scriptEnvironmentVariables,
scriptContent: scriptContent))
scriptContent: scriptContent),
false)
{
}

Expand Down Expand Up @@ -83,13 +85,29 @@ alter role db_owner add member ${APPUSERNAME}
SCRIPT_END

./sqlcmd -S ${DBSERVER} -d ${DBNAME} -U ${SQLADMIN} -i ./initDb.sql
"""))
"""),
false)
{
AssignProperty(data => data.EnvironmentVariables[0].SecureValue, appUserPasswordSecret);
AssignProperty(data => data.EnvironmentVariables[1].SecureValue, sqlAdminPasswordSecret);
AssignProperty(data => data.EnvironmentVariables[2].Value, databaseServerName);
}

/// <summary>
/// Creates a new instance of the <see cref="DeploymentScript"/> class referencing an existing instance.
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="name">The resource name.</param>
/// <param name="parent">The resource group.</param>
/// <returns>The KeyVault instance.</returns>
public static DeploymentScript FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
=> new DeploymentScript(scope, parent: parent, name: name, isExisting: true);

private DeploymentScript(IConstruct scope, string name, ResourceGroup? parent = null, bool isExisting = false)
: base(scope, parent, name, ResourceTypeName, _defaultVersion, Empty, isExisting)
{
}

/// <inheritdoc/>
protected override Resource? FindParentInScope(IConstruct scope)
{
Expand Down
Loading