Skip to content

Commit e04ff96

Browse files
authored
Merge pull request #123 from DrFaust92/ctx
use ctx for data sources
2 parents e2e7f9e + 9a7422c commit e04ff96

23 files changed

+96
-77
lines changed

GNUmakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ TEST?=$$(go list ./... |grep -v 'vendor')
22
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
33
WEBSITE_REPO=github.com/hashicorp/terraform-website
44
PKG_NAME=bitbucket
5+
ACCTEST_PARALLELISM=5
56

67
default: build
78

@@ -14,7 +15,7 @@ test: fmtcheck
1415
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
1516

1617
testacc: fmtcheck
17-
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
18+
TF_ACC=1 go test $(TEST) -v -parallel $(ACCTEST_PARALLELISM) $(TESTARGS) -timeout 120m
1819

1920
vet:
2021
@echo "go vet ."

bitbucket/data_current_user.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package bitbucket
22

33
import (
4+
"context"
45
"encoding/json"
5-
"fmt"
66
"io"
77
"log"
88
"net/http"
99

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
)
1213

@@ -25,7 +26,7 @@ type UserEmail struct {
2526

2627
func dataCurrentUser() *schema.Resource {
2728
return &schema.Resource{
28-
Read: dataReadCurrentUser,
29+
ReadWithoutTimeout: dataReadCurrentUser,
2930

3031
Schema: map[string]*schema.Schema{
3132
"username": {
@@ -64,34 +65,34 @@ func dataCurrentUser() *schema.Resource {
6465
}
6566
}
6667

67-
func dataReadCurrentUser(d *schema.ResourceData, m interface{}) error {
68+
func dataReadCurrentUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
6869
c := m.(Clients).genClient
6970
httpClient := m.(Clients).httpClient
7071
usersApi := c.ApiClient.UsersApi
7172

7273
curUser, curUserRes, err := usersApi.UserGet(c.AuthContext)
7374
if err != nil {
74-
return fmt.Errorf("error reading current user: %w", err)
75+
return diag.Errorf("error reading current user: %s", err)
7576
}
7677

7778
if curUserRes.StatusCode == http.StatusNotFound {
78-
return fmt.Errorf("user not found")
79+
return diag.Errorf("user not found")
7980
}
8081

8182
if curUserRes.StatusCode >= http.StatusInternalServerError {
82-
return fmt.Errorf("internal server error fetching user")
83+
return diag.Errorf("internal server error fetching user")
8384
}
8485

8586
log.Printf("[DEBUG] Current User: %#v", curUser)
8687

8788
curUserEmails, err := httpClient.Get("2.0/user/emails")
8889
if err != nil {
89-
return err
90+
return diag.FromErr(err)
9091
}
9192

9293
emailBody, readerr := io.ReadAll(curUserEmails.Body)
9394
if readerr != nil {
94-
return readerr
95+
return diag.FromErr(readerr)
9596
}
9697

9798
log.Printf("[DEBUG] Current User Emails Response JSON: %v", string(emailBody))
@@ -100,7 +101,7 @@ func dataReadCurrentUser(d *schema.ResourceData, m interface{}) error {
100101

101102
decodeerr := json.Unmarshal(emailBody, &emails)
102103
if decodeerr != nil {
103-
return decodeerr
104+
return diag.FromErr(decodeerr)
104105
}
105106

106107
log.Printf("[DEBUG] Current User Emails Response Decoded: %#v", emails)

bitbucket/data_current_user_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
77
)
88

9-
func TestAccCurrentUser_basic(t *testing.T) {
9+
func TestAccDataSourceCurrentUser_basic(t *testing.T) {
1010
dataSourceName := "data.bitbucket_current_user.test"
1111
resource.Test(t, resource.TestCase{
1212
PreCheck: func() { testAccPreCheck(t) },

bitbucket/data_group.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package bitbucket
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
78
"log"
89

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012
)
1113

1214
func dataGroup() *schema.Resource {
1315
return &schema.Resource{
14-
Read: dataReadGroup,
16+
ReadWithoutTimeout: dataReadGroup,
1517

1618
Schema: map[string]*schema.Schema{
1719
"workspace": {
@@ -42,7 +44,7 @@ func dataGroup() *schema.Resource {
4244
}
4345
}
4446

45-
func dataReadGroup(d *schema.ResourceData, m interface{}) error {
47+
func dataReadGroup(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
4648
client := m.(Clients).httpClient
4749

4850
workspace := d.Get("workspace").(string)
@@ -51,21 +53,21 @@ func dataReadGroup(d *schema.ResourceData, m interface{}) error {
5153
groupsReq, _ := client.Get(fmt.Sprintf("1.0/groups/%s/%s", workspace, slug))
5254

5355
if groupsReq.Body == nil {
54-
return fmt.Errorf("error reading Group (%s): empty response", d.Id())
56+
return diag.Errorf("error reading Group (%s): empty response", d.Id())
5557
}
5658

5759
var grp *UserGroup
5860

5961
body, readerr := io.ReadAll(groupsReq.Body)
6062
if readerr != nil {
61-
return readerr
63+
return diag.FromErr(readerr)
6264
}
6365

6466
log.Printf("[DEBUG] Group Response JSON: %v", string(body))
6567

6668
decodeerr := json.Unmarshal(body, &grp)
6769
if decodeerr != nil {
68-
return decodeerr
70+
return diag.FromErr(decodeerr)
6971
}
7072

7173
log.Printf("[DEBUG] Group Response Decoded: %#v", grp)

bitbucket/data_group_members.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package bitbucket
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
78
"log"
89

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012
)
1113

1214
func dataGroupMembers() *schema.Resource {
1315
return &schema.Resource{
14-
Read: dataReadGroupMembers,
16+
ReadWithoutTimeout: dataReadGroupMembers,
1517

1618
Schema: map[string]*schema.Schema{
1719
"workspace": {
@@ -31,7 +33,7 @@ func dataGroupMembers() *schema.Resource {
3133
}
3234
}
3335

34-
func dataReadGroupMembers(d *schema.ResourceData, m interface{}) error {
36+
func dataReadGroupMembers(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
3537
client := m.(Clients).httpClient
3638

3739
workspace := d.Get("workspace").(string)
@@ -40,21 +42,21 @@ func dataReadGroupMembers(d *schema.ResourceData, m interface{}) error {
4042
groupsReq, _ := client.Get(fmt.Sprintf("1.0/groups/%s/%s/members", workspace, slug))
4143

4244
if groupsReq.Body == nil {
43-
return fmt.Errorf("error reading Group (%s): empty response", d.Id())
45+
return diag.Errorf("error reading Group (%s): empty response", d.Id())
4446
}
4547

4648
var members []*UserGroupMembership
4749

4850
body, readerr := io.ReadAll(groupsReq.Body)
4951
if readerr != nil {
50-
return readerr
52+
return diag.FromErr(readerr)
5153
}
5254

5355
log.Printf("[DEBUG] Group Membership Response JSON: %v", string(body))
5456

5557
decodeerr := json.Unmarshal(body, &members)
5658
if decodeerr != nil {
57-
return decodeerr
59+
return diag.FromErr(decodeerr)
5860
}
5961

6062
log.Printf("[DEBUG] Group Membership Response Decoded: %#v", members)

bitbucket/data_group_members_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010
)
1111

12-
func TestAccDataGroupMembers_basic(t *testing.T) {
12+
func TestAccDataSourceDataGroupMembers_basic(t *testing.T) {
1313
dataSourceName := "data.bitbucket_group_members.test"
1414
groupResourceName := "bitbucket_group.test"
1515

bitbucket/data_group_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010
)
1111

12-
func TestAccDataGroup_basic(t *testing.T) {
12+
func TestAccDataSourceDataGroup_basic(t *testing.T) {
1313
dataSourceName := "data.bitbucket_group.test"
1414
groupResourceName := "bitbucket_group.test"
1515

bitbucket/data_groups.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package bitbucket
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
78
"log"
89

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012
)
1113

1214
func dataGroups() *schema.Resource {
1315
return &schema.Resource{
14-
Read: dataReadGroups,
16+
ReadWithoutTimeout: dataReadGroups,
1517

1618
Schema: map[string]*schema.Schema{
1719
"workspace": {
@@ -50,29 +52,29 @@ func dataGroups() *schema.Resource {
5052
}
5153
}
5254

53-
func dataReadGroups(d *schema.ResourceData, m interface{}) error {
55+
func dataReadGroups(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
5456
client := m.(Clients).httpClient
5557

5658
workspace := d.Get("workspace").(string)
5759

5860
groupsReq, _ := client.Get(fmt.Sprintf("1.0/groups/%s", workspace))
5961

6062
if groupsReq.Body == nil {
61-
return fmt.Errorf("error reading Groups (%s): empty response", d.Id())
63+
return diag.Errorf("error reading Groups (%s): empty response", d.Id())
6264
}
6365

6466
var grps []*UserGroup
6567

6668
body, readerr := io.ReadAll(groupsReq.Body)
6769
if readerr != nil {
68-
return readerr
70+
return diag.FromErr(readerr)
6971
}
7072

7173
log.Printf("[DEBUG] Groups Response JSON: %v", string(body))
7274

7375
decodeerr := json.Unmarshal(body, &grps)
7476
if decodeerr != nil {
75-
return decodeerr
77+
return diag.FromErr(decodeerr)
7678
}
7779

7880
log.Printf("[DEBUG] Groups Response Decoded: %#v", grps)

bitbucket/data_groups_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
99
)
1010

11-
func TestAccDataGroups_basic(t *testing.T) {
11+
func TestAccDataSourceDataGroups_basic(t *testing.T) {
1212
dataSourceName := "data.bitbucket_groups.test"
1313

1414
workspace := os.Getenv("BITBUCKET_TEAM")

bitbucket/data_hook_types.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package bitbucket
22

33
import (
4-
"fmt"
4+
"context"
55
"log"
66
"net/http"
77

88
"github.com/DrFaust92/bitbucket-go-client"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
)
1112

1213
func dataHookTypes() *schema.Resource {
1314
return &schema.Resource{
14-
Read: dataReadHookTypes,
15+
ReadWithoutTimeout: dataReadHookTypes,
1516

1617
Schema: map[string]*schema.Schema{
1718
"subject_type": {
@@ -46,22 +47,22 @@ func dataHookTypes() *schema.Resource {
4647
}
4748
}
4849

49-
func dataReadHookTypes(d *schema.ResourceData, m interface{}) error {
50+
func dataReadHookTypes(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
5051
c := m.(Clients).genClient
5152
webhooksApi := c.ApiClient.WebhooksApi
5253

5354
subjectType := d.Get("subject_type").(string)
5455
hookTypes, res, err := webhooksApi.HookEventsSubjectTypeGet(c.AuthContext, subjectType)
5556
if err != nil {
56-
return err
57+
return diag.FromErr(err)
5758
}
5859

5960
if res.StatusCode == http.StatusNotFound {
60-
return fmt.Errorf("user not found")
61+
return diag.Errorf("user not found")
6162
}
6263

6364
if res.StatusCode >= http.StatusInternalServerError {
64-
return fmt.Errorf("internal server error fetching hook types")
65+
return diag.Errorf("internal server error fetching hook types")
6566
}
6667

6768
d.SetId(subjectType)

0 commit comments

Comments
 (0)