Skip to content

Commit 7dc8d0d

Browse files
Migrate the extended attributes data source and resource (#378)
* Migrate the extended attributes data source and resource * Remove old sdk files
1 parent 9e3e9bf commit 7dc8d0d

13 files changed

Lines changed: 634 additions & 365 deletions

File tree

docs/data-sources/extended_attributes.md

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

99
# dbtcloud_extended_attributes (Data Source)
1010

11-
11+
Extended attributes data source
1212

1313
## Example Usage
1414

@@ -24,11 +24,11 @@ data "dbtcloud_extended_attributes" "my_extended_attributes" {
2424

2525
### Required
2626

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

3030
### Read-Only
3131

32-
- `extended_attributes` (String) A JSON string listing the extended attributes mapping
33-
- `id` (String) The ID of this resource.
32+
- `extended_attributes` (String) Extended attributes
33+
- `id` (String) The ID of this resource. Contains the project ID and the credential ID.
3434
- `state` (Number) The state of the extended attributes (1 = active, 2 = inactive)

docs/resources/extended_attributes.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
page_title: "dbtcloud_extended_attributes Resource - dbtcloud"
33
subcategory: ""
44
description: |-
5-
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).
5+
Extended attributes resource
66
---
77

88
# dbtcloud_extended_attributes (Resource)
99

1010

11-
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).
11+
Extended attributes resource
1212

1313
## Example Usage
1414

@@ -51,12 +51,12 @@ resource "dbtcloud_environment" "issue_depl" {
5151

5252
### Optional
5353

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

5656
### Read-Only
5757

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

6161
## Import
6262

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package extended_attributes
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/datasource"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
)
11+
12+
var (
13+
_ datasource.DataSource = &extendedAttributesDataSource{}
14+
_ datasource.DataSourceWithConfigure = &extendedAttributesDataSource{}
15+
)
16+
17+
func ExtendedAttributesDataSource() datasource.DataSource {
18+
return &extendedAttributesDataSource{}
19+
}
20+
21+
type extendedAttributesDataSource struct {
22+
client *dbt_cloud.Client
23+
}
24+
25+
func (p *extendedAttributesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
26+
if req.ProviderData == nil {
27+
return
28+
}
29+
30+
client, ok := req.ProviderData.(*dbt_cloud.Client)
31+
if !ok {
32+
resp.Diagnostics.AddError(
33+
"Unexpected Data Source Configure Type",
34+
fmt.Sprintf("Expected *dbt_cloud.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
35+
)
36+
return
37+
}
38+
39+
p.client = client
40+
}
41+
42+
func (p *extendedAttributesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
43+
resp.TypeName = req.ProviderTypeName + "_extended_attributes"
44+
}
45+
46+
func (p *extendedAttributesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
47+
var state ExtendedAttributesDataSourceModel
48+
diags := req.Config.Get(ctx, &state)
49+
resp.Diagnostics.Append(diags...)
50+
if resp.Diagnostics.HasError() {
51+
return
52+
}
53+
54+
projectId := int(state.ProjectID.ValueInt64())
55+
extendedAttributesId := int(state.ExtendedAttributesID.ValueInt64())
56+
57+
extendedAttributes, err := p.client.GetExtendedAttributes(projectId, extendedAttributesId)
58+
if err != nil {
59+
resp.Diagnostics.AddError(
60+
"Error reading Extended attributes",
61+
"Could not read Extended attributes ID "+state.ID.ValueString()+": "+err.Error(),
62+
)
63+
return
64+
}
65+
66+
state.ID = types.StringValue(fmt.Sprintf("%d%s%d", *extendedAttributes.ID, dbt_cloud.ID_DELIMITER, *extendedAttributes.ID))
67+
state.ProjectID = types.Int64Value(int64(extendedAttributes.ProjectID))
68+
state.ExtendedAttributesID = types.Int64Value(int64(*extendedAttributes.ID))
69+
state.ExtendedAttributes = types.StringValue(string(extendedAttributes.ExtendedAttributes))
70+
state.State = types.Int64Value(int64(extendedAttributes.State))
71+
72+
diags = resp.State.Set(ctx, &state)
73+
resp.Diagnostics.Append(diags...)
74+
if resp.Diagnostics.HasError() {
75+
return
76+
}
77+
}
78+
79+
func (p *extendedAttributesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
80+
resp.Schema = dataSourceSchema
81+
}

pkg/sdkv2/data_sources/extended_attributes_acceptance_test.go renamed to pkg/framework/objects/extended_attributes/data_source_acceptance_test.go

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

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

7+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config"
78
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
89
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
910
)
@@ -76,5 +77,5 @@ func extendedAttributes() string {
7677
project_id = dbtcloud_project.test_project.id
7778
environment_id = dbtcloud_environment.test_environment.environment_id
7879
}
79-
`, DBT_CLOUD_VERSION)
80+
`, acctest_config.DBT_CLOUD_VERSION)
8081
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package extended_attributes
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
)
6+
7+
type ExtendedAttributesResourceModel struct {
8+
ID types.String `tfsdk:"id"`
9+
ExtendedAttributesID types.Int64 `tfsdk:"extended_attributes_id"`
10+
State types.Int64 `tfsdk:"state"`
11+
ProjectID types.Int64 `tfsdk:"project_id"`
12+
ExtendedAttributes types.String `tfsdk:"extended_attributes"`
13+
}
14+
15+
type ExtendedAttributesDataSourceModel struct {
16+
ID types.String `tfsdk:"id"`
17+
ExtendedAttributesID types.Int64 `tfsdk:"extended_attributes_id"`
18+
ProjectID types.Int64 `tfsdk:"project_id"`
19+
State types.Int64 `tfsdk:"state"`
20+
ExtendedAttributes types.String `tfsdk:"extended_attributes"`
21+
}

0 commit comments

Comments
 (0)