Skip to content

Commit eb0ebb3

Browse files
Migrate environment variable and environment variable job override (#384)
* Migrate env variable resource and data source * Migrate env variable job override * Fix failing tests and add conformance tests
1 parent 7ed5a09 commit eb0ebb3

20 files changed

Lines changed: 1594 additions & 984 deletions

docs/data-sources/environment_variable.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "dbtcloud_environment_variable Data Source - dbtcloud"
44
subcategory: ""
55
description: |-
6-
6+
Environment variable credential data source
77
---
88

99
# dbtcloud_environment_variable (Data Source)
1010

11-
11+
Environment variable credential data source
1212

1313

1414

@@ -17,10 +17,10 @@ description: |-
1717

1818
### Required
1919

20-
- `name` (String) Name for the variable
21-
- `project_id` (Number) Project ID the variable exists in
20+
- `name` (String) Name for the variable, must be unique within a project, must be prefixed with 'DBT_'
21+
- `project_id` (Number) Project ID to create the environment variable in
2222

2323
### Read-Only
2424

25-
- `environment_values` (Map of String) Map containing the environment variables
26-
- `id` (String) The ID of this resource.
25+
- `environment_values` (Map of String) Map from environment names to respective variable value, a special key `project` should be set for the project default variable value. This field is not set as sensitive so take precautions when using secret environment variables.
26+
- `id` (String) The ID of this resource. Contains the project ID and the environment variable ID.

docs/resources/environment_variable.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
page_title: "dbtcloud_environment_variable Resource - dbtcloud"
33
subcategory: ""
44
description: |-
5-
5+
Environment variable resource
66
---
77

88
# dbtcloud_environment_variable (Resource)
@@ -38,11 +38,11 @@ resource "dbtcloud_environment_variable" "dbt_my_env_var" {
3838

3939
- `environment_values` (Map of String) Map from environment names to respective variable value, a special key `project` should be set for the project default variable value. This field is not set as sensitive so take precautions when using secret environment variables.
4040
- `name` (String) Name for the variable, must be unique within a project, must be prefixed with 'DBT_'
41-
- `project_id` (Number) Project for the variable to be created in
41+
- `project_id` (Number) Project ID to create the environment variable in
4242

4343
### Read-Only
4444

45-
- `id` (String) The ID of this resource.
45+
- `id` (String) The ID of this resource. Contains the project ID and the environment variable ID.
4646

4747
## Import
4848

docs/resources/environment_variable_job_override.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
page_title: "dbtcloud_environment_variable_job_override Resource - dbtcloud"
33
subcategory: ""
44
description: |-
5-
5+
Environment variable job override resource
66
---
77

88
# dbtcloud_environment_variable_job_override (Resource)
99

1010

11-
11+
Environment variable job override resource
1212

1313
## Example Usage
1414

@@ -28,13 +28,14 @@ resource "dbtcloud_environment_variable_job_override" "my_env_var_job_override"
2828

2929
- `job_definition_id` (Number) The job ID for which the environment variable is being overridden
3030
- `name` (String) The environment variable name to override
31-
- `project_id` (Number) The project ID for which the environment variable is being overridden
31+
- `project_id` (Number) Project ID to create the environment variable job override in
3232
- `raw_value` (String) The value for the override of the environment variable
3333

3434
### Read-Only
3535

36-
- `environment_variable_job_override_id` (Number) The ID of the environment variable job override
37-
- `id` (String) The ID of this resource.
36+
- `account_id` (Number) The account id
37+
- `environment_variable_job_override_id` (Number) The internal ID of this resource. Contains the project ID and the environment variable job override ID.
38+
- `id` (String) The ID of this resource. Contains the project ID and the environment variable job override ID.
3839

3940
## Import
4041

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

pkg/sdkv2/data_sources/environment_variable_acceptance_test.go renamed to pkg/framework/objects/environment_variable/datasource_acceptance_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package data_sources_test
1+
package environment_variable_test
22

33
import (
44
"fmt"
55
"strings"
66
"testing"
77

8+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
89
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
910
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1011
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
@@ -88,5 +89,5 @@ data "dbtcloud_environment_variable" "test_env_var_read" {
8889
name = dbtcloud_environment_variable.test_env_var.name
8990
project_id = dbtcloud_environment_variable.test_env_var.project_id
9091
}
91-
`, projectName, environmentName, DBT_CLOUD_VERSION, environmentVariableName, environmentName)
92+
`, projectName, environmentName, acctest_config.DBT_CLOUD_VERSION, environmentVariableName, environmentName)
9293
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package environment_variable
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
)
6+
7+
// EnvironmentVariableResourceModel is the model for the resource
8+
type EnvironmentVariableResourceModel struct {
9+
ID types.String `tfsdk:"id"`
10+
ProjectID types.Int64 `tfsdk:"project_id"`
11+
Name types.String `tfsdk:"name"`
12+
EnvironmentValues types.Map `tfsdk:"environment_values"`
13+
}
14+
15+
// EnvironmentVariableDataSourceModel is the model for the data source
16+
type EnvironmentVariableDataSourceModel struct {
17+
ID types.String `tfsdk:"id"`
18+
ProjectID types.Int64 `tfsdk:"project_id"`
19+
Name types.String `tfsdk:"name"`
20+
EnvironmentValues types.Map `tfsdk:"environment_values"`
21+
}

0 commit comments

Comments
 (0)