Skip to content

Commit ac2460f

Browse files
authored
Merge pull request #256 from dbt-labs/release-0.3.4
2 parents 4560c7c + d50ea78 commit ac2460f

File tree

63 files changed

+1451
-435
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1451
-435
lines changed

CHANGELOG.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.3...HEAD)
5+
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.4...HEAD)
6+
7+
## [0.3.4](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.3...v0.3.4)
8+
9+
## Changes
10+
11+
- [#255](https://github.com/dbt-labs/terraform-provider-dbtcloud/issues/255) - Add new datasource `dbtcloud_environments` to return all environments across an account, or all environments for a give project ID
12+
13+
## Behind the scenes
14+
15+
- Move the `dbtcloud_environment` datasource to the Terraform Plugin Framework
616

717
## [0.3.3](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.2...v0.3.3)
818

VERSION

-1
This file was deleted.

docs/data-sources/environment.md

+9-11
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "dbtcloud_environment Data Source - dbtcloud"
44
subcategory: ""
55
description: |-
6-
6+
Retrieve data for a single environment
77
---
88

99
# dbtcloud_environment (Data Source)
1010

11-
11+
Retrieve data for a single environment
1212

1313

1414

@@ -17,18 +17,16 @@ description: |-
1717

1818
### Required
1919

20-
- `environment_id` (Number) ID of the environment
21-
- `project_id` (Number) Project ID to create the environment in
20+
- `environment_id` (Number) The ID of the environment
21+
- `project_id` (Number) The project ID to which the environment belong
2222

2323
### Read-Only
2424

25-
- `credential_id` (Number) Credential ID to create the environment with
26-
- `custom_branch` (String) Which custom branch to use in this environment
25+
- `credentials_id` (Number) The project ID to which the environment belong
26+
- `custom_branch` (String) The type of deployment environment (currently 'production', 'staging' or empty)
2727
- `dbt_version` (String) Version number of dbt to use in this environment, usually in the format 1.2.0-latest rather than core versions
28-
- `deployment_type` (String) The type of deployment environment (currently 'production' or empty)
28+
- `deployment_type` (String) The name of the environment
2929
- `extended_attributes_id` (Number) The ID of the extended attributes applied
30-
- `id` (String) The ID of this resource.
31-
- `is_active` (Boolean) Whether the environment is active
32-
- `name` (String) Environment name
33-
- `type` (String) The type of environment (must be either development or deployment)
30+
- `name` (String) The name of the environment
31+
- `type` (String) The name of the environment
3432
- `use_custom_branch` (Boolean) Whether to use a custom git branch in this environment

docs/data-sources/environments.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbtcloud_environments Data Source - dbtcloud"
4+
subcategory: ""
5+
description: |-
6+
Retrieve data for multiple environments
7+
---
8+
9+
# dbtcloud_environments (Data Source)
10+
11+
Retrieve data for multiple environments
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- `project_id` (Number) The project ID to filter the environments for [Optional]
21+
22+
### Read-Only
23+
24+
- `environments` (Attributes Set) The list of environments (see [below for nested schema](#nestedatt--environments))
25+
26+
<a id="nestedatt--environments"></a>
27+
### Nested Schema for `environments`
28+
29+
Read-Only:
30+
31+
- `credentials_id` (Number) The project ID to which the environment belong
32+
- `custom_branch` (String) The type of deployment environment (currently 'production', 'staging' or empty)
33+
- `dbt_version` (String) Version number of dbt to use in this environment, usually in the format 1.2.0-latest rather than core versions
34+
- `deployment_type` (String) The name of the environment
35+
- `environment_id` (Number) The ID of the environment
36+
- `extended_attributes_id` (Number) The ID of the extended attributes applied
37+
- `name` (String) The name of the environment
38+
- `project_id` (Number) The project ID to which the environment belong
39+
- `type` (String) The name of the environment
40+
- `use_custom_branch` (Boolean) Whether to use a custom git branch in this environment

docs/resources/group_partial_permissions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: |-
88
The current behavior of the resource is the following:
99
when using dbt_cloud_group_partial_permissions, don't use dbt_cloud_group for the same group in any other project/workspace. Otherwise, the behavior is undefined and partial permissions might be removed.when defining a new dbt_cloud_group_partial_permissions
1010
11-
if the group doesn't exist with the given name, it will be createdif a group exists with the given name, permissions will be added in the dbt Cloud group if they are not present yetin a given Terraform project/workspace, avoid having different ~~dbtcloudgrouppartialpermissions` for the same group name to prevent sync issues. Add all the permissions in the same resource.all resources for the same group name need to have the same values for assign_by_default and sso_mapping_groups. Those fields are not considered "partial". (Please raise feedback in GitHub if you think that sso_mapping_groups should be "partial" as well)when a resource is updated, the dbt Cloud group will be updated accordingly, removing and adding permissionswhen the resource is deleted/destroyed, if the resulting permission sets is empty, the group will be deleted ; otherwise, the group will be updated, removing the permissions from the deleted resource
11+
if the group doesn't exist with the given name, it will be createdif a group exists with the given name, permissions will be added in the dbt Cloud group if they are not present yetin a given Terraform project/workspace, avoid having different dbt_cloud_group_partial_permissions for the same group name to prevent sync issues. Add all the permissions in the same resource.all resources for the same group name need to have the same values for assign_by_default and sso_mapping_groups. Those fields are not considered "partial". (Please raise feedback in GitHub if you think that sso_mapping_groups should be "partial" as well)when a resource is updated, the dbt Cloud group will be updated accordingly, removing and adding permissionswhen the resource is deleted/destroyed, if the resulting permission sets is empty, the group will be deleted ; otherwise, the group will be updated, removing the permissions from the deleted resource
1212
---
1313

1414
# dbtcloud_group_partial_permissions (Resource)
@@ -26,7 +26,7 @@ The current behavior of the resource is the following:
2626
- when defining a new `dbt_cloud_group_partial_permissions`
2727
- if the group doesn't exist with the given `name`, it will be created
2828
- if a group exists with the given `name`, permissions will be added in the dbt Cloud group if they are not present yet
29-
- in a given Terraform project/workspace, avoid having different ~~dbt_cloud_group_partial_permissions` for the same group name to prevent sync issues. Add all the permissions in the same resource.
29+
- in a given Terraform project/workspace, avoid having different `dbt_cloud_group_partial_permissions` for the same group name to prevent sync issues. Add all the permissions in the same resource.
3030
- all resources for the same group name need to have the same values for `assign_by_default` and `sso_mapping_groups`. Those fields are not considered "partial". (Please raise feedback in GitHub if you think that `sso_mapping_groups` should be "partial" as well)
3131
- when a resource is updated, the dbt Cloud group will be updated accordingly, removing and adding permissions
3232
- when the resource is deleted/destroyed, if the resulting permission sets is empty, the group will be deleted ; otherwise, the group will be updated, removing the permissions from the deleted resource

pkg/dbt_cloud/paginate.go

+23
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,26 @@ func (c *Client) GetAllGroupIDsByName(groupName string) []int {
105105
return 0, false
106106
})
107107
}
108+
109+
func (c *Client) GetAllEnvironments(projectID int) ([]Environment, error) {
110+
url := fmt.Sprintf("%s/v3/accounts/%d/environments/", c.HostURL, c.AccountID)
111+
112+
if projectID != 0 {
113+
url = fmt.Sprintf("%s?project_id=%d", url, projectID)
114+
}
115+
116+
allEnvironmentsRaw := c.GetData(url)
117+
118+
allEnvs := []Environment{}
119+
for _, env := range allEnvironmentsRaw {
120+
121+
data, _ := json.Marshal(env)
122+
currentEnv := Environment{}
123+
err := json.Unmarshal(data, &currentEnv)
124+
if err != nil {
125+
return nil, err
126+
}
127+
allEnvs = append(allEnvs, currentEnv)
128+
}
129+
return allEnvs, nil
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package environment
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
8+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
11+
)
12+
13+
var (
14+
_ datasource.DataSource = &environmentDataSource{}
15+
_ datasource.DataSourceWithConfigure = &environmentDataSource{}
16+
)
17+
18+
func EnvironmentDataSource() datasource.DataSource {
19+
return &environmentDataSource{}
20+
}
21+
22+
type environmentDataSource struct {
23+
client *dbt_cloud.Client
24+
}
25+
26+
func (d *environmentDataSource) Metadata(
27+
_ context.Context,
28+
req datasource.MetadataRequest,
29+
resp *datasource.MetadataResponse,
30+
) {
31+
resp.TypeName = req.ProviderTypeName + "_environment"
32+
}
33+
34+
func (d *environmentDataSource) Read(
35+
ctx context.Context,
36+
req datasource.ReadRequest,
37+
resp *datasource.ReadResponse,
38+
) {
39+
var config EnvironmentDataSourceModel
40+
41+
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
42+
43+
environment, err := d.client.GetEnvironment(
44+
int(config.ProjectID.ValueInt64()),
45+
int(config.EnvironmentID.ValueInt64()),
46+
)
47+
if err != nil {
48+
resp.Diagnostics.AddError(
49+
fmt.Sprintf("Did not find environment with this ID: %s", config.EnvironmentID),
50+
err.Error(),
51+
)
52+
return
53+
}
54+
55+
state := config
56+
57+
state.CredentialsID = types.Int64PointerValue(
58+
helper.IntPointerToInt64Pointer(environment.Credential_Id),
59+
)
60+
state.Name = types.StringValue(environment.Name)
61+
state.DbtVersion = types.StringValue(environment.Dbt_Version)
62+
state.Type = types.StringValue(environment.Type)
63+
state.UseCustomBranch = types.BoolValue(environment.Use_Custom_Branch)
64+
state.CustomBranch = types.StringPointerValue(environment.Custom_Branch)
65+
state.DeploymentType = types.StringPointerValue(environment.DeploymentType)
66+
state.ExtendedAttributesID = types.Int64PointerValue(
67+
helper.IntPointerToInt64Pointer(environment.ExtendedAttributesID),
68+
)
69+
70+
diags := resp.State.Set(ctx, &state)
71+
resp.Diagnostics.Append(diags...)
72+
if resp.Diagnostics.HasError() {
73+
return
74+
}
75+
}
76+
77+
func (d *environmentDataSource) Configure(
78+
_ context.Context,
79+
req datasource.ConfigureRequest,
80+
_ *datasource.ConfigureResponse,
81+
) {
82+
if req.ProviderData == nil {
83+
return
84+
}
85+
86+
d.client = req.ProviderData.(*dbt_cloud.Client)
87+
}

pkg/sdkv2/data_sources/environment_acceptance_test.go renamed to pkg/framework/objects/environment/data_source_acceptance_test.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package data_sources_test
1+
package environment_test
22

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

7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
8+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
910
)
1011

1112
func TestAccDbtCloudEnvironmentDataSource(t *testing.T) {
@@ -16,19 +17,25 @@ func TestAccDbtCloudEnvironmentDataSource(t *testing.T) {
1617
config := environment(randomProjectName, randomEnvironmentName)
1718

1819
check := resource.ComposeAggregateTestCheckFunc(
19-
resource.TestCheckResourceAttr("data.dbtcloud_environment.test", "name", randomEnvironmentName),
20+
resource.TestCheckResourceAttr(
21+
"data.dbtcloud_environment.test",
22+
"name",
23+
randomEnvironmentName,
24+
),
2025
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "environment_id"),
2126
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "project_id"),
22-
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "is_active"),
23-
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "credential_id"),
2427
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "dbt_version"),
2528
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "type"),
2629
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "use_custom_branch"),
27-
resource.TestCheckResourceAttr("data.dbtcloud_environment.test", "custom_branch", "customBranchName"),
30+
resource.TestCheckResourceAttr(
31+
"data.dbtcloud_environment.test",
32+
"custom_branch",
33+
"customBranchName",
34+
),
2835
)
2936

3037
resource.ParallelTest(t, resource.TestCase{
31-
Providers: providers(),
38+
ProtoV6ProviderFactories: acctest_helper.TestAccProtoV6ProviderFactories,
3239
Steps: []resource.TestStep{
3340
{
3441
Config: config,
@@ -57,5 +64,5 @@ func environment(projectName, environmentName string) string {
5764
project_id = dbtcloud_project.test_project.id
5865
environment_id = dbtcloud_environment.test_environment.environment_id
5966
}
60-
`, projectName, environmentName, DBT_CLOUD_VERSION)
67+
`, projectName, environmentName, acctest_helper.DBT_CLOUD_VERSION)
6168
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package environment
2+
3+
import (
4+
"context"
5+
6+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
7+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
)
11+
12+
var (
13+
_ datasource.DataSource = &environmentsDataSources{}
14+
_ datasource.DataSourceWithConfigure = &environmentsDataSources{}
15+
)
16+
17+
func EnvironmentsDataSources() datasource.DataSource {
18+
return &environmentsDataSources{}
19+
}
20+
21+
type environmentsDataSources struct {
22+
client *dbt_cloud.Client
23+
}
24+
25+
func (d *environmentsDataSources) Metadata(
26+
_ context.Context,
27+
req datasource.MetadataRequest,
28+
resp *datasource.MetadataResponse,
29+
) {
30+
resp.TypeName = req.ProviderTypeName + "_environments"
31+
}
32+
33+
func (d *environmentsDataSources) Read(
34+
ctx context.Context,
35+
req datasource.ReadRequest,
36+
resp *datasource.ReadResponse,
37+
) {
38+
var config EnvironmentsDataSourceModel
39+
40+
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
41+
42+
var projectID int
43+
if config.ProjectID.IsNull() {
44+
projectID = 0
45+
} else {
46+
projectID = int(config.ProjectID.ValueInt64())
47+
}
48+
49+
environments, err := d.client.GetAllEnvironments(projectID)
50+
51+
if err != nil {
52+
resp.Diagnostics.AddError(
53+
"Issue when retrieving environments",
54+
err.Error(),
55+
)
56+
return
57+
}
58+
59+
state := config
60+
61+
allEnvs := []EnvironmentDataSourceModel{}
62+
for _, environment := range environments {
63+
currentEnv := EnvironmentDataSourceModel{}
64+
65+
currentEnv.EnvironmentID = types.Int64PointerValue(
66+
helper.IntPointerToInt64Pointer(environment.Environment_Id),
67+
)
68+
currentEnv.ProjectID = types.Int64Value(int64(environment.Project_Id))
69+
70+
currentEnv.CredentialsID = types.Int64PointerValue(
71+
helper.IntPointerToInt64Pointer(environment.Credential_Id),
72+
)
73+
currentEnv.Name = types.StringValue(environment.Name)
74+
currentEnv.DbtVersion = types.StringValue(environment.Dbt_Version)
75+
currentEnv.Type = types.StringValue(environment.Type)
76+
currentEnv.UseCustomBranch = types.BoolValue(environment.Use_Custom_Branch)
77+
currentEnv.CustomBranch = types.StringPointerValue(environment.Custom_Branch)
78+
currentEnv.DeploymentType = types.StringPointerValue(environment.DeploymentType)
79+
currentEnv.ExtendedAttributesID = types.Int64PointerValue(
80+
helper.IntPointerToInt64Pointer(environment.ExtendedAttributesID),
81+
)
82+
allEnvs = append(allEnvs, currentEnv)
83+
}
84+
state.Environments = allEnvs
85+
86+
diags := resp.State.Set(ctx, &state)
87+
resp.Diagnostics.Append(diags...)
88+
if resp.Diagnostics.HasError() {
89+
return
90+
}
91+
}
92+
93+
func (d *environmentsDataSources) Configure(
94+
_ context.Context,
95+
req datasource.ConfigureRequest,
96+
_ *datasource.ConfigureResponse,
97+
) {
98+
if req.ProviderData == nil {
99+
return
100+
}
101+
102+
d.client = req.ProviderData.(*dbt_cloud.Client)
103+
}

0 commit comments

Comments
 (0)