Skip to content

Commit

Permalink
Merge pull request #302 from dbt-labs/feature/erro-handling-split-ids
Browse files Browse the repository at this point in the history
  • Loading branch information
b-per authored Sep 26, 2024
2 parents 68a36fa + 9eef3d4 commit f345654
Show file tree
Hide file tree
Showing 26 changed files with 371 additions and 254 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

All notable changes to this project will be documented in this file.

## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.17...HEAD)
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.18...HEAD)

# [0.3.18](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.17...v0.3.18)

### Behind the scenes

- Add better error handling when importing resources like in [#299](https://github.com/dbt-labs/terraform-provider-dbtcloud/issues/299)

# [0.3.17](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.16...v0.3.17)

Expand Down
51 changes: 51 additions & 0 deletions pkg/helper/split_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package helper

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

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
)

func SplitIDToStrings(id string, resource_type string) (string, string, error) {
parts := strings.Split(id, dbt_cloud.ID_DELIMITER)
if len(parts) != 2 {

err := fmt.Errorf(
"expected ID in the format 'id1%sid2' to import a %s, got: %s",
dbt_cloud.ID_DELIMITER,
resource_type,
id,
)
return "", "", err
}

return parts[0], parts[1], nil
}

func SplitIDToInts(id string, resource_type string) (int, int, error) {
parts := strings.Split(id, dbt_cloud.ID_DELIMITER)
if len(parts) != 2 {

err := fmt.Errorf(
"expected ID in the format 'id1%sid2' to import a %s, got: %s",
dbt_cloud.ID_DELIMITER,
resource_type,
id,
)
return 0, 0, err
}

id1, err := strconv.Atoi(parts[0])
if err != nil {
return 0, 0, fmt.Errorf("error converting %s to int when splitting the ID", parts[0])
}

id2, err := strconv.Atoi(parts[1])
if err != nil {
return 0, 0, fmt.Errorf("error converting %s to int when splitting the ID", parts[1])
}

return id1, id2, nil
}
18 changes: 14 additions & 4 deletions pkg/sdkv2/resources/bigquery_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,13 @@ func resourceBigQueryConnectionRead(

var diags diag.Diagnostics

projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
d.Id(),
"dbtcloud_bigquery_connection",
)
if err != nil {
return diag.FromErr(err)
}

connection, err := c.GetBigQueryConnection(connectionIdString, projectIdString)
if err != nil {
Expand Down Expand Up @@ -407,8 +412,13 @@ func resourceBigQueryConnectionUpdate(
) diag.Diagnostics {
c := m.(*dbt_cloud.Client)

projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
d.Id(),
"bigquery_connection",
)
if err != nil {
return diag.FromErr(err)
}

if d.HasChange("name") ||
d.HasChange("type") ||
Expand Down
8 changes: 5 additions & 3 deletions pkg/sdkv2/resources/bigquery_connection_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
"testing"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down Expand Up @@ -253,8 +253,10 @@ func testAccCheckDbtCloudBigQueryConnectionDestroy(s *terraform.State) error {
if rs.Type != "dbtcloud_bigquery_connection" {
continue
}
projectId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0]
connectionId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1]
projectId, connectionId, _ := helper.SplitIDToStrings(
rs.Primary.ID,
"dbtcloud_bigquery_connection",
)

_, err := apiClient.GetConnection(connectionId, projectId)
if err == nil {
Expand Down
33 changes: 17 additions & 16 deletions pkg/sdkv2/resources/bigquery_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package resources
import (
"context"
"fmt"
"strconv"
"strings"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -104,12 +104,10 @@ func resourceBigQueryCredentialRead(
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics

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

BigQueryCredentialId, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1])
projectId, BigQueryCredentialId, err := helper.SplitIDToInts(
d.Id(),
"dbtcloud_bigquery_credential",
)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -149,12 +147,10 @@ func resourceBigQueryCredentialUpdate(
) diag.Diagnostics {
c := m.(*dbt_cloud.Client)

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

BigQueryCredentialId, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1])
projectId, BigQueryCredentialId, err := helper.SplitIDToInts(
d.Id(),
"dbtcloud_bigquery_credential",
)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -192,10 +188,15 @@ func resourceBigQueryCredentialDelete(

var diags diag.Diagnostics

projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
BigQueryCredentialIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
projectIdString, BigQueryCredentialIdString, err := helper.SplitIDToStrings(
d.Id(),
"dbtcloud_bigquery_credential",
)
if err != nil {
return diag.FromErr(err)
}

_, err := c.DeleteCredential(BigQueryCredentialIdString, projectIdString)
_, err = c.DeleteCredential(BigQueryCredentialIdString, projectIdString)
if err != nil {
return diag.FromErr(err)
}
Expand Down
26 changes: 12 additions & 14 deletions pkg/sdkv2/resources/bigquery_credential_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package resources_test
import (
"fmt"
"regexp"
"strconv"
"strings"
"testing"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down Expand Up @@ -73,13 +72,13 @@ func testAccCheckDbtCloudBigQueryCredentialExists(resource string) resource.Test
if rs.Primary.ID == "" {
return fmt.Errorf("No Record ID is set")
}
projectId, err := strconv.Atoi(strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0])
if err != nil {
return fmt.Errorf("Can't get projectId")
}
credentialId, err := strconv.Atoi(strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1])

