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

Access private state when testing resources and data sources #321

Open
marshallford opened this issue Apr 6, 2024 · 3 comments
Open

Access private state when testing resources and data sources #321

marshallford opened this issue Apr 6, 2024 · 3 comments
Labels
enhancement New feature or request upstream-terraform Issues that relate to Terraform CLI changes

Comments

@marshallford
Copy link

terraform-plugin-testing version

❯ go list -m github.com/hashicorp/terraform-plugin-testing/...
github.com/hashicorp/terraform-plugin-testing v1.7.0

Use cases

Utilize and/or validate private state when testing resources or data sources.

Attempted solutions

N/A

Proposal

N/A

References

N/A

@marshallford marshallford added the enhancement New feature or request label Apr 6, 2024
@austinvalle
Copy link
Member

For future readers, there is an upstream blocker to exposing private state data in terraform-plugin-testing.

As of v1.7.0, the testing framework uses the terraform show -json command to retrieve plan and state data, which is then marshaled to terraform-json structs for usage in plan/state checks.

The terraform show -json command currently isn't a "passthrough" for all data in the state file and the private field has not been added to the JSON output. Before we can add support in the testing framework, the Terraform CLI will need to be updated to return this private field.

Example State

{
  "version": 4,
  "terraform_version": "1.8.0",
  "serial": 3,
  "lineage": "dd9272a9-ab86-4166-d83b-d08ba2f78f34",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "examplecloud_thing",
      "name": "test",
      "provider": "provider[\"registry.terraform.io/austinvalle/sandbox\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "cities": {
              "computed": "true",
              "season": "spring"
            },
            "id": "123",
            "name": "john"
          },
          "sensitive_attributes": [],
          "private": "eyJoZWxsbyI6ImV5SnJaWGt4SWpvZ2RISjFaWDA9In0="
        }
      ]
    }
  ],
  "check_results": null
}

State show output

 $ terraform show -json terraform.tfstate | jq

{
  "format_version": "1.0",
  "terraform_version": "1.8.0",
  "values": {
    "root_module": {
      "resources": [
        {
          "address": "examplecloud_thing.test",
          "mode": "managed",
          "type": "examplecloud_thing",
          "name": "test",
          "provider_name": "registry.terraform.io/austinvalle/sandbox",
          "schema_version": 0,
          "values": {
            "cities": {
              "computed": "true",
              "season": "spring"
            },
            "id": "123",
            "name": "john"
          },
          "sensitive_values": {
            "cities": {}
          }
        }
      ]
    }
  }
}

@austinvalle austinvalle added the upstream-terraform Issues that relate to Terraform CLI changes label Apr 8, 2024
@marshallford
Copy link
Author

Thank you for the info and apologies if this was already well known!

@austinvalle
Copy link
Member

Thank you for the info and apologies if this was already well known!

No worries! We don't have this specific situation of "private field missing" documented anywhere, so thanks for opening the issue! We can use this to gauge general interest in the feature from a provider testing perspective.

An issue can be created in the main Terraform repository to ask for that support in terraform show -json. Once added over there, updating terraform-json and this testing framework will be relatively straightforward 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request upstream-terraform Issues that relate to Terraform CLI changes
Projects
None yet
Development

No branches or pull requests

2 participants