-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
terraform show -json
omits data when nested blocks are used and SchemaConfigModeAttr
is set
#29022
Comments
Hi @noelbundick, Thanks for filing the issue. It does look like the fixup to turn the config block into an attribute is somehow causing the reference to be missed here. Since
|
Thanks for the quick reply. Looks like that indeed fixes up the schema mismatch, but it doesn't solve my problem. Context: I'm looking to analyze Terraform plans to better understand the semantics of what's going to happen during apply. Ex: Terraform does a great job at showing me that "NSG is going to block port 9000", but I'm looking to process that type of information to extract a higher-level insight, such as "This configuration change will block Application X from being accessed from your on-premises network", or "You've enabled HTTPS-only traffic on your storage account, but subnet X has an NSG rule that doesn't allow outbound TCP 443. This is possibly an invalid configuration" Currently, the docs prescribe the block syntax (which functions correctly during plan/apply) and all of the configuration that I've seen out in the wild also uses block syntax - save for the explicit empty array usage to delete all subnets. So I can fix my own files, but that's not particularly useful for anyone else There are 26 or so uses of Intuitively, I expected the data for Terraform show:
Terraform show -json:
Prettified JSON {
"format_version": "0.1",
"terraform_version": "1.0.0",
"planned_values": {
"root_module": {
"resources": [
{
"address": "azurerm_network_security_group.nsg1",
"mode": "managed",
"type": "azurerm_network_security_group",
"name": "nsg1",
"provider_name": "registry.terraform.io/hashicorp/azurerm",
"schema_version": 0,
"values": {
"location": "westus2",
"name": "nsg1",
"resource_group_name": "test-rg100",
"security_rule": [
{
"access": "allow",
"description": "value",
"destination_address_prefix": "*",
"destination_address_prefixes": [],
"destination_application_security_group_ids": [],
"destination_port_range": "80",
"destination_port_ranges": [],
"direction": "inbound",
"name": "allow http",
"priority": 100,
"protocol": "tcp",
"source_address_prefix": "*",
"source_address_prefixes": [],
"source_application_security_group_ids": [],
"source_port_range": "*",
"source_port_ranges": []
}
],
"tags": null,
"timeouts": null
}
},
{
"address": "azurerm_resource_group.rg",
"mode": "managed",
"type": "azurerm_resource_group",
"name": "rg",
"provider_name": "registry.terraform.io/hashicorp/azurerm",
"schema_version": 0,
"values": {
"location": "westus2",
"name": "test-rg100",
"tags": null,
"timeouts": null
}
},
{
"address": "azurerm_virtual_network.vnet1",
"mode": "managed",
"type": "azurerm_virtual_network",
"name": "vnet1",
"provider_name": "registry.terraform.io/hashicorp/azurerm",
"schema_version": 0,
"values": {
"address_space": [
"10.0.0.0/16"
],
"bgp_community": null,
"ddos_protection_plan": [],
"dns_servers": null,
"location": "westus2",
"name": "vnet1",
"resource_group_name": "test-rg100",
"subnet": [
{
"address_prefix": "10.0.0.0/24",
"name": "subnet1"
}
],
"tags": null,
"timeouts": null,
"vm_protection_enabled": false
}
}
]
}
},
"resource_changes": [
{
"address": "azurerm_network_security_group.nsg1",
"mode": "managed",
"type": "azurerm_network_security_group",
"name": "nsg1",
"provider_name": "registry.terraform.io/hashicorp/azurerm",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"location": "westus2",
"name": "nsg1",
"resource_group_name": "test-rg100",
"security_rule": [
{
"access": "allow",
"description": "value",
"destination_address_prefix": "*",
"destination_address_prefixes": [],
"destination_application_security_group_ids": [],
"destination_port_range": "80",
"destination_port_ranges": [],
"direction": "inbound",
"name": "allow http",
"priority": 100,
"protocol": "tcp",
"source_address_prefix": "*",
"source_address_prefixes": [],
"source_application_security_group_ids": [],
"source_port_range": "*",
"source_port_ranges": []
}
],
"tags": null,
"timeouts": null
},
"after_unknown": {
"id": true,
"security_rule": [
{
"destination_address_prefixes": [],
"destination_application_security_group_ids": [],
"destination_port_ranges": [],
"source_address_prefixes": [],
"source_application_security_group_ids": [],
"source_port_ranges": []
}
]
},
"before_sensitive": false,
"after_sensitive": {
"security_rule": [
{
"destination_address_prefixes": [],
"destination_application_security_group_ids": [],
"destination_port_ranges": [],
"source_address_prefixes": [],
"source_application_security_group_ids": [],
"source_port_ranges": []
}
]
}
}
},
{
"address": "azurerm_resource_group.rg",
"mode": "managed",
"type": "azurerm_resource_group",
"name": "rg",
"provider_name": "registry.terraform.io/hashicorp/azurerm",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"location": "westus2",
"name": "test-rg100",
"tags": null,
"timeouts": null
},
"after_unknown": {
"id": true
},
"before_sensitive": false,
"after_sensitive": {}
}
},
{
"address": "azurerm_virtual_network.vnet1",
"mode": "managed",
"type": "azurerm_virtual_network",
"name": "vnet1",
"provider_name": "registry.terraform.io/hashicorp/azurerm",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"address_space": [
"10.0.0.0/16"
],
"bgp_community": null,
"ddos_protection_plan": [],
"dns_servers": null,
"location": "westus2",
"name": "vnet1",
"resource_group_name": "test-rg100",
"subnet": [
{
"address_prefix": "10.0.0.0/24",
"name": "subnet1"
}
],
"tags": null,
"timeouts": null,
"vm_protection_enabled": false
},
"after_unknown": {
"address_space": [
false
],
"ddos_protection_plan": [],
"guid": true,
"id": true,
"subnet": [
{
"id": true,
"security_group": true
}
]
},
"before_sensitive": false,
"after_sensitive": {
"address_space": [
false
],
"ddos_protection_plan": [],
"subnet": [
{}
]
}
}
}
],
"configuration": {
"provider_config": {
"azurerm": {
"name": "azurerm",
"version_constraint": "2.61.0",
"expressions": {
"features": [
{}
]
}
}
},
"root_module": {
"resources": [
{
"address": "azurerm_network_security_group.nsg1",
"mode": "managed",
"type": "azurerm_network_security_group",
"name": "nsg1",
"provider_config_key": "azurerm",
"expressions": {
"location": {
"references": [
"azurerm_resource_group.rg"
]
},
"name": {
"constant_value": "nsg1"
},
"resource_group_name": {
"references": [
"azurerm_resource_group.rg"
]
}
},
"schema_version": 0
},
{
"address": "azurerm_resource_group.rg",
"mode": "managed",
"type": "azurerm_resource_group",
"name": "rg",
"provider_config_key": "azurerm",
"expressions": {
"location": {
"constant_value": "westus2"
},
"name": {
"constant_value": "test-rg100"
}
},
"schema_version": 0
},
{
"address": "azurerm_virtual_network.vnet1",
"mode": "managed",
"type": "azurerm_virtual_network",
"name": "vnet1",
"provider_config_key": "azurerm",
"expressions": {
"address_space": {
"constant_value": [
"10.0.0.0/16"
]
},
"location": {
"references": [
"azurerm_resource_group.rg"
]
},
"name": {
"constant_value": "vnet1"
},
"resource_group_name": {
"references": [
"azurerm_resource_group.rg"
]
}
},
"schema_version": 0
}
]
}
}
} |
In the debugger, I see that
I'm new to the codebase, so I'm not sure if the fix needs to happen before the call to |
Hi @noelbundick, can you please clarify for me exactly what it is you're looking for? The original post seems to claim that what you need are the missing Now I cannot guarantee that we will be able to restore these references in all cases. |
@jbardin I am looking for the JSON output generated by From an end-user point of view, block syntax is still "correct" because that's what the docs/samples explicitly instruct them to use (https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) Highlights in the diff below shows the data that is present in the plan but missing from the JSON: {
"address": "azurerm_virtual_network.vnet1",
"mode": "managed",
"type": "azurerm_virtual_network",
"name": "vnet1",
"provider_config_key": "azurerm",
"expressions": {
"address_space": {
"constant_value": [
"10.0.0.0/16"
]
},
"location": {
"references": [
"azurerm_resource_group.rg"
]
},
"name": {
"constant_value": "vnet1"
},
"resource_group_name": {
"references": [
"azurerm_resource_group.rg"
]
},
+ "subnet": [
+ {
+ "address_prefix": {
+ "constant_value": "10.0.0.0/24"
+ },
+ "name": {
+ "constant_value": "subnet1"
+ },
+ "security_group": {
+ "references": [
+ "azurerm_network_security_group.nsg1"
+ ]
+ }
} |
@noelbundick sorry, I did not mean to imply that changing all configuration to attribute syntax was the final solution, but it does offer a workaround if you are managing your own config. This issue is still open and labeled as a bug. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Terraform Version
Terraform Configuration Files
Debug Output
https://gist.github.com/noelbundick/9cdb1d7960b6970f2c861fd7e16f1626
Crash Output
No crash
Expected Behavior
The JSON output for the plan should include the references from vnet1.subnet1 -> nsg1
Actual Behavior
The schema for
azurerm_virtual_network
explicitly definessubnet
asSchemaConfigModeAttr
, so it gets dropped when marshaling to JSON. There is no way to see the link from subnet to NSG in the generated JSON for the plan.Relevant code paths:
terraform/internal/legacy/helper/schema/core_schema.go
Line 57 in a945b37
Steps to Reproduce
terraform init
terraform plan -out=azurerm.tfplan
terraform show -json azurerm.tfplan
Additional Context
azurerm_virtual_network
supports usingsubnet
both as a nested block as well as an attribute (setting to[]
to delete subnets): https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network#subnetChanging
SchemaConfigModeAttr
toSchemaConfigModeAuto
resolves the issue with JSON exports, but breaks the ability to delete subnets. @tombuildsstuff indicated this is a bug in Terraform Core vs. being azurerm provider specific.References
terraform show -json
terraform-provider-azurerm#12324 (comment)cc: @rguthriemsft @Joshua-Phelps
The text was updated successfully, but these errors were encountered: