Skip to content

Commit f345654

Browse files
authored
Merge pull request #302 from dbt-labs/feature/erro-handling-split-ids
2 parents 68a36fa + 9eef3d4 commit f345654

26 files changed

+371
-254
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

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

5-
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.17...HEAD)
5+
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.18...HEAD)
6+
7+
# [0.3.18](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.3.17...v0.3.18)
8+
9+
### Behind the scenes
10+
11+
- Add better error handling when importing resources like in [#299](https://github.com/dbt-labs/terraform-provider-dbtcloud/issues/299)
612

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

pkg/helper/split_id.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package helper
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
strings "strings"
7+
8+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
9+
)
10+
11+
func SplitIDToStrings(id string, resource_type string) (string, string, error) {
12+
parts := strings.Split(id, dbt_cloud.ID_DELIMITER)
13+
if len(parts) != 2 {
14+
15+
err := fmt.Errorf(
16+
"expected ID in the format 'id1%sid2' to import a %s, got: %s",
17+
dbt_cloud.ID_DELIMITER,
18+
resource_type,
19+
id,
20+
)
21+
return "", "", err
22+
}
23+
24+
return parts[0], parts[1], nil
25+
}
26+
27+
func SplitIDToInts(id string, resource_type string) (int, int, error) {
28+
parts := strings.Split(id, dbt_cloud.ID_DELIMITER)
29+
if len(parts) != 2 {
30+
31+
err := fmt.Errorf(
32+
"expected ID in the format 'id1%sid2' to import a %s, got: %s",
33+
dbt_cloud.ID_DELIMITER,
34+
resource_type,
35+
id,
36+
)
37+
return 0, 0, err
38+
}
39+
40+
id1, err := strconv.Atoi(parts[0])
41+
if err != nil {
42+
return 0, 0, fmt.Errorf("error converting %s to int when splitting the ID", parts[0])
43+
}
44+
45+
id2, err := strconv.Atoi(parts[1])
46+
if err != nil {
47+
return 0, 0, fmt.Errorf("error converting %s to int when splitting the ID", parts[1])
48+
}
49+
50+
return id1, id2, nil
51+
}

pkg/sdkv2/resources/bigquery_connection.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,13 @@ func resourceBigQueryConnectionRead(
312312

313313
var diags diag.Diagnostics
314314

315-
projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
316-
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
315+
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
316+
d.Id(),
317+
"dbtcloud_bigquery_connection",
318+
)
319+
if err != nil {
320+
return diag.FromErr(err)
321+
}
317322

318323
connection, err := c.GetBigQueryConnection(connectionIdString, projectIdString)
319324
if err != nil {
@@ -407,8 +412,13 @@ func resourceBigQueryConnectionUpdate(
407412
) diag.Diagnostics {
408413
c := m.(*dbt_cloud.Client)
409414

410-
projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
411-
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
415+
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
416+
d.Id(),
417+
"bigquery_connection",
418+
)
419+
if err != nil {
420+
return diag.FromErr(err)
421+
}
412422

413423
if d.HasChange("name") ||
414424
d.HasChange("type") ||

pkg/sdkv2/resources/bigquery_connection_acceptance_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"strings"
77
"testing"
88

9-
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
109
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
10+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
1111
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1212
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1313
"github.com/hashicorp/terraform-plugin-testing/terraform"
@@ -253,8 +253,10 @@ func testAccCheckDbtCloudBigQueryConnectionDestroy(s *terraform.State) error {
253253
if rs.Type != "dbtcloud_bigquery_connection" {
254254
continue
255255
}
256-
projectId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0]
257-
connectionId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1]
256+
projectId, connectionId, _ := helper.SplitIDToStrings(
257+
rs.Primary.ID,
258+
"dbtcloud_bigquery_connection",
259+
)
258260

259261
_, err := apiClient.GetConnection(connectionId, projectId)
260262
if err == nil {

pkg/sdkv2/resources/bigquery_credential.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package resources
33
import (
44
"context"
55
"fmt"
6-
"strconv"
76
"strings"
87

98
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
9+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
)
@@ -104,12 +104,10 @@ func resourceBigQueryCredentialRead(
104104
// Warning or errors can be collected in a slice type
105105
var diags diag.Diagnostics
106106

107-
projectId, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0])
108-
if err != nil {
109-
return diag.FromErr(err)
110-
}
111-
112-
BigQueryCredentialId, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1])
107+
projectId, BigQueryCredentialId, err := helper.SplitIDToInts(
108+
d.Id(),
109+
"dbtcloud_bigquery_credential",
110+
)
113111
if err != nil {
114112
return diag.FromErr(err)
115113
}
@@ -149,12 +147,10 @@ func resourceBigQueryCredentialUpdate(
149147
) diag.Diagnostics {
150148
c := m.(*dbt_cloud.Client)
151149

152-
projectId, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0])
153-
if err != nil {
154-
return diag.FromErr(err)
155-
}
156-
157-
BigQueryCredentialId, err := strconv.Atoi(strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1])
150+
projectId, BigQueryCredentialId, err := helper.SplitIDToInts(
151+
d.Id(),
152+
"dbtcloud_bigquery_credential",
153+
)
158154
if err != nil {
159155
return diag.FromErr(err)
160156
}
@@ -192,10 +188,15 @@ func resourceBigQueryCredentialDelete(
192188

193189
var diags diag.Diagnostics
194190

195-
projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
196-
BigQueryCredentialIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
191+
projectIdString, BigQueryCredentialIdString, err := helper.SplitIDToStrings(
192+
d.Id(),
193+
"dbtcloud_bigquery_credential",
194+
)
195+
if err != nil {
196+
return diag.FromErr(err)
197+
}
197198

198-
_, err := c.DeleteCredential(BigQueryCredentialIdString, projectIdString)
199+
_, err = c.DeleteCredential(BigQueryCredentialIdString, projectIdString)
199200
if err != nil {
200201
return diag.FromErr(err)
201202
}

pkg/sdkv2/resources/bigquery_credential_acceptance_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package resources_test
33
import (
44
"fmt"
55
"regexp"
6-
"strconv"
76
"strings"
87
"testing"
98

10-
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
119
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
10+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
1211
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1312
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1413
"github.com/hashicorp/terraform-plugin-testing/terraform"
@@ -73,13 +72,13 @@ func testAccCheckDbtCloudBigQueryCredentialExists(resource string) resource.Test
7372
if rs.Primary.ID == "" {
7473
return fmt.Errorf("No Record ID is set")
7574
}
76-
projectId, err := strconv.Atoi(strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0])
77-
if err != nil {
78-
return fmt.Errorf("Can't get projectId")
79-
}
80-
credentialId, err := strconv.Atoi(strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1])
75+
76+
projectId, credentialId, err := helper.SplitIDToInts(
77+
rs.Primary.ID,
78+
"dbtcloud_bigquery_credential",
79+
)
8180
if err != nil {
82-
return fmt.Errorf("Can't get credentialId")
81+
return err
8382
}
8483

