Skip to content

Commit

Permalink
Fix - Circular reference with project/ connection/ repository (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
GtheSheep authored Aug 13, 2022
1 parent d8d5d5c commit b91e8e6
Show file tree
Hide file tree
Showing 12 changed files with 617 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ install: build
mkdir -p $(HOME)/.terraform.d/plugins
mv ./$(BINARY) $(HOME)/.terraform.d/plugins/$(BINARY)

docs:
doc:
go get github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.87
0.1.0
2 changes: 0 additions & 2 deletions docs/resources/dbt_cloud_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ description: |-

### Optional

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

### Read-Only

Expand Down
27 changes: 27 additions & 0 deletions docs/resources/dbt_cloud_project_connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "dbt_cloud_project_connection Resource - terraform-provider-dbt-cloud"
subcategory: ""
description: |-
---

# dbt_cloud_project_connection (Resource)





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

### Required

- `connection_id` (Number) Connection ID
- `project_id` (Number) Project ID

### Read-Only

- `id` (String) The ID of this resource.


27 changes: 27 additions & 0 deletions docs/resources/dbt_cloud_project_repository.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "dbt_cloud_project_repository Resource - terraform-provider-dbt-cloud"
subcategory: ""
description: |-
---

# dbt_cloud_project_repository (Resource)





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

### Required

- `project_id` (Number) Project ID
- `repository_id` (Number) Repository ID

### Read-Only

- `id` (String) The ID of this resource.


10 changes: 1 addition & 9 deletions pkg/dbt_cloud/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dbt_cloud
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -49,7 +48,7 @@ func (c *Client) GetProject(projectID string) (*Project, error) {
return &projectResponse.Data, nil
}

func (c *Client) CreateProject(name string, dbtProjectSubdirectory string, connectionID int, repositoryID int) (*Project, error) {
func (c *Client) CreateProject(name string, dbtProjectSubdirectory string) (*Project, error) {
newProject := Project{
Name: name,
State: STATE_ACTIVE,
Expand All @@ -58,18 +57,11 @@ func (c *Client) CreateProject(name string, dbtProjectSubdirectory string, conne
if dbtProjectSubdirectory != "" {
newProject.DbtProjectSubdirectory = &dbtProjectSubdirectory
}
if connectionID != 0 {
newProject.ConnectionID = &connectionID
}
if repositoryID != 0 {
newProject.RepositoryID = &repositoryID
}

newProjectData, err := json.Marshal(newProject)
if err != nil {
return nil, err
}
log.Println(string(newProjectData))

req, err := http.NewRequest("POST", fmt.Sprintf("%s/v3/accounts/%s/projects/", c.HostURL, strconv.Itoa(c.AccountID)), strings.NewReader(string(newProjectData)))
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"dbt_cloud_job": resources.ResourceJob(),
"dbt_cloud_project": resources.ResourceProject(),
"dbt_cloud_project_connection": resources.ResourceProjectConnection(),
"dbt_cloud_project_repository": resources.ResourceProjectRepository(),
"dbt_cloud_environment": resources.ResourceEnvironment(),
"dbt_cloud_environment_variable": resources.ResourceEnvironmentVariable(),
"dbt_cloud_snowflake_credential": resources.ResourceSnowflakeCredential(),
Expand Down
30 changes: 2 additions & 28 deletions pkg/resources/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ var projectSchema = map[string]*schema.Schema{
Optional: true,
Description: "DBT project subdirectory path",
},
"connection_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "Connection ID",
},
"repository_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "Repository ID",
},
}

func ResourceProject() *schema.Resource {
Expand Down Expand Up @@ -64,12 +54,6 @@ func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interfac
if err := d.Set("dbt_project_subdirectory", project.DbtProjectSubdirectory); err != nil {
return diag.FromErr(err)
}
if err := d.Set("connection_id", project.ConnectionID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("repository_id", project.RepositoryID); err != nil {
return diag.FromErr(err)
}

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

name := d.Get("name").(string)
dbtProjectSubdirectory := d.Get("dbt_project_subdirectory").(string)
connectionID := d.Get("connection_id").(int)
repositoryID := d.Get("repository_id").(int)

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

if d.HasChange("name") || d.HasChange("dbt_project_subdirectory") || d.HasChange("connection_id") || d.HasChange("repository_id") {
if d.HasChange("name") || d.HasChange("dbt_project_subdirectory") {
project, err := c.GetProject(projectID)
if err != nil {
return diag.FromErr(err)
Expand All @@ -114,14 +96,6 @@ func resourceProjectUpdate(ctx context.Context, d *schema.ResourceData, m interf
dbtProjectSubdirectory := d.Get("dbt_project_subdirectory").(string)
project.DbtProjectSubdirectory = &dbtProjectSubdirectory
}
if d.HasChange("connection_id") {
connectionID := d.Get("connection_id").(int)
project.ConnectionID = &connectionID
}
if d.HasChange("repository_id") {
repositoryID := d.Get("repository_id").(int)
project.RepositoryID = &repositoryID
}

_, err = c.UpdateProject(projectID, *project)
if err != nil {
Expand Down
127 changes: 127 additions & 0 deletions pkg/resources/project_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package resources

import (
"context"
"fmt"
"strconv"
"strings"

"github.com/gthesheep/terraform-provider-dbt-cloud/pkg/dbt_cloud"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var projectConnectionSchema = map[string]*schema.Schema{
"connection_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "Connection ID",
},
"project_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "Project ID",
},
}

func ResourceProjectConnection() *schema.Resource {
return &schema.Resource{
CreateContext: resourceProjectConnectionCreate,
ReadContext: resourceProjectConnectionRead,
UpdateContext: resourceProjectConnectionUpdate,
DeleteContext: resourceProjectConnectionDelete,

CustomizeDiff: customdiff.All(
customdiff.ForceNewIfChange("connection_id", func(_ context.Context, old, new, meta interface{}) bool { return true }),
customdiff.ForceNewIfChange("project_id", func(_ context.Context, old, new, meta interface{}) bool { return true }),
),

Schema: projectConnectionSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

func resourceProjectConnectionCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*dbt_cloud.Client)

var diags diag.Diagnostics

connectionID := d.Get("connection_id").(int)
projectID := d.Get("project_id").(int)
projectIDString := strconv.Itoa(projectID)

project, err := c.GetProject(projectIDString)
if err != nil {
return diag.FromErr(err)
}

project.ConnectionID = &connectionID

_, err = c.UpdateProject(projectIDString, *project)
if err != nil {
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf("%d%s%d", *project.ID, dbt_cloud.ID_DELIMITER, project.ConnectionID))

resourceProjectConnectionRead(ctx, d, m)

return diags
}

func resourceProjectConnectionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*dbt_cloud.Client)

var diags diag.Diagnostics

projectID, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0])
if err != nil {
return diag.FromErr(err)
}
projectIDString := strconv.Itoa(projectID)

project, err := c.GetProject(projectIDString)
if err != nil {
return diag.FromErr(err)
}

if err := d.Set("connection_id", project.ConnectionID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("project_id", project.ID); err != nil {
return diag.FromErr(err)
}

return diags
}

func resourceProjectConnectionUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

return resourceProjectConnectionRead(ctx, d, m)
}

func resourceProjectConnectionDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*dbt_cloud.Client)

var diags diag.Diagnostics

projectID := d.Get("project_id").(int)
projectIDString := strconv.Itoa(projectID)

project, err := c.GetProject(projectIDString)
if err != nil {
return diag.FromErr(err)
}

project.ConnectionID = nil

_, err = c.UpdateProject(projectIDString, *project)
if err != nil {
return diag.FromErr(err)
}

return diags
}
Loading

0 comments on commit b91e8e6

Please sign in to comment.