|
| 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/types" |
| 13 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 14 | + "github.com/supabase/cli/pkg/api" |
| 15 | +) |
| 16 | + |
| 17 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 18 | +var _ datasource.DataSource = &ProjectAPIKeysDataSource{} |
| 19 | + |
| 20 | +func NewProjectAPIKeysDataSource() datasource.DataSource { |
| 21 | + return &ProjectAPIKeysDataSource{} |
| 22 | +} |
| 23 | + |
| 24 | +// ProjectAPIKeysDataSource defines the data source implementation. |
| 25 | +type ProjectAPIKeysDataSource struct { |
| 26 | + client *api.ClientWithResponses |
| 27 | +} |
| 28 | + |
| 29 | +// ProjectAPIKeysDataSourceModel describes the data source data model. |
| 30 | +type ProjectAPIKeysDataSourceModel struct { |
| 31 | + ProjectId types.String `tfsdk:"project_id"` |
| 32 | + AnonKey types.String `tfsdk:"anon_key"` |
| 33 | + ServiceRoleKey types.String `tfsdk:"service_role_key"` |
| 34 | +} |
| 35 | + |
| 36 | +func (d *ProjectAPIKeysDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 37 | + resp.TypeName = req.ProviderTypeName + "_project_apikeys" |
| 38 | +} |
| 39 | + |
| 40 | +func (d *ProjectAPIKeysDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 41 | + resp.Schema = schema.Schema{ |
| 42 | + MarkdownDescription: "Project API Keys data source", |
| 43 | + |
| 44 | + Attributes: map[string]schema.Attribute{ |
| 45 | + "project_id": schema.StringAttribute{ |
| 46 | + MarkdownDescription: "Project identifier", |
| 47 | + Required: true, |
| 48 | + }, |
| 49 | + "anon_key": schema.StringAttribute{ |
| 50 | + MarkdownDescription: "Anonymous API key for the project", |
| 51 | + Computed: true, |
| 52 | + Sensitive: true, |
| 53 | + }, |
| 54 | + "service_role_key": schema.StringAttribute{ |
| 55 | + MarkdownDescription: "Service role API key for the project", |
| 56 | + Computed: true, |
| 57 | + Sensitive: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func (d *ProjectAPIKeysDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 64 | + // Prevent panic if the provider has not been configured. |
| 65 | + if req.ProviderData == nil { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + client, ok := req.ProviderData.(*api.ClientWithResponses) |
| 70 | + if !ok { |
| 71 | + resp.Diagnostics.AddError( |
| 72 | + "Unexpected Data Source Configure Type", |
| 73 | + fmt.Sprintf("Expected *api.ClientWithResponses, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 74 | + ) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + d.client = client |
| 79 | +} |
| 80 | + |
| 81 | +func (d *ProjectAPIKeysDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 82 | + var data ProjectAPIKeysDataSourceModel |
| 83 | + |
| 84 | + // Read Terraform configuration data into the model |
| 85 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 86 | + if resp.Diagnostics.HasError() { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + httpResp, err := d.client.V1GetProjectApiKeysWithResponse(ctx, data.ProjectId.ValueString(), &api.V1GetProjectApiKeysParams{}) |
| 91 | + if err != nil { |
| 92 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read project API keys, got error: %s", err)) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + if httpResp.JSON200 == nil { |
| 97 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read project API keys, got status %d: %s", httpResp.StatusCode(), httpResp.Body)) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + for _, key := range *httpResp.JSON200 { |
| 102 | + switch key.Name { |
| 103 | + case "anon": |
| 104 | + data.AnonKey = types.StringValue(key.ApiKey) |
| 105 | + case "service_role": |
| 106 | + data.ServiceRoleKey = types.StringValue(key.ApiKey) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + tflog.Trace(ctx, "read project API keys") |
| 111 | + |
| 112 | + // Save data into Terraform state |
| 113 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 114 | +} |
0 commit comments