hashicorp / terraform-provider-awscc

Terraform AWS Cloud Control provider
https://registry.terraform.io/providers/hashicorp/awscc/latest/docs
Mozilla Public License 2.0
259 stars 119 forks source link

CloudFormation schema properties of type `object` with no nested properties are JSON strings #514

Closed ewbankkit closed 1 year ago

ewbankkit commented 2 years ago

Community Note

Description

The Terraform schema code generator currently handles CFN properties of type object with no sub-properties as a Terraform TypeMap: https://github.com/hashicorp/terraform-provider-awscc/blob/7f1d724fa0a081556fd48fcfa4d6c2a65ce8ae1a/internal/provider/generators/shared/codegen/emitter.go#L503-L512

In actuality, all resources which declare such properties are expecting JSON strings ({"key": "value", ...}). Modify the code generator and maybe runtime handling?

https://github.com/hashicorp/terraform-provider-awscc/blob/7f1d724fa0a081556fd48fcfa4d6c2a65ce8ae1a/internal/generic/translate.go#L223-L233

https://github.com/hashicorp/terraform-provider-awscc/blob/96c7ab7e79afe69fd4afaf15fe8d437989671967/internal/generic/translate.go#L127-L135

Relates: https://github.com/hashicorp/terraform-provider-awscc/issues/509.

ewbankkit commented 2 years ago

Blindly changing this affects these attributes:

Spot-checking some of the non-obvious ones:

suggests that this change makes sense.

ewbankkit commented 2 years ago

Naive implementation leads to:

│ Error: AWS SDK Go Service Operation Unsuccessful
│ 
│   with awscc_s3_access_point.test,
│   on main.tf line 77, in resource "awscc_s3_access_point" "test":
│   77: resource "awscc_s3_access_point" "test" {
│ 
│ Calling Cloud Control API service CreateResource operation returned: operation error CloudControl: CreateResource, https response error
│ StatusCode: 400, RequestID: 20810c35-6eb2-40b6-b0f4-4abc5bacad21, api error ValidationException: Model validation failed (#/Policy: expected
│ type: JSONObject, found: String)

"Policy": {"Version": "2008-10-17", ... } vs. "Policy": "{\"Version\": \"2008-10-16\", ...}".

This can be mitigated by handling the special case of attributes of type JSONString in the generic Terraform to Cloud Control translator (internal/generic/translate.go) where the value will get marshalled as map[string]interface{}, not string.

ewbankkit commented 2 years ago

Currently a solution to this is blocked until https://github.com/hashicorp/terraform-plugin-framework/pull/178 has been implemented in the Framework, scheduled for v1.0.0. Without this enhancement terraform plan (resource Read) will always show a diff.

ewbankkit commented 2 years ago

https://github.com/hashicorp/terraform-provider-awscc/pull/537 (and the use of jsonencode(jsondecode(...)) in configuration) addresses this just for the AWS::NetworkManager::CoreNetwork.PolicyDocument special case as a breaking change (https://github.com/hashicorp/terraform-provider-awscc/issues/550) was introduced for this property.

kadrach commented 1 year ago

Semantic equality is now supported

See also https://github.com/hashicorp/terraform-plugin-framework/issues/803