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

Use tenant function and resourceGroup().location #42257

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 13 additions & 0 deletions sdk/provisioning/Azure.Provisioning/src/Construct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ private string GetEnvironmentName()
return _environmentName is null ? Scope!.EnvironmentName : _environmentName;
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public Infrastructure? FindInfrastructure()
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved
{
if (this is Infrastructure infrastructure)
{
return infrastructure;
}
return Scope?.FindInfrastructure();
}

/// <summary>
/// Registers an existing resource with this construct that will be used by other resources in the construct.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions sdk/provisioning/Azure.Provisioning/src/IConstruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,11 @@ public interface IConstruct
/// </summary>
/// <param name="output">The <see cref="Output"/> to add.</param>
public void AddOutput(Output output);

/// <summary>
/// Finds the infrastructure for the construct.
/// </summary>
/// <returns></returns>
public Infrastructure? FindInfrastructure();
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved
}
}
27 changes: 27 additions & 0 deletions sdk/provisioning/Azure.Provisioning/src/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public abstract class Resource : IPersistableModel<Resource>
{
internal Dictionary<object, Dictionary<string, Parameter>> ParameterOverrides { get; }

internal Dictionary<object, Dictionary<string, string>> PropertyOverrides { get; }
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved

private IList<Resource> Dependencies { get; }

internal void AddDependency(Resource resource)
Expand Down Expand Up @@ -85,6 +87,7 @@ protected Resource(IConstruct scope, Resource? parent, string resourceName, Reso
ResourceData = createProperties(azureName);
Version = version;
ParameterOverrides = new Dictionary<object, Dictionary<string, Parameter>>();
PropertyOverrides = new Dictionary<object, Dictionary<string, string>>();
Dependencies = new List<Resource>();
ResourceType = resourceType;
Id = Parent is null
Expand Down Expand Up @@ -161,6 +164,18 @@ private protected void AssignParameter(object instance, string propertyName, Par
Parameters.Add(parameter);
}

private protected void AssignProperty(object instance, string propertyName, string propertyValue)
{
if (PropertyOverrides.TryGetValue(instance, out var overrides))
{
overrides[propertyName] = propertyValue;
}
else
{
PropertyOverrides.Add(instance, new Dictionary<string, string> { { propertyName, propertyValue } });
}
}

/// <summary>
/// Adds an output to the resource.
/// </summary>
Expand Down Expand Up @@ -245,6 +260,18 @@ private BinaryData SerializeModule(ModelReaderWriterOptions options)
}
bicepOptions.ParameterOverrides.Add(parameter.Key, dict);
}
foreach (var propertyOverride in PropertyOverrides)
{
if (!bicepOptions.ParameterOverrides.TryGetValue(propertyOverride.Key, out var dict))
{
dict = new Dictionary<string, string>();
bicepOptions.ParameterOverrides.Add(propertyOverride.Key, dict);
}
foreach (var kvp in propertyOverride.Value)
{
dict.Add(kvp.Key, kvp.Value);
}
}
var data = ModelReaderWriter.Write(ResourceData, bicepOptions).ToMemory();

#if NET6_0_OR_GREATER
Expand Down
28 changes: 26 additions & 2 deletions sdk/provisioning/Azure.Provisioning/src/ResourceOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,22 @@ public abstract class Resource<T> : Resource
/// <param name="resourceType">The resourceType.</param>
/// <param name="version">The version.</param>
/// <param name="createProperties">Lambda to create the ARM properties.</param>
protected Resource(IConstruct scope, Resource? parent, string resourceName, ResourceType resourceType, string version, Func<string, T> createProperties)
: base(scope, parent, resourceName, resourceType, version, (name) => createProperties(name))
/// <param name="locationSelector"></param>
protected Resource(
IConstruct scope,
Resource? parent,
string resourceName,
ResourceType resourceType,
string version,
Func<string, T> createProperties,
Expression<Func<T, object?>>? locationSelector)
: base(scope, parent, resourceName, resourceType, version, name => createProperties(name))
{
Properties = (T)ResourceData;
if (locationSelector != null && ((Construct)scope).FindInfrastructure()?.UseAnonymousResourceGroup == true)
{
AssignProperty(locationSelector, "resourceGroup().location");
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <summary>
Expand All @@ -51,6 +63,18 @@ public void AssignParameter(Expression<Func<T, object?>> propertySelector, Param
AssignParameter(instance, name, parameter);
}

/// <summary>
///
/// </summary>
/// <param name="propertySelector"></param>
/// <param name="propertyValue"></param>
/// <exception cref="NotSupportedException"></exception>
public void AssignProperty(Expression<Func<T, object?>> propertySelector, string propertyValue)
{
(object instance, string name, string expression) = EvaluateLambda(propertySelector);
AssignProperty(instance, name, propertyValue);
}

/// <summary>
/// Adds an output to the resource.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public AppConfigurationStore(IConstruct scope, string name = "store", string ver
name: name,
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
skuName: "free"))
skuName: "free"),
data => data.Location)
{
AddOutput(store => store.Endpoint, $"{Name}_endpoint");
}
Expand Down
7 changes: 6 additions & 1 deletion sdk/provisioning/Azure.Provisioning/src/keyvault/KeyVault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ public KeyVault(IConstruct scope, ResourceGroup? parent = default, string name =
}
})
} : default,
enableRbacAuthorization: true)))
enableRbacAuthorization: true)),
data => data.Location)
{
AddOutput(kv => kv.Properties.VaultUri, "vaultUri");
if (scope.Root.Properties.TenantId == Guid.Empty)
{
AssignProperty(kv => kv.Properties.TenantId, "tenant()");
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public KeyVaultAddAccessPolicy(IConstruct scope, Parameter principalIdParameter,
IdentityAccessSecretPermission.List
}
})
}))
}),
data => data.Location)
{
AssignParameter(p => p.AccessPolicies[0].ObjectId, principalIdParameter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public KeyVaultSecret(IConstruct scope, string name = "kvs", string version = "2
resourceType: ResourceTypeName,
properties: ArmKeyVaultModelFactory.SecretProperties(
value: Guid.Empty.ToString())
))
),
data => data.Location)
{
}

Expand All @@ -44,7 +45,8 @@ public KeyVaultSecret(IConstruct scope, string name, ConnectionString connection
resourceType: ResourceTypeName,
properties: ArmKeyVaultModelFactory.SecretProperties(
value: connectionString.Value)
))
),
data => data.Location)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public ResourceGroup(IConstruct scope, string? name = "rg", string version = "20
name: name,
resourceType: ResourceType,
tags: new Dictionary<string, string> { { "azd-env-name", scope.EnvironmentName } },
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS))
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS),
null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public Subscription(IConstruct scope, Guid? guid = default)
(name) => ResourceManagerModelFactory.SubscriptionData(
id: SubscriptionResource.CreateResourceIdentifier(name),
subscriptionId: name,
tenantId: scope.Root.Properties.TenantId))
tenantId: scope.Root.Properties.TenantId),
null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Tenant : Resource<TenantData>
/// <param name="tenantId">The tenant id.</param>
public Tenant(IConstruct scope, Guid? tenantId = null)
: base(scope, null, tenantId?.ToString()!, ResourceTypeName, "2022-12-01", (name) => ResourceManagerModelFactory.TenantData(
tenantId: tenantId.HasValue ? tenantId.Value : Guid.Parse(name)))
tenantId: tenantId.HasValue ? tenantId.Value : Guid.Parse(name)), null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public DeploymentScript(IConstruct scope, string resourceName, IEnumerable<Scrip
timeout: TimeSpan.FromMinutes(5),
cleanupPreference: ScriptCleanupOptions.OnSuccess,
environmentVariables: scriptEnvironmentVariables,
scriptContent: scriptContent))
scriptContent: scriptContent),
data => data.Location)
{
}

Expand Down Expand Up @@ -83,7 +84,8 @@ alter role db_owner add member ${APPUSERNAME}
SCRIPT_END

./sqlcmd -S ${DBSERVER} -d ${DBNAME} -U ${SQLADMIN} -i ./initDb.sql
"""))
"""),
data => data.Location)
{
AssignParameter(data => data.EnvironmentVariables[0].SecureValue, appUserPasswordSecret);
AssignParameter(data => data.EnvironmentVariables[1].SecureValue, sqlAdminPasswordSecret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public SqlDatabase(IConstruct scope, SqlServer? parent = null, string name = "db
: base(scope, parent, name, ResourceTypeName, version, (name) => ArmSqlModelFactory.SqlDatabaseData(
name: name,
resourceType: ResourceTypeName,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS))
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS),
data => data.Location)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public SqlFirewallRule(IConstruct scope, string name = "fw", string version = "2
resourceType: ResourceTypeName,
startIPAddress: "0.0.0.1",
endIPAddress: "255.255.255.254"
))
),
null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public SqlServer(IConstruct scope, string name, string version = "2022-08-01-pre
minTlsVersion: "1.2",
publicNetworkAccess: ServerNetworkAccessFlag.Enabled,
administratorLogin: "sqladmin",
administratorLoginPassword: Guid.Empty.ToString()))
administratorLoginPassword: Guid.Empty.ToString()),
data => data.Location)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class BlobService : Resource<BlobServiceData>
public BlobService(IConstruct scope)
: base(scope, null, "default", ResourceTypeName, "2022-09-01", (name) => ArmStorageModelFactory.BlobServiceData(
name: "default",
resourceType: ResourceTypeName))
resourceType: ResourceTypeName),
null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ public StorageAccount(IConstruct scope, StorageKind kind, StorageSkuName sku, Re
resourceType: ResourceTypeName,
location: Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
sku: new StorageSku(sku),
kind: StorageKind.StorageV2))
kind: StorageKind.StorageV2),
data => data.Location)
{
if (scope.FindInfrastructure()?.UseAnonymousResourceGroup == true)
{
AssignProperty(sa => sa.Location, "resourceGroup().location");
}
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public AppServicePlan(IConstruct scope, string resourceName, string version = "2
name: name,
location: location ?? Environment.GetEnvironmentVariable("AZURE_LOCATION") ?? AzureLocation.WestUS,
sku: new AppServiceSkuDescription() { Name = "B1" },
isReserved: true))
isReserved: true),
data => data.Location)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class ApplicationSettingsResource : Resource<AppServiceConfigurationDic
public ApplicationSettingsResource(IConstruct scope, IDictionary<string, string> appSettings, WebSite? parent = null, string version = "2021-02-01")
: base(scope, parent, "appsettings", ResourceTypeName, version, (name) => ArmAppServiceModelFactory.AppServiceConfigurationDictionary(
name: "appsettings",
properties: appSettings))
properties: appSettings), null)
{
}

Expand Down
3 changes: 2 additions & 1 deletion sdk/provisioning/Azure.Provisioning/src/websites/WebSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public WebSite(IConstruct scope, string resourceName, AppServicePlan appServiceP
}
}),
isHttpsOnly: true,
identity: new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssigned)))
identity: new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssigned)),
data => data.Location)
{
var appSettings = runtime == WebSiteRuntime.Dotnetcore
? new Dictionary<string, string>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public WebSiteConfigLogs(IConstruct scope, string resourceName, WebSite? parent
RetentionInDays = 1,
RetentionInMb = 35
}
}))
}),
null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class WebSitePublishingCredentialPolicy : Resource<CsmPublishingCredentia
public WebSitePublishingCredentialPolicy(IConstruct scope, string resourceName, string version = "2021-02-01", AzureLocation? location = default)
: base(scope, null, resourceName, ResourceTypeName, version, (name) => ArmAppServiceModelFactory.CsmPublishingCredentialsPoliciesEntityData(
name: name,
allow: false))
allow: false),
null)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ resource keyVault_j1ww9730M 'Microsoft.KeyVault/vaults@2023-02-01' = {
name: 'kv-TEST'
location: 'westus'
properties: {
tenantId: '00000000-0000-0000-0000-000000000000'
tenantId: tenant()
sku: {
name: 'standard'
family: 'A'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

resource storageAccount_NHn0GuaqX 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'photoacct84d98ba67edc4e3'
resource storageAccount_v03fg7zUF 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'photoacct44b504f6e12d4a4'
location: 'westus'
sku: {
name: 'Premium_LRS'
Expand All @@ -10,8 +10,8 @@ resource storageAccount_NHn0GuaqX 'Microsoft.Storage/storageAccounts@2022-09-01'
}
}

resource blobService_58OA4T8kA 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_NHn0GuaqX
resource blobService_MYBzZwJkK 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_v03fg7zUF
name: 'default'
properties: {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

resource storageAccount_hOJq7fwUJ 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'photoacct98d7a228f178408'
resource storageAccount_8IFaxJjQB 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'photoacct5482f9ff12ce4b8'
location: 'westus'
sku: {
name: 'Premium_LRS'
Expand All @@ -10,8 +10,8 @@ resource storageAccount_hOJq7fwUJ 'Microsoft.Storage/storageAccounts@2022-09-01'
}
}

resource blobService_c88nS1we4 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_hOJq7fwUJ
resource blobService_NLeppAjLn 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_8IFaxJjQB
name: 'default'
properties: {
deleteRetentionPolicy: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ resource keyVault_6DI6zrlsS 'Microsoft.KeyVault/vaults@2023-02-01' = {
name: 'kv-TEST'
location: 'westus'
properties: {
tenantId: '00000000-0000-0000-0000-000000000000'
tenantId: tenant()
sku: {
name: 'standard'
family: 'A'
Expand Down
Loading
Loading