Skip to content

Commit d028c5a

Browse files
authored
Environments and Credentials
2 parents 30733a4 + 25df710 commit d028c5a

20 files changed

+1276
-12
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.46
1+
0.0.69
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_environment Data Source - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_environment (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **environment_id** (Number) Project ID to create the environment in
21+
- **project_id** (Number) Project ID to create the environment in
22+
23+
### Optional
24+
25+
- **id** (String) The ID of this resource.
26+
27+
### Read-Only
28+
29+
- **credential_id** (Number) Credential ID to create the environment with
30+
- **custom_branch** (String) Which custom branch to use in this environment
31+
- **dbt_version** (String) Version number of dbt to use in this environment
32+
- **is_active** (Boolean) Whether the environment is active
33+
- **name** (String) Environment name
34+
- **type** (String) The type of environment (must be either development or deployment)
35+
- **use_custom_branch** (Boolean) Whether to use a custom git branch in this environment
36+
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_snowflake_credential Data Source - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_snowflake_credential (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **credential_id** (Number) Credential ID
21+
- **project_id** (Number) Project ID
22+
23+
### Optional
24+
25+
- **id** (String) The ID of this resource.
26+
27+
### Read-Only
28+
29+
- **auth_type** (String) The type of Snowflake credential ('password' only currently supported in Terraform)
30+
- **is_active** (Boolean) Whether the Snowflake credential is active
31+
- **num_threads** (Number) Number of threads to use
32+
- **password** (String, Sensitive) Password for Snowflake
33+
- **schema** (String) Default schema name
34+
- **user** (String) Username for Snowflake
35+
36+
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_environment Resource - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_environment (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **dbt_version** (String) Version number of dbt to use in this environment
21+
- **name** (String) Environment name
22+
- **project_id** (Number) Project ID to create the environment in
23+
- **type** (String) The type of environment (must be either development or deployment)
24+
25+
### Optional
26+
27+
- **credential_id** (Number) Credential ID to create the environment with
28+
- **custom_branch** (String) Which custom branch to use in this environment
29+
- **id** (String) The ID of this resource.
30+
- **is_active** (Boolean) Whether the environment is active
31+
- **use_custom_branch** (Boolean) Whether to use a custom git branch in this environment
32+
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_snowflake_credential Resource - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_snowflake_credential (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **auth_type** (String) The type of Snowflake credential ('password' only currently supported in Terraform)
21+
- **num_threads** (Number) Number of threads to use
22+
- **password** (String, Sensitive) Password for Snowflake
23+
- **project_id** (Number) Project ID to create the Snowflake credential in
24+
- **schema** (String) Default schema name
25+
- **user** (String) Username for Snowflake
26+
27+
### Optional
28+
29+
- **id** (String) The ID of this resource.
30+
- **is_active** (Boolean) Whether the Snowflake credential is active
31+
32+
### Read-Only
33+
34+
- **credential_id** (Number) The system Snowflake credential ID
35+
36+

pkg/data_sources/environment.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package data_sources
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/gthesheep/terraform-provider-dbt-cloud/pkg/dbt_cloud"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
var environmentSchema = map[string]*schema.Schema{
13+
"environment_id": &schema.Schema{
14+
Type: schema.TypeInt,
15+
Required: true,
16+
Description: "Project ID to create the environment in",
17+
},
18+
"project_id": &schema.Schema{
19+
Type: schema.TypeInt,
20+
Required: true,
21+
Description: "Project ID to create the environment in",
22+
},
23+
"is_active": &schema.Schema{
24+
Type: schema.TypeBool,
25+
Computed: true,
26+
Description: "Whether the environment is active",
27+
},
28+
"credential_id": &schema.Schema{
29+
Type: schema.TypeInt,
30+
Computed: true,
31+
Description: "Credential ID to create the environment with",
32+
},
33+
"name": &schema.Schema{
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Description: "Environment name",
37+
},
38+
"dbt_version": &schema.Schema{
39+
Type: schema.TypeString,
40+
Computed: true,
41+
Description: "Version number of dbt to use in this environment",
42+
},
43+
"type": &schema.Schema{
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "The type of environment (must be either development or deployment)",
47+
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
48+
type_ := val.(string)
49+
switch type_ {
50+
case
51+
"development",
52+
"deployment":
53+
return
54+
}
55+
errs = append(errs, fmt.Errorf("%q must be either development or deployment, got: %q", key, type_))
56+
return
57+
},
58+
},
59+
"use_custom_branch": &schema.Schema{
60+
Type: schema.TypeBool,
61+
Computed: true,
62+
Description: "Whether to use a custom git branch in this environment",
63+
},
64+
"custom_branch": &schema.Schema{
65+
Type: schema.TypeString,
66+
Computed: true,
67+
Description: "Which custom branch to use in this environment",
68+
},
69+
}
70+
71+
func DatasourceEnvironment() *schema.Resource {
72+
return &schema.Resource{
73+
ReadContext: datasourceEnvironmentRead,
74+
Schema: environmentSchema,
75+
}
76+
}
77+
78+
func datasourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
79+
c := m.(*dbt_cloud.Client)
80+
81+
var diags diag.Diagnostics
82+
83+
environmentID := d.Get("environment_id").(int)
84+
projectID := d.Get("project_id").(int)
85+
86+
environment, err := c.GetEnvironment(projectID, environmentID)
87+
if err != nil {
88+
return diag.FromErr(err)
89+
}
90+
91+
if err := d.Set("is_active", environment.State == dbt_cloud.STATE_ACTIVE); err != nil {
92+
return diag.FromErr(err)
93+
}
94+
if err := d.Set("project_id", environment.Project_Id); err != nil {
95+
return diag.FromErr(err)
96+
}
97+
if err := d.Set("credential_id", environment.Credential_Id); err != nil {
98+
return diag.FromErr(err)
99+
}
100+
if err := d.Set("name", environment.Name); err != nil {
101+
return diag.FromErr(err)
102+
}
103+
if err := d.Set("dbt_version", environment.Dbt_Version); err != nil {
104+
return diag.FromErr(err)
105+
}
106+
if err := d.Set("type", environment.Type); err != nil {
107+
return diag.FromErr(err)
108+
}
109+
if err := d.Set("use_custom_branch", environment.Use_Custom_Branch); err != nil {
110+
return diag.FromErr(err)
111+
}
112+
if err := d.Set("custom_branch", environment.Custom_Branch); err != nil {
113+
return diag.FromErr(err)
114+
}
115+
116+
d.SetId(fmt.Sprintf("%d%s%d", environment.Project_Id, dbt_cloud.ID_DELIMITER, *environment.ID))
117+
118+
return diags
119+
}

pkg/data_sources/environment_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package data_sources_test
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
)
11+
12+
func TestAccDbtCloudEnvironmentDataSource(t *testing.T) {
13+
14+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
15+
randomIDInt, _ := strconv.Atoi(randomID)
16+
17+
config := fmt.Sprintf(`
18+
data "dbt_cloud_environment" "test" {
19+
project_id = 123
20+
environment_id = %d
21+
}
22+
`, randomIDInt)
23+
24+
check := resource.ComposeAggregateTestCheckFunc(
25+
resource.TestCheckResourceAttr("data.dbt_cloud_environment.test", "environment_id", randomID),
26+
resource.TestCheckResourceAttr("data.dbt_cloud_environment.test", "project_id", "123"),
27+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "name"),
28+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "is_active"),
29+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "credential_id"),
30+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "dbt_version"),
31+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "type"),
32+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "use_custom_branch"),
33+
resource.TestCheckResourceAttrSet("data.dbt_cloud_project.test", "custom_branch"),
34+
)
35+
36+
resource.ParallelTest(t, resource.TestCase{
37+
Providers: providers(),
38+
Steps: []resource.TestStep{
39+
{
40+
Config: config,
41+
Check: check,
42+
},
43+
},
44+
})
45+
}

0 commit comments

Comments
 (0)