I would really love to hear some feedback about the design of this provider.
tl;dr
Right now I have a provider that hits the rancher api at /v1
and you can set the credentials for that.
provider "rancher" {
api_url = "http://rancher.my-domain.com/v1"
access_key = "${var.rancher_access_key}"
secret_key = "${var.rancher_secret_key}"
}
With this provider I should be able to implement any resource that hits the api at the root level. For example environments.
resource "rancher_environment" "default" {
name = "staging"
description = "The staging environment"
orchestration = "cattle"
}
But then I want to implement resources that depend on an environment. For example: registries, stacks, etc (most of the resources will hit the api to v1/projects/<project_id>
(projects = environment in the api).
So here is where I don’t know if the design of the provider should follow the design of the api or not.
The first options is to pass the environment to the resources, like this:
resource "rancher_registry" "foo" {
name = "foo"
description = "registry test"
server_address = "http://foo.com:8080"
environment_id = "1a26"
}
But what happens if I want to use environment credentials instead of the global ones?? Should I pass the credentials to every resource?
The other option is that the provider can define an environment, it would look something like this.
provider "rancher" {
api_url = "http://rancher.my-domain.com/v1"
environment_id = "1a26"
access_key = "${var.rancher_access_key}"
secret_key = "${var.rancher_secret_key}"
}
resource "rancher_registry" "foo" {
name = "foo"
description = "registry test"
server_address = "http://foo.com:8080"
}
The problem is how I create the environment in the first place…
One alternative would be to leverage the alias features for providers so I can define multiple providers.
# Provider to hit the global api
provider "rancher" {
api_url = "http://rancher.my-domain.com/v1"
access_key = "${var.rancher_access_key}"
secret_key = "${var.rancher_secret_key}"
}
# create an environment
resource "rancher_environment" "staging" {
name = "staging"
description = "The staging environment"
orchestration = "cattle"
}
# provider to hit the environment api
provider "rancher" {
alias = "staging"
api_url = "http://rancher.my-domain.com/v1"
environment_id = "${rancher_environment.staging.id}"
access_key = "${var.rancher_access_key}"
secret_key = "${var.rancher_secret_key}"
}
resource "rancher_registry" "foo" {
provider = "rancher.staging"
name = "foo"
description = "registry test"
server_address = "http://foo.com:8080"
}
I don’t really know it terraform let me initialize a provider interpolating a variable from a resource of other providers.
thanks for the feedback!