Skip to content

Commit

Permalink
Added default type definitions. (#7174)
Browse files Browse the repository at this point in the history
* Added default type definitions.

* Added baseline test changes.

* Fixed some tests.
  • Loading branch information
davidcho23 authored Jun 14, 2022
1 parent 0ad0969 commit 75911c0
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 17 deletions.
83 changes: 81 additions & 2 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3090,9 +3090,88 @@ public void Test_Issue6423()
}

/// <summary>
/// https://github.com/Azure/bicep/issues/3169
/// https://github.com/Azure/bicep/issues/3356
/// </summary>
[TestMethod]
public void Test_Issue_3356_Accept_Correct_Type_Definitions()
{
var result = CompilationHelper.Compile(@"
#disable-next-line BCP081
resource foo 'Microsoft.Storage/storageAccounts@2021-09-00' = {
name: 'test'
kind: 'StorageV2'
location: resourceGroup().location
sku: {
name: 'Standard_LRS'
capacity: 1
}
extendedLocation: {
type: 'NotSpecified'
}
scale: {
capacity: 2
minimum: 1
}
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
clientId: 'client1'
principalId: 'principal1'
}
}
}
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}

/// <summary>
/// https://github.com/Azure/bicep/issues/3356
/// </summary>
[TestMethod]
public void Test_Issue_3356_Warn_On_Bad_Type_Definitions()
{
var result = CompilationHelper.Compile(@"
resource foo 'Microsoft.Storage/storageAccounts@2021-09-00' = {
name: 'test'
kind: 'StorageV2'
location: resourceGroup().location
sku: {
name: 123
capacity: '1'
}
extendedLocation: {
type: 1
}
scale: {
capacity: '2'
minimum: 1
}
identity: {
type: 'noType'
tenantId: 3
userAssignedIdentities: {
clientId: 1
principalId: 2
}
}
}
");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[] {
("BCP081", DiagnosticLevel.Warning, "Resource type \"Microsoft.Storage/storageAccounts@2021-09-00\" does not have types available."),
("BCP036", DiagnosticLevel.Warning, "The property \"name\" expected a value of type \"string\" but the provided value is of type \"int\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"capacity\" expected a value of type \"int\" but the provided value is of type \"'1'\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"type\" expected a value of type \"'ArcZone' | 'CustomLocation' | 'EdgeZone' | 'NotSpecified' | string\" but the provided value is of type \"int\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"capacity\" expected a value of type \"int\" but the provided value is of type \"'2'\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"tenantId\" expected a value of type \"string\" but the provided value is of type \"int\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"clientId\" expected a value of type \"string\" but the provided value is of type \"int\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"principalId\" expected a value of type \"string\" but the provided value is of type \"int\". If this is an inaccuracy in the documentation, please report it to the Bicep Team.")
});
}

/// <summary>
/// https://github.com/Azure/bicep/issues/4600
/// </summary>
public void Test_Issue_4600()
{
var result = CompilationHelper.Compile(@"
Expand Down Expand Up @@ -3132,7 +3211,7 @@ param userAssignedManagedIdentityName string
}

/// <summary>
/// https://github.com/Azure/bicep/issues/3169
/// https://github.com/Azure/bicep/issues/4600
/// </summary>
[TestMethod]
public void Test_Issue_4600_2()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public static IEnumerable<object[]> FallbackProperties
yield return new object[] { "kind", "''" };
yield return new object[] { "managedBy", "''" };
yield return new object[] { "managedByExtended", "[]" };
yield return new object[] { "extendedLocation", "{}" };
yield return new object[] { "extendedLocation", "{'type': 'NotSpecified'}" };
yield return new object[] { "zones", "[]" };
yield return new object[] { "plan", "{}" };
yield return new object[] { "eTag", "''" };
yield return new object[] { "scale", "{}" };
yield return new object[] { "identity", "{}" };
yield return new object[] { "scale", "{'capacity': 1}" };
yield return new object[] { "identity", "{'type': 'NotSpecified'}" };
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,7 @@ resource issue3000LogicApp1 'Microsoft.Logic/workflows@2019-05-01' = {
}
extendedLocation: {}
//@[002:018) [BCP187 (Warning)] The property "extendedLocation" does not exist in the resource definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |extendedLocation|
//@[002:018) [BCP035 (Warning)] The specified "object" declaration is missing the following required properties: "type". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |extendedLocation|
sku: {}
//@[002:005) [BCP187 (Warning)] The property "sku" does not exist in the resource definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |sku|
kind: 'V1'
Expand All @@ -2107,6 +2108,7 @@ resource issue3000LogicApp1 'Microsoft.Logic/workflows@2019-05-01' = {
//@[002:006) [BCP187 (Warning)] The property "eTag" does not exist in the resource definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |eTag|
scale: {}
//@[002:007) [BCP187 (Warning)] The property "scale" does not exist in the resource definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |scale|
//@[002:007) [BCP035 (Warning)] The specified "object" declaration is missing the following required properties: "capacity". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |scale|
}

resource issue3000LogicApp2 'Microsoft.Logic/workflows@2019-05-01' = {
Expand All @@ -2121,10 +2123,10 @@ resource issue3000LogicApp2 'Microsoft.Logic/workflows@2019-05-01' = {
//@[012:028) [BCP036 (Warning)] The property "identity" expected a value of type "ManagedServiceIdentity | null" but the provided value is of type "'SystemAssigned'". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |'SystemAssigned'|
extendedLocation: 'eastus'
//@[002:018) [BCP187 (Warning)] The property "extendedLocation" does not exist in the resource definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |extendedLocation|
//@[020:028) [BCP036 (Warning)] The property "extendedLocation" expected a value of type "object" but the provided value is of type "'eastus'". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |'eastus'|
//@[020:028) [BCP036 (Warning)] The property "extendedLocation" expected a value of type "extendedLocation" but the provided value is of type "'eastus'". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |'eastus'|
sku: 'Basic'
//@[002:005) [BCP187 (Warning)] The property "sku" does not exist in the resource definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |sku|
//@[007:014) [BCP036 (Warning)] The property "sku" expected a value of type "object" but the provided value is of type "'Basic'". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |'Basic'|
//@[007:014) [BCP036 (Warning)] The property "sku" expected a value of type "sku" but the provided value is of type "'Basic'". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |'Basic'|
kind: {
//@[002:006) [BCP187 (Warning)] The property "kind" does not exist in the resource definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |kind|
//@[008:030) [BCP036 (Warning)] The property "kind" expected a value of type "string" but the provided value is of type "object". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |{\r\n name: 'V1'\r\n }|
Expand Down Expand Up @@ -2153,7 +2155,7 @@ resource issue3000LogicApp2 'Microsoft.Logic/workflows@2019-05-01' = {
//@[008:010) [BCP036 (Warning)] The property "eTag" expected a value of type "string" but the provided value is of type "object". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |{}|
scale: [
//@[002:007) [BCP187 (Warning)] The property "scale" does not exist in the resource definition, although it might still be valid. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |scale|
//@[009:021) [BCP036 (Warning)] The property "scale" expected a value of type "object" but the provided value is of type "object[]". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |[\r\n {}\r\n ]|
//@[009:021) [BCP036 (Warning)] The property "scale" expected a value of type "scale" but the provided value is of type "object[]". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |[\r\n {}\r\n ]|
{}
]
}
Expand Down
58 changes: 49 additions & 9 deletions src/Bicep.Core/TypeSystem/Az/AzResourceTypeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,29 +146,69 @@ public static IEnumerable<TypeProperty> KnownTopLevelResourceProperties()

yield return new TypeProperty("properties", LanguageConstants.Object);

// TODO: Model type fully
yield return new TypeProperty("sku", LanguageConstants.Object);
yield return new TypeProperty("sku", new ObjectType("sku", TypeSymbolValidationFlags.Default, new[]
{
new TypeProperty("name", LanguageConstants.String),
new TypeProperty("tier", LanguageConstants.String),
new TypeProperty("size", LanguageConstants.String),
new TypeProperty("family", LanguageConstants.String),
new TypeProperty("model", LanguageConstants.String),
new TypeProperty("capacity", LanguageConstants.Int),
}, null));

yield return new TypeProperty("kind", LanguageConstants.String);
yield return new TypeProperty("managedBy", LanguageConstants.String);

var stringArray = new TypedArrayType(LanguageConstants.String, TypeSymbolValidationFlags.Default);
yield return new TypeProperty("managedByExtended", stringArray);

// TODO: Model type fully
yield return new TypeProperty("extendedLocation", LanguageConstants.Object);
var extendedLocationType = TypeHelper.CreateTypeUnion(
new StringLiteralType("NotSpecified"),
new StringLiteralType("EdgeZone"),
new StringLiteralType("CustomLocation"),
new StringLiteralType("ArcZone"),
LanguageConstants.String);

yield return new TypeProperty("extendedLocation", new ObjectType("extendedLocation", TypeSymbolValidationFlags.Default, new[]
{
new TypeProperty("type", extendedLocationType, TypePropertyFlags.Required),
new TypeProperty("name", LanguageConstants.String),
}, null));

yield return new TypeProperty("zones", stringArray);

yield return new TypeProperty("plan", LanguageConstants.Object);

yield return new TypeProperty("eTag", LanguageConstants.String);

// TODO: Model type fully
yield return new TypeProperty("scale", LanguageConstants.Object);

// TODO: Model type fully
yield return new TypeProperty("identity", LanguageConstants.Object);
yield return new TypeProperty("scale", new ObjectType("scale", TypeSymbolValidationFlags.Default, new[]
{
new TypeProperty("capacity", LanguageConstants.Int, TypePropertyFlags.Required),
new TypeProperty("maximum", LanguageConstants.Int),
new TypeProperty("minimum", LanguageConstants.Int),
}, null));

var resourceIdentityType = TypeHelper.CreateTypeUnion(
new StringLiteralType("NotSpecified"),
new StringLiteralType("SystemAssigned"),
new StringLiteralType("UserAssigned"),
new StringLiteralType("None"),
new StringLiteralType("Actor"),
LanguageConstants.String);

yield return new TypeProperty("identity", new ObjectType("identity", TypeSymbolValidationFlags.Default, new[]
{
new TypeProperty("principalId", LanguageConstants.String),
new TypeProperty("tenantId", LanguageConstants.String),
new TypeProperty("type", resourceIdentityType, TypePropertyFlags.Required),
new TypeProperty("identityIds", new TypedArrayType(LanguageConstants.String, TypeSymbolValidationFlags.Default)),
new TypeProperty("userAssignedIdentities", new ObjectType("userAssignedIdentityProperties", TypeSymbolValidationFlags.Default, new []
{
new TypeProperty("principalId", LanguageConstants.String),
new TypeProperty("clientId", LanguageConstants.String)
}, null)),
new TypeProperty("delegatedResources", LanguageConstants.Object),
}, null));
}

public AzResourceTypeProvider(IAzResourceTypeLoader resourceTypeLoader)
Expand Down

0 comments on commit 75911c0

Please sign in to comment.