Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## Schema

### Required

- `project_id` (String) Project identifier
- `project_ref` (String) Project reference ID

### Read-Only

Expand Down
58 changes: 29 additions & 29 deletions docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
}
}
}
}
Expand Down
18 changes: 6 additions & 12 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand All @@ -80,7 +79,7 @@ import {
id = var.linked_project
}

# Create a project resource
# Import a project resource
resource "supabase_project" "production" {
organization_id = "<your-org-id>"
name = "tf-example"
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions examples/data-sources/supabase_apikeys/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "supabase_apikeys" "production" {
project_ref = "mayuaycdtijbctgqbycg"
}
2 changes: 2 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -78,23 +78,23 @@ 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)...)
if resp.Diagnostics.HasError() {
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
}

Expand All @@ -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)...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"
}
`
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (p *SupabaseProvider) DataSources(ctx context.Context) []func() datasource.
return []func() datasource.DataSource{
NewBranchDataSource,
NewPoolerDataSource,
NewProjectAPIKeysDataSource,
NewAPIKeysDataSource,
}
}

Expand Down