projectId, credentialId, err := helper.SplitIDToInts(
rs.Primary.ID,
"dbtcloud_bigquery_credential",
)
if err != nil {
return fmt.Errorf("Can't get credentialId")
return err
}

apiClient, err := acctest_helper.SharedClient()
Expand All @@ -104,13 +103,12 @@ func testAccCheckDbtCloudBigQueryCredentialDestroy(s *terraform.State) error {
if rs.Type != "dbtcloud_bigquery_credential" {
continue
}
projectId, err := strconv.Atoi(strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0])
if err != nil {
return fmt.Errorf("Can't get projectId")
}
credentialId, err := strconv.Atoi(strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1])
projectId, credentialId, err := helper.SplitIDToInts(
rs.Primary.ID,
"dbtcloud_bigquery_credential",
)
if err != nil {
return fmt.Errorf("Can't get credentialId")
return err
}

_, err = apiClient.GetBigQueryCredential(projectId, credentialId)
Expand Down
30 changes: 22 additions & 8 deletions pkg/sdkv2/resources/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,13 @@ func resourceConnectionRead(

var diags diag.Diagnostics

projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
d.Id(),
"dbtcloud_connection",
)
if err != nil {
return diag.FromErr(err)
}

connection, err := c.GetConnection(connectionIdString, projectIdString)
if err != nil {
Expand All @@ -240,7 +245,6 @@ func resourceConnectionRead(
return diag.FromErr(err)
}

// TODO: Remove when done better
connection.Details.OAuthClientID = d.Get("oauth_client_id").(string)
connection.Details.OAuthClientSecret = d.Get("oauth_client_secret").(string)

Expand Down Expand Up @@ -347,8 +351,13 @@ func resourceConnectionUpdate(
) diag.Diagnostics {
c := m.(*dbt_cloud.Client)

projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
d.Id(),
"dbtcloud_connection",
)
if err != nil {
return diag.FromErr(err)
}

if d.HasChange("name") ||
d.HasChange("type") ||
Expand Down Expand Up @@ -467,10 +476,15 @@ func resourceConnectionDelete(

var diags diag.Diagnostics

projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
d.Id(),
"dbtcloud_connection",
)
if err != nil {
return diag.FromErr(err)
}

_, err := c.DeleteConnection(connectionIdString, projectIdString)
_, err = c.DeleteConnection(connectionIdString, projectIdString)
if err != nil {
return diag.FromErr(err)
}
Expand Down
22 changes: 16 additions & 6 deletions pkg/sdkv2/resources/connection_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"testing"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down Expand Up @@ -587,8 +587,13 @@ func testAccCheckDbtCloudConnectionExists(resource string) resource.TestCheckFun
if err != nil {
return fmt.Errorf("Issue getting the client")
}
projectId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0]
connectionId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1]
projectId, connectionId, err := helper.SplitIDToStrings(
rs.Primary.ID,
"dbtcloud_connection",
)
if err != nil {
return err
}

_, err = apiClient.GetConnection(connectionId, projectId)
if err != nil {
Expand All @@ -608,10 +613,15 @@ func testAccCheckDbtCloudConnectionDestroy(s *terraform.State) error {
if rs.Type != "dbtcloud_connection" {
continue
}
projectId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0]
connectionId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1]
projectId, connectionId, err := helper.SplitIDToStrings(
rs.Primary.ID,
"dbtcloud_connection",
)
if err != nil {
return err
}

_, err := apiClient.GetConnection(connectionId, projectId)
_, err = apiClient.GetConnection(connectionId, projectId)
if err == nil {
return fmt.Errorf("Connection still exists")
}
Expand Down
Loading

0 comments on commit f345654

Please sign in to comment.