hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.42k stars 9.36k forks source link

Terraform mock provider default_tags #35139

Closed jiba21 closed 3 weeks ago

jiba21 commented 3 weeks ago

Terraform Version

Terraform v1.7.5

Terraform Configuration Files

mock_provider "aws" {
  alias = "mock-provider"

  mock_resource "aws_parameter_store" {
    defaults = {
      parameter_store_mock_arn = "arn:aws:ssm:::parameter/global/mock/test/TEST_USERNAME"
    }
  }

  override_data {
    target = data.aws_default_tags.provider.tags
    values = {
      project              = ""
      application          = ""
      environment          = ""
    }
  }
}

run "sets_correct_arn" {
  command = plan

  providers = {
    aws = aws.mock-provider
  }

  variables {
    config = {
      path_file  = "secrets/project_name"
      name_files = ["encrypted.yaml"]
      extra_tags = {}
    }
  }

  assert {
    condition     = "${"arn:aws:ssm:eeeee:aaaaaa:parameter"}${module.parameterstore.this.parameters_map[0].name}" ==  == defaults.parameter_store_mock_arn 
    error_message = "Incorrect parameter store ARN"
  }
}

Debug Output

╷ │ Error: Invalid address │ │ on parameter_store.tftest.hcl line 42, in mock_provider "aws": │ 42: target = data.aws_default_tags.provider.tags │ │ Resource instance key must be given in square brackets. ╵

Expected Behavior

It was great something


mock_provider "aws" {
  alias = "mock-provider"

  mock_resource "aws_parameter_store" {
    defaults = {
      parameter_store_mock_arn = "arn:aws:ssm:::parameter/global/mock/test/TEST_USERNAME"
    }
  }

  override_default_tags {
    tags = {
      project              = ""
      application          = ""
      environment          = ""
    }
  }
}

run "sets_correct_arn" {
  command = plan

  providers = {
    aws = aws.mock-provider
  }

  variables {
    config = {
      path_file  = "secrets/project_name"
      name_files = ["encrypted.yaml"]
      extra_tags = {}
    }
  }

  assert {
    condition     = "${"arn:aws:ssm:eeeee:aaaaaa:parameter"}${module.parameterstore.this.parameters_map[0].name}" ==  == defaults.parameter_store_mock_arn 
    error_message = "Incorrect parameter store ARN"
  }
}

 Error: Unsupported block type
│ 
│   on parameter_store.tftest.hcl line 39, in mock_provider "aws":
│   39:   override_default_tags {
│ 
│ Blocks of type "override_default_tags" are not expected here.
╵

### Actual Behavior

╷
│ Error: Invalid address
│ 
│   on parameter_store.tftest.hcl line 42, in mock_provider "aws":
│   42:     target = data.aws_default_tags.provider.tags
│ 
│ Resource instance key must be given in square brackets.
╵

### Steps to Reproduce

terraform test

### Additional Context

_No response_

### References

_No response_
jbardin commented 3 weeks ago

Hi @jiba21,

The Resource instance key must be given in square brackets error is because you have too many segments in the data source address. The address of that data source can only be data.aws_default_tags.provider. If there are other instances of that data source, you would put those in brackets like data.aws_default_tags.provider["tags"], but in this case it looks like a typo where you were working with the tags attribute of that data source.

We use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help.

Thanks!