Skip to content

Commit 80ae472

Browse files
committed
Use context-aware CRUD functions for Project Share Group resource
1 parent c9632cb commit 80ae472

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

gitlab/resource_gitlab_project_share_group.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package gitlab
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"strconv"
78

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
911
"github.com/xanzy/go-gitlab"
1012
)
@@ -13,9 +15,9 @@ func resourceGitlabProjectShareGroup() *schema.Resource {
1315
acceptedAccessLevels := []string{"guest", "reporter", "developer", "maintainer"}
1416

1517
return &schema.Resource{
16-
Create: resourceGitlabProjectShareGroupCreate,
17-
Read: resourceGitlabProjectShareGroupRead,
18-
Delete: resourceGitlabProjectShareGroupDelete,
18+
CreateContext: resourceGitlabProjectShareGroupCreate,
19+
ReadContext: resourceGitlabProjectShareGroupRead,
20+
DeleteContext: resourceGitlabProjectShareGroupDelete,
1921
Importer: &schema.ResourceImporter{
2022
StateContext: schema.ImportStatePassthroughContext,
2123
},
@@ -41,7 +43,7 @@ func resourceGitlabProjectShareGroup() *schema.Resource {
4143
}
4244
}
4345

44-
func resourceGitlabProjectShareGroupCreate(d *schema.ResourceData, meta interface{}) error {
46+
func resourceGitlabProjectShareGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
4547
client := meta.(*gitlab.Client)
4648

4749
groupId := d.Get("group_id").(int)
@@ -54,33 +56,33 @@ func resourceGitlabProjectShareGroupCreate(d *schema.ResourceData, meta interfac
5456
}
5557
log.Printf("[DEBUG] create gitlab project membership for %d in %s", options.GroupID, projectId)
5658

57-
_, err := client.Projects.ShareProjectWithGroup(projectId, options)
59+
_, err := client.Projects.ShareProjectWithGroup(projectId, options, gitlab.WithContext(ctx))
5860
if err != nil {
59-
return err
61+
return diag.FromErr(err)
6062
}
6163
groupIdString := strconv.Itoa(groupId)
6264
d.SetId(buildTwoPartID(&projectId, &groupIdString))
63-
return resourceGitlabProjectShareGroupRead(d, meta)
65+
return resourceGitlabProjectShareGroupRead(ctx, d, meta)
6466
}
6567

66-
func resourceGitlabProjectShareGroupRead(d *schema.ResourceData, meta interface{}) error {
68+
func resourceGitlabProjectShareGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
6769
client := meta.(*gitlab.Client)
6870
id := d.Id()
6971
log.Printf("[DEBUG] read gitlab project projectMember %s", id)
7072

7173
projectId, groupId, err := projectIdAndGroupIdFromId(id)
7274
if err != nil {
73-
return err
75+
return diag.FromErr(err)
7476
}
7577

76-
projectInformation, _, err := client.Projects.GetProject(projectId, nil)
78+
projectInformation, _, err := client.Projects.GetProject(projectId, nil, gitlab.WithContext(ctx))
7779
if err != nil {
7880
if is404(err) {
7981
log.Printf("[DEBUG] failed to read gitlab project %s: %s", id, err)
8082
d.SetId("")
8183
return nil
8284
}
83-
return err
85+
return diag.FromErr(err)
8486
}
8587

8688
for _, v := range projectInformation.SharedWithGroups {
@@ -106,19 +108,23 @@ func projectIdAndGroupIdFromId(id string) (string, int, error) {
106108
return projectId, groupId, nil
107109
}
108110

109-
func resourceGitlabProjectShareGroupDelete(d *schema.ResourceData, meta interface{}) error {
111+
func resourceGitlabProjectShareGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
110112
client := meta.(*gitlab.Client)
111113

112114
id := d.Id()
113115
projectId, groupId, err := projectIdAndGroupIdFromId(id)
114116
if err != nil {
115-
return err
117+
return diag.FromErr(err)
116118
}
117119

118120
log.Printf("[DEBUG] Delete gitlab project membership %v for %s", groupId, projectId)
119121

120-
_, err = client.Projects.DeleteSharedProjectFromGroup(projectId, groupId)
121-
return err
122+
_, err = client.Projects.DeleteSharedProjectFromGroup(projectId, groupId, gitlab.WithContext(ctx))
123+
if err != nil {
124+
return diag.FromErr(err)
125+
}
126+
127+
return nil
122128
}
123129

124130
func resourceGitlabProjectShareGroupSetToState(d *schema.ResourceData, group struct {

0 commit comments

Comments
 (0)