8584
apiClient, err := acctest_helper.SharedClient()
@@ -104,13 +103,12 @@ func testAccCheckDbtCloudBigQueryCredentialDestroy(s *terraform.State) error {
104103
if rs.Type != "dbtcloud_bigquery_credential" {
105104
continue
106105
}
107-
projectId, err := strconv.Atoi(strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0])
108-
if err != nil {
109-
return fmt.Errorf("Can't get projectId")
110-
}
111-
credentialId, err := strconv.Atoi(strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1])
106+
projectId, credentialId, err := helper.SplitIDToInts(
107+
rs.Primary.ID,
108+
"dbtcloud_bigquery_credential",
109+
)
112110
if err != nil {
113-
return fmt.Errorf("Can't get credentialId")
111+
return err
114112
}
115113

116114
_, err = apiClient.GetBigQueryCredential(projectId, credentialId)

pkg/sdkv2/resources/connection.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,13 @@ func resourceConnectionRead(
228228

229229
var diags diag.Diagnostics
230230

231-
projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
232-
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
231+
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
232+
d.Id(),
233+
"dbtcloud_connection",
234+
)
235+
if err != nil {
236+
return diag.FromErr(err)
237+
}
233238

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

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

@@ -347,8 +351,13 @@ func resourceConnectionUpdate(
347351
) diag.Diagnostics {
348352
c := m.(*dbt_cloud.Client)
349353

350-
projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
351-
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
354+
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
355+
d.Id(),
356+
"dbtcloud_connection",
357+
)
358+
if err != nil {
359+
return diag.FromErr(err)
360+
}
352361

353362
if d.HasChange("name") ||
354363
d.HasChange("type") ||
@@ -467,10 +476,15 @@ func resourceConnectionDelete(
467476

468477
var diags diag.Diagnostics
469478

470-
projectIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[0]
471-
connectionIdString := strings.Split(d.Id(), dbt_cloud.ID_DELIMITER)[1]
479+
projectIdString, connectionIdString, err := helper.SplitIDToStrings(
480+
d.Id(),
481+
"dbtcloud_connection",
482+
)
483+
if err != nil {
484+
return diag.FromErr(err)
485+
}
472486

473-
_, err := c.DeleteConnection(connectionIdString, projectIdString)
487+
_, err = c.DeleteConnection(connectionIdString, projectIdString)
474488
if err != nil {
475489
return diag.FromErr(err)
476490
}

pkg/sdkv2/resources/connection_acceptance_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"strings"
88
"testing"
99

10-
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
1110
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
11+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/helper"
1212
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1313
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1414
"github.com/hashicorp/terraform-plugin-testing/terraform"
@@ -587,8 +587,13 @@ func testAccCheckDbtCloudConnectionExists(resource string) resource.TestCheckFun
587587
if err != nil {
588588
return fmt.Errorf("Issue getting the client")
589589
}
590-
projectId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0]
591-
connectionId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1]
590+
projectId, connectionId, err := helper.SplitIDToStrings(
591+
rs.Primary.ID,
592+
"dbtcloud_connection",
593+
)
594+
if err != nil {
595+
return err
596+
}
592597

593598
_, err = apiClient.GetConnection(connectionId, projectId)
594599
if err != nil {
@@ -608,10 +613,15 @@ func testAccCheckDbtCloudConnectionDestroy(s *terraform.State) error {
608613
if rs.Type != "dbtcloud_connection" {
609614
continue
610615
}
611-
projectId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[0]
612-
connectionId := strings.Split(rs.Primary.ID, dbt_cloud.ID_DELIMITER)[1]
616+
projectId, connectionId, err := helper.SplitIDToStrings(
617+
rs.Primary.ID,
618+
"dbtcloud_connection",
619+
)
620+
if err != nil {
621+
return err
622+
}
613623

614-
_, err := apiClient.GetConnection(connectionId, projectId)
624+
_, err = apiClient.GetConnection(connectionId, projectId)
615625
if err == nil {
616626
return fmt.Errorf("Connection still exists")
617627
}

0 commit comments

Comments
 (0)