Skip to content

Commit

Permalink
Allow same param to be used for multiple properties (#42330)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored Mar 2, 2024
1 parent 68b441a commit f9a32a8
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 31 deletions.
14 changes: 0 additions & 14 deletions sdk/provisioning/Azure.Provisioning/src/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ protected virtual string GetAzureName(IConstruct scope, string resourceName)
/// <param name="parameter">The <see cref="Parameter"/> to assign.</param>
private protected void AssignParameter(object instance, string propertyName, Parameter parameter)
{
ValidateOverrideCanBeAdded(instance, propertyName);
if (ParameterOverrides.TryGetValue(instance, out var overrides))
{
overrides[propertyName] = parameter;
Expand All @@ -170,7 +169,6 @@ private protected void AssignParameter(object instance, string propertyName, Par

private protected void AssignProperty(object instance, string propertyName, string propertyValue)
{
ValidateOverrideCanBeAdded(instance, propertyName);
if (PropertyOverrides.TryGetValue(instance, out var overrides))
{
overrides[propertyName] = propertyValue;
Expand All @@ -181,18 +179,6 @@ private protected void AssignProperty(object instance, string propertyName, stri
}
}

private void ValidateOverrideCanBeAdded(object instance, string name)
{
if ((PropertyOverrides.TryGetValue(instance, out var instancePropertyOverrides) &&
instancePropertyOverrides.ContainsKey(name)) ||
(ParameterOverrides.TryGetValue(instance, out var instanceParameterOverrides) &&
instanceParameterOverrides.ContainsKey(name)))
{
throw new InvalidOperationException(
$"A parameter or property override was already defined for property {name} in type {instance.GetType()}");
}
}

/// <summary>
/// Adds an output to the resource.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
targetScope = 'resourceGroup'

@description('')
param location string = resourceGroup().location

@description('')
param myLocationParam string


resource storageAccount_YRiDhR43q 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: toLower(take(concat('photoAcct', uniqueString(resourceGroup().id)), 24))
location: myLocationParam
sku: {
name: 'Premium_LRS'
}
kind: 'StorageV2'
properties: {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
targetScope = 'subscription'

@description('')
param overrideLocation string


resource resourceGroup_I6QNkoPsb 'Microsoft.Resources/resourceGroups@2023-07-01' = {
name: 'rg-TEST'
location: 'westus'
tags: {
'azd-env-name': 'TEST'
}
}

module rg_TEST_module './resources/rg_TEST_module/rg_TEST_module.bicep' = {
name: 'rg_TEST_module'
scope: resourceGroup_I6QNkoPsb
params: {
overrideLocation: overrideLocation
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@description('')
param overrideLocation string


resource storageAccount_melvnlpF2 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: toLower(take(concat('photoAcct', uniqueString(resourceGroup().id)), 24))
location: 'westus'
sku: {
name: 'Premium_LRS'
}
kind: 'StorageV2'
properties: {
}
}

resource storageAccount_DysMV79Ig 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: toLower(take(concat('sa1', uniqueString(resourceGroup().id)), 24))
location: overrideLocation
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
}
}

resource storageAccount_I0kTuAmmD 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: toLower(take(concat('sa2', uniqueString(resourceGroup().id)), 24))
location: overrideLocation
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
}
}
53 changes: 36 additions & 17 deletions sdk/provisioning/Azure.Provisioning/tests/ProvisioningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,31 +275,50 @@ public async Task StorageBlobDefaultsInPromptMode()
}

[Test]
public void CannotAddLocationParameterInPromptMode()
public async Task CanAddCustomLocationParameterInInteractiveMode()
{
var infra = new TestInfrastructure(configuration: new Configuration { UseInteractiveMode = true });
var sa = infra.AddStorageAccount(name: "photoAcct", sku: StorageSkuName.PremiumLrs, kind: StorageKind.BlockBlobStorage);
Assert.Throws<InvalidOperationException>(() =>
sa.AssignParameter(d => d.Location, new Parameter("myLocationParam")));
sa.AssignParameter(d => d.Location, new Parameter("myLocationParam"));

infra.Build(GetOutputPath());

await ValidateBicepAsync(BinaryData.FromObjectAsJson(
new
{
myLocationParam = new { value = "eastus" },
}),
interactiveMode: true);
}

[Test]
public void CannotOverrideSamePropertyMoreThanOnce()
public async Task CanAssignParameterToMultipleResources()
{
var infra = new TestInfrastructure();
var sa = infra.AddStorageAccount(name: "photoAcct", sku: StorageSkuName.PremiumLrs, kind: StorageKind.BlockBlobStorage);
infra.AddStorageAccount(name: "photoAcct", sku: StorageSkuName.PremiumLrs, kind: StorageKind.BlockBlobStorage);
var overrideLocation = new Parameter("overrideLocation");

var account1 = infra.AddStorageAccount(
name: "sa1",
kind: StorageKind.BlobStorage,
sku: StorageSkuName.StandardLrs
);
account1.AssignParameter(a => a.Location, overrideLocation);

var account2 = infra.AddStorageAccount(
name: "sa2",
kind: StorageKind.BlobStorage,
sku: StorageSkuName.StandardLrs
);
account2.AssignParameter(a => a.Location, overrideLocation);

infra.Build(GetOutputPath());

sa.AssignParameter(d => d.Kind, new Parameter("skuParam"));
Assert.Throws<InvalidOperationException>(() =>
sa.AssignProperty(d => d.Kind, StorageKind.BlockBlobStorage.ToString()));
Assert.Throws<InvalidOperationException>(() =>
sa.AssignParameter(d => d.Kind, new Parameter("skuParam")));

sa.AssignProperty(d => d.AccessTier, StorageAccountAccessTier.Cool.ToString());
Assert.Throws<InvalidOperationException>(() =>
sa.AssignProperty(d => d.AccessTier, StorageAccountAccessTier.Cool.ToString()));
Assert.Throws<InvalidOperationException>(() =>
sa.AssignParameter(d => d.AccessTier, new Parameter("tierParam")));
await ValidateBicepAsync(BinaryData.FromObjectAsJson(
new
{
overrideLocation = new { value = "eastus" },
}));
}

[Test]
Expand Down Expand Up @@ -405,7 +424,7 @@ public async Task ValidateBicepAsync(BinaryData? parameters = null, bool interac
{
var error = process.StandardError.ReadLine();
TestContext.Progress.WriteLine(error);
if (error!.StartsWith("ERROR"))
if (error!.Contains("Error"))
{
Assert.Fail(error);
}
Expand Down

0 comments on commit f9a32a8

Please sign in to comment.