|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 15 | + "github.com/supabase/cli/pkg/api" |
| 16 | +) |
| 17 | + |
| 18 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 19 | +var _ datasource.DataSource = &PoolerDataSource{} |
| 20 | + |
| 21 | +func NewPoolerDataSource() datasource.DataSource { |
| 22 | + return &PoolerDataSource{} |
| 23 | +} |
| 24 | + |
| 25 | +// PoolerDataSource defines the data source implementation. |
| 26 | +type PoolerDataSource struct { |
| 27 | + client *api.ClientWithResponses |
| 28 | +} |
| 29 | + |
| 30 | +// PoolerDataSourceModel describes the data source data model. |
| 31 | +type PoolerDataSourceModel struct { |
| 32 | + ProjectRef types.String `tfsdk:"project_ref"` |
| 33 | + Url types.MapType `tfsdk:"url"` |
| 34 | +} |
| 35 | + |
| 36 | +func (d *PoolerDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 37 | + resp.TypeName = req.ProviderTypeName + "_pooler" |
| 38 | +} |
| 39 | + |
| 40 | +func (d *PoolerDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 41 | + resp.Schema = schema.Schema{ |
| 42 | + // This description is used by the documentation generator and the language server. |
| 43 | + MarkdownDescription: "Pooler data source", |
| 44 | + |
| 45 | + Attributes: map[string]schema.Attribute{ |
| 46 | + "project_ref": schema.StringAttribute{ |
| 47 | + MarkdownDescription: "Project ref", |
| 48 | + Required: true, |
| 49 | + }, |
| 50 | + "url": schema.MapAttribute{ |
| 51 | + MarkdownDescription: "Map of pooler mode to connection string", |
| 52 | + Computed: true, |
| 53 | + ElementType: types.StringType, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (d *PoolerDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 60 | + // Prevent panic if the provider has not been configured. |
| 61 | + if req.ProviderData == nil { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + client, ok := req.ProviderData.(*api.ClientWithResponses) |
| 66 | + |
| 67 | + if !ok { |
| 68 | + resp.Diagnostics.AddError( |
| 69 | + "Unexpected Data Source Configure Type", |
| 70 | + fmt.Sprintf("Expected *api.ClientWithResponses, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 71 | + ) |
| 72 | + |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + d.client = client |
| 77 | +} |
| 78 | + |
| 79 | +func (d *PoolerDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 80 | + var projectRef types.String |
| 81 | + |
| 82 | + // Read Terraform configuration data into the model |
| 83 | + resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("project_ref"), &projectRef)...) |
| 84 | + if resp.Diagnostics.HasError() { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + // If applicable, this is a great opportunity to initialize any necessary |
| 89 | + // provider client data and make a call using it. |
| 90 | + httpResp, err := d.client.V1GetPgbouncerConfigWithResponse(ctx, projectRef.ValueString()) |
| 91 | + if err != nil { |
| 92 | + msg := fmt.Sprintf("Unable to read pooler, got error: %s", err) |
| 93 | + resp.Diagnostics.AddError("Client Error", msg) |
| 94 | + return |
| 95 | + } |
| 96 | + if httpResp.JSON200 == nil { |
| 97 | + msg := fmt.Sprintf("Unable to read pooler, got status %d: %s", httpResp.StatusCode(), httpResp.Body) |
| 98 | + resp.Diagnostics.AddError("Client Error", msg) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + url := map[string]string{} |
| 103 | + if httpResp.JSON200.PoolMode != nil && httpResp.JSON200.ConnectionString != nil { |
| 104 | + mode := string(*httpResp.JSON200.PoolMode) |
| 105 | + url[mode] = *httpResp.JSON200.ConnectionString |
| 106 | + } |
| 107 | + |
| 108 | + // Write logs using the tflog package |
| 109 | + // Documentation: https://terraform.io/plugin/log |
| 110 | + tflog.Trace(ctx, "read a data source") |
| 111 | + |
| 112 | + // Save data into Terraform state |
| 113 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("url"), url)...) |
| 114 | +} |
0 commit comments