1
1
package gitlab
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"log"
6
7
"strconv"
7
8
9
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9
11
"github.com/xanzy/go-gitlab"
10
12
)
@@ -13,9 +15,9 @@ func resourceGitlabProjectShareGroup() *schema.Resource {
13
15
acceptedAccessLevels := []string {"guest" , "reporter" , "developer" , "maintainer" }
14
16
15
17
return & schema.Resource {
16
- Create : resourceGitlabProjectShareGroupCreate ,
17
- Read : resourceGitlabProjectShareGroupRead ,
18
- Delete : resourceGitlabProjectShareGroupDelete ,
18
+ CreateContext : resourceGitlabProjectShareGroupCreate ,
19
+ ReadContext : resourceGitlabProjectShareGroupRead ,
20
+ DeleteContext : resourceGitlabProjectShareGroupDelete ,
19
21
Importer : & schema.ResourceImporter {
20
22
StateContext : schema .ImportStatePassthroughContext ,
21
23
},
@@ -41,7 +43,7 @@ func resourceGitlabProjectShareGroup() *schema.Resource {
41
43
}
42
44
}
43
45
44
- func resourceGitlabProjectShareGroupCreate (d * schema.ResourceData , meta interface {}) error {
46
+ func resourceGitlabProjectShareGroupCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
45
47
client := meta .(* gitlab.Client )
46
48
47
49
groupId := d .Get ("group_id" ).(int )
@@ -54,33 +56,33 @@ func resourceGitlabProjectShareGroupCreate(d *schema.ResourceData, meta interfac
54
56
}
55
57
log .Printf ("[DEBUG] create gitlab project membership for %d in %s" , options .GroupID , projectId )
56
58
57
- _ , err := client .Projects .ShareProjectWithGroup (projectId , options )
59
+ _ , err := client .Projects .ShareProjectWithGroup (projectId , options , gitlab . WithContext ( ctx ) )
58
60
if err != nil {
59
- return err
61
+ return diag . FromErr ( err )
60
62
}
61
63
groupIdString := strconv .Itoa (groupId )
62
64
d .SetId (buildTwoPartID (& projectId , & groupIdString ))
63
- return resourceGitlabProjectShareGroupRead (d , meta )
65
+ return resourceGitlabProjectShareGroupRead (ctx , d , meta )
64
66
}
65
67
66
- func resourceGitlabProjectShareGroupRead (d * schema.ResourceData , meta interface {}) error {
68
+ func resourceGitlabProjectShareGroupRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
67
69
client := meta .(* gitlab.Client )
68
70
id := d .Id ()
69
71
log .Printf ("[DEBUG] read gitlab project projectMember %s" , id )
70
72
71
73
projectId , groupId , err := projectIdAndGroupIdFromId (id )
72
74
if err != nil {
73
- return err
75
+ return diag . FromErr ( err )
74
76
}
75
77
76
- projectInformation , _ , err := client .Projects .GetProject (projectId , nil )
78
+ projectInformation , _ , err := client .Projects .GetProject (projectId , nil , gitlab . WithContext ( ctx ) )
77
79
if err != nil {
78
80
if is404 (err ) {
79
81
log .Printf ("[DEBUG] failed to read gitlab project %s: %s" , id , err )
80
82
d .SetId ("" )
81
83
return nil
82
84
}
83
- return err
85
+ return diag . FromErr ( err )
84
86
}
85
87
86
88
for _ , v := range projectInformation .SharedWithGroups {
@@ -106,19 +108,23 @@ func projectIdAndGroupIdFromId(id string) (string, int, error) {
106
108
return projectId , groupId , nil
107
109
}
108
110
109
- func resourceGitlabProjectShareGroupDelete (d * schema.ResourceData , meta interface {}) error {
111
+ func resourceGitlabProjectShareGroupDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
110
112
client := meta .(* gitlab.Client )
111
113
112
114
id := d .Id ()
113
115
projectId , groupId , err := projectIdAndGroupIdFromId (id )
114
116
if err != nil {
115
- return err
117
+ return diag . FromErr ( err )
116
118
}
117
119
118
120
log .Printf ("[DEBUG] Delete gitlab project membership %v for %s" , groupId , projectId )
119
121
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
122
128
}
123
129
124
130
func resourceGitlabProjectShareGroupSetToState (d * schema.ResourceData , group struct {
0 commit comments