1
1
package gitlab
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"log"
6
7
8
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7
9
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8
10
gitlab "github.com/xanzy/go-gitlab"
9
11
)
10
12
11
13
func resourceGitlabServiceGithub () * schema.Resource {
12
14
return & schema.Resource {
13
- Create : resourceGitlabServiceGithubCreate ,
14
- Read : resourceGitlabServiceGithubRead ,
15
- Update : resourceGitlabServiceGithubUpdate ,
16
- Delete : resourceGitlabServiceGithubDelete ,
15
+ CreateContext : resourceGitlabServiceGithubCreate ,
16
+ ReadContext : resourceGitlabServiceGithubRead ,
17
+ UpdateContext : resourceGitlabServiceGithubUpdate ,
18
+ DeleteContext : resourceGitlabServiceGithubDelete ,
17
19
Importer : & schema.ResourceImporter {
18
- State : resourceGitlabServiceGithubImportState ,
20
+ StateContext : resourceGitlabServiceGithubImportState ,
19
21
},
20
22
21
23
Schema : map [string ]* schema.Schema {
@@ -71,7 +73,7 @@ func resourceGitlabServiceGithubSetToState(d *schema.ResourceData, service *gitl
71
73
d .Set ("active" , service .Active )
72
74
}
73
75
74
- func resourceGitlabServiceGithubCreate (d * schema.ResourceData , meta interface {}) error {
76
+ func resourceGitlabServiceGithubCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
75
77
client := meta .(* gitlab.Client )
76
78
project := d .Get ("project" ).(string )
77
79
@@ -83,21 +85,21 @@ func resourceGitlabServiceGithubCreate(d *schema.ResourceData, meta interface{})
83
85
StaticContext : gitlab .Bool (d .Get ("static_context" ).(bool )),
84
86
}
85
87
86
- _ , err := client .Services .SetGithubService (project , opts )
88
+ _ , err := client .Services .SetGithubService (project , opts , gitlab . WithContext ( ctx ) )
87
89
if err != nil {
88
- return err
90
+ return diag . FromErr ( err )
89
91
}
90
92
91
- return resourceGitlabServiceGithubRead (d , meta )
93
+ return resourceGitlabServiceGithubRead (ctx , d , meta )
92
94
}
93
95
94
- func resourceGitlabServiceGithubRead (d * schema.ResourceData , meta interface {}) error {
96
+ func resourceGitlabServiceGithubRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
95
97
client := meta .(* gitlab.Client )
96
98
project := d .Get ("project" ).(string )
97
99
98
100
log .Printf ("[DEBUG] read gitlab github service for project %s" , project )
99
101
100
- service , _ , err := client .Services .GetGithubService (project )
102
+ service , _ , err := client .Services .GetGithubService (project , gitlab . WithContext ( ctx ) )
101
103
if err != nil {
102
104
if is404 (err ) {
103
105
log .Printf ("[DEBUG] gitlab service github not found %s / %s / %s" ,
@@ -107,29 +109,33 @@ func resourceGitlabServiceGithubRead(d *schema.ResourceData, meta interface{}) e
107
109
d .SetId ("" )
108
110
return nil
109
111
}
110
- return err
112
+ return diag . FromErr ( err )
111
113
}
112
114
113
115
resourceGitlabServiceGithubSetToState (d , service )
114
116
115
117
return nil
116
118
}
117
119
118
- func resourceGitlabServiceGithubUpdate (d * schema.ResourceData , meta interface {}) error {
119
- return resourceGitlabServiceGithubCreate (d , meta )
120
+ func resourceGitlabServiceGithubUpdate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
121
+ return resourceGitlabServiceGithubCreate (ctx , d , meta )
120
122
}
121
123
122
- func resourceGitlabServiceGithubDelete (d * schema.ResourceData , meta interface {}) error {
124
+ func resourceGitlabServiceGithubDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
123
125
client := meta .(* gitlab.Client )
124
126
project := d .Get ("project" ).(string )
125
127
126
128
log .Printf ("[DEBUG] delete gitlab github service for project %s" , project )
127
129
128
- _ , err := client .Services .DeleteGithubService (project )
129
- return err
130
+ _ , err := client .Services .DeleteGithubService (project , gitlab .WithContext (ctx ))
131
+ if err != nil {
132
+ return diag .FromErr (err )
133
+ }
134
+
135
+ return nil
130
136
}
131
137
132
- func resourceGitlabServiceGithubImportState (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
138
+ func resourceGitlabServiceGithubImportState (ctx context. Context , d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
133
139
d .Set ("project" , d .Id ())
134
140
135
141
return []* schema.ResourceData {d }, nil
0 commit comments