Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
module github.com/supabase/terraform-provider-supabase

go 1.23.2
go 1.24.4

require (
github.com/google/uuid v1.6.0
github.com/hashicorp/terraform-plugin-docs v0.20.1
github.com/hashicorp/terraform-plugin-framework v1.13.0
github.com/hashicorp/terraform-plugin-framework-jsontypes v0.2.0
github.com/hashicorp/terraform-plugin-go v0.26.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.11.0
github.com/supabase/cli v1.226.4
github.com/oapi-codegen/nullable v1.1.0
github.com/oapi-codegen/runtime v1.1.2
github.com/supabase/cli/pkg v1.1.5
gopkg.in/h2non/gock.v1 v1.1.2
)

require (
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect
github.com/BurntSushi/toml v1.5.0 // indirect
github.com/Kunde21/markdownfmt/v3 v3.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
Expand All @@ -29,8 +32,7 @@ require (
github.com/cloudflare/circl v1.6.1 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/hashicorp/cli v1.1.6 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down Expand Up @@ -62,31 +64,30 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yuin/goldmark v1.7.7 // indirect
github.com/yuin/goldmark-meta v1.1.0 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/crypto v0.42.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.27.0 // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.29.0 // indirect
golang.org/x/tools v0.37.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect
google.golang.org/grpc v1.69.4 // indirect
google.golang.org/protobuf v1.36.3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250804133106-a7a43d27e69b // indirect
google.golang.org/grpc v1.76.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
114 changes: 60 additions & 54 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions internal/provider/apikeys_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func (d *APIKeysDataSource) Read(ctx context.Context, req datasource.ReadRequest
for _, key := range *httpResp.JSON200 {
switch key.Name {
case "anon":
data.AnonKey = types.StringValue(key.ApiKey)
data.AnonKey = NullableToString(key.ApiKey)
case "service_role":
data.ServiceRoleKey = types.StringValue(key.ApiKey)
data.ServiceRoleKey = NullableToString(key.ApiKey)
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/provider/apikeys_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/oapi-codegen/nullable"
"github.com/supabase/cli/pkg/api"
"github.com/supabase/terraform-provider-supabase/examples"
"gopkg.in/h2non/gock.v1"
Expand All @@ -23,11 +24,11 @@ func TestAccProjectAPIKeysDataSource(t *testing.T) {
JSON([]api.ApiKeyResponse{
{
Name: "anon",
ApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.anon",
ApiKey: nullable.NewNullableWithValue("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.anon"),
},
{
Name: "service_role",
ApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.service_role",
ApiKey: nullable.NewNullableWithValue("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.service_role"),
},
})

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/branch_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (d *BranchDataSource) Read(ctx context.Context, req datasource.ReadRequest,
continue
}
branches = append(branches, BranchDataSourceModel{
Id: types.StringValue(branch.Id),
Id: types.StringValue(branch.Id.String()),
GitBranch: types.StringPointerValue(branch.GitBranch),
ProjectRef: types.StringValue(branch.ProjectRef),
})
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/branch_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"testing"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/supabase/cli/pkg/api"
"github.com/supabase/terraform-provider-supabase/examples"
Expand All @@ -20,7 +21,7 @@ func TestAccBranchDataSource(t *testing.T) {
Get("/v1/projects/mayuaycdtijbctgqbycg/branches").
Times(3).
Reply(http.StatusOK).
JSON([]api.BranchResponse{{Id: "test"}})
JSON([]api.BranchResponse{{Id: uuid.New()}})
// Run test
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/branch_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func createBranch(ctx context.Context, plan *BranchResourceModel, client *api.Cl
return diag.Diagnostics{diag.NewErrorDiagnostic("Client Error", msg)}
}
// Update computed fields
plan.Id = types.StringValue(httpResp.JSON201.Id)
plan.Id = types.StringValue(httpResp.JSON201.Id.String())
if diag := readBranchDatabase(ctx, plan, client); diag.HasError() {
for _, err := range diag.Errors() {
tflog.Warn(ctx, fmt.Sprintf("%s: %s", err.Summary(), err.Detail()))
Expand Down
29 changes: 17 additions & 12 deletions internal/provider/branch_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package provider

import (
"fmt"
"net/http"
"testing"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/supabase/cli/pkg/api"
"github.com/supabase/terraform-provider-supabase/examples"
Expand All @@ -17,62 +19,65 @@ func TestAccBranchResource(t *testing.T) {
// Setup mock api
defer gock.OffAll()
// Step 1: create
testBranchUUID := uuid.New()
gock.New("https://api.supabase.com").
Get("/v1/projects/mayuaycdtijbctgqbycg/branches").
Reply(http.StatusUnprocessableEntity)
gock.New("https://api.supabase.com").
Post("/v1/projects/mayuaycdtijbctgqbycg/branches").
Reply(http.StatusCreated).
JSON(api.BranchResponse{
Id: "prod-branch",
Id: uuid.New(),
ParentProjectRef: "mayuaycdtijbctgqbycg",
IsDefault: true,
})
gock.New("https://api.supabase.com").
Post("/v1/projects/mayuaycdtijbctgqbycg/branches").
Reply(http.StatusCreated).
JSON(api.BranchResponse{
Id: "test-branch",
Id: testBranchUUID,
ParentProjectRef: "mayuaycdtijbctgqbycg",
GitBranch: Ptr("main"),
})

testBranchIDEndpoint := fmt.Sprintf("/v1/branches/%s", testBranchUUID.String())
gock.New("https://api.supabase.com").
Get("/v1/branches/test-branch").
Get(testBranchIDEndpoint).
Reply(http.StatusOK).
JSON(api.BranchDetailResponse{})
gock.New("https://api.supabase.com").
Get("/v1/branches/test-branch").
Get(testBranchIDEndpoint).
Reply(http.StatusOK).
JSON(api.BranchDetailResponse{})
// Step 2: read
gock.New("https://api.supabase.com").
Get("/v1/branches/test-branch").
Get(testBranchIDEndpoint).
Reply(http.StatusOK).
JSON(api.BranchDetailResponse{})
gock.New("https://api.supabase.com").
Get("/v1/branches/test-branch").
Get(testBranchIDEndpoint).
Reply(http.StatusOK).
JSON(api.BranchDetailResponse{})
// Step 3: update and read
gock.New("https://api.supabase.com").
Get("/v1/branches/test-branch").
Get(testBranchIDEndpoint).
Reply(http.StatusOK).
JSON(api.BranchDetailResponse{})
gock.New("https://api.supabase.com").
Patch("/v1/branches/test-branch").
Patch(testBranchIDEndpoint).
Reply(http.StatusOK).
JSON(api.BranchResponse{
Id: "test-branch",
Id: testBranchUUID,
ParentProjectRef: "mayuaycdtijbctgqbycg",
GitBranch: Ptr("develop"),
})
gock.New("https://api.supabase.com").
Get("/v1/branches/test-branch").
Get(testBranchIDEndpoint).
Reply(http.StatusOK).
JSON(api.BranchDetailResponse{})
// Step 4: delete
gock.New("https://api.supabase.com").
Delete("/v1/branches/test-branch").
Delete(testBranchIDEndpoint).
Reply(http.StatusOK)
// Run test
resource.Test(t, resource.TestCase{
Expand All @@ -83,7 +88,7 @@ func TestAccBranchResource(t *testing.T) {
{
Config: examples.BranchResourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("supabase_branch.new", "id", "test-branch"),
resource.TestCheckResourceAttr("supabase_branch.new", "id", testBranchUUID.String()),
),
},
// ImportState testing
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/pooler_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (d *PoolerDataSource) Read(ctx context.Context, req datasource.ReadRequest,

// If applicable, this is a great opportunity to initialize any necessary
// provider client data and make a call using it.
httpResp, err := d.client.V1GetSupavisorConfigWithResponse(ctx, projectRef.ValueString())
httpResp, err := d.client.V1GetPoolerConfigWithResponse(ctx, projectRef.ValueString())
if err != nil {
msg := fmt.Sprintf("Unable to read pooler, got error: %s", err)
resp.Diagnostics.AddError("Client Error", msg)
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/pooler_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/oapi-codegen/nullable"
"github.com/supabase/cli/pkg/api"
"github.com/supabase/terraform-provider-supabase/examples"
"gopkg.in/h2non/gock.v1"
Expand All @@ -24,8 +25,8 @@ func TestAccPoolerDataSource(t *testing.T) {
JSON([]api.SupavisorConfigResponse{{
DatabaseType: api.PRIMARY,
ConnectionString: poolerUrl,
DefaultPoolSize: Ptr(int(15)),
MaxClientConn: Ptr(int(200)),
DefaultPoolSize: nullable.NewNullableWithValue(15),
MaxClientConn: nullable.NewNullableWithValue(200),
PoolMode: api.SupavisorConfigResponsePoolModeTransaction,
}})
// Run test
Expand Down
24 changes: 18 additions & 6 deletions internal/provider/project_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,26 @@ func (r *ProjectResource) ImportState(ctx context.Context, req resource.ImportSt
}

func createProject(ctx context.Context, data *ProjectResourceModel, client *api.ClientWithResponses) diag.Diagnostics {
body := api.V1CreateProjectBodyDto{
OrganizationId: data.OrganizationId.ValueString(),
Name: data.Name.ValueString(),
DbPass: data.DatabasePassword.ValueString(),
Region: api.V1CreateProjectBodyDtoRegion(data.Region.ValueString()),
regionSelection := api.V1CreateProjectBodyRegionSelection0{
Type: api.Specific,
Code: api.V1CreateProjectBodyRegionSelection0Code(data.Region.ValueString()),
}

region := api.V1CreateProjectBody_RegionSelection{}
if err := region.FromV1CreateProjectBodyRegionSelection0(regionSelection); err != nil {
return diag.Diagnostics{diag.NewErrorDiagnostic(
"Internal Error",
fmt.Sprintf("Failed to configure region selection: %s", err),
)}
}
body := api.V1CreateAProjectJSONRequestBody{
OrganizationId: data.OrganizationId.ValueString(),
Name: data.Name.ValueString(),
DbPass: data.DatabasePassword.ValueString(),
RegionSelection: &region,
}
if !data.InstanceSize.IsNull() {
body.DesiredInstanceSize = Ptr(api.V1CreateProjectBodyDtoDesiredInstanceSize(data.InstanceSize.ValueString()))
body.DesiredInstanceSize = Ptr(api.V1CreateProjectBodyDesiredInstanceSize(data.InstanceSize.ValueString()))
}

httpResp, err := client.V1CreateAProjectWithResponse(ctx, body)
Expand Down
34 changes: 25 additions & 9 deletions internal/provider/settings_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func readApiConfig(ctx context.Context, state *SettingsResourceModel, client *ap
}

func updateApiConfig(ctx context.Context, plan *SettingsResourceModel, client *api.ClientWithResponses) diag.Diagnostics {
var body api.UpdatePostgrestConfigBody
var body api.V1UpdatePostgrestConfigBody
if diags := plan.Api.Unmarshal(&body); diags.HasError() {
return diags
}
Expand Down Expand Up @@ -399,7 +399,7 @@ func updateDatabaseConfig(ctx context.Context, plan *SettingsResourceModel, clie
}

func parseConfig(field jsontypes.Normalized, config any) (jsontypes.Normalized, error) {
partial := make(map[string]interface{})
partial := make(map[string]any)
if diags := field.Unmarshal(&partial); !diags.HasError() {
pickConfig(config, partial)
} else {
Expand All @@ -413,7 +413,7 @@ func parseConfig(field jsontypes.Normalized, config any) (jsontypes.Normalized,
return jsontypes.NewNormalizedValue(string(value)), nil
}

func pickConfig(source any, target map[string]interface{}) {
func pickConfig(source any, target map[string]any) {
v := reflect.ValueOf(source)
t := reflect.TypeOf(source)
for i := 0; i < v.NumField(); i++ {
Expand All @@ -426,17 +426,33 @@ func pickConfig(source any, target map[string]interface{}) {
}
}

func copyConfig(source any, target map[string]interface{}) {
func copyConfig(source any, target map[string]any) {
v := reflect.ValueOf(source)
t := reflect.TypeOf(source)
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
// Add omitempty tag by default
if f.Kind() != reflect.Ptr || !f.IsNil() {
tag := t.Field(i).Tag.Get("json")
k := strings.Split(tag, ",")[0]
target[k] = f.Interface()

if f.Kind() == reflect.Pointer && f.IsNil() {
continue
}

if v, ok := f.Interface().(interface {
IsNull() bool
IsSpecified() bool
}); ok {
if !v.IsSpecified() {
continue
}

if v.IsNull() {
continue
}
}

// Add omitempty tag by default
tag := t.Field(i).Tag.Get("json")
k := strings.Split(tag, ",")[0]
target[k] = f.Interface()
}
}

Expand Down
Loading