|
| 1 | +//go:build alpha |
| 2 | + |
| 3 | +package datasource |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + _ "embed" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | + |
| 16 | + "github.com/ClickHouse/terraform-provider-clickhouse/pkg/internal/api" |
| 17 | +) |
| 18 | + |
| 19 | +//go:embed descriptions/postgres_service.md |
| 20 | +var postgresServiceDataSourceDescription string |
| 21 | + |
| 22 | +// postgresReservedTagPrefix mirrors the resource's chc_ reservation; system |
| 23 | +// tags are filtered out of data-source output the same way. |
| 24 | +const postgresReservedTagPrefix = "chc_" |
| 25 | + |
| 26 | +// postgresDefaultPort mirrors the resource's hardcoded listening port. |
| 27 | +const postgresDefaultPort int64 = 5432 |
| 28 | + |
| 29 | +var _ datasource.DataSource = &postgresServiceDataSource{} |
| 30 | + |
| 31 | +// NewPostgresServiceDataSource fetches a single Managed Postgres service by ID. |
| 32 | +func NewPostgresServiceDataSource() datasource.DataSource { |
| 33 | + return &postgresServiceDataSource{} |
| 34 | +} |
| 35 | + |
| 36 | +type postgresServiceDataSource struct { |
| 37 | + client api.Client |
| 38 | +} |
| 39 | + |
| 40 | +type postgresServiceDataSourceModel struct { |
| 41 | + ID types.String `tfsdk:"id"` |
| 42 | + Name types.String `tfsdk:"name"` |
| 43 | + CloudProvider types.String `tfsdk:"cloud_provider"` |
| 44 | + Region types.String `tfsdk:"region"` |
| 45 | + PostgresVersion types.String `tfsdk:"postgres_version"` |
| 46 | + Size types.String `tfsdk:"size"` |
| 47 | + HaType types.String `tfsdk:"ha_type"` |
| 48 | + State types.String `tfsdk:"state"` |
| 49 | + CreatedAt types.String `tfsdk:"created_at"` |
| 50 | + IsPrimary types.Bool `tfsdk:"is_primary"` |
| 51 | + Hostname types.String `tfsdk:"hostname"` |
| 52 | + Port types.Int64 `tfsdk:"port"` |
| 53 | + Username types.String `tfsdk:"username"` |
| 54 | + ConnectionString types.String `tfsdk:"connection_string"` |
| 55 | + Tags types.Map `tfsdk:"tags"` |
| 56 | + PgConfig types.Map `tfsdk:"pg_config"` |
| 57 | + PgBouncerConfig types.Map `tfsdk:"pgbouncer_config"` |
| 58 | +} |
| 59 | + |
| 60 | +func (d *postgresServiceDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 61 | + if req.ProviderData == nil { |
| 62 | + return |
| 63 | + } |
| 64 | + client, ok := req.ProviderData.(api.Client) |
| 65 | + if !ok { |
| 66 | + resp.Diagnostics.AddError( |
| 67 | + "Unexpected Data Source Configure Type", |
| 68 | + "Expected api.Client, got something else. Please report this issue to the provider developers.", |
| 69 | + ) |
| 70 | + return |
| 71 | + } |
| 72 | + d.client = client |
| 73 | +} |
| 74 | + |
| 75 | +func (d *postgresServiceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 76 | + resp.TypeName = req.ProviderTypeName + "_postgres_service" |
| 77 | +} |
| 78 | + |
| 79 | +func (d *postgresServiceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 80 | + resp.Schema = schema.Schema{ |
| 81 | + MarkdownDescription: postgresServiceDataSourceDescription, |
| 82 | + Attributes: map[string]schema.Attribute{ |
| 83 | + "id": schema.StringAttribute{Description: "Unique identifier of the Postgres service to look up.", Required: true}, |
| 84 | + "name": schema.StringAttribute{Description: "Human-readable name.", Computed: true}, |
| 85 | + "cloud_provider": schema.StringAttribute{Description: "Cloud provider hosting the instance.", Computed: true}, |
| 86 | + "region": schema.StringAttribute{Description: "Cloud region.", Computed: true}, |
| 87 | + "postgres_version": schema.StringAttribute{Description: "Major Postgres version.", Computed: true}, |
| 88 | + "size": schema.StringAttribute{Description: "Instance size (VM SKU).", Computed: true}, |
| 89 | + "ha_type": schema.StringAttribute{Description: "High-availability mode ('none', 'async', 'sync').", Computed: true}, |
| 90 | + "state": schema.StringAttribute{Description: "Server-reported state.", Computed: true}, |
| 91 | + "created_at": schema.StringAttribute{Description: "RFC3339 creation timestamp.", Computed: true}, |
| 92 | + "is_primary": schema.BoolAttribute{Description: "True for a primary; false for a read replica.", Computed: true}, |
| 93 | + "hostname": schema.StringAttribute{Description: "Network hostname for client connections.", Computed: true}, |
| 94 | + "port": schema.Int64Attribute{Description: "TCP port for client connections.", Computed: true}, |
| 95 | + "username": schema.StringAttribute{Description: "Default superuser name.", Computed: true}, |
| 96 | + "connection_string": schema.StringAttribute{Description: "Full connection URI (embeds the password). Sensitive.", Computed: true, Sensitive: true}, |
| 97 | + "tags": schema.MapAttribute{ |
| 98 | + Description: "User tags (server-reserved 'chc_' tags are filtered out). Read-only; represented as a map.", |
| 99 | + Computed: true, |
| 100 | + ElementType: types.StringType, |
| 101 | + }, |
| 102 | + "pg_config": schema.MapAttribute{ |
| 103 | + Description: "Postgres server parameters currently set on the instance. Read-only; represented as a map.", |
| 104 | + Computed: true, |
| 105 | + ElementType: types.StringType, |
| 106 | + }, |
| 107 | + "pgbouncer_config": schema.MapAttribute{ |
| 108 | + Description: "PgBouncer parameters currently set on the instance. Read-only; represented as a map.", |
| 109 | + Computed: true, |
| 110 | + ElementType: types.StringType, |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (d *postgresServiceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 117 | + var data postgresServiceDataSourceModel |
| 118 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 119 | + if resp.Diagnostics.HasError() { |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + pg, err := d.client.GetPostgres(ctx, data.ID.ValueString()) |
| 124 | + if err != nil { |
| 125 | + resp.Diagnostics.AddError("Error reading Postgres service", "Could not read Postgres service "+data.ID.ValueString()+": "+err.Error()) |
| 126 | + return |
| 127 | + } |
| 128 | + cfg, err := d.client.GetPostgresConfig(ctx, data.ID.ValueString()) |
| 129 | + if err != nil { |
| 130 | + resp.Diagnostics.AddError("Error reading Postgres configuration", "Could not read config for Postgres service "+data.ID.ValueString()+": "+err.Error()) |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + data.ID = types.StringValue(pg.Id) |
| 135 | + data.Name = types.StringValue(pg.Name) |
| 136 | + data.CloudProvider = types.StringValue(pg.Provider) |
| 137 | + data.Region = types.StringValue(pg.Region) |
| 138 | + data.PostgresVersion = types.StringValue(pg.PostgresVersion) |
| 139 | + data.Size = types.StringValue(pg.Size) |
| 140 | + if pg.HaType != "" { |
| 141 | + data.HaType = types.StringValue(pg.HaType) |
| 142 | + } else { |
| 143 | + data.HaType = types.StringValue("none") |
| 144 | + } |
| 145 | + data.State = types.StringValue(pg.State) |
| 146 | + data.CreatedAt = types.StringValue(pg.CreatedAt) |
| 147 | + if pg.IsPrimary != nil { |
| 148 | + data.IsPrimary = types.BoolValue(*pg.IsPrimary) |
| 149 | + } else { |
| 150 | + data.IsPrimary = types.BoolValue(true) |
| 151 | + } |
| 152 | + data.Hostname = stringOrNull(pg.Hostname) |
| 153 | + data.Port = types.Int64Value(postgresDefaultPort) |
| 154 | + data.Username = stringOrNull(pg.Username) |
| 155 | + data.ConnectionString = stringOrNull(pg.ConnectionString) |
| 156 | + |
| 157 | + tags, diags := apiTagsToMap(pg.Tags) |
| 158 | + resp.Diagnostics.Append(diags...) |
| 159 | + pgCfg, diags := configToMap(cfg.PgConfig) |
| 160 | + resp.Diagnostics.Append(diags...) |
| 161 | + pbCfg, diags := configToMap(cfg.PgBouncerConfig) |
| 162 | + resp.Diagnostics.Append(diags...) |
| 163 | + if resp.Diagnostics.HasError() { |
| 164 | + return |
| 165 | + } |
| 166 | + data.Tags = tags |
| 167 | + data.PgConfig = pgCfg |
| 168 | + data.PgBouncerConfig = pbCfg |
| 169 | + |
| 170 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 171 | +} |
| 172 | + |
| 173 | +// --- shared helpers (datasource package, alpha) --------------------------- |
| 174 | + |
| 175 | +func stringOrNull(s *string) types.String { |
| 176 | + if s == nil { |
| 177 | + return types.StringNull() |
| 178 | + } |
| 179 | + return types.StringValue(*s) |
| 180 | +} |
| 181 | + |
| 182 | +// apiTagsToMap converts api tags to a string map, dropping chc_-prefixed |
| 183 | +// (server-reserved) tags. An empty value becomes an empty string. |
| 184 | +func apiTagsToMap(tags []api.Tag) (types.Map, diag.Diagnostics) { |
| 185 | + m := make(map[string]attr.Value, len(tags)) |
| 186 | + for _, t := range tags { |
| 187 | + if strings.HasPrefix(t.Key, postgresReservedTagPrefix) { |
| 188 | + continue |
| 189 | + } |
| 190 | + m[t.Key] = types.StringValue(t.Value) |
| 191 | + } |
| 192 | + return types.MapValue(types.StringType, m) |
| 193 | +} |
| 194 | + |
| 195 | +func configToMap(c api.PgConfigMap) (types.Map, diag.Diagnostics) { |
| 196 | + m := make(map[string]attr.Value, len(c)) |
| 197 | + for k, v := range c { |
| 198 | + m[k] = types.StringValue(v) |
| 199 | + } |
| 200 | + return types.MapValue(types.StringType, m) |
| 201 | +} |
0 commit comments