From b525549de18b204c4afedd8b15aa548811b912be Mon Sep 17 00:00:00 2001 From: Heng Lu <79895375+ms-henglu@users.noreply.github.com> Date: Mon, 22 Apr 2024 14:16:32 +0800 Subject: [PATCH] Branch 240419 body dynamic schema (#122) * Revert "`azapi_*` - support `payload` (#121)" This reverts commit 5cf0788e7449561c3070c327afc9e3b80392d3e7. * `azapi_*` - the `body` and `output` fields support dynamic schema * fix tests --- cmd/migrate_command_test.go | 28 +++++++++++----------- tf/terraform.go | 4 ++-- tf/utils.go | 47 +++++++++++++++++++------------------ 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/cmd/migrate_command_test.go b/cmd/migrate_command_test.go index 37baab6b..bee6bcb8 100644 --- a/cmd/migrate_command_test.go +++ b/cmd/migrate_command_test.go @@ -600,7 +600,7 @@ resource "azapi_resource" "test" { type = "SystemAssigned" } - payload = { + body = { properties = { sku = { name = local.AutomationSku @@ -614,13 +614,13 @@ resource "azapi_resource" "test2" { parent_id = azurerm_resource_group.test.id type = "Microsoft.Automation/automationAccounts@2020-01-13-preview" location = azurerm_resource_group.test.location - body = jsonencode({ + body = { properties = { sku = { - name = jsondecode(azapi_resource.test.output).properties.sku.name + name = azapi_resource.test.output.properties.sku.name } } - }) + } } resource "azurerm_automation_account" "test1" { @@ -634,19 +634,19 @@ resource "azapi_update_resource" "test" { resource_id = azurerm_automation_account.test1.id type = "Microsoft.Automation/automationAccounts@2020-01-13-preview" response_export_values = ["properties.sku"] - body = jsonencode({ + body = { tags = { key = var.Label } - }) + } } output "accountName" { - value = jsondecode(azapi_resource.test.output).name + value = azapi_resource.test.output.name } output "patchAccountSKU" { - value = jsondecode(azapi_update_resource.test.output).properties.sku.name + value = azapi_update_resource.test.output.properties.sku.name } `, template(), randomResourceName(), randomResourceName()) } @@ -684,7 +684,7 @@ resource "azapi_resource" "test" { type = "SystemAssigned" } - payload = { + body = { properties = { sku = { name = each.value.sku @@ -724,7 +724,7 @@ resource "azapi_resource" "test" { parent_id = azurerm_resource_group.test.id type = "Microsoft.Network/serviceEndpointPolicies@2020-11-01" - payload = { + body = { location = "westeurope" tags = {} properties = { @@ -756,7 +756,7 @@ resource "azapi_resource" "test" { parent_id = azurerm_resource_group.test.id type = "Microsoft.Automation/automationAccounts@2020-01-13-preview" location = azurerm_resource_group.test.location - payload = { + body = { properties = { sku = { name = "Basic" @@ -801,7 +801,7 @@ variable "action" { resource "azapi_update_resource" "test" { resource_id = azurerm_container_registry.test.id type = "Microsoft.ContainerRegistry/registries@2019-05-01" - payload = { + body = { properties = { networkRuleSet = { defaultAction = "Deny" @@ -837,7 +837,7 @@ resource "azapi_resource" "test" { type = "SystemAssigned" } - payload = { + body = { properties = { sku = { name = "Basic" @@ -868,7 +868,7 @@ resource "azapi_resource" "test1" { type = "SystemAssigned" } - payload = { + body = { properties = { sku = { name = "Basic" diff --git a/tf/terraform.go b/tf/terraform.go index 41ec020f..7ad1841f 100644 --- a/tf/terraform.go +++ b/tf/terraform.go @@ -137,8 +137,8 @@ func (t *Terraform) ListGenericResources(p *tfjson.Plan) []types.GenericResource for j, instance := range resource.Instances { resources[i].Instances[j].Outputs = getOutputsForAddress(resource.OldAddress(instance.Index), refValueMap) for _, output := range resources[i].Instances[j].Outputs { - prop := strings.TrimPrefix(output.OldName, fmt.Sprintf("jsondecode(%s.output).", resource.OldAddress(instance.Index))) - prop = strings.TrimPrefix(prop, fmt.Sprintf("%s.output_payload.", resource.OldAddress(instance.Index))) + prop := strings.TrimPrefix(output.OldName, fmt.Sprintf("%s.output.", resource.OldAddress(instance.Index))) + prop = strings.TrimPrefix(prop, fmt.Sprintf("jsondecode(%s.output).", resource.OldAddress(instance.Index))) if strings.HasPrefix(prop, "identity.userAssignedIdentities") { prop = "identity.userAssignedIdentities" } diff --git a/tf/utils.go b/tf/utils.go index ccaa1747..021a36e0 100644 --- a/tf/utils.go +++ b/tf/utils.go @@ -33,13 +33,13 @@ func getId(value interface{}) string { func getOutputsForAddress(address string, refValueMap map[string]interface{}) []types.Output { res := make([]types.Output, 0) for key, value := range refValueMap { - if strings.HasPrefix(key, fmt.Sprintf("jsondecode(%s.output)", address)) { + if strings.HasPrefix(key, fmt.Sprintf("jsondecode(%s.output).", address)) { res = append(res, types.Output{ OldName: key, Value: value, }) } - if strings.HasPrefix(key, fmt.Sprintf("%s.output_payload", address)) { + if strings.HasPrefix(key, fmt.Sprintf("%s.output.", address)) { res = append(res, types.Output{ OldName: key, Value: value, @@ -135,12 +135,13 @@ func getRefValueMap(p *tfjson.Plan) map[string]interface{} { for key, value := range propValueMap { refValueMap[key] = value } - } - } - if payloadObj := beforeMap["payload"]; payloadObj != nil { - propValueMap := getPropValueMap(payloadObj, fmt.Sprintf("%s.output_payload", prefix)) - for key, value := range propValueMap { - refValueMap[key] = value + } else { + if outputObj := beforeMap["output"]; outputObj != nil { + propValueMap := getPropValueMap(outputObj, fmt.Sprintf("%s.output", prefix)) + for key, value := range propValueMap { + refValueMap[key] = value + } + } } } } @@ -232,22 +233,22 @@ func getInputProperties(address string, p *tfjson.Plan) []string { props = append(props, key) } } - } - } - - if payloadObj := stateMap["payload"]; payloadObj != nil { - propValueMap := getPropValueMap(payloadObj, "") - propSet := make(map[string]bool) - for key := range propValueMap { - key = strings.TrimPrefix(key, ".") - if strings.HasPrefix(key, "tags") { - key = "tags" + } else { + if bodyObj := stateMap["body"]; bodyObj != nil { + propValueMap := getPropValueMap(bodyObj, "") + propSet := make(map[string]bool) + for key := range propValueMap { + key = strings.TrimPrefix(key, ".") + if strings.HasPrefix(key, "tags") { + key = "tags" + } + propSet[key] = true + } + for key := range propSet { + key = removeIndexOfProp(key) + props = append(props, key) + } } - propSet[key] = true - } - for key := range propSet { - key = removeIndexOfProp(key) - props = append(props, key) } }