Skip to content

Commit 7b44464

Browse files
authored
update FromExisting to always treat as expression (#42434)
* update FromExisting to always treat as expression add FromExisting to all resources * update api
1 parent 58d3284 commit 7b44464

File tree

22 files changed

+460
-72
lines changed

22 files changed

+460
-72
lines changed

sdk/provisioning/Azure.Provisioning/api/Azure.Provisioning.net6.0.cs

Lines changed: 20 additions & 6 deletions
Large diffs are not rendered by default.

sdk/provisioning/Azure.Provisioning/api/Azure.Provisioning.netstandard2.0.cs

Lines changed: 20 additions & 6 deletions
Large diffs are not rendered by default.

sdk/provisioning/Azure.Provisioning/src/ModuleConstruct.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void WriteExistingResources(MemoryStream stream)
147147
{
148148
stream.WriteLine();
149149
stream.WriteLine($"resource {resource.Name} '{resource.Id.ResourceType}@{resource.Version}' existing = {{");
150-
stream.WriteLine($" name: '{resource.Id.Name}'");
150+
stream.WriteLine($" name: {resource.Id.Name}");
151151
stream.WriteLine($"}}");
152152
}
153153
}

sdk/provisioning/Azure.Provisioning/src/appconfiguration/AppConfigurationStore.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Azure.Provisioning.AppConfiguration
1515
public class AppConfigurationStore : Resource<AppConfigurationStoreData>
1616
{
1717
private const string ResourceTypeName = "Microsoft.AppConfiguration/configurationStores";
18+
private static readonly Func<string, AppConfigurationStoreData> Empty = (name) => ArmAppConfigurationModelFactory.AppConfigurationStoreData();
1819

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

37+
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)
38+
: base(scope, null, name, ResourceTypeName, version, creator ?? Empty, isExisting)
39+
{
40+
}
41+
42+
/// <summary>
43+
/// Creates a new instance of the <see cref="AppConfigurationStore"/> class referencing an existing instance.
44+
/// </summary>
45+
/// <param name="scope">The scope.</param>
46+
/// <param name="name">The resource name.</param>
47+
/// <param name="parent">The resource group.</param>
48+
/// <returns>The KeyVault instance.</returns>
49+
public static AppConfigurationStore FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
50+
=> new AppConfigurationStore(scope, name, parent: parent, isExisting: true);
51+
3652
/// <inheritdoc/>
3753
protected override Resource? FindParentInScope(IConstruct scope)
3854
{

sdk/provisioning/Azure.Provisioning/src/keyvault/KeyVault.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ namespace Azure.Provisioning.KeyVaults
1616
public class KeyVault : Resource<KeyVaultData>
1717
{
1818
private const string ResourceTypeName = "Microsoft.KeyVault/vaults";
19+
private static readonly Func<string, KeyVaultData> Empty = (name) => ArmKeyVaultModelFactory.KeyVaultData();
1920

2021
/// <summary>
21-
/// Creates a new instance of the <see cref="KeyVault"/> class referencing an existing KeyVault.
22+
/// Creates a new instance of the <see cref="KeyVault"/> class referencing an existing instance.
2223
/// </summary>
2324
/// <param name="scope">The scope.</param>
2425
/// <param name="name">The resource name.</param>
@@ -36,12 +37,7 @@ public static KeyVault FromExisting(IConstruct scope, string name, ResourceGroup
3637
/// <param name="location">The location.</param>
3738
/// <param name="parent"></param>
3839
public KeyVault(IConstruct scope, ResourceGroup? parent = default, string name = "kv", string version = "2023-02-01", AzureLocation? location = default)
39-
: this(scope, parent, name, version, location, false)
40-
{
41-
}
42-
43-
private KeyVault(IConstruct scope, ResourceGroup? parent = default, string name = "kv", string version = "2023-02-01", AzureLocation? location = default, bool isExisting = false)
44-
: base(scope, parent, name, ResourceTypeName, version, (name) => ArmKeyVaultModelFactory.KeyVaultData(
40+
: this(scope, parent, name, version, location, false, (name) => ArmKeyVaultModelFactory.KeyVaultData(
4541
name: name,
4642
resourceType: ResourceTypeName,
4743
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
@@ -58,20 +54,21 @@ private KeyVault(IConstruct scope, ResourceGroup? parent = default, string name
5854
}
5955
})
6056
} : default,
61-
enableRbacAuthorization: true)),
62-
isExisting: isExisting)
57+
enableRbacAuthorization: true)))
6358
{
64-
if (!isExisting)
65-
{
66-
AssignProperty(data => data.Name, GetAzureName(scope, name));
59+
AssignProperty(data => data.Name, GetAzureName(scope, name));
6760

68-
if (scope.Root.Properties.TenantId == Guid.Empty)
69-
{
70-
AssignProperty(kv => kv.Properties.TenantId, Tenant.TenantIdExpression);
71-
}
61+
if (scope.Root.Properties.TenantId == Guid.Empty)
62+
{
63+
AssignProperty(kv => kv.Properties.TenantId, Tenant.TenantIdExpression);
7264
}
7365
}
7466

67+
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)
68+
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting: isExisting)
69+
{
70+
}
71+
7572
/// <summary>
7673
/// Adds an access policy to the <see cref="KeyVault"/>.
7774
/// </summary>

sdk/provisioning/Azure.Provisioning/src/keyvault/KeyVaultSecret.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using Azure.Provisioning.Sql;
65
using Azure.ResourceManager.KeyVault;
76
using Azure.ResourceManager.KeyVault.Models;
87

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

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

35+
private KeyVaultSecret(IConstruct scope, KeyVault? parent = null, string name = "kvs", string version = "2023-02-01", bool isExisting = false, Func<string, KeyVaultSecretData>? createProperties = null)
36+
: base(scope, parent, name, ResourceTypeName, version, createProperties ?? Empty, isExisting)
37+
{
38+
}
39+
3540
/// <summary>
3641
/// Initializes a new instance of the <see cref="KeyVaultSecret"/>.
3742
/// </summary>
@@ -41,7 +46,7 @@ public KeyVaultSecret(IConstruct scope, KeyVault? parent = null, string name = "
4146
/// <param name="connectionString">The connection string.</param>
4247
/// <param name="version">The version.</param>
4348
public KeyVaultSecret(IConstruct scope, string name, ConnectionString connectionString, KeyVault? parent = null, string version = "2023-02-01")
44-
: base(scope, parent, name, ResourceTypeName, version, (name) => ArmKeyVaultModelFactory.KeyVaultSecretData(
49+
: this(scope, parent, name, version, false, (name) => ArmKeyVaultModelFactory.KeyVaultSecretData(
4550
name: name,
4651
resourceType: ResourceTypeName,
4752
properties: ArmKeyVaultModelFactory.SecretProperties(
@@ -50,6 +55,16 @@ public KeyVaultSecret(IConstruct scope, string name, ConnectionString connection
5055
{
5156
}
5257

58+
/// <summary>
59+
/// Creates a new instance of the <see cref="KeyVaultSecret"/> class referencing an existing instance.
60+
/// </summary>
61+
/// <param name="scope">The scope.</param>
62+
/// <param name="name">The resource name.</param>
63+
/// <param name="parent">The resource group.</param>
64+
/// <returns>The KeyVault instance.</returns>
65+
public static KeyVaultSecret FromExisting(IConstruct scope, string name, KeyVault? parent = null)
66+
=> new KeyVaultSecret(scope, parent, name, isExisting: true);
67+
5368
/// <inheritdoc/>
5469
protected override Resource? FindParentInScope(IConstruct scope)
5570
{

sdk/provisioning/Azure.Provisioning/src/postgresql/PostgreSqlFlexibleServer.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
using Azure.Core;
66
using Azure.Provisioning.Redis;
77
using Azure.Provisioning.ResourceManager;
8-
using Azure.ResourceManager.PostgreSql;
98
using Azure.ResourceManager.PostgreSql.FlexibleServers;
109
using Azure.ResourceManager.PostgreSql.FlexibleServers.Models;
11-
using Azure.ResourceManager.PostgreSql.Models;
12-
using Azure.ResourceManager.Redis.Models;
1310

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

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

67+
private PostgreSqlFlexibleServer(
68+
IConstruct scope,
69+
Parameter administratorLogin = default,
70+
Parameter administratorPassword = default,
71+
PostgreSqlFlexibleServerSku? sku = default,
72+
PostgreSqlFlexibleServerHighAvailability? highAvailability = default,
73+
PostgreSqlFlexibleServerStorage? storage = default,
74+
PostgreSqlFlexibleServerBackupProperties? backup = default,
75+
PostgreSqlFlexibleServerNetwork? network = default,
76+
string? availabilityZone = default,
77+
ResourceGroup? parent = default,
78+
string name = "postgres",
79+
string version = "2020-06-01",
80+
AzureLocation? location = default,
81+
bool isExisting = false,
82+
Func<string, PostgreSqlFlexibleServerData>? creator = null)
83+
: base(scope, parent, name, ResourceTypeName, version, creator ?? Empty, isExisting)
84+
{
85+
}
86+
6987
/// <inheritdoc/>
7088
protected override Resource? FindParentInScope(IConstruct scope)
7189
{
@@ -77,6 +95,16 @@ public PostgreSqlFlexibleServer(
7795
return result;
7896
}
7997

98+
/// <summary>
99+
/// Creates a new instance of the <see cref="PostgreSqlFlexibleServer"/> class referencing an existing instance.
100+
/// </summary>
101+
/// <param name="scope">The scope.</param>
102+
/// <param name="name">The resource name.</param>
103+
/// <param name="parent">The resource group.</param>
104+
/// <returns>The KeyVault instance.</returns>
105+
public static PostgreSqlFlexibleServer FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
106+
=> new PostgreSqlFlexibleServer(scope, parent: parent, name: name, isExisting: true);
107+
80108
/// <summary>
81109
/// Gets the connection string for the <see cref="RedisCache"/>.
82110
/// </summary>

sdk/provisioning/Azure.Provisioning/src/redis/RedisCache.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Azure.Provisioning.Redis
1515
public class RedisCache : Resource<RedisData>
1616
{
1717
private const string ResourceTypeName = "Microsoft.Cache/Redis";
18+
private static readonly Func<string, RedisData> Empty = (name) => ArmRedisModelFactory.RedisData(updateChannel: null);
1819

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

41+
private RedisCache(IConstruct scope, RedisSku? sku = default, ResourceGroup? parent = default, string name = "redis", AzureLocation? location = default, bool isExisting = false, Func<string, RedisData>? creator = null)
42+
: base(scope, parent, name, ResourceTypeName, "2020-06-01", creator ?? Empty, isExisting)
43+
{
44+
}
45+
4046
/// <summary>
4147
/// Gets the connection string for the <see cref="RedisCache"/>.
4248
/// </summary>
@@ -54,6 +60,16 @@ public RedisCacheConnectionString GetConnectionString(bool useSecondary = false)
5460
return result;
5561
}
5662

63+
/// <summary>
64+
/// Creates a new instance of the <see cref="RedisCache"/> class referencing an existing instance.
65+
/// </summary>
66+
/// <param name="scope">The scope.</param>
67+
/// <param name="name">The resource name.</param>
68+
/// <param name="parent">The resource group.</param>
69+
/// <returns>The KeyVault instance.</returns>
70+
public static RedisCache FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
71+
=> new RedisCache(scope, parent: parent, name: name, isExisting: true);
72+
5773
/// <inheritdoc/>
5874
protected override string GetAzureName(IConstruct scope, string resourceName) => GetGloballyUniqueName(resourceName);
5975
}

sdk/provisioning/Azure.Provisioning/src/resources/DeploymentScript.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class DeploymentScript : Resource<AzureCliScript>
1616
{
1717
private const string ResourceTypeName = "Microsoft.Resources/deploymentScripts";
1818
private const string _defaultVersion = "2020-10-01";
19+
private static readonly Func<string, AzureCliScript> Empty = (name) => ArmResourcesModelFactory.AzureCliScript();
1920

2021
/// <summary>
2122
/// Initializes a new instance of the <see cref="DeploymentScript"/>.
@@ -36,7 +37,8 @@ public DeploymentScript(IConstruct scope, string resourceName, IEnumerable<Scrip
3637
timeout: TimeSpan.FromMinutes(5),
3738
cleanupPreference: ScriptCleanupOptions.OnSuccess,
3839
environmentVariables: scriptEnvironmentVariables,
39-
scriptContent: scriptContent))
40+
scriptContent: scriptContent),
41+
false)
4042
{
4143
}
4244

@@ -83,13 +85,29 @@ alter role db_owner add member ${APPUSERNAME}
8385
SCRIPT_END
8486
8587
./sqlcmd -S ${DBSERVER} -d ${DBNAME} -U ${SQLADMIN} -i ./initDb.sql
86-
"""))
88+
"""),
89+
false)
8790
{
8891
AssignProperty(data => data.EnvironmentVariables[0].SecureValue, appUserPasswordSecret);
8992
AssignProperty(data => data.EnvironmentVariables[1].SecureValue, sqlAdminPasswordSecret);
9093
AssignProperty(data => data.EnvironmentVariables[2].Value, databaseServerName);
9194
}
9295

96+
/// <summary>
97+
/// Creates a new instance of the <see cref="DeploymentScript"/> class referencing an existing instance.
98+
/// </summary>
99+
/// <param name="scope">The scope.</param>
100+
/// <param name="name">The resource name.</param>
101+
/// <param name="parent">The resource group.</param>
102+
/// <returns>The KeyVault instance.</returns>
103+
public static DeploymentScript FromExisting(IConstruct scope, string name, ResourceGroup? parent = null)
104+
=> new DeploymentScript(scope, parent: parent, name: name, isExisting: true);
105+
106+
private DeploymentScript(IConstruct scope, string name, ResourceGroup? parent = null, bool isExisting = false)
107+
: base(scope, parent, name, ResourceTypeName, _defaultVersion, Empty, isExisting)
108+
{
109+
}
110+
93111
/// <inheritdoc/>
94112
protected override Resource? FindParentInScope(IConstruct scope)
95113
{

0 commit comments

Comments
 (0)