Skip to content

Commit

Permalink
Support symbolic management group references to be used as scopes (#4476
Browse files Browse the repository at this point in the history
)
  • Loading branch information
anthony-c-martin authored Sep 21, 2021
1 parent de6f077 commit 154e57d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,39 @@ param tags object
});
}

/// <summary>
/// https://github.com/Azure/bicep/issues/1833
/// </summary>
[TestMethod]
public void Test_Issue1833()
{
var result = CompilationHelper.Compile(
("managementGroup.bicep", @"
targetScope = 'managementGroup'
"),
("main.bicep", @"
targetScope = 'tenant'
param mainMgName string
param managementGroups array
resource mainMg 'Microsoft.Management/managementGroups@2020-05-01' = {
name: mainMgName
}
resource mgs 'Microsoft.Management/managementGroups@2020-05-01' = [for (mg, i) in managementGroups: {
name: mg
}]
module singleMgModule 'managementGroup.bicep' = {
name: 'single-mg'
scope: mainMg
}
"));

result.Should().NotHaveAnyDiagnostics();
}

[TestMethod]
// https://github.com/Azure/bicep/issues/3617
public void Test_Issue3617()
Expand Down
18 changes: 18 additions & 0 deletions src/Bicep.Core/Emit/ScopeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ public class ScopeData
}
}

if (StringComparer.OrdinalIgnoreCase.Equals(targetResource.TypeReference.FullyQualifiedType, AzResourceTypeProvider.ResourceTypeManagementGroup))
{
// special-case 'Microsoft.Management/managementGroups' in order to allow it to create a managementGroup-scope resource
// ignore diagnostics - these will be collected separately in the pass over resources
var hasErrors = false;
var mgScopeData = ScopeHelper.ValidateScope(semanticModel, (_, _, _) => { hasErrors = true; }, targetResource.Type.ValidParentScopes, targetResource.Symbol.DeclaringResource.Value, targetResource.ScopeSyntax);
if (!hasErrors)
{
if (!supportedScopes.HasFlag(ResourceScope.ManagementGroup))
{
logInvalidScopeFunc(scopeValue, ResourceScope.ManagementGroup, supportedScopes);
return null;
}

return new ScopeData { RequestedScope = ResourceScope.ManagementGroup, ManagementGroupNameProperty = targetResource.NameSyntax, IndexExpression = indexExpression };
}
}

if (!supportedScopes.HasFlag(ResourceScope.Resource))
{
logInvalidScopeFunc(scopeValue, ResourceScope.Resource, supportedScopes);
Expand Down
1 change: 1 addition & 0 deletions src/Bicep.Core/TypeSystem/Az/AzResourceTypeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public ResourceType GetOrAdd(ResourceTypeGenerationFlags flags, ResourceTypeRefe

public const string ResourceTypeDeployments = "Microsoft.Resources/deployments";
public const string ResourceTypeResourceGroup = "Microsoft.Resources/resourceGroups";
public const string ResourceTypeManagementGroup = "Microsoft.Management/managementGroups";

private readonly IResourceTypeLoader resourceTypeLoader;
private readonly ImmutableHashSet<ResourceTypeReference> availableResourceTypes;
Expand Down

0 comments on commit 154e57d

Please sign in to comment.