Skip to content

Commit

Permalink
Merge pull request #256 from dbt-labs/release-0.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
b-per authored May 14, 2024
2 parents 4560c7c + d50ea78 commit ac2460f
Show file tree
Hide file tree
Showing 63 changed files with 1,451 additions and 435 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

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

## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.3...HEAD)
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.4...HEAD)

## [0.3.4](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.3...v0.3.4)

## Changes

- [#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

## Behind the scenes

- Move the `dbtcloud_environment` datasource to the Terraform Plugin Framework

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

Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

20 changes: 9 additions & 11 deletions docs/data-sources/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "dbtcloud_environment Data Source - dbtcloud"
subcategory: ""
description: |-
Retrieve data for a single environment
---

# dbtcloud_environment (Data Source)


Retrieve data for a single environment



Expand All @@ -17,18 +17,16 @@ description: |-

### Required

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

### Read-Only

- `credential_id` (Number) Credential ID to create the environment with
- `custom_branch` (String) Which custom branch to use in this environment
- `credentials_id` (Number) The project ID to which the environment belong
- `custom_branch` (String) The type of deployment environment (currently 'production', 'staging' or empty)
- `dbt_version` (String) Version number of dbt to use in this environment, usually in the format 1.2.0-latest rather than core versions
- `deployment_type` (String) The type of deployment environment (currently 'production' or empty)
- `deployment_type` (String) The name of the environment
- `extended_attributes_id` (Number) The ID of the extended attributes applied
- `id` (String) The ID of this resource.
- `is_active` (Boolean) Whether the environment is active
- `name` (String) Environment name
- `type` (String) The type of environment (must be either development or deployment)
- `name` (String) The name of the environment
- `type` (String) The name of the environment
- `use_custom_branch` (Boolean) Whether to use a custom git branch in this environment
40 changes: 40 additions & 0 deletions docs/data-sources/environments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "dbtcloud_environments Data Source - dbtcloud"
subcategory: ""
description: |-
Retrieve data for multiple environments
---

# dbtcloud_environments (Data Source)

Retrieve data for multiple environments



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `project_id` (Number) The project ID to filter the environments for [Optional]

### Read-Only

- `environments` (Attributes Set) The list of environments (see [below for nested schema](#nestedatt--environments))

<a id="nestedatt--environments"></a>
### Nested Schema for `environments`

Read-Only:

- `credentials_id` (Number) The project ID to which the environment belong
- `custom_branch` (String) The type of deployment environment (currently 'production', 'staging' or empty)
- `dbt_version` (String) Version number of dbt to use in this environment, usually in the format 1.2.0-latest rather than core versions
- `deployment_type` (String) The name of the environment
- `environment_id` (Number) The ID of the environment
- `extended_attributes_id` (Number) The ID of the extended attributes applied
- `name` (String) The name of the environment
- `project_id` (Number) The project ID to which the environment belong
- `type` (String) The name of the environment
- `use_custom_branch` (Boolean) Whether to use a custom git branch in this environment
4 changes: 2 additions & 2 deletions docs/resources/group_partial_permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |-
The current behavior of the resource is the following:
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
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
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
---

# dbtcloud_group_partial_permissions (Resource)
Expand All @@ -26,7 +26,7 @@ The current behavior of the resource is the following:
- when defining a new `dbt_cloud_group_partial_permissions`
- if the group doesn't exist with the given `name`, it will be created
- if a group exists with the given `name`, permissions will be added in the dbt Cloud group if they are not present yet
- 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.
- 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.
- 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 permissions
- 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
Expand Down
23 changes: 23 additions & 0 deletions pkg/dbt_cloud/paginate.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,26 @@ func (c *Client) GetAllGroupIDsByName(groupName string) []int {
return 0, false
})
}

func (c *Client) GetAllEnvironments(projectID int) ([]Environment, error) {
url := fmt.Sprintf("%s/v3/accounts/%d/environments/", c.HostURL, c.AccountID)

if projectID != 0 {
url = fmt.Sprintf("%s?project_id=%d", url, projectID)
}

allEnvironmentsRaw := c.GetData(url)

allEnvs := []Environment{}
for _, env := range allEnvironmentsRaw {

data, _ := json.Marshal(env)
currentEnv := Environment{}
err := json.Unmarshal(data, &currentEnv)
if err != nil {
return nil, err
}
allEnvs = append(allEnvs, currentEnv)
}
return allEnvs, nil
}
87 changes: 87 additions & 0 deletions pkg/framework/objects/environment/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package environment

import (
"context"
"fmt"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ datasource.DataSource = &environmentDataSource{}
_ datasource.DataSourceWithConfigure = &environmentDataSource{}
)

func EnvironmentDataSource() datasource.DataSource {
return &environmentDataSource{}
}

type environmentDataSource struct {
client *dbt_cloud.Client
}

func (d *environmentDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_environment"
}

func (d *environmentDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var config EnvironmentDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)

environment, err := d.client.GetEnvironment(
int(config.ProjectID.ValueInt64()),
int(config.EnvironmentID.ValueInt64()),
)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Did not find environment with this ID: %s", config.EnvironmentID),
err.Error(),
)
return
}

state := config

state.CredentialsID = types.Int64PointerValue(
helper.IntPointerToInt64Pointer(environment.Credential_Id),
)
state.Name = types.StringValue(environment.Name)
state.DbtVersion = types.StringValue(environment.Dbt_Version)
state.Type = types.StringValue(environment.Type)
state.UseCustomBranch = types.BoolValue(environment.Use_Custom_Branch)
state.CustomBranch = types.StringPointerValue(environment.Custom_Branch)
state.DeploymentType = types.StringPointerValue(environment.DeploymentType)
state.ExtendedAttributesID = types.Int64PointerValue(
helper.IntPointerToInt64Pointer(environment.ExtendedAttributesID),
)

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

func (d *environmentDataSource) Configure(
_ context.Context,
req datasource.ConfigureRequest,
_ *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}

d.client = req.ProviderData.(*dbt_cloud.Client)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package data_sources_test
package environment_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

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

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.dbtcloud_environment.test", "name", randomEnvironmentName),
resource.TestCheckResourceAttr(
"data.dbtcloud_environment.test",
"name",
randomEnvironmentName,
),
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "environment_id"),
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "project_id"),
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "is_active"),
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "credential_id"),
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "dbt_version"),
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "type"),
resource.TestCheckResourceAttrSet("data.dbtcloud_environment.test", "use_custom_branch"),
resource.TestCheckResourceAttr("data.dbtcloud_environment.test", "custom_branch", "customBranchName"),
resource.TestCheckResourceAttr(
"data.dbtcloud_environment.test",
"custom_branch",
"customBranchName",
),
)

resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
ProtoV6ProviderFactories: acctest_helper.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config,
Expand Down Expand Up @@ -57,5 +64,5 @@ func environment(projectName, environmentName string) string {
project_id = dbtcloud_project.test_project.id
environment_id = dbtcloud_environment.test_environment.environment_id
}
`, projectName, environmentName, DBT_CLOUD_VERSION)
`, projectName, environmentName, acctest_helper.DBT_CLOUD_VERSION)
}
103 changes: 103 additions & 0 deletions pkg/framework/objects/environment/data_source_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package environment

import (
"context"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ datasource.DataSource = &environmentsDataSources{}
_ datasource.DataSourceWithConfigure = &environmentsDataSources{}
)

func EnvironmentsDataSources() datasource.DataSource {
return &environmentsDataSources{}
}

type environmentsDataSources struct {
client *dbt_cloud.Client
}

func (d *environmentsDataSources) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_environments"
}

func (d *environmentsDataSources) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var config EnvironmentsDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)

var projectID int
if config.ProjectID.IsNull() {
projectID = 0
} else {
projectID = int(config.ProjectID.ValueInt64())
}

environments, err := d.client.GetAllEnvironments(projectID)

if err != nil {
resp.Diagnostics.AddError(
"Issue when retrieving environments",
err.Error(),
)
return
}

state := config

allEnvs := []EnvironmentDataSourceModel{}
for _, environment := range environments {
currentEnv := EnvironmentDataSourceModel{}

currentEnv.EnvironmentID = types.Int64PointerValue(
helper.IntPointerToInt64Pointer(environment.Environment_Id),
)
currentEnv.ProjectID = types.Int64Value(int64(environment.Project_Id))

currentEnv.CredentialsID = types.Int64PointerValue(
helper.IntPointerToInt64Pointer(environment.Credential_Id),
)
currentEnv.Name = types.StringValue(environment.Name)
currentEnv.DbtVersion = types.StringValue(environment.Dbt_Version)
currentEnv.Type = types.StringValue(environment.Type)
currentEnv.UseCustomBranch = types.BoolValue(environment.Use_Custom_Branch)
currentEnv.CustomBranch = types.StringPointerValue(environment.Custom_Branch)
currentEnv.DeploymentType = types.StringPointerValue(environment.DeploymentType)
currentEnv.ExtendedAttributesID = types.Int64PointerValue(
helper.IntPointerToInt64Pointer(environment.ExtendedAttributesID),
)
allEnvs = append(allEnvs, currentEnv)
}
state.Environments = allEnvs

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

func (d *environmentsDataSources) Configure(
_ context.Context,
req datasource.ConfigureRequest,
_ *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}

d.client = req.ProviderData.(*dbt_cloud.Client)
}
Loading

0 comments on commit ac2460f

Please sign in to comment.