-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix - Circular reference with project/ connection/ repository (#74)
- Loading branch information
Showing
12 changed files
with
617 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.87 | ||
0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.