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

Added ResourceGroup and SubscriptionId fields #1433

Merged
merged 6 commits into from Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Expand Up @@ -474,5 +474,25 @@ public void Test_Issue1402()
template.SelectToken("$.resources[?(@.name == 'subDeploy')].location")!.Should().BeNull();
}
}

[TestMethod]
public void Test_Issue1391()
{
var bicepContents = @"
resource dep 'Microsoft.Resources/deployments@2020-06-01' = {
name: 'nestedDeployment'
resourceGroup: 'bicep-rg'
subscriptionId: 'abcd-efgh'
location: 'westus'
properties: {
mode: 'Incremental'
}
}
";
var jsonOutput = CompilationHelper.AssertSuccessWithTemplateOutput(bicepContents);
var template = JToken.Parse(jsonOutput);
template.SelectToken("$.resources[?(@.name == 'nestedDeployment')].subscriptionId")!.Should().DeepEqual("abcd-efgh");
template.SelectToken("$.resources[?(@.name == 'nestedDeployment')].resourceGroup")!.Should().DeepEqual("bicep-rg");
}
}
}
38 changes: 37 additions & 1 deletion src/Bicep.Core/TypeSystem/Az/AzResourceTypeFactory.cs
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Collections.Generic;
using Bicep.Core.Resources;
using Bicep.Core.Emit;

namespace Bicep.Core.TypeSystem.Az
{
Expand Down Expand Up @@ -66,6 +67,25 @@ private static TypePropertyFlags GetTypePropertyFlags(Azure.Bicep.Types.Concrete
return flags;
}

private static ObjectType AddResourceProperties(ObjectType objectType)
TSunny007 marked this conversation as resolved.
Show resolved Hide resolved
{
var properties = objectType.Properties.Values.ToList();
if (string.Equals(objectType.Name, TemplateWriter.NestedDeploymentResourceType, StringComparison.CurrentCultureIgnoreCase))
{
properties.Add(new TypeProperty("resourceGroup", LanguageConstants.String));
properties.Add(new TypeProperty("subscriptionId", LanguageConstants.String));
}

// TODO: remove 'dependsOn' from the type library and add it here

return new NamedObjectType(
objectType.Name,
objectType.ValidationFlags,
properties,
objectType.AdditionalPropertiesType,
objectType.AdditionalPropertiesFlags);
}

private TypeSymbol ToTypeSymbol(Azure.Bicep.Types.Concrete.TypeBase typeBase, bool isResourceBodyType)
{
switch (typeBase)
Expand Down Expand Up @@ -97,7 +117,23 @@ private TypeSymbol ToTypeSymbol(Azure.Bicep.Types.Concrete.TypeBase typeBase, bo
{
var resourceTypeReference = ResourceTypeReference.Parse(resourceType.Name);
var bodyType = GetTypeSymbol(resourceType.Body.Type, true);

switch (bodyType)
{
case ObjectType bodyObjectType:
bodyType = AddResourceProperties(bodyObjectType);
break;
case DiscriminatedObjectType bodyDiscriminatedType:
var bodyTypes = bodyDiscriminatedType.UnionMembersByKey.Values.ToList().Select(x => x.Type as ObjectType ?? throw new ArgumentException());
bodyTypes = bodyTypes.Select(x => AddResourceProperties(x));
bodyType = new DiscriminatedObjectType(
bodyDiscriminatedType.Name,
bodyDiscriminatedType.ValidationFlags,
bodyDiscriminatedType.DiscriminatorKey,
bodyTypes);
break;
default:
throw new ArgumentException();
}
return new ResourceType(resourceTypeReference, ToResourceScope(resourceType.ScopeType), bodyType);
}
case Azure.Bicep.Types.Concrete.UnionType unionType:
Expand Down