Skip to content

Commit

Permalink
Branch 240419 body dynamic schema (#96)
Browse files Browse the repository at this point in the history
* Revert "`azapi_*` - support `payload` (#95)"

This reverts commit 861bed6.

* `azapi_*` - the `body` field supports dynamic schema

* fix tests

* fix crash
  • Loading branch information
ms-henglu authored Apr 22, 2024
1 parent bd6b63c commit 92e3621
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 157 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## v1.13.0 (unreleased)
ENHANCEMENTS:
- Support the new bicep types.
- `azapi_resource` resource: Support for the `payload` property.
- `azapi_update_resource` resource: Support for the `payload` property.
- `azapi_resource_action` resource: Support for the `payload` property.
- `azapi_resource_action` data source: Support for the `payload` property.
- `azapi_resource` resource: The `body` field supports dynamic schema.
- `azapi_update_resource` resource: The `body` field supports dynamic schema.
- `azapi_resource_action` resource: The `body` field supports dynamic schema.
- `azapi_resource_action` data source: The `body` field supports dynamic schema. d

## v1.12.0
ENHANCEMENTS:
Expand Down
21 changes: 4 additions & 17 deletions internal/langserver/handlers/hover/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,6 @@ func HoverAtPos(data []byte, filename string, pos hcl.Pos, logger *log.Logger) *
}
}
}
case "payload":
if parser.ContainsPos(attribute.NameRange, pos) {
return Hover(property.Name, property.Modifier, property.Type, property.Description, attribute.NameRange)
}

bodyDef := tfschema.BodyDefinitionFromBlock(block)
if bodyDef == nil {
return nil
}

tokens, _ := hclsyntax.LexExpression(data[attribute.Expr.Range().Start.Byte:attribute.Expr.Range().End.Byte], filename, attribute.Expr.Range().Start)
hclNode := parser.BuildHclNode(tokens)
if hclNode == nil {
break
}

return hoverOnBody(hclNode, pos, bodyDef)
case "body":
if parser.ContainsPos(attribute.NameRange, pos) {
return Hover(property.Name, property.Modifier, property.Type, property.Description, attribute.NameRange)
Expand All @@ -75,6 +58,10 @@ func HoverAtPos(data []byte, filename string, pos hcl.Pos, logger *log.Logger) *
}

hclNode := parser.JsonEncodeExpressionToHclNode(data, attribute.Expr)
if hclNode == nil {
tokens, _ := hclsyntax.LexExpression(data[attribute.Expr.Range().Start.Byte:attribute.Expr.Range().End.Byte], filename, attribute.Expr.Range().Start)
hclNode = parser.BuildHclNode(tokens)
}
if hclNode == nil {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,36 +210,6 @@
"command": "editor.action.triggerSuggest"
}
},
{
"label": "payload",
"labelDetails": {},
"kind": 10,
"detail": "payload (Optional)",
"documentation": {
"kind": "markdown",
"value": "Type: `dynamic` \nA dynamic attribute that contains the request body used to create and update azure resource.\n"
},
"sortText": "0006",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
"range": {
"start": {
"line": 2,
"character": 2
},
"end": {
"line": 2,
"character": 2
}
},
"newText": "payload = $0"
},
"command": {
"title": "Suggest",
"command": "editor.action.triggerSuggest"
}
},
{
"label": "tags",
"labelDetails": {},
Expand All @@ -249,7 +219,7 @@
"kind": "markdown",
"value": "Type: `map<string, string>` \nA mapping of tags which should be assigned to the azure resource.\n"
},
"sortText": "0007",
"sortText": "0006",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
Expand Down Expand Up @@ -279,7 +249,7 @@
"kind": "markdown",
"value": "Type: `list<string>` \nA list of path that needs to be exported from response body.\n"
},
"sortText": "0008",
"sortText": "0007",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
Expand Down Expand Up @@ -309,7 +279,7 @@
"kind": "markdown",
"value": "Type: `bool` \nWhether enabled the validation on `type` and `body` with embedded schema. Defaults to `true`.\n"
},
"sortText": "0009",
"sortText": "0008",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
Expand Down Expand Up @@ -339,7 +309,7 @@
"kind": "markdown",
"value": "Type: `list<string>` \nA list of ARM resource IDs which are used to avoid create/modify/delete azapi resources at the same time.\n"
},
"sortText": "0010",
"sortText": "0009",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
Expand Down Expand Up @@ -369,7 +339,7 @@
"kind": "markdown",
"value": "Type: `list<string>` \nA list of properties that should be ignored when comparing the `body` with its current state..\n"
},
"sortText": "0011",
"sortText": "0010",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
Expand Down Expand Up @@ -399,7 +369,7 @@
"kind": "markdown",
"value": "Type: `bool` \nWhether ignore incorrect casing returned in `body` to suppress plan-diff. Defaults to `false`.\n"
},
"sortText": "0012",
"sortText": "0011",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
Expand Down Expand Up @@ -429,7 +399,7 @@
"kind": "markdown",
"value": "Type: `bool` \nWhether ignore not returned properties like credentials in `body` to suppress plan-diff. Defaults to `false`.\n"
},
"sortText": "0013",
"sortText": "0012",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"isIncomplete": false,
"items": [
{
"label": "jsonencode({})",
"label": "{}",
"labelDetails": {},
"kind": 12,
"documentation": {
"kind": "markdown",
"value": "`jsonencode` encodes a given value to a string using JSON syntax."
"value": "dynamic attribute allows any valid HCL object."
},
"sortText": "jsonencode",
"sortText": "{}",
"insertTextFormat": 2,
"insertTextMode": 2,
"textEdit": {
Expand All @@ -26,7 +26,7 @@
"character": 12
}
},
"newText": "jsonencode({\n\t$0\n})"
"newText": "{\n\t$0\n}"
},
"command": {
"title": "Suggest",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "azapi_resource_action" "test" {
type = "Microsoft.AppPlatform/Spring@2022-05-01-preview"
action = "regenerateTestKey"
payload = {
body = {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ resource "azapi_resource" "dataflow" {
type = "Microsoft.DataFactory/factories/dataflows@2018-06-01"
name = "hengludf0509"
parent_id = azurerm_data_factory.test.id
payload = {
body = {
properties = {
type = "Flowlet"
typeProperties = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ resource "azapi_resource" "test" {
name = "acctest1774"
parent_id = azurerm_batch_account.test.id
type = "Microsoft.DataFactory/factories@2018-06-01"
payload = {
body = {
identity = {
type = "SystemAssigned"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ resource "azapi_resource" "test" {
name = "acctest1774"
parent_id = azurerm_batch_account.test.id
type = "Microsoft.DataFactory/factories@2018-06-01"
payload = {
body = {
"properties": {
"encryption": {
"identity": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ resource "azapi_resource" "test" {
name = "acctest1774"
parent_id = azurerm_batch_account.test.id
type = "Microsoft.DataFactory/factories@2018-06-01"
payload = {
body = {
identity = {
type = ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ resource "azapi_resource" "test" {
name = "acctest1774"
parent_id = azurerm_batch_account.test.id
type = "Microsoft.DataFactory/factories@2018-06-01"
payload = {
body = {
identity = {
type = "SystemAssigned"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resource "azapi_resource" "route" {
name = "henglu38"
parent_id = azapi_resource.gateway.id

payload = {
body = {
properties = {
appResourceId = azapi_resource.app.id
routes = [
Expand Down
23 changes: 2 additions & 21 deletions internal/langserver/handlers/tfschema/body_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,9 @@ func bodyCandidates(data []byte, filename string, block *hclsyntax.Block, attrib

hclNode := parser.JsonEncodeExpressionToHclNode(data, attribute.Expr)
if hclNode == nil {
return nil
}

return buildCandidates(hclNode, filename, pos, bodyDef)
}

func payloadCandidates(data []byte, filename string, block *hclsyntax.Block, attribute *hclsyntax.Attribute, pos hcl.Pos, property *Property) []lsp.CompletionItem {
if attribute.Expr != nil {
if _, ok := attribute.Expr.(*hclsyntax.LiteralValueExpr); ok && parser.ToLiteral(attribute.Expr) == nil {
if property != nil {
return property.ValueCandidatesFunc(nil, editRangeFromExprRange(attribute.Expr, pos))
}
}
tokens, _ := hclsyntax.LexExpression(data[attribute.Expr.Range().Start.Byte:attribute.Expr.Range().End.Byte], filename, attribute.Expr.Range().Start)
hclNode = parser.BuildHclNode(tokens)
}

bodyDef := BodyDefinitionFromBlock(block)
if bodyDef == nil {
return nil
}

tokens, _ := hclsyntax.LexExpression(data[attribute.Expr.Range().Start.Byte:attribute.Expr.Range().End.Byte], filename, attribute.Expr.Range().Start)
hclNode := parser.BuildHclNode(tokens)
if hclNode == nil {
return nil
}
Expand Down
18 changes: 0 additions & 18 deletions internal/langserver/handlers/tfschema/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,6 @@ func boolCandidates(_ *string, r lsp.Range) []lsp.CompletionItem {
return valueCandidates([]string{"true", "false"}, r, false)
}

func bodyJsonencodeFuncCandidate() lsp.CompletionItem {
return lsp.CompletionItem{
Label: `jsonencode({})`,
Kind: lsp.ValueCompletion,
Documentation: lsp.MarkupContent{
Kind: "markdown",
Value: "`jsonencode` encodes a given value to a string using JSON syntax.",
},
SortText: `jsonencode`,
InsertTextFormat: lsp.SnippetTextFormat,
InsertTextMode: lsp.AdjustIndentation,
TextEdit: &lsp.TextEdit{
NewText: "jsonencode({\n\t$0\n})",
},
Command: constTriggerSuggestCommand(),
}
}

func dynamicPlaceholderCandidate() lsp.CompletionItem {
return lsp.CompletionItem{
Label: `{}`,
Expand Down
48 changes: 4 additions & 44 deletions internal/langserver/handlers/tfschema/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,8 @@ func init() {
Type: "string <JSON>",
Description: "A JSON object that contains the request body used to create and update azure resource.",
CompletionNewText: `body = $0`,
ValueCandidatesFunc: FixedValueCandidatesFunc([]lsp.CompletionItem{bodyJsonencodeFuncCandidate()}),
GenericCandidatesFunc: bodyCandidates,
},

{
Name: "payload",
Modifier: "Optional",
Type: "dynamic",
Description: "A dynamic attribute that contains the request body used to create and update azure resource.",
CompletionNewText: `payload = $0`,
ValueCandidatesFunc: FixedValueCandidatesFunc([]lsp.CompletionItem{dynamicPlaceholderCandidate()}),
GenericCandidatesFunc: payloadCandidates,
GenericCandidatesFunc: bodyCandidates,
},

{
Expand Down Expand Up @@ -191,18 +181,8 @@ func init() {
Type: "string <JSON>",
Description: "A JSON object that contains the request body used to create and update azure resource.",
CompletionNewText: `body = $0`,
ValueCandidatesFunc: FixedValueCandidatesFunc([]lsp.CompletionItem{bodyJsonencodeFuncCandidate()}),
GenericCandidatesFunc: bodyCandidates,
},

{
Name: "payload",
Modifier: "Optional",
Type: "dynamic",
Description: "A dynamic attribute that contains the request body used to create and update azure resource.",
CompletionNewText: `payload = $0`,
ValueCandidatesFunc: FixedValueCandidatesFunc([]lsp.CompletionItem{dynamicPlaceholderCandidate()}),
GenericCandidatesFunc: payloadCandidates,
GenericCandidatesFunc: bodyCandidates,
},

{
Expand Down Expand Up @@ -329,18 +309,8 @@ func init() {
Type: "string <JSON>",
Description: "A JSON object that contains the request body.",
CompletionNewText: `body = $0`,
ValueCandidatesFunc: FixedValueCandidatesFunc([]lsp.CompletionItem{bodyJsonencodeFuncCandidate()}),
GenericCandidatesFunc: bodyCandidates,
},

{
Name: "payload",
Modifier: "Optional",
Type: "dynamic",
Description: "A dynamic attribute that contains the request body used to create and update azure resource.",
CompletionNewText: `payload = $0`,
ValueCandidatesFunc: FixedValueCandidatesFunc([]lsp.CompletionItem{dynamicPlaceholderCandidate()}),
GenericCandidatesFunc: payloadCandidates,
GenericCandidatesFunc: bodyCandidates,
},

{
Expand Down Expand Up @@ -396,18 +366,8 @@ func init() {
Type: "string <JSON>",
Description: "A JSON object that contains the request body.",
CompletionNewText: `body = $0`,
ValueCandidatesFunc: FixedValueCandidatesFunc([]lsp.CompletionItem{bodyJsonencodeFuncCandidate()}),
GenericCandidatesFunc: bodyCandidates,
},

{
Name: "payload",
Modifier: "Optional",
Type: "dynamic",
Description: "A dynamic attribute that contains the request body used to create and update azure resource.",
CompletionNewText: `payload = $0`,
ValueCandidatesFunc: FixedValueCandidatesFunc([]lsp.CompletionItem{dynamicPlaceholderCandidate()}),
GenericCandidatesFunc: payloadCandidates,
GenericCandidatesFunc: bodyCandidates,
},

{
Expand Down
Loading

0 comments on commit 92e3621

Please sign in to comment.