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

Add task_env to provider blocks #157

Merged
merged 4 commits into from
Dec 14, 2020
Merged

Add task_env to provider blocks #157

merged 4 commits into from
Dec 14, 2020

Conversation

findkim
Copy link
Contributor

@findkim findkim commented Dec 14, 2020

Adds support for terraform_provider blocks to configure provider environment variables using the meta block task_env. Dynamic config source is supported within the task_env block.

terraform_provider "foo" {
  address = "foo.example.com"
  task_env {
     "FOO_TOKEN" = "{{ env \"CTS_FOO_TOKEN\" }}"
  }
}

This is useful to keep sensitive provider arguments out of the generated terraform.tfvars for each task. The environment variables are passed to Terraform workspaces that use that provider. The envvars are not accessible to other tasks or set to the global env scope.

The task_env is also useful to decouple the expected environment name for Terraform providers. A few use cases here:

  • CTS configuring multiple instances of a provider for different tasks. Without the task_env block, the provider instances would consume from the same environment value which may not be desired in preference for different tokens per instance.
  • An existing process is consuming from an env name that a provider in CTS also consumes from but requires different configuration.

@findkim findkim added the enhancement New feature or request label Dec 14, 2020
@findkim findkim added this to the Technology Preview 2 milestone Dec 14, 2020
@findkim findkim requested a review from a team December 14, 2020 17:10
Copy link
Member

@lornasong lornasong left a comment

Choose a reason for hiding this comment

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

Looks good! Excited for this feature! I'm not sure what remaining work if any you have in mind for this feature. The logic is pretty straightforward, but I was thinking it would be nice (not necessarily this PR) to have some unit tests for terraform_provider.go. Slightly motivated by our driver package coverage being a little low (~35%).

// TerraformProviderBlock contains provider arguments and environment variables
// for the Terraform provider.
type TerraformProviderBlock struct {
block hcltmpl.NamedBlock
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion (minor optional, not blocker) to name block to something else - like arguments mentioned in the comments. For me, at first read, block sounded broader/inclusive of env i.e. environment variables are declared in the block. For me the block splits into environment and provider config information. I might be misunderstanding 'block' terminology though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Heh not "block"-er. To provide a bit more background on why I chose to keep block here. Block is an HCL terminology for an object

object {
  arg = "value"
}

And in context of Terraform configuration, it would be the provider block which includes an HCL label to the block. I felt that renaming to arguments would imply only the body of the block and not the HCL label/provider name, which isn't the case.

provider "foo" {
  arg = "value"
}

Block in both of these context refers to the syntax for a configuration file and I would believe is exclusive of the environment. However, since this adds CTS support for task_env as a meta-argument, which is not directly supported by the provider, the task_env is initially in the block when loading the user-CTS configs, which then gets removed before it gets written as TF config files.

Does block sound a bit more ok in context of HCL and Terraform syntax?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for taking the time to share your thinking behind it! To your point about task_env initially being in the block, when I came across NewTerraformProviderBlock, I understood the relationship between the two, so it was more of an initial thing.

@findkim
Copy link
Contributor Author

findkim commented Dec 14, 2020

Thanks Lorna for the review! No remaining work. The task_env block is evaluated for dynamic configs and fetched from external sources along with the other configs (#143), then the changes in this PR splits the meta-arguments from the provider config that ultimately gets written to Terraform configuration files.

On the point about tests for terraform_provider.go, I don't think it's valuable to write tests for functions that are wrappers around creating maps and lists. The bulk of the uncovered code in the driver package would require integration tests (TF download and generating TF files, which are already tested individually in the templates/tftmpl package)

@lornasong
Copy link
Member

Sorry for the confusion - the question about the remaining work was more that I didn't want to assume you didn't have other plans for tests. Yea, it sounds like this feature is complete! I look at test coverage when deciding to use a repo so had thought that if others were like me they might have reservations. I think this is personal preference so ok with you to go ahead and merge when ready

@findkim
Copy link
Contributor Author

findkim commented Dec 14, 2020

Appreciate the follow-up -- I'll go ahead and merge

Increasing the coverage is a good goal but I don't like to strive for numbers for the sake of numbers. I can see value in writing an integration test for generation of all TF files. However for the TF downloads, if we do have an integration test in the future, it's not valuable to be included as a part of the CI tests which would still keep the coverage on the lower side.

@findkim findkim merged commit 11b8b5a into master Dec 14, 2020
@findkim findkim deleted the task-env branch December 14, 2020 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants