Skip to content

Commit

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

This reverts commit 5cf0788.

* `azapi_*` - the `body` and `output` fields support dynamic schema

* fix tests
  • Loading branch information
ms-henglu authored Apr 22, 2024
1 parent 5cf0788 commit b525549
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
28 changes: 14 additions & 14 deletions cmd/migrate_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ resource "azapi_resource" "test" {
type = "SystemAssigned"
}
payload = {
body = {
properties = {
sku = {
name = local.AutomationSku
Expand All @@ -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" {
Expand All @@ -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())
}
Expand Down Expand Up @@ -684,7 +684,7 @@ resource "azapi_resource" "test" {
type = "SystemAssigned"
}
payload = {
body = {
properties = {
sku = {
name = each.value.sku
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -837,7 +837,7 @@ resource "azapi_resource" "test" {
type = "SystemAssigned"
}
payload = {
body = {
properties = {
sku = {
name = "Basic"
Expand Down Expand Up @@ -868,7 +868,7 @@ resource "azapi_resource" "test1" {
type = "SystemAssigned"
}
payload = {
body = {
properties = {
sku = {
name = "Basic"
Expand Down
4 changes: 2 additions & 2 deletions tf/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
47 changes: 24 additions & 23 deletions tf/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
}
}
}
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit b525549

Please sign in to comment.