Skip to content

Commit b91e8e6

Browse files
authored
Fix - Circular reference with project/ connection/ repository (#74)
1 parent d8d5d5c commit b91e8e6

12 files changed

+617
-41
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ install: build
1515
mkdir -p $(HOME)/.terraform.d/plugins
1616
mv ./$(BINARY) $(HOME)/.terraform.d/plugins/$(BINARY)
1717

18-
docs:
18+
doc:
1919
go get github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
2020
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
2121

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.87
1+
0.1.0

docs/resources/dbt_cloud_project.md

-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ description: |-
2121

2222
### Optional
2323

24-
- `connection_id` (Number) Connection ID
2524
- `dbt_project_subdirectory` (String) DBT project subdirectory path
26-
- `repository_id` (Number) Repository ID
2725

2826
### Read-Only
2927

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_project_connection Resource - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_project_connection (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `connection_id` (Number) Connection ID
21+
- `project_id` (Number) Project ID
22+
23+
### Read-Only
24+
25+
- `id` (String) The ID of this resource.
26+
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_project_repository Resource - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_project_repository (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `project_id` (Number) Project ID
21+
- `repository_id` (Number) Repository ID
22+
23+
### Read-Only
24+
25+
- `id` (String) The ID of this resource.
26+
27+

pkg/dbt_cloud/project.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dbt_cloud
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
76
"net/http"
87
"strconv"
98
"strings"
@@ -49,7 +48,7 @@ func (c *Client) GetProject(projectID string) (*Project, error) {
4948
return &projectResponse.Data, nil
5049
}
5150

52-
func (c *Client) CreateProject(name string, dbtProjectSubdirectory string, connectionID int, repositoryID int) (*Project, error) {
51+
func (c *Client) CreateProject(name string, dbtProjectSubdirectory string) (*Project, error) {
5352
newProject := Project{
5453
Name: name,
5554
State: STATE_ACTIVE,
@@ -58,18 +57,11 @@ func (c *Client) CreateProject(name string, dbtProjectSubdirectory string, conne
5857
if dbtProjectSubdirectory != "" {
5958
newProject.DbtProjectSubdirectory = &dbtProjectSubdirectory
6059
}
61-
if connectionID != 0 {
62-
newProject.ConnectionID = &connectionID
63-
}
64-
if repositoryID != 0 {
65-
newProject.RepositoryID = &repositoryID
66-
}
6760

6861
newProjectData, err := json.Marshal(newProject)
6962
if err != nil {
7063
return nil, err
7164
}
72-
log.Println(string(newProjectData))
7365

7466
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v3/accounts/%s/projects/", c.HostURL, strconv.Itoa(c.AccountID)), strings.NewReader(string(newProjectData)))
7567
if err != nil {

pkg/provider/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func Provider() *schema.Provider {
4646
ResourcesMap: map[string]*schema.Resource{
4747
"dbt_cloud_job": resources.ResourceJob(),
4848
"dbt_cloud_project": resources.ResourceProject(),
49+
"dbt_cloud_project_connection": resources.ResourceProjectConnection(),
50+
"dbt_cloud_project_repository": resources.ResourceProjectRepository(),
4951
"dbt_cloud_environment": resources.ResourceEnvironment(),
5052
"dbt_cloud_environment_variable": resources.ResourceEnvironmentVariable(),
5153
"dbt_cloud_snowflake_credential": resources.ResourceSnowflakeCredential(),

pkg/resources/project.go

+2-28
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ var projectSchema = map[string]*schema.Schema{
2020
Optional: true,
2121
Description: "DBT project subdirectory path",
2222
},
23-
"connection_id": &schema.Schema{
24-
Type: schema.TypeInt,
25-
Optional: true,
26-
Description: "Connection ID",
27-
},
28-
"repository_id": &schema.Schema{
29-
Type: schema.TypeInt,
30-
Optional: true,
31-
Description: "Repository ID",
32-
},
3323
}
3424

3525
func ResourceProject() *schema.Resource {
@@ -64,12 +54,6 @@ func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interfac
6454
if err := d.Set("dbt_project_subdirectory", project.DbtProjectSubdirectory); err != nil {
6555
return diag.FromErr(err)
6656
}
67-
if err := d.Set("connection_id", project.ConnectionID); err != nil {
68-
return diag.FromErr(err)
69-
}
70-
if err := d.Set("repository_id", project.RepositoryID); err != nil {
71-
return diag.FromErr(err)
72-
}
7357

7458
return diags
7559
}
@@ -81,10 +65,8 @@ func resourceProjectCreate(ctx context.Context, d *schema.ResourceData, m interf
8165

8266
name := d.Get("name").(string)
8367
dbtProjectSubdirectory := d.Get("dbt_project_subdirectory").(string)
84-
connectionID := d.Get("connection_id").(int)
85-
repositoryID := d.Get("repository_id").(int)
8668

87-
p, err := c.CreateProject(name, dbtProjectSubdirectory, connectionID, repositoryID)
69+
p, err := c.CreateProject(name, dbtProjectSubdirectory)
8870
if err != nil {
8971
return diag.FromErr(err)
9072
}
@@ -100,7 +82,7 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interf
10082
c := m.(*dbt_cloud.Client)
10183
projectID := d.Id()
10284

103-
if d.HasChange("name") || d.HasChange("dbt_project_subdirectory") || d.HasChange("connection_id") || d.HasChange("repository_id") {
85+
if d.HasChange("name") || d.HasChange("dbt_project_subdirectory") {
10486
project, err := c.GetProject(projectID)
10587
if err != nil {
10688
return diag.FromErr(err)
@@ -114,14 +96,6 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interf
11496
dbtProjectSubdirectory := d.Get("dbt_project_subdirectory").(string)
11597
project.DbtProjectSubdirectory = &dbtProjectSubdirectory
11698
}
117-
if d.HasChange("connection_id") {
118-
connectionID := d.Get("connection_id").(int)
119-
project.ConnectionID = &connectionID
120-
}
121-
if d.HasChange("repository_id") {
122-
repositoryID := d.Get("repository_id").(int)
123-
project.RepositoryID = &repositoryID
124-
}
12599

126100
_, err = c.UpdateProject(projectID, *project)
127101
if err != nil {

pkg/resources/project_connection.go

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package resources
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/gthesheep/terraform-provider-dbt-cloud/pkg/dbt_cloud"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
)
14+
15+
var projectConnectionSchema = map[string]*schema.Schema{
16+
"connection_id": &schema.Schema{
17+
Type: schema.TypeInt,
18+
Required: true,
19+
Description: "Connection ID",
20+
},
21+
"project_id": &schema.Schema{
22+
Type: schema.TypeInt,
23+
Required: true,
24+
Description: "Project ID",
25+
},
26+
}
27+
28+
func ResourceProjectConnection() *schema.Resource {
29+
return &schema.Resource{
30+
CreateContext: resourceProjectConnectionCreate,
31+
ReadContext: resourceProjectConnectionRead,
32+
UpdateContext: resourceProjectConnectionUpdate,
33+
DeleteContext: resourceProjectConnectionDelete,
34+
35+
CustomizeDiff: customdiff.All(
36+
customdiff.ForceNewIfChange("connection_id", func(_ context.Context, old, new, meta interface{}) bool { return true }),
37+
customdiff.ForceNewIfChange("project_id", func(_ context.Context, old, new, meta interface{}) bool { return true }),
38+
),
39+
40+
Schema: projectConnectionSchema,
41+
Importer: &schema.ResourceImporter{
42+
StateContext: schema.ImportStatePassthroughContext,
43+
},
44+
}
45+
}
46+
47+
func resourceProjectConnectionCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
48+
c := m.(*dbt_cloud.Client)
49+
50+
var diags diag.Diagnostics
51+
52+
connectionID := d.Get("connection_id").(int)
53+
projectID := d.Get("project_id").(int)
54+
projectIDString := strconv.Itoa(projectID)
55+
56+
project, err := c.GetProject(projectIDString)
57+
if err != nil {
58+
return diag.FromErr(err)
59+
}
60+
61+
project.ConnectionID = &connectionID
62+
63+
_, err = c.UpdateProject(projectIDString, *project)
64+
if err != nil {
65+
return diag.FromErr(err)
66+
}
67+
68+
d.SetId(fmt.Sprintf("%d%s%d", *project.ID, dbt_cloud.ID_DELIMITER, project.ConnectionID))
69+
70+
resourceProjectConnectionRead(ctx, d, m)
71+
72+
return diags
73+
}
74+
75+
func resourceProjectConnectionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
76+
c := m.(*dbt_cloud.Client)
77+
78+
var diags diag.Diagnostics
79+
80+
projectID, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0])
81+
if err != nil {
82+
return diag.FromErr(err)
83+
}
84+
projectIDString := strconv.Itoa(projectID)
85+
86+
project, err := c.GetProject(projectIDString)
87+
if err != nil {
88+
return diag.FromErr(err)
89+
}
90+
91+
if err := d.Set("connection_id", project.ConnectionID); err != nil {
92+
return diag.FromErr(err)
93+
}
94+
if err := d.Set("project_id", project.ID); err != nil {
95+
return diag.FromErr(err)
96+
}
97+
98+
return diags
99+
}
100+
101+
func resourceProjectConnectionUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
102+
103+
return resourceProjectConnectionRead(ctx, d, m)
104+
}
105+
106+
func resourceProjectConnectionDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
107+
c := m.(*dbt_cloud.Client)
108+
109+
var diags diag.Diagnostics
110+
111+
projectID := d.Get("project_id").(int)
112+
projectIDString := strconv.Itoa(projectID)
113+
114+
project, err := c.GetProject(projectIDString)
115+
if err != nil {
116+
return diag.FromErr(err)
117+
}
118+
119+
project.ConnectionID = nil
120+
121+
_, err = c.UpdateProject(projectIDString, *project)
122+
if err != nil {
123+
return diag.FromErr(err)
124+
}
125+
126+
return diags
127+
}

0 commit comments

Comments
 (0)