diff --git a/hcl/parse.go b/hcl/parse.go index 537c1ed..a403865 100644 --- a/hcl/parse.go +++ b/hcl/parse.go @@ -241,7 +241,19 @@ func ParseAzapiResource(f hcl.File) (*[]AzapiResource, []error) { return nil, errs } - r.Body = body.AsString() + if body.Type() == cty.String { + r.Body = body.AsString() + } else { + logrus.Debugf("jsonencode for azapi_resource %s with dynamic schema body: %+v", r.Name, body) + // azapi dynamic schema is used + v, err := stdlib.JSONEncode(*body) + if err != nil { + errs = append(errs, fmt.Errorf("jsonencode for azapi dynamic schema: %+v", err)) + return nil, errs + } + + r.Body = v.AsString() + } } results = append(results, r) diff --git a/hcl/testdata/test.tf b/hcl/testdata/test.tf index 7ef9d0f..9306ce5 100644 --- a/hcl/testdata/test.tf +++ b/hcl/testdata/test.tf @@ -228,3 +228,51 @@ resource "azapi_resource" "virtualMachine2" { schema_validation_enabled = false response_export_values = ["*"] } + +resource "azapi_resource" "virtualMachine2_dynamic" { + type = "Microsoft.Compute/virtualMachines@2023-03-01" + parent_id = azapi_resource.resourceGroup.id + name = "${var.resource_name}-2-d" + location = var.location + body = { + properties = { + hardwareProfile = { + vmSize = "Standard_F2" + } + networkProfile = { + networkInterfaces = [ + { + id = azapi_resource.networkInterface.id + properties = { + primary = false + } + }, + ] + } + osProfile = { + adminPassword = var.password + adminUsername = "testadmin" + computerName = "hostname230630032848831820" + linuxConfiguration = { + disablePasswordAuthentication = false + } + } + storageProfile = { + imageReference = { + offer = "UbuntuServer" + publisher = "Canonical" + sku = "16.04-LTS" + version = "latest" + } + osDisk = { + caching = "ReadWrite" + createOption = "FromImage" + name = "myosdisk1" + writeAcceleratorEnabled = false + } + } + } + } + schema_validation_enabled = false + response_export_values = ["*"] +} diff --git a/hcl/testdata/test2.tf b/hcl/testdata/test2.tf index 3325455..cb19c38 100644 --- a/hcl/testdata/test2.tf +++ b/hcl/testdata/test2.tf @@ -30,3 +30,17 @@ resource "azapi_resource" "test" { } }) } + +resource "azapi_resource" "test_dynamic" { + type = "Microsoft.AppPlatform/spring/storages@2024-01-01-preview" + name = "acctest-ss-39" + parent_id = azurerm_spring_cloud_service.test.id + + body = { + properties = { + accountKey = azurerm_storage_account.test.primary_access_key + accountName = azurerm_storage_account.test.name + storageType = "StorageAccount" + } + } +}