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

feat: support create/delete project #6

Merged
merged 5 commits into from
Jan 31, 2024
Merged

Conversation

S96EA
Copy link
Contributor

@S96EA S96EA commented Jan 26, 2024

What kind of change does this PR introduce?

suuport create/delete project

@S96EA S96EA requested a review from a team as a code owner January 26, 2024 09:24
@S96EA
Copy link
Contributor Author

S96EA commented Jan 26, 2024

Added a basic version that works well for creating or deleting projects. Additional tests need to be implemented.

@S96EA S96EA changed the title [wip] feat: support create/delete project feat: support create/delete project Jan 29, 2024
@S96EA
Copy link
Contributor Author

S96EA commented Jan 29, 2024

Added a basic version that works well for creating or deleting projects. Additional tests need to be implemented.

Completed

@sweatybridge
Copy link
Collaborator

sweatybridge commented Jan 30, 2024

I made some minor edits based on my local testing. The rest LGTM.

There's still a small issue where remote project states are not synced to local. For eg.

  1. Run terraform apply with the following main.tf
terraform {
  required_providers {
    supabase = {
      source  = "supabase/supabase"
      version = "~> 1.0"
    }
  }
}

provider "supabase" {
  access_token = file("${path.module}/access-token")
}

resource "supabase_project" "test" {
  organization_id   = "nknnyrtlhxudbsbuazsu"
  name              = "tf-project"
  database_password = "example-password"
  region            = "ap-southeast-1"
}
  1. Delete the generated local state by rm terraform.tfstate. This is equivalent to someone cloning the TF config to start from a fresh state.

  2. Run terraform plan and observe that TF wants to create a new test project.

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # supabase_project.staging will be created
  + resource "supabase_project" "staging" {
      + database_password = (sensitive value)
      + id                = (known after apply)
      + name              = "tf-project"
      + organization_id   = "nknnyrtlhxudbsbuazsu"
      + region            = "ap-southeast-1"
    }

I think this happens because we use a generated project ID returned from API. If the local state file doesn't exist, the Id field will default to empty string so it cannot match any of the projects returned from API.

I'm not sure what is the best solution here so any ideas will be appreciated.

@S96EA
Copy link
Contributor Author

S96EA commented Jan 30, 2024

I made some minor edits based on my local testing. The rest LGTM.

There's still a small issue where remote project states are not synced to local. For eg.

  1. Run terraform apply with the following main.tf
terraform {
  required_providers {
    supabase = {
      source  = "supabase/supabase"
      version = "~> 1.0"
    }
  }
}

provider "supabase" {
  access_token = file("${path.module}/access-token")
}

resource "supabase_project" "test" {
  organization_id   = "nknnyrtlhxudbsbuazsu"
  name              = "tf-project"
  database_password = "example-password"
  region            = "ap-southeast-1"
}
  1. Delete the generated local state by rm terraform.tfstate. This is equivalent to someone cloning the TF config to start from a fresh state.
  2. Run terraform plan and observe that TF wants to create a new test project.
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # supabase_project.staging will be created
  + resource "supabase_project" "staging" {
      + database_password = (sensitive value)
      + id                = (known after apply)
      + name              = "tf-project"
      + organization_id   = "nknnyrtlhxudbsbuazsu"
      + region            = "ap-southeast-1"
    }

I think this happens because we use a generated project ID returned from API. If the local state file doesn't exist, the Id field will default to empty string so it cannot match any of the projects returned from API.

I'm not sure what is the best solution here so any ideas will be appreciated.

I attempted to replicate the issue and discovered that Terraform does not invoke the Read method in the absence of a Terraform state file when run terraform plan. Consequently, this prevents the utilization of a fallback approach, such as matching a project using a combination of organization and name when an ID is not available.
However, we can circumvent this limitation by employing the import command to generate a Terraform state file. For instance, you can use terraform import supabase_project.new ztfwpfmyrtiubpbeagvu to accomplish this.

@sweatybridge
Copy link
Collaborator

Thanks for suggesting the import command. I ended up adding the import block to main.tf to keep these version controlled. It does require a recent version of terraform but I think it is a good solution.

@sweatybridge sweatybridge merged commit b703456 into supabase:main Jan 31, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants