generated from hashicorp/terraform-provider-scaffolding-framework
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: datasource for supabase_project_apikeys (#212)
* ♻️ (provider): refactor data source implementation to follow standard Go coding conventions and improve readability * ♻️ (internal/provider/project_apikeys_data_source_test.go): refactor test functions for better organization and reusability * ✨ (docs/tutorial.md): add support for retrieving project API keys using data source ♻️ (docs/tutorial.md): refactor code to use Terraform data sources instead of hardcoded values * fix tests * linter and schema --------- Co-authored-by: Han Qiao <[email protected]>
- Loading branch information
1 parent
df6a6c2
commit 4f4f315
Showing
6 changed files
with
245 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "supabase_project_apikeys Data Source - terraform-provider-supabase" | ||
subcategory: "" | ||
description: |- | ||
Project API Keys data source | ||
--- | ||
|
||
# supabase_project_apikeys (Data Source) | ||
|
||
Project API Keys data source | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `project_id` (String) Project identifier | ||
|
||
### Read-Only | ||
|
||
- `anon_key` (String, Sensitive) Anonymous API key for the project | ||
- `service_role_key` (String, Sensitive) Service role API key for the project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/supabase/cli/pkg/api" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ datasource.DataSource = &ProjectAPIKeysDataSource{} | ||
|
||
func NewProjectAPIKeysDataSource() datasource.DataSource { | ||
return &ProjectAPIKeysDataSource{} | ||
} | ||
|
||
// ProjectAPIKeysDataSource defines the data source implementation. | ||
type ProjectAPIKeysDataSource struct { | ||
client *api.ClientWithResponses | ||
} | ||
|
||
// ProjectAPIKeysDataSourceModel describes the data source data model. | ||
type ProjectAPIKeysDataSourceModel struct { | ||
ProjectId types.String `tfsdk:"project_id"` | ||
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 *ProjectAPIKeysDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "Project API Keys data source", | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"project_id": schema.StringAttribute{ | ||
MarkdownDescription: "Project identifier", | ||
Required: true, | ||
}, | ||
"anon_key": schema.StringAttribute{ | ||
MarkdownDescription: "Anonymous API key for the project", | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
"service_role_key": schema.StringAttribute{ | ||
MarkdownDescription: "Service role API key for the project", | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *ProjectAPIKeysDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*api.ClientWithResponses) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *api.ClientWithResponses, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
return | ||
} | ||
|
||
d.client = client | ||
} | ||
|
||
func (d *ProjectAPIKeysDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data ProjectAPIKeysDataSourceModel | ||
|
||
// 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{}) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read project 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)) | ||
return | ||
} | ||
|
||
for _, key := range *httpResp.JSON200 { | ||
switch key.Name { | ||
case "anon": | ||
data.AnonKey = types.StringValue(key.ApiKey) | ||
case "service_role": | ||
data.ServiceRoleKey = types.StringValue(key.ApiKey) | ||
} | ||
} | ||
|
||
tflog.Trace(ctx, "read project API keys") | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/supabase/cli/pkg/api" | ||
"gopkg.in/h2non/gock.v1" | ||
) | ||
|
||
func TestAccProjectAPIKeysDataSource(t *testing.T) { | ||
// Setup mock api | ||
defer gock.OffAll() | ||
gock.New("https://api.supabase.com"). | ||
Get("/v1/projects/mayuaycdtijbctgqbycg/api-keys"). | ||
Times(3). | ||
Reply(http.StatusOK). | ||
JSON([]api.ApiKeyResponse{ | ||
{ | ||
Name: "anon", | ||
ApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.anon", | ||
}, | ||
{ | ||
Name: "service_role", | ||
ApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.service_role", | ||
}, | ||
}) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Read testing | ||
{ | ||
Config: testAccProjectAPIKeysDataSourceConfig, | ||
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccProjectAPIKeysDataSourceConfig = ` | ||
data "supabase_project_apikeys" "production" { | ||
project_id = "mayuaycdtijbctgqbycg" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters