Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/data-sources/extended_attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "dbtcloud_extended_attributes Data Source - dbtcloud"
subcategory: ""
description: |-

Extended attributes data source
---

# dbtcloud_extended_attributes (Data Source)


Extended attributes data source

## Example Usage

Expand All @@ -24,11 +24,11 @@ data "dbtcloud_extended_attributes" "my_extended_attributes" {

### Required

- `extended_attributes_id` (Number) ID of the extended attributes
- `project_id` (Number) Project ID the extended attributes refers to
- `extended_attributes_id` (Number) Extended attributes ID
- `project_id` (Number) Project ID

### Read-Only

- `extended_attributes` (String) A JSON string listing the extended attributes mapping
- `id` (String) The ID of this resource.
- `extended_attributes` (String) Extended attributes
- `id` (String) The ID of this resource. Contains the project ID and the credential ID.
- `state` (Number) The state of the extended attributes (1 = active, 2 = inactive)
10 changes: 5 additions & 5 deletions docs/resources/extended_attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
page_title: "dbtcloud_extended_attributes Resource - dbtcloud"
subcategory: ""
description: |-
This resource allows setting extended attributes which can be assigned to a given environment (see docs https://docs.getdbt.com/docs/dbt-cloud-environments#extended-attributes).In dbt Cloud those values are provided as YML but in the provider they need to be provided as JSON (see example below).
Extended attributes resource
---

# dbtcloud_extended_attributes (Resource)


This resource allows setting extended attributes which can be assigned to a given environment ([see docs](https://docs.getdbt.com/docs/dbt-cloud-environments#extended-attributes)).<br/><br/>In dbt Cloud those values are provided as YML but in the provider they need to be provided as JSON (see example below).
Extended attributes resource

## Example Usage

Expand Down Expand Up @@ -51,12 +51,12 @@ resource "dbtcloud_environment" "issue_depl" {

### Optional

- `state` (Number, Deprecated) Extended Attributes state (1 is active, 2 is inactive)
- `state` (Number) The state of the extended attributes (1 = active, 2 = inactive)

### Read-Only

- `extended_attributes_id` (Number) Extended Attributes ID
- `id` (String) The ID of this resource.
- `extended_attributes_id` (Number) Extended attributes ID
- `id` (String) The ID of this resource. Contains the project ID and the extended attributes ID.

## Import

Expand Down
81 changes: 81 additions & 0 deletions pkg/framework/objects/extended_attributes/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package extended_attributes

import (
"context"
"fmt"

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

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

func ExtendedAttributesDataSource() datasource.DataSource {
return &extendedAttributesDataSource{}
}

type extendedAttributesDataSource struct {
client *dbt_cloud.Client
}

func (p *extendedAttributesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*dbt_cloud.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *dbt_cloud.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

p.client = client
}

func (p *extendedAttributesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_extended_attributes"
}

func (p *extendedAttributesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state ExtendedAttributesDataSourceModel
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

projectId := int(state.ProjectID.ValueInt64())
extendedAttributesId := int(state.ExtendedAttributesID.ValueInt64())

extendedAttributes, err := p.client.GetExtendedAttributes(projectId, extendedAttributesId)
if err != nil {
resp.Diagnostics.AddError(
"Error reading Extended attributes",
"Could not read Extended attributes ID "+state.ID.ValueString()+": "+err.Error(),
)
return
}

state.ID = types.StringValue(fmt.Sprintf("%d%s%d", *extendedAttributes.ID, dbt_cloud.ID_DELIMITER, *extendedAttributes.ID))
state.ProjectID = types.Int64Value(int64(extendedAttributes.ProjectID))
state.ExtendedAttributesID = types.Int64Value(int64(*extendedAttributes.ID))
state.ExtendedAttributes = types.StringValue(string(extendedAttributes.ExtendedAttributes))
state.State = types.Int64Value(int64(extendedAttributes.State))

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

func (p *extendedAttributesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = dataSourceSchema
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package data_sources_test
package extended_attributes_test

import (
"fmt"
"testing"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
Expand Down Expand Up @@ -76,5 +77,5 @@ func extendedAttributes() string {
project_id = dbtcloud_project.test_project.id
environment_id = dbtcloud_environment.test_environment.environment_id
}
`, DBT_CLOUD_VERSION)
`, acctest_config.DBT_CLOUD_VERSION)
}
21 changes: 21 additions & 0 deletions pkg/framework/objects/extended_attributes/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package extended_attributes

import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

type ExtendedAttributesResourceModel struct {
ID types.String `tfsdk:"id"`
ExtendedAttributesID types.Int64 `tfsdk:"extended_attributes_id"`
State types.Int64 `tfsdk:"state"`
ProjectID types.Int64 `tfsdk:"project_id"`
ExtendedAttributes types.String `tfsdk:"extended_attributes"`
}

type ExtendedAttributesDataSourceModel struct {
ID types.String `tfsdk:"id"`
ExtendedAttributesID types.Int64 `tfsdk:"extended_attributes_id"`
ProjectID types.Int64 `tfsdk:"project_id"`
State types.Int64 `tfsdk:"state"`
ExtendedAttributes types.String `tfsdk:"extended_attributes"`
}
Loading
Loading