|
| 1 | +package environment_variable |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | +) |
| 12 | + |
| 13 | +// Ensure the implementation satisfies the expected interfaces. |
| 14 | +var ( |
| 15 | + _ datasource.DataSource = &environmentVariableDataSource{} |
| 16 | + _ datasource.DataSourceWithConfigure = &environmentVariableDataSource{} |
| 17 | +) |
| 18 | + |
| 19 | +// EnvironmentVariableDataSource is a helper function to simplify the provider implementation. |
| 20 | +func EnvironmentVariableDataSource() datasource.DataSource { |
| 21 | + return &environmentVariableDataSource{} |
| 22 | +} |
| 23 | + |
| 24 | +// environmentVariableDataSource is the data source implementation. |
| 25 | +type environmentVariableDataSource struct { |
| 26 | + client *dbt_cloud.Client |
| 27 | +} |
| 28 | + |
| 29 | +// Configure adds the provider configured client to the data source. |
| 30 | +func (d *environmentVariableDataSource) Configure( |
| 31 | + ctx context.Context, |
| 32 | + req datasource.ConfigureRequest, |
| 33 | + resp *datasource.ConfigureResponse, |
| 34 | +) { |
| 35 | + if req.ProviderData == nil { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + client, ok := req.ProviderData.(*dbt_cloud.Client) |
| 40 | + if !ok { |
| 41 | + resp.Diagnostics.AddError( |
| 42 | + "Unexpected Data Source Configure Type", |
| 43 | + fmt.Sprintf( |
| 44 | + "Expected *dbt_cloud.Client, got: %T. Please report this issue to the provider developers.", |
| 45 | + req.ProviderData, |
| 46 | + ), |
| 47 | + ) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + d.client = client |
| 52 | +} |
| 53 | + |
| 54 | +// Metadata returns the data source type name. |
| 55 | +func (d *environmentVariableDataSource) Metadata( |
| 56 | + ctx context.Context, |
| 57 | + req datasource.MetadataRequest, |
| 58 | + resp *datasource.MetadataResponse, |
| 59 | +) { |
| 60 | + resp.TypeName = req.ProviderTypeName + "_environment_variable" |
| 61 | +} |
| 62 | + |
| 63 | +// Schema defines the schema for the data source. |
| 64 | +func (d *environmentVariableDataSource) Schema( |
| 65 | + ctx context.Context, |
| 66 | + req datasource.SchemaRequest, |
| 67 | + resp *datasource.SchemaResponse, |
| 68 | +) { |
| 69 | + resp.Schema = datasourceSchema |
| 70 | +} |
| 71 | + |
| 72 | +// Read refreshes the Terraform state with the latest data. |
| 73 | +func (d *environmentVariableDataSource) Read( |
| 74 | + ctx context.Context, |
| 75 | + req datasource.ReadRequest, |
| 76 | + resp *datasource.ReadResponse, |
| 77 | +) { |
| 78 | + var state EnvironmentVariableDataSourceModel |
| 79 | + diags := req.Config.Get(ctx, &state) |
| 80 | + resp.Diagnostics.Append(diags...) |
| 81 | + if resp.Diagnostics.HasError() { |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + projectID := int(state.ProjectID.ValueInt64()) |
| 86 | + name := state.Name.ValueString() |
| 87 | + |
| 88 | + envVar, err := d.client.GetEnvironmentVariable(projectID, name) |
| 89 | + if err != nil { |
| 90 | + resp.Diagnostics.AddError( |
| 91 | + "Error reading environment variable", |
| 92 | + "Could not read environment variable ID "+state.ID.ValueString()+": "+err.Error(), |
| 93 | + ) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + // Map response body to model |
| 98 | + state.ID = types.StringValue(fmt.Sprintf("%d:%s", envVar.ProjectID, envVar.Name)) |
| 99 | + state.Name = types.StringValue(envVar.Name) |
| 100 | + |
| 101 | + envVarElements := make(map[string]attr.Value) |
| 102 | + for key, value := range envVar.EnvironmentNameValues { |
| 103 | + envVarElements[key] = types.StringValue(value) |
| 104 | + } |
| 105 | + |
| 106 | + envVarMap, diag := types.MapValue(types.StringType, envVarElements) |
| 107 | + resp.Diagnostics.Append(diag...) |
| 108 | + if resp.Diagnostics.HasError() { |
| 109 | + return |
| 110 | + } |
| 111 | + state.EnvironmentValues = envVarMap |
| 112 | + |
| 113 | + // Set state |
| 114 | + diags = resp.State.Set(ctx, &state) |
| 115 | + resp.Diagnostics.Append(diags...) |
| 116 | + if resp.Diagnostics.HasError() { |
| 117 | + return |
| 118 | + } |
| 119 | +} |
0 commit comments