franckverrot / terraform-provider-stripe

A Terraform Provider for Stripe
https://registry.terraform.io/providers/franckverrot/stripe/latest
Mozilla Public License 2.0
242 stars 51 forks source link

readme possible improvement: how to use "test" mode #14

Closed allanlegalstart closed 5 years ago

allanlegalstart commented 5 years ago

The readme says:

Migrate resources from test mode to live mode

It would be great if the readme explained how to do this :)

franckverrot commented 5 years ago

I’m not sure what @clintonb-stripe actually. I’ll let him chime in. I interpreted it as “you can set your resources active” when I did the code review. Thanks!

clintonb-stripe commented 5 years ago

The idea behind that line is that you could create resources with with you test API token and do your testing. Once your testing is complete, you can run the Terraform with your live API key. In hindsight, this is not well thought out on my part.

I am fine with removing that line from the README since it is not normal for Terraform users to change API keys, and a user would essentially need to maintain two copies of their Terraform configuration to avoid conflicts.

franckverrot commented 5 years ago

I’ll delete that line. Thanks for responding so promptly @clintonb-stripe !

slobo commented 4 years ago

I'm assuming live vs prod should be done using workspaces (providing different API keys when running in different workspaces), or is there another mechanism recommended to test out the changes before push to live?

Thanks

(sorry to dig up the old thread)

clintonb-stripe commented 4 years ago

@slobo yes, using workspaces makes sense. That said, the functionality is new to me, and I have not tested it. Can you test this, and report back on whether it works?

franckverrot commented 4 years ago

👍 on what @clintonb-stripe just said. I'd also love to document how to use workspaces in the README/wiki because it definitely looks like something people may want to use. Thanks folks!

slobo commented 4 years ago

So this worked for me.

product.tf

variable "stripe_api_token" {}

provider "stripe" {
  api_token = "${var.stripe_api_token}"
}

resource "stripe_product" "my_product" {
  name = "Test Product"
  type = "service"
}

Testing

terraform init
terraform workplace select Testing || terraform workplace new Testing
terraform apply
# When prompted, enter the test token, ex. rk_test_XXXXXXXX

Live

terraform init
terraform workplace select Live || terraform workplace new Live
terraform apply
# When prompted, enter the live token, ex. rk_live_YYYYYYY

Stripe API token in scripts

Depending on your needs, you have options on automatically passing the API token to terraform apply

I tend to keep a copy of test token in a .tfvars file for developer convenience. But on the build server we provide live token via an environment variable. YMMV.

Collaborating (Shared State)

If you are collaborating with a team, make sure to store the state in some central location, say in an S3 bucket. Workspaces work with remote storage without problem.

clintonb-stripe commented 4 years ago

This is great. Thanks @slobo!

@franckverrot should we update the README to state that the provider is compatible with workspaces?

goodhoko commented 1 year ago

Sorry, to digup this once again. Just wanted to note that I was able to solve this (IMO bit more elegantly) using provider aliases. It's as simple as this:

variable "stripe_test_secret_api_key" {}

provider "stripe" {
  alias = "test"
  api_token = var.stripe_test_secret_api_key
}

variable "stripe_live_secret_api_key" {}

provider "stripe" {
  alias = "live"
  api_token = var.stripe_live_secret_api_key
}

# A webhook in test mode
resource "stripe_webhook_endpoint" "my_webhook" {
  # Use the test mode provider.
  provider = stripe.test
  ...

}

# A webhook in live mode
resource "stripe_webhook_endpoint" "my_webhook" {
  # Use the test mode provider.
  provider = stripe.live
  ...
}

Hope this helps. .)