Skip to content

Commit

Permalink
Add list method call on Azure resource references (#3430)
Browse files Browse the repository at this point in the history
* Add list method call on Azure resource references

* Address PR feedback

* Update baselines

* Fix tests
  • Loading branch information
anthony-c-martin authored Jul 8, 2021
1 parent f1a4461 commit 6987e17
Show file tree
Hide file tree
Showing 45 changed files with 128 additions and 691 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.Diagnostics;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bicep.Core.IntegrationTests.Scenarios
{
[TestClass]
public class ResourceListFunctionTests
{
[TestMethod]
public void List_wildcard_function_on_resource_references()
{
var result = CompilationHelper.Compile(@"
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: 'testacc'
location: 'West US'
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
output pkStandard string = listKeys(stg.id, stg.apiVersion).keys[0].value
output pkMethod string = stg.listKeys().keys[0].value
output pkMethodVersionOverride string = stg.listKeys('2021-01-01').keys[0].value
output pkMethodPayload string = stg.listKeys(stg.apiVersion, {
key1: 'val1'
})
");

result.Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.outputs['pkStandard'].value", "[listKeys(resourceId('Microsoft.Storage/storageAccounts', 'testacc'), '2019-06-01').keys[0].value]");
result.Template.Should().HaveValueAtPath("$.outputs['pkMethod'].value", "[listKeys(resourceId('Microsoft.Storage/storageAccounts', 'testacc'), '2019-06-01').keys[0].value]");
result.Template.Should().HaveValueAtPath("$.outputs['pkMethodVersionOverride'].value", "[listKeys(resourceId('Microsoft.Storage/storageAccounts', 'testacc'), '2021-01-01').keys[0].value]");
result.Template.Should().HaveValueAtPath("$.outputs['pkMethodPayload'].value", "[listKeys(resourceId('Microsoft.Storage/storageAccounts', 'testacc'), '2019-06-01', createObject('key1', 'val1'))]");
}

[TestMethod]
public void List_wildcard_function_on_cross_scope_resource_references()
{
var result = CompilationHelper.Compile(@"
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
scope: resourceGroup('other')
name: 'testacc'
}
output pkStandard string = listKeys(stg.id, stg.apiVersion).keys[0].value
output pkMethod string = stg.listKeys().keys[0].value
output pkMethodVersionOverride string = stg.listKeys('2021-01-01').keys[0].value
output pkMethodPayload string = stg.listKeys(stg.apiVersion, {
key1: 'val1'
})
");

result.Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.outputs['pkStandard'].value", "[listKeys(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, 'other'), 'Microsoft.Storage/storageAccounts', 'testacc'), '2019-06-01').keys[0].value]");
result.Template.Should().HaveValueAtPath("$.outputs['pkMethod'].value", "[listKeys(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, 'other'), 'Microsoft.Storage/storageAccounts', 'testacc'), '2019-06-01').keys[0].value]");
result.Template.Should().HaveValueAtPath("$.outputs['pkMethodVersionOverride'].value", "[listKeys(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, 'other'), 'Microsoft.Storage/storageAccounts', 'testacc'), '2021-01-01').keys[0].value]");
result.Template.Should().HaveValueAtPath("$.outputs['pkMethodPayload'].value", "[listKeys(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, 'other'), 'Microsoft.Storage/storageAccounts', 'testacc'), '2019-06-01', createObject('key1', 'val1'))]");
}

[TestMethod]
public void Only_list_methods_are_permitted()
{
var result = CompilationHelper.Compile(@"
resource stg 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
name: 'testacc'
}
var allowed = {
a: stg.list()
b: stg.listA()
c: stg.listTotallyMadeUpMethod()
}
var disallowed = {
a: stg.lis()
b: stg.lsit()
c: stg.totallyMadeUpMethod()
}
");
result.Should().HaveDiagnostics(new[] {
("no-unused-vars", DiagnosticLevel.Warning, "Variable \"allowed\" is declared but never used."),
("no-unused-vars", DiagnosticLevel.Warning, "Variable \"disallowed\" is declared but never used."),
("BCP109", DiagnosticLevel.Error, "The type \"Microsoft.Storage/storageAccounts\" does not contain function \"lis\"."),
("BCP109", DiagnosticLevel.Error, "The type \"Microsoft.Storage/storageAccounts\" does not contain function \"lsit\"."),
("BCP109", DiagnosticLevel.Error, "The type \"Microsoft.Storage/storageAccounts\" does not contain function \"totallyMadeUpMethod\"."),
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,6 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "managementGroup",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1166,23 +1166,6 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ var test1 = listKeys('abcd')
// list spelled wrong
var test2 = lsitKeys('abcd', '2020-01-01')
//@[4:9) [no-unused-vars (Warning)] Variable "test2" is declared but never used. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-unused-vars)) |test2|
//@[12:20) [BCP082 (Error)] The name "lsitKeys" does not exist in the current context. Did you mean "listKeys"? (CodeDescription: none) |lsitKeys|
//@[12:20) [BCP057 (Error)] The name "lsitKeys" does not exist in the current context. (CodeDescription: none) |lsitKeys|

// just 'lis' instead of 'list'
var test3 = lis('abcd', '2020-01-01')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,23 +1043,6 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,23 +1043,6 @@
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2622,23 +2622,6 @@
"newText": "letsAccessTheDashes2"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2586,23 +2586,6 @@
"newText": "letsAccessTheDashes2"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2614,23 +2614,6 @@
"newText": "letsAccessTheDashes2"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2845,23 +2845,6 @@
"newText": "letsAccessTheDashes2"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2845,23 +2845,6 @@
"newText": "letsAccessTheDashes2"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2845,23 +2845,6 @@
"newText": "letsAccessTheDashes2"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2845,23 +2845,6 @@
"newText": "letsAccessTheDashes2"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2607,23 +2607,6 @@
"newText": "letsAccessTheDashes2"
}
},
{
"label": "listKeys",
"kind": "function",
"detail": "listKeys()",
"deprecated": false,
"preselect": false,
"sortText": "3_listKeys",
"insertTextFormat": "snippet",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "listKeys($0)"
},
"command": {
"command": "editor.action.triggerParameterHints"
}
},
{
"label": "loadFileAsBase64",
"kind": "function",
Expand Down
Loading

0 comments on commit 6987e17

Please sign in to comment.