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

Cannot use a symbolic name of Microsoft.Management/managementGroups as scope of an MG module #1833

Closed
majastrz opened this issue Mar 12, 2021 · 2 comments · Fixed by #4476
Closed
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@majastrz
Copy link
Member

Bicep version
Latest in main

Describe the bug
We allow a resource of type Microsoft.Resources/resourceGroup to be used as a scope of an RG module. We should support using a resources of type Microsoft.Management/managementGroups as scopes of MG modules.

Currently, the error you get is the following:
Scope "resource" is not valid for this module. Permitted scopes: "managementGroup".

To Reproduce
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 'modules/managementGroup.bicep' = {
  name: 'single-mg'
  scope: mainMg
}

managementGroup.bicep:

targetScope = 'managementGroup'

Additional context
Add any other context about the problem here.

@anthony-c-martin
Copy link
Member

Some implementation info - here's where we do this for resourceGroup resources. I expect it should be straightforward to do the same for subscription/managementGroup by adding similar logic:

if (StringComparer.OrdinalIgnoreCase.Equals(resourceType.TypeReference.FullyQualifiedType, AzResourceTypeProvider.ResourceTypeResourceGroup))
{
// special-case 'Microsoft.Resources/resourceGroups' in order to allow it to create a resourceGroup-scope resource
var rgScopeProperty = targetResourceSymbol.SafeGetBodyProperty(LanguageConstants.ResourceScopePropertyName);
var rgNameProperty = targetResourceSymbol.SafeGetBodyProperty(LanguageConstants.ResourceNamePropertyName);
// ignore diagnostics - these will be collected separately in the pass over resources
var hasErrors = false;
var rgScopeData = ScopeHelper.ValidateScope(semanticModel, (_, _, _) => { hasErrors = true; }, resourceType.ValidParentScopes, targetResourceSymbol.DeclaringResource.Value, rgScopeProperty);
if (rgNameProperty is not null && !hasErrors)
{
if (!supportedScopes.HasFlag(ResourceScope.ResourceGroup))
{
logInvalidScopeFunc(scopeProperty.Value, ResourceScope.ResourceGroup, supportedScopes);
return null;
}
return new ScopeData { RequestedScope = ResourceScope.ResourceGroup, SubscriptionIdProperty = rgScopeData?.SubscriptionIdProperty, ResourceGroupProperty = rgNameProperty.Value, IndexExpression = indexExpression };
}
}

@slapointe
Copy link
Contributor

I've encountered this problem too. I declare an existing MG as a resource and cannot use it as a managemenGroup scope to other modules and resources.

@alex-frankel alex-frankel modified the milestones: Committed Backlog, v0.5 May 17, 2021
@anthony-c-martin anthony-c-martin self-assigned this Sep 17, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants