diff --git a/docs/data-sources/project_apikeys.md b/docs/data-sources/apikeys.md similarity index 52% rename from docs/data-sources/project_apikeys.md rename to docs/data-sources/apikeys.md index 49756f0..d0d8ddf 100644 --- a/docs/data-sources/project_apikeys.md +++ b/docs/data-sources/apikeys.md @@ -1,23 +1,29 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "supabase_project_apikeys Data Source - terraform-provider-supabase" +page_title: "supabase_apikeys Data Source - terraform-provider-supabase" subcategory: "" description: |- - Project API Keys data source + API Keys data source --- -# supabase_project_apikeys (Data Source) +# supabase_apikeys (Data Source) -Project API Keys data source +API Keys data source +## Example Usage +```terraform +data "supabase_apikeys" "production" { + project_ref = "mayuaycdtijbctgqbycg" +} +``` ## Schema ### Required -- `project_id` (String) Project identifier +- `project_ref` (String) Project reference ID ### Read-Only diff --git a/docs/schema.json b/docs/schema.json index 39525b0..35f0301 100644 --- a/docs/schema.json +++ b/docs/schema.json @@ -222,6 +222,35 @@ } }, "data_source_schemas": { + "supabase_apikeys": { + "version": 0, + "block": { + "attributes": { + "anon_key": { + "type": "string", + "description": "Anonymous API key for the project", + "description_kind": "markdown", + "computed": true, + "sensitive": true + }, + "project_ref": { + "type": "string", + "description": "Project reference ID", + "description_kind": "markdown", + "required": true + }, + "service_role_key": { + "type": "string", + "description": "Service role API key for the project", + "description_kind": "markdown", + "computed": true, + "sensitive": true + } + }, + "description": "API Keys data source", + "description_kind": "markdown" + } + }, "supabase_branch": { "version": 0, "block": { @@ -288,35 +317,6 @@ "description": "Pooler data source", "description_kind": "markdown" } - }, - "supabase_project_apikeys": { - "version": 0, - "block": { - "attributes": { - "anon_key": { - "type": "string", - "description": "Anonymous API key for the project", - "description_kind": "markdown", - "computed": true, - "sensitive": true - }, - "project_id": { - "type": "string", - "description": "Project identifier", - "description_kind": "markdown", - "required": true - }, - "service_role_key": { - "type": "string", - "description": "Service role API key for the project", - "description_kind": "markdown", - "computed": true, - "sensitive": true - } - }, - "description": "Project API Keys data source", - "description_kind": "markdown" - } } } } diff --git a/docs/tutorial.md b/docs/tutorial.md index b766f0c..15278d6 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -42,19 +42,18 @@ resource "supabase_project" "production" { } } -# Retrieve project API keys -data "supabase_project_apikeys" "production" { - project_id = supabase_project.production.id +# Retrieve project API keys (careful with sensitive data!) +data "supabase_apikeys" "production" { + project_ref = supabase_project.production.id } -# Output the API keys (careful with sensitive data!) output "anon_key" { - value = data.supabase_project_apikeys.production.anon_key + value = data.supabase_apikeys.production.anon_key sensitive = true } output "service_role_key" { - value = data.supabase_project_apikeys.production.service_role_key + value = data.supabase_apikeys.production.service_role_key sensitive = true } ``` @@ -80,7 +79,7 @@ import { id = var.linked_project } -# Create a project resource +# Import a project resource resource "supabase_project" "production" { organization_id = "" name = "tf-example" @@ -91,11 +90,6 @@ resource "supabase_project" "production" { ignore_changes = [database_password] } } - -# Retrieve project API keys -data "supabase_project_apikeys" "production" { - project_id = supabase_project.production.id -} ``` Run `terraform -chdir=module apply`. Enter the ID of your Supabase project at the prompt. If your local TF state is empty, your project will be imported from remote rather than recreated. diff --git a/examples/data-sources/supabase_apikeys/data-source.tf b/examples/data-sources/supabase_apikeys/data-source.tf new file mode 100644 index 0000000..ca7357e --- /dev/null +++ b/examples/data-sources/supabase_apikeys/data-source.tf @@ -0,0 +1,3 @@ +data "supabase_apikeys" "production" { + project_ref = "mayuaycdtijbctgqbycg" +} diff --git a/examples/examples.go b/examples/examples.go index 8e64a26..8e37835 100644 --- a/examples/examples.go +++ b/examples/examples.go @@ -13,4 +13,6 @@ var ( BranchDataSourceConfig string //go:embed data-sources/supabase_pooler/data-source.tf PoolerDataSourceConfig string + //go:embed data-sources/supabase_apikeys/data-source.tf + APIKeysDataSourceConfig string ) diff --git a/internal/provider/project_apikeys_data_source.go b/internal/provider/apikeys_data_source.go similarity index 60% rename from internal/provider/project_apikeys_data_source.go rename to internal/provider/apikeys_data_source.go index 062cb4a..5a1a124 100644 --- a/internal/provider/project_apikeys_data_source.go +++ b/internal/provider/apikeys_data_source.go @@ -15,35 +15,35 @@ import ( ) // Ensure provider defined types fully satisfy framework interfaces. -var _ datasource.DataSource = &ProjectAPIKeysDataSource{} +var _ datasource.DataSource = &APIKeysDataSource{} -func NewProjectAPIKeysDataSource() datasource.DataSource { - return &ProjectAPIKeysDataSource{} +func NewAPIKeysDataSource() datasource.DataSource { + return &APIKeysDataSource{} } -// ProjectAPIKeysDataSource defines the data source implementation. -type ProjectAPIKeysDataSource struct { +// APIKeysDataSource defines the data source implementation. +type APIKeysDataSource struct { client *api.ClientWithResponses } -// ProjectAPIKeysDataSourceModel describes the data source data model. -type ProjectAPIKeysDataSourceModel struct { - ProjectId types.String `tfsdk:"project_id"` +// APIKeysDataSourceModel describes the data source data model. +type APIKeysDataSourceModel struct { + ProjectRef types.String `tfsdk:"project_ref"` AnonKey types.String `tfsdk:"anon_key"` ServiceRoleKey types.String `tfsdk:"service_role_key"` } -func (d *ProjectAPIKeysDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { - resp.TypeName = req.ProviderTypeName + "_project_apikeys" +func (d *APIKeysDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_apikeys" } -func (d *ProjectAPIKeysDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { +func (d *APIKeysDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ - MarkdownDescription: "Project API Keys data source", + MarkdownDescription: "API Keys data source", Attributes: map[string]schema.Attribute{ - "project_id": schema.StringAttribute{ - MarkdownDescription: "Project identifier", + "project_ref": schema.StringAttribute{ + MarkdownDescription: "Project reference ID", Required: true, }, "anon_key": schema.StringAttribute{ @@ -60,7 +60,7 @@ func (d *ProjectAPIKeysDataSource) Schema(ctx context.Context, req datasource.Sc } } -func (d *ProjectAPIKeysDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { +func (d *APIKeysDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { // Prevent panic if the provider has not been configured. if req.ProviderData == nil { return @@ -78,8 +78,8 @@ func (d *ProjectAPIKeysDataSource) Configure(ctx context.Context, req datasource d.client = client } -func (d *ProjectAPIKeysDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var data ProjectAPIKeysDataSourceModel +func (d *APIKeysDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data APIKeysDataSourceModel // Read Terraform configuration data into the model resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) @@ -87,14 +87,14 @@ func (d *ProjectAPIKeysDataSource) Read(ctx context.Context, req datasource.Read return } - httpResp, err := d.client.V1GetProjectApiKeysWithResponse(ctx, data.ProjectId.ValueString(), &api.V1GetProjectApiKeysParams{}) + httpResp, err := d.client.V1GetProjectApiKeysWithResponse(ctx, data.ProjectRef.ValueString(), &api.V1GetProjectApiKeysParams{}) if err != nil { - resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read project API keys, got error: %s", err)) + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read API keys, got error: %s", err)) return } if httpResp.JSON200 == nil { - resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read project API keys, got status %d: %s", httpResp.StatusCode(), httpResp.Body)) + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read API keys, got status %d: %s", httpResp.StatusCode(), httpResp.Body)) return } @@ -107,7 +107,7 @@ func (d *ProjectAPIKeysDataSource) Read(ctx context.Context, req datasource.Read } } - tflog.Trace(ctx, "read project API keys") + tflog.Trace(ctx, "read API keys") // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) diff --git a/internal/provider/project_apikeys_data_source_test.go b/internal/provider/apikeys_data_source_test.go similarity index 67% rename from internal/provider/project_apikeys_data_source_test.go rename to internal/provider/apikeys_data_source_test.go index 9797544..86a19eb 100644 --- a/internal/provider/project_apikeys_data_source_test.go +++ b/internal/provider/apikeys_data_source_test.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/supabase/cli/pkg/api" + "github.com/supabase/terraform-provider-supabase/examples" "gopkg.in/h2non/gock.v1" ) @@ -36,18 +37,12 @@ func TestAccProjectAPIKeysDataSource(t *testing.T) { Steps: []resource.TestStep{ // Read testing { - Config: testAccProjectAPIKeysDataSourceConfig, + Config: examples.APIKeysDataSourceConfig, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("data.supabase_project_apikeys.production", "anon_key", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.anon"), - resource.TestCheckResourceAttr("data.supabase_project_apikeys.production", "service_role_key", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.service_role"), + resource.TestCheckResourceAttr("data.supabase_apikeys.production", "anon_key", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.anon"), + resource.TestCheckResourceAttr("data.supabase_apikeys.production", "service_role_key", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.service_role"), ), }, }, }) } - -const testAccProjectAPIKeysDataSourceConfig = ` -data "supabase_project_apikeys" "production" { - project_id = "mayuaycdtijbctgqbycg" -} -` diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 4560081..74a55e8 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -96,7 +96,7 @@ func (p *SupabaseProvider) DataSources(ctx context.Context) []func() datasource. return []func() datasource.DataSource{ NewBranchDataSource, NewPoolerDataSource, - NewProjectAPIKeysDataSource, + NewAPIKeysDataSource, } }