digitalocean / terraform-provider-digitalocean

Terraform DigitalOcean provider
https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs
Mozilla Public License 2.0
500 stars 268 forks source link

Default region and project for resources #226

Open syntaqx opened 5 years ago

syntaqx commented 5 years ago

Feature request: Provider default region and project

Sorry if I'm doing this wrong

Similar to that of other Terraform providers, it would be fantastic to be able to provide default, overridable values for resource region and project.

These variables are very often the same within a single state, I'd love to not have to specify them for each resource as well as not need to configure the value for modules relying on my provider.

As an added benefit, this also creates consistency between other DO integrations, such as docker-machine and how the environment variables are used when creating resources.

Example

Usage

provider "digitalocean" {
    region  = "nyc3" # or DIGITALOCEAN_REGION
    project = "my-do-project" # or DIGITALOCEAN_PROJECT
}

Arguments

PR Template stuff

These are probably not useful to you in this context, but here:

Terraform Version v0.11.13

Affect Resources(s)

region

project

Let me know if I did this right!

lfarnell commented 5 years ago

I would like to take a stab at this. Looking at other providers it seems like its either set the region at the provider level(AWS) or you can set the region at the provider level but still override the region at the resource level(GCP). I believe it would be a better experience to follow the GCP way of doing this. Basically, for each resource, the region argument would become optional. If the value is null, we use the value provided in the provider. Otherwise, we override the region.

I think I re-iterated what you said @syntaqx but just want to make my intentions explicit.

eddiezane commented 5 years ago

Sorry, I meant to update here and never got around to doing it.

I took a stab at implementing this and the code came out pretty messy—bunch of if checks and extra logic. There's also a nasty side effect because you have to mark the region attribute on the resources as computed/optional. Doing so causes region to be completely absent from the plan which to me is a no-go.

This spun off a conversation on the Terraform committers Slack because I think the right way for this to work is to either have multiple provider blocks per region or have proper support for inheriting provider level/default attributes into resources. I'm supposed to open an issue on Terraform core about this for research and discussion.

With all that said, @lfarnell, I'd be very interested in hearing/seeing your thoughts or implementation.

lfarnell commented 5 years ago

@eddiezane Let me hack on this tonight and see what I can figure out. I will try to see how the GCP folks accomplished this. I'll update this issue once I have a better understanding of the changes I envision.

lfarnell commented 5 years ago

So I did some hacking last night and found some interesting results similar to @eddiezane.

  1. I added the region attribute on the provider and marked it as optional.
  2. Marked the region attribute on the resource(droplet in my case) as computed and/or optional. I also made it so that if the resource couldn't find the value of the region attribute that it would use the region from the provider. My droplet was created successfully but the plan did not present the region when I only had the region in the provider. I want to believe that this is somewhat intended by Terraform but I don't have any proof of this.
  3. When I moved the region from the provider and added it to the resource it began to work as previously. This still included that changes at the provider level.

I did some additional digging around some other providers. AWS sets the region by default in the provider and that if multiple regions are needed that you can alias them and include them in the resources. I also noticed that the region(or equivalent to in AWS parlance) does not show up in the plan according to examples and documentation that I have seen. I still want to go down the rabbit hole a little more to see if anything more can be done.

syntaqx commented 5 years ago

Exciting updates, hoping this is something we're able to get working.