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

Add location parameter for prompt mode #42286

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ public readonly partial struct Parameter
private readonly object _dummy;
private readonly int _dummyPrimitive;
public Parameter(Azure.Provisioning.Output output) { throw null; }
public Parameter(string name, string? description = null, object? defaultValue = null, bool isSecure = false) { throw null; }
public Parameter(string name, string? description = null, object? defaultValue = null, bool isSecure = false, bool isExpression = false) { throw null; }
public object? DefaultValue { get { throw null; } }
public string? Description { get { throw null; } }
public bool IsExpression { get { throw null; } }
public bool IsSecure { get { throw null; } }
public string Name { get { throw null; } }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ public readonly partial struct Parameter
private readonly object _dummy;
private readonly int _dummyPrimitive;
public Parameter(Azure.Provisioning.Output output) { throw null; }
public Parameter(string name, string? description = null, object? defaultValue = null, bool isSecure = false) { throw null; }
public Parameter(string name, string? description = null, object? defaultValue = null, bool isSecure = false, bool isExpression = false) { throw null; }
public object? DefaultValue { get { throw null; } }
public string? Description { get { throw null; } }
public bool IsExpression { get { throw null; } }
public bool IsSecure { get { throw null; } }
public string Name { get { throw null; } }
}
Expand Down
5 changes: 4 additions & 1 deletion sdk/provisioning/Azure.Provisioning/src/ModuleConstruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ private void WriteParameters(MemoryStream stream)
{
continue;
}
string defaultValue = parameter.DefaultValue is null ? string.Empty : $" = '{parameter.DefaultValue}'";
string defaultValue =
parameter.DefaultValue is null ?
string.Empty :
parameter.IsExpression ? $" = {parameter.DefaultValue}" : $" = '{parameter.DefaultValue}'";

if (parameter.IsSecure)
stream.WriteLine($"@secure()");
Expand Down
9 changes: 7 additions & 2 deletions sdk/provisioning/Azure.Provisioning/src/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public readonly struct Parameter
/// Gets a value indicating whether the parameter is secure.
/// </summary>
public bool IsSecure { get; }
/// <summary>
/// Gets a value indicating whether the parameter is an expression.
/// </summary>
public bool IsExpression { get; }

internal bool IsFromOutput => Output != null;
internal bool IsLiteral => Output?.IsLiteral ?? false;
Expand Down Expand Up @@ -67,14 +71,15 @@ internal Parameter(string name, string? description, object? defaultValue, bool
/// <param name="description">The parameter description.</param>
/// <param name="defaultValue">The parameter defaultValue.</param>
/// <param name="isSecure">Is the parameter secure.</param>
public Parameter(string name, string? description = default, object? defaultValue = default, bool isSecure = false)
/// <param name="isExpression">Is the parameter an expression.</param>
public Parameter(string name, string? description = default, object? defaultValue = default, bool isSecure = false, bool isExpression = false)
{
Name = name;
Description = description;
DefaultValue = defaultValue;
IsSecure = isSecure;
IsExpression = isExpression;
}

internal string GetParameterString(IConstruct parentScope)
{
// If the parameter is not from an output, use the parameter name.
Expand Down
25 changes: 25 additions & 0 deletions sdk/provisioning/Azure.Provisioning/src/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ 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 @@ -167,6 +168,7 @@ 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 @@ -177,6 +179,18 @@ 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 Expand Up @@ -284,6 +298,17 @@ private BinaryData SerializeModule(ModelReaderWriterOptions options)
return new BinaryData(stream.GetBuffer().AsMemory(0, (int)stream.Position));
}

private void AddToOverrides(BicepModelReaderWriterOptions bicepOptions, object instance, string propertyName, string propertyValue)
{
if (!bicepOptions.ParameterOverrides.TryGetValue(instance, out IDictionary<string, string>? instanceOverrides))
{
instanceOverrides = new Dictionary<string, string>();
bicepOptions.ParameterOverrides[instance] = instanceOverrides;
}

instanceOverrides[propertyName] = propertyValue;
}

private bool NeedsScope()
{
Debug.Assert(ModuleScope != null, "ModuleScope should not be null");
Expand Down
2 changes: 1 addition & 1 deletion sdk/provisioning/Azure.Provisioning/src/ResourceOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected Resource(
{
// We can't use the lambda overload because not all of the T's will inherit from TrackedResourceData
// TODO we may need to add a protected LocationSelector property in the future if there are exceptions to the rule
AssignProperty(Properties, "Location", $"{ResourceGroup.AnonymousResourceGroupName}.location");
AssignParameter(Properties, "Location", new Parameter("location", defaultValue: $"{ResourceGroup.AnonymousResourceGroupName}.location", isExpression: true));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

resource storageAccount_hWQR13jUf 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'photoacct71d9792260684fd'
resource storageAccount_rJRF56wLn 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'photoacctdbd29b86eb654ef'
location: 'westus'
sku: {
name: 'Premium_LRS'
Expand All @@ -10,8 +10,8 @@ resource storageAccount_hWQR13jUf 'Microsoft.Storage/storageAccounts@2022-09-01'
}
}

resource blobService_UnN8ydpj5 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_hWQR13jUf
resource blobService_wGH0Fapvd 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_rJRF56wLn
name: 'default'
properties: {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
targetScope = 'resourceGroup'

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

resource storageAccount_Ls2Cd9uGu 'Microsoft.Storage/storageAccounts@2022-09-01' = {

resource storageAccount_1JetI2q6o 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: toLower(take(concat('photoAcct', uniqueString(resourceGroup().id)), 24))
location: resourceGroup().location
location: location
sku: {
name: 'Premium_LRS'
}
Expand All @@ -12,8 +15,8 @@ resource storageAccount_Ls2Cd9uGu 'Microsoft.Storage/storageAccounts@2022-09-01'
}
}

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

resource storageAccount_dx0zUTplq 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'photoacctc31a9d6584644ae'
resource storageAccount_3S3yTe8Uk 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'photoacct0cba824071e046a'
location: 'westus'
sku: {
name: 'Premium_LRS'
Expand All @@ -10,8 +10,8 @@ resource storageAccount_dx0zUTplq 'Microsoft.Storage/storageAccounts@2022-09-01'
}
}

resource blobService_PYZrLaKZo 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_dx0zUTplq
resource blobService_Mv9WBXOiS 'Microsoft.Storage/storageAccounts/blobServices@2022-09-01' = {
parent: storageAccount_3S3yTe8Uk
name: 'default'
properties: {
deleteRetentionPolicy: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ resource sqlServer_RgHXTrwDE 'Microsoft.Sql/servers@2022-08-01-preview' = {
resource sqlDatabase_6M6mjEKjO 'Microsoft.Sql/servers/databases@2022-08-01-preview' = {
parent: sqlServer_RgHXTrwDE
name: 'db-TEST'
location: 'westus'
properties: {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ resource sqlServer_RgHXTrwDE 'Microsoft.Sql/servers@2022-08-01-preview' = {
resource sqlDatabase_6M6mjEKjO 'Microsoft.Sql/servers/databases@2022-08-01-preview' = {
parent: sqlServer_RgHXTrwDE
name: 'db-TEST'
location: 'westus'
properties: {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ resource sqlServer_RgHXTrwDE 'Microsoft.Sql/servers@2022-08-01-preview' = {
resource sqlDatabase_6M6mjEKjO 'Microsoft.Sql/servers/databases@2022-08-01-preview' = {
parent: sqlServer_RgHXTrwDE
name: 'db-TEST'
location: 'westus'
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved
properties: {
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
targetScope = 'resourceGroup'

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

@secure()
@description('SQL Server administrator password')
param sqlAdminPassword string
Expand All @@ -11,7 +14,7 @@ param appUserPassword string

resource appServicePlan_PxkuWnuWL 'Microsoft.Web/serverfarms@2021-02-01' = {
name: 'appServicePlan-TEST'
location: resourceGroup().location
location: location
sku: {
name: 'B1'
}
Expand All @@ -22,7 +25,7 @@ resource appServicePlan_PxkuWnuWL 'Microsoft.Web/serverfarms@2021-02-01' = {

resource keyVault_zomsD2kWf 'Microsoft.KeyVault/vaults@2023-02-01' = {
name: 'kv-TEST'
location: resourceGroup().location
location: location
tags: {
'key': 'value'
}
Expand Down Expand Up @@ -81,7 +84,7 @@ resource keyVaultSecret_YNErVycWe 'Microsoft.KeyVault/vaults/secrets@2023-02-01'

resource webSite_IGuzwfciS 'Microsoft.Web/sites@2021-02-01' = {
name: 'frontEnd-TEST'
location: resourceGroup().location
location: location
kind: 'app,linux'
properties: {
serverFarmId: '/subscriptions/subscription()/resourceGroups/resourceGroup()/providers/Microsoft.Web/serverfarms/appServicePlan-TEST'
Expand Down Expand Up @@ -134,7 +137,7 @@ resource webSiteConfigLogs_GwVSHGFxS 'Microsoft.Web/sites/config@2021-02-01' = {

resource sqlServer_2CRay8gJr 'Microsoft.Sql/servers@2022-08-01-preview' = {
name: 'sqlserver-TEST'
location: resourceGroup().location
location: location
properties: {
administratorLogin: 'sqladmin'
administratorLoginPassword: sqlAdminPassword
Expand Down Expand Up @@ -162,7 +165,7 @@ resource sqlFirewallRule_MTg5B9jZr 'Microsoft.Sql/servers/firewallRules@2020-11-

resource deploymentScript_qloqQ8wU0 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: 'cliScript-TEST'
location: resourceGroup().location
location: location
kind: 'AzureCLI'
properties: {
cleanupPreference: 'OnSuccess'
Expand Down Expand Up @@ -212,7 +215,7 @@ SCRIPT_END

resource webSite_TR8bo87ZZ 'Microsoft.Web/sites@2021-02-01' = {
name: 'backEnd-TEST'
location: resourceGroup().location
location: location
kind: 'app,linux'
properties: {
serverFarmId: '/subscriptions/subscription()/resourceGroups/resourceGroup()/providers/Microsoft.Web/serverfarms/appServicePlan-TEST'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ resource sqlServer_zjdvvB2wl 'Microsoft.Sql/servers@2022-08-01-preview' = {
resource sqlDatabase_U7NzorRJT 'Microsoft.Sql/servers/databases@2022-08-01-preview' = {
parent: sqlServer_zjdvvB2wl
name: 'db-TEST'
location: 'westus'
properties: {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ public async Task StorageBlobDefaultsInPromptMode()
await ValidateBicepAsync(promptMode: true);
}

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

[Test]
public async Task StorageBlobDropDown()
{
Expand Down
Loading