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

Return null instead of an empty tftypes.List when the list is empty #368

Merged
merged 3 commits into from
Jan 27, 2022

Conversation

gdavison
Copy link
Contributor

@gdavison gdavison commented Jan 21, 2022

Empty lists returned by the provider were causing errors on any subsequent state refresh. The state stored a null value, whereas the generated plan would return an empty tftypes.List. This would cause Terraform to fail with an error like

│ Error: Provider produced invalid plan
│ 
│ Provider "registry.terraform.io/hashicorp/awscc" planned an invalid value for awscc_ec2_vpc.test.tags: planned value
│ cty.ListValEmpty(cty.Object(map[string]cty.Type{"key":cty.String, "value":cty.String})) for a non-computed attribute.
│ 
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.

This PR short-circuits the empty list case to return a null value of the list type instead of an empty list. This allows Terraform to continue and encounter behaviour such as seen in #301, where default values are not handled as expected because fields that should have default values or be computed are not marked as such in the Cloud Control schemas.

Terraform configuration

terraform {
  required_providers {
    awscc = {
      source  = "hashicorp/awscc"
    }
  }
}

provider "awscc" {
  region = "us-west-2"
}

resource "awscc_ec2_vpc" "test" {
  cidr_block = "10.0.0.0/16"
}

Before the update, running terraform apply and then terraform plan results in the error above.

After the update, the terraform plan succeeds, but shows the following diff

Note: Objects have changed outside of Terraform

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

  # awscc_ec2_vpc.test has changed
  ~ resource "awscc_ec2_vpc" "test" {
      + enable_dns_hostnames    = false
      + enable_dns_support      = true
        id                      = "vpc-056c110e94f3ed041"
      + instance_tenancy        = "default"
        # (5 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_ec2_vpc.test will be updated in-place
  ~ resource "awscc_ec2_vpc" "test" {
      - enable_dns_hostnames    = false -> null
      - enable_dns_support      = true -> null
        id                      = "vpc-056c110e94f3ed041"
      - instance_tenancy        = "default" -> null
        # (5 unchanged attributes hidden)
    }

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

Note that a second terraform apply still results in the error as reported in #301.

Closes #342
Closes #351
Closes #324

@gdavison gdavison marked this pull request as draft January 21, 2022 01:43
@gdavison gdavison marked this pull request as ready for review January 21, 2022 18:42
@ewbankkit ewbankkit self-assigned this Jan 27, 2022
@ewbankkit ewbankkit self-requested a review January 27, 2022 13:27
@ewbankkit ewbankkit removed their assignment Jan 27, 2022
Copy link
Contributor

@ewbankkit ewbankkit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants