generated from hashicorp/terraform-provider-scaffolding-framework
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support pooler data source (#54)
* chore: refactor settings config parse * feat: support pooler data source * chore: update ci workflow * docs: update json schema file * chore: reduce tf version matrix used in tests --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5da2a33
commit c69a5fd
Showing
11 changed files
with
273 additions
and
43 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
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,30 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "supabase_pooler Data Source - terraform-provider-supabase" | ||
subcategory: "" | ||
description: |- | ||
Pooler data source | ||
--- | ||
|
||
# supabase_pooler (Data Source) | ||
|
||
Pooler data source | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "supabase_pooler" "production" { | ||
project_ref = "mayuaycdtijbctgqbycg" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `project_ref` (String) Project ref | ||
|
||
### Read-Only | ||
|
||
- `url` (Map of String) Map of pooler mode to connection string |
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,3 @@ | ||
data "supabase_pooler" "production" { | ||
project_ref = "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
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/path" | ||
"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 = &PoolerDataSource{} | ||
|
||
func NewPoolerDataSource() datasource.DataSource { | ||
return &PoolerDataSource{} | ||
} | ||
|
||
// PoolerDataSource defines the data source implementation. | ||
type PoolerDataSource struct { | ||
client *api.ClientWithResponses | ||
} | ||
|
||
// PoolerDataSourceModel describes the data source data model. | ||
type PoolerDataSourceModel struct { | ||
ProjectRef types.String `tfsdk:"project_ref"` | ||
Url types.MapType `tfsdk:"url"` | ||
} | ||
|
||
func (d *PoolerDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_pooler" | ||
} | ||
|
||
func (d *PoolerDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
// This description is used by the documentation generator and the language server. | ||
MarkdownDescription: "Pooler data source", | ||
|
||
Attributes: map[string]schema.Attribute{ | ||
"project_ref": schema.StringAttribute{ | ||
MarkdownDescription: "Project ref", | ||
Required: true, | ||
}, | ||
"url": schema.MapAttribute{ | ||
MarkdownDescription: "Map of pooler mode to connection string", | ||
Computed: true, | ||
ElementType: types.StringType, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *PoolerDataSource) 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 *PoolerDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var projectRef types.String | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("project_ref"), &projectRef)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// If applicable, this is a great opportunity to initialize any necessary | ||
// provider client data and make a call using it. | ||
httpResp, err := d.client.V1GetPgbouncerConfigWithResponse(ctx, projectRef.ValueString()) | ||
if err != nil { | ||
msg := fmt.Sprintf("Unable to read pooler, got error: %s", err) | ||
resp.Diagnostics.AddError("Client Error", msg) | ||
return | ||
} | ||
if httpResp.JSON200 == nil { | ||
msg := fmt.Sprintf("Unable to read pooler, got status %d: %s", httpResp.StatusCode(), httpResp.Body) | ||
resp.Diagnostics.AddError("Client Error", msg) | ||
return | ||
} | ||
|
||
url := map[string]string{} | ||
if httpResp.JSON200.PoolMode != nil && httpResp.JSON200.ConnectionString != nil { | ||
mode := string(*httpResp.JSON200.PoolMode) | ||
url[mode] = *httpResp.JSON200.ConnectionString | ||
} | ||
|
||
// Write logs using the tflog package | ||
// Documentation: https://terraform.io/plugin/log | ||
tflog.Trace(ctx, "read a data source") | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("url"), url)...) | ||
} |
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,45 @@ | ||
// 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" | ||
"github.com/supabase/terraform-provider-supabase/examples" | ||
"gopkg.in/h2non/gock.v1" | ||
) | ||
|
||
func TestAccPoolerDataSource(t *testing.T) { | ||
poolerUrl := "postgres://user:pass@db.supabase.co:5432/postgres" | ||
// Setup mock api | ||
defer gock.OffAll() | ||
gock.New("https://api.supabase.com"). | ||
Get("/v1/projects/mayuaycdtijbctgqbycg/config/database/pgbouncer"). | ||
Times(3). | ||
Reply(http.StatusOK). | ||
JSON(api.V1PgbouncerConfigResponse{ | ||
ConnectionString: &poolerUrl, | ||
DefaultPoolSize: Ptr(float32(15)), | ||
IgnoreStartupParameters: Ptr(""), | ||
MaxClientConn: Ptr(float32(200)), | ||
PoolMode: Ptr(api.Transaction), | ||
}) | ||
// Run test | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Read testing | ||
{ | ||
Config: examples.PoolerDataSourceConfig, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.supabase_pooler.production", "url.transaction", poolerUrl), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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
Oops, something went wrong.