Skip to content
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

Default attribute values #119

Closed
ewbankkit opened this issue Aug 20, 2021 · 4 comments · Fixed by #150
Closed

Default attribute values #119

ewbankkit opened this issue Aug 20, 2021 · 4 comments · Fixed by #150
Assignees
Labels
code-generation Relates to the conversion of CloudFormation schema to Terraform schema at buildtime. runtime-handling Relates to runtime handling and conversion of Terraform configuration to CloudFormation. upstream-aws Unable to proceed due to missing or broken functionality from an AWS dependency. upstream-plugin-framework Unable to proceed due to missing or broken functionality from terraform-plugin-framework
Milestone

Comments

@ewbankkit
Copy link
Contributor

There are a couple of outstanding issues around default attribute values.
Firstly, although the CloudFormation resource type schema allows a property's JSON Schema default value to be specfied, relatively few of the current schemas are doing so.
For example with AWS::KMS::Key:

resource "awscc_kms_key" "test" {
  key_policy = jsonencode(...)
}
% terraform apply
...
% terraform plan
...
awscc_kms_key.test: Refreshing state... [id=64dc65ee-9e6b-4511-bc2f-8165dd84a38c]

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the last "terraform apply":

  # awscc_kms_key.test has been changed
  ~ resource "awscc_kms_key" "test" {
      + enable_key_rotation = false
      + enabled             = true
        id                  = "64dc65ee-9e6b-4511-bc2f-8165dd84a38c"
      + key_spec            = "SYMMETRIC_DEFAULT"
      + key_usage           = "ENCRYPT_DECRYPT"
      + multi_region        = false
      + tags                = [
        ]
        # (3 unchanged attributes hidden)
    }

Unless you have made equivalent changes to your configuration, or ignored the relevant attributes using ignore_changes, the following plan may include actions to undo
or respond to these changes.

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # awscc_kms_key.test will be updated in-place
  ~ resource "awscc_kms_key" "test" {
      - enable_key_rotation = false -> null
      - enabled             = true -> null
        id                  = "64dc65ee-9e6b-4511-bc2f-8165dd84a38c"
      - key_spec            = "SYMMETRIC_DEFAULT" -> null
      - key_usage           = "ENCRYPT_DECRYPT" -> null
      - multi_region        = false -> null
      - tags                = [
        ]
        # (3 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

No default is specified for the KeySpec or KeyUsage properties (although a value is mentioned in the description).

The second issue is that even if a default value were specified we would do nothing with it 😄.
hashicorp/terraform-plugin-framework#34, hashicorp/terraform-plugin-framework#102 will add Plugin Framework support which we must exploit.

@ewbankkit ewbankkit added this to the v0.1.0 milestone Aug 20, 2021
@ewbankkit ewbankkit added code-generation Relates to the conversion of CloudFormation schema to Terraform schema at buildtime. runtime-handling Relates to runtime handling and conversion of Terraform configuration to CloudFormation. upstream-aws Unable to proceed due to missing or broken functionality from an AWS dependency. upstream-plugin-framework Unable to proceed due to missing or broken functionality from terraform-plugin-framework labels Aug 20, 2021
@ewbankkit ewbankkit self-assigned this Sep 10, 2021
@ewbankkit
Copy link
Contributor Author

@ewbankkit
Copy link
Contributor Author

Most default values are primitive values:

internal/service/cloudformation/schemas/AWS_GlobalAccelerator_Accelerator.json:      "default": "IPV4",
internal/service/cloudformation/schemas/AWS_GlobalAccelerator_Accelerator.json:      "default": true,
internal/service/cloudformation/schemas/AWS_GlobalAccelerator_EndpointGroup.json:          "default": 100

There are some that are arrays of simple values:

internal/service/cloudformation/schemas/AWS_CloudFront_Distribution.json

                "CachedMethods": {
                    "default": [
                        "GET",
                        "HEAD"
                    ],
                    "items": {
                        "type": "string"
                    },
                    "type": "array",
                    "uniqueItems": false
                },

or objects

internal/service/cloudformation/schemas/AWS_DataSync_LocationNFS.json

    "MountOptions": {
      "$ref": "#/definitions/MountOptions",
      "default": {
        "Version": "AUTOMATIC"
      }
    },

@ewbankkit
Copy link
Contributor Author

Note that since default values are implemented as a plan modification, attributes with default values must be marked as Computed, else Terraform reports errors such as

awscc_kms_key.test: Refreshing state... [id=2cef7028-5142-4a72-8902-a4ed89784e57]
╷
│ Error: Provider produced invalid plan
│ 
│ Provider "registry.terraform.io/hashicorp/awscc" planned an invalid value for awscc_kms_key.test.key_usage: planned value cty.StringVal("ENCRYPT_DECRYPT") for a non-computed
│ attribute.
│ 
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.
╵

@ewbankkit
Copy link
Contributor Author

ewbankkit commented Sep 13, 2021

Note that there are some resources that declare required properties that have default values:

internal/service/cloudformation/schemas/AWS_Cassandra_Table.json

    "Mode": {
      "description": "Capacity mode for the specified table",
      "type": "string",
      "enum": [
        "PROVISIONED",
        "ON_DEMAND"
      ],
      "default": "ON_DEMAND"
    },
    "BillingMode": {
      "type": "object",
      "properties": {
        "Mode": {
          "$ref": "#/definitions/Mode"
        },
        "ProvisionedThroughput": {
          "$ref": "#/definitions/ProvisionedThroughput"
        }
      },
      "required": [
        "Mode"
      ],
      "additionalProperties": false
    },

We will emit these as Optional/Computed attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-generation Relates to the conversion of CloudFormation schema to Terraform schema at buildtime. runtime-handling Relates to runtime handling and conversion of Terraform configuration to CloudFormation. upstream-aws Unable to proceed due to missing or broken functionality from an AWS dependency. upstream-plugin-framework Unable to proceed due to missing or broken functionality from terraform-plugin-framework
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant