Skip to content

Commit be055fd

Browse files
committed
resource/service_slack: use pass through importer and start alignment with upstream API. Refs #790
This is a start towards correctly implementing the Slack Notification Integration Service. When I started I wasn't aware of the problems in the upstream GitLab API and the misalignment between `go-gitlab` and the upstream GitLab API (maybe by intention because the upstream API is broken). Long story short, there is a GitLab upstream issue here: * https://gitlab.com/gitlab-org/gitlab/-/issues/28903 a go-gitlab issue here: * xanzy/go-gitlab#1354 And we have one here: * https://github.com/gitlabhq/terraform-provider-gitlab/issues/790 Nevertheless, I think we should merge this change set. It uses the pass through importer for the resource and also cleans up a few things. In addition I've aligned the resource schema with the upstream API and commented out the missing parts with matching comments and links to the issues. I'd say we continue work on this once both GitLab and go-gitlab APIs are fixed / aligned.
1 parent a9713fa commit be055fd

File tree

3 files changed

+151
-102
lines changed

3 files changed

+151
-102
lines changed

docs/resources/service_slack.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ resource "gitlab_service_slack" "slack" {
5050
- **note_channel** (String) The name of the channel to receive note events notifications.
5151
- **note_events** (Boolean) Enable notifications for note events.
5252
- **notify_only_broken_pipelines** (Boolean) Send notifications for broken pipelines.
53-
- **notify_only_default_branch** (Boolean, Deprecated) DEPRECATED: This parameter has been replaced with `branches_to_be_notified`.
53+
- **notify_only_default_branch** (Boolean, Deprecated) This parameter has been replaced with `branches_to_be_notified`.
5454
- **pipeline_channel** (String) The name of the channel to receive pipeline events notifications.
5555
- **pipeline_events** (Boolean) Enable notifications for pipeline events.
5656
- **push_channel** (String) The name of the channel to receive push events notifications.
@@ -63,7 +63,7 @@ resource "gitlab_service_slack" "slack" {
6363

6464
### Read-Only
6565

66-
- **job_events** (Boolean) Enable notifications for job events.
66+
- **job_events** (Boolean) Enable notifications for job events. **ATTENTION**: This attribute is currently not being submitted to the GitLab API, due to https://github.com/xanzy/go-gitlab/issues/1354.
6767

6868
## Import
6969

gitlab/resource_gitlab_service_slack.go

Lines changed: 134 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gitlab
22

33
import (
44
"context"
5-
"fmt"
65
"log"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -19,7 +18,7 @@ func resourceGitlabServiceSlack() *schema.Resource {
1918
UpdateContext: resourceGitlabServiceSlackUpdate,
2019
DeleteContext: resourceGitlabServiceSlackDelete,
2120
Importer: &schema.ResourceImporter{
22-
StateContext: resourceGitlabServiceSlackImportState,
21+
StateContext: schema.ImportStatePassthroughContext,
2322
},
2423

2524
Schema: map[string]*schema.Schema{
@@ -46,7 +45,7 @@ func resourceGitlabServiceSlack() *schema.Resource {
4645
Computed: true,
4746
},
4847
"notify_only_default_branch": {
49-
Description: "DEPRECATED: This parameter has been replaced with `branches_to_be_notified`.",
48+
Description: "This parameter has been replaced with `branches_to_be_notified`.",
5049
Type: schema.TypeBool,
5150
Optional: true,
5251
Computed: true,
@@ -58,58 +57,84 @@ func resourceGitlabServiceSlack() *schema.Resource {
5857
Optional: true,
5958
Computed: true,
6059
},
61-
"push_events": {
62-
Description: "Enable notifications for push events.",
63-
Type: schema.TypeBool,
60+
// TODO: Currently, go-gitlab doesn't implement this option yet.
61+
// see https://github.com/xanzy/go-gitlab/issues/1354
62+
// "commit_events": {
63+
// Description: "Enable notifications for commit events.",
64+
// Type: schema.TypeBool,
65+
// Optional: true,
66+
// Computed: true,
67+
// },
68+
"confidential_issue_channel": {
69+
Description: "The name of the channel to receive confidential issue events notifications.",
70+
Type: schema.TypeString,
6471
Optional: true,
65-
Computed: true,
6672
},
67-
"push_channel": {
68-
Description: "The name of the channel to receive push events notifications.",
69-
Type: schema.TypeString,
73+
"confidential_issues_events": {
74+
Description: "Enable notifications for confidential issues events.",
75+
Type: schema.TypeBool,
7076
Optional: true,
77+
Computed: true,
7178
},
72-
"issues_events": {
73-
Description: "Enable notifications for issues events.",
79+
// TODO: Currently, GitLab ignores this option (not implemented yet?), so
80+
// there is no way to set it. Uncomment when this is fixed.
81+
// See: https://gitlab.com/gitlab-org/gitlab-ce/issues/49730
82+
// "confidential_note_channel": {
83+
// Description: "The name of the channel to receive confidential note events notifications.",
84+
// Type: schema.TypeString,
85+
// Optional: true,
86+
// },
87+
"confidential_note_events": {
88+
Description: "Enable notifications for confidential note events.",
7489
Type: schema.TypeBool,
7590
Optional: true,
7691
Computed: true,
7792
},
93+
// TODO: Currently, GitLab doesn't correctly implement the API, so this is
94+
// impossible to implement here at the moment.
95+
// see https://gitlab.com/gitlab-org/gitlab/-/issues/28903
96+
// "deployment_channel": {
97+
// Description: "The name of the channel to receive deployment events notifications.",
98+
// Type: schema.TypeString,
99+
// Optional: true,
100+
// },
101+
// "deployment_events": {
102+
// Description: "Enable notifications for deployment events.",
103+
// Type: schema.TypeBool,
104+
// Optional: true,
105+
// Computed: true,
106+
// },
78107
"issue_channel": {
79108
Description: "The name of the channel to receive issue events notifications.",
80109
Type: schema.TypeString,
81110
Optional: true,
82111
},
83-
"confidential_issues_events": {
84-
Description: "Enable notifications for confidential issues events.",
112+
"issues_events": {
113+
Description: "Enable notifications for issues events.",
85114
Type: schema.TypeBool,
86115
Optional: true,
87116
Computed: true,
88117
},
89-
"confidential_issue_channel": {
90-
Description: "The name of the channel to receive confidential issue events notifications.",
91-
Type: schema.TypeString,
92-
Optional: true,
93-
},
94-
"merge_requests_events": {
95-
Description: "Enable notifications for merge requests events.",
118+
// TODO: Currently, go-gitlab doesn't implement this option yet.
119+
// see https://github.com/xanzy/go-gitlab/issues/1354
120+
"job_events": {
121+
Description: "Enable notifications for job events. **ATTENTION**: This attribute is currently not being submitted to the GitLab API, due to https://github.com/xanzy/go-gitlab/issues/1354.",
96122
Type: schema.TypeBool,
97-
Optional: true,
98123
Computed: true,
99124
},
100125
"merge_request_channel": {
101126
Description: "The name of the channel to receive merge request events notifications.",
102127
Type: schema.TypeString,
103128
Optional: true,
104129
},
105-
"tag_push_events": {
106-
Description: "Enable notifications for tag push events.",
130+
"merge_requests_events": {
131+
Description: "Enable notifications for merge requests events.",
107132
Type: schema.TypeBool,
108133
Optional: true,
109134
Computed: true,
110135
},
111-
"tag_push_channel": {
112-
Description: "The name of the channel to receive tag push events notifications.",
136+
"note_channel": {
137+
Description: "The name of the channel to receive note events notifications.",
113138
Type: schema.TypeString,
114139
Optional: true,
115140
},
@@ -119,38 +144,35 @@ func resourceGitlabServiceSlack() *schema.Resource {
119144
Optional: true,
120145
Computed: true,
121146
},
122-
"note_channel": {
123-
Description: "The name of the channel to receive note events notifications.",
147+
"pipeline_channel": {
148+
Description: "The name of the channel to receive pipeline events notifications.",
124149
Type: schema.TypeString,
125150
Optional: true,
126151
},
127-
"confidential_note_events": {
128-
Description: "Enable notifications for confidential note events.",
152+
"pipeline_events": {
153+
Description: "Enable notifications for pipeline events.",
129154
Type: schema.TypeBool,
130155
Optional: true,
131156
Computed: true,
132157
},
133-
// TODO: Currently, GitLab ignores this option (not implemented yet?), so
134-
// there is no way to set it. Uncomment when this is fixed.
135-
// See: https://gitlab.com/gitlab-org/gitlab-ce/issues/49730
136-
//"confidential_note_channel": {
137-
// Type: schema.TypeString,
138-
// Optional: true,
139-
// Computed: true,
140-
//},
141-
"pipeline_events": {
142-
Description: "Enable notifications for pipeline events.",
158+
"push_channel": {
159+
Description: "The name of the channel to receive push events notifications.",
160+
Type: schema.TypeString,
161+
Optional: true,
162+
},
163+
"push_events": {
164+
Description: "Enable notifications for push events.",
143165
Type: schema.TypeBool,
144166
Optional: true,
145167
Computed: true,
146168
},
147-
"pipeline_channel": {
148-
Description: "The name of the channel to receive pipeline events notifications.",
169+
"tag_push_channel": {
170+
Description: "The name of the channel to receive tag push events notifications.",
149171
Type: schema.TypeString,
150172
Optional: true,
151173
},
152-
"wiki_page_events": {
153-
Description: "Enable notifications for wiki page events.",
174+
"tag_push_events": {
175+
Description: "Enable notifications for tag push events.",
154176
Type: schema.TypeBool,
155177
Optional: true,
156178
Computed: true,
@@ -160,49 +182,20 @@ func resourceGitlabServiceSlack() *schema.Resource {
160182
Type: schema.TypeString,
161183
Optional: true,
162184
},
163-
"job_events": {
164-
Description: "Enable notifications for job events.",
185+
"wiki_page_events": {
186+
Description: "Enable notifications for wiki page events.",
165187
Type: schema.TypeBool,
188+
Optional: true,
166189
Computed: true,
167190
},
168191
},
169192
}
170193
}
171194

172-
func resourceGitlabServiceSlackSetToState(d *schema.ResourceData, service *gitlab.SlackService) error {
173-
d.SetId(fmt.Sprintf("%d", service.ID))
174-
d.Set("webhook", service.Properties.WebHook)
175-
d.Set("username", service.Properties.Username)
176-
d.Set("notify_only_broken_pipelines", bool(service.Properties.NotifyOnlyBrokenPipelines))
177-
d.Set("notify_only_default_branch", bool(service.Properties.NotifyOnlyDefaultBranch))
178-
d.Set("branches_to_be_notified", service.Properties.BranchesToBeNotified)
179-
d.Set("push_events", service.PushEvents)
180-
d.Set("push_channel", service.Properties.PushChannel)
181-
d.Set("issues_events", service.IssuesEvents)
182-
d.Set("issue_channel", service.Properties.IssueChannel)
183-
d.Set("confidential_issues_events", service.ConfidentialIssuesEvents)
184-
d.Set("confidential_issue_channel", service.Properties.ConfidentialIssueChannel)
185-
d.Set("merge_requests_events", service.MergeRequestsEvents)
186-
d.Set("merge_request_channel", service.Properties.MergeRequestChannel)
187-
d.Set("tag_push_events", service.TagPushEvents)
188-
d.Set("tag_push_channel", service.Properties.TagPushChannel)
189-
d.Set("note_events", service.NoteEvents)
190-
d.Set("note_channel", service.Properties.NoteChannel)
191-
d.Set("confidential_note_events", service.ConfidentialNoteEvents)
192-
// See comment to "confidential_note_channel" in resourceGitlabServiceSlack()
193-
//d.Set("confidential_note_channel", service.Properties.ConfidentialNoteChannel)
194-
d.Set("pipeline_events", service.PipelineEvents)
195-
d.Set("pipeline_channel", service.Properties.PipelineChannel)
196-
d.Set("wiki_page_events", service.WikiPageEvents)
197-
d.Set("wiki_page_channel", service.Properties.WikiPageChannel)
198-
d.Set("job_events", service.JobEvents)
199-
200-
return nil
201-
}
202-
203195
func resourceGitlabServiceSlackCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
204196
client := meta.(*gitlab.Client)
205197
project := d.Get("project").(string)
198+
d.SetId(project)
206199

207200
log.Printf("[DEBUG] create gitlab slack service for project %s", project)
208201

@@ -214,23 +207,38 @@ func resourceGitlabServiceSlackCreate(ctx context.Context, d *schema.ResourceDat
214207
opts.NotifyOnlyBrokenPipelines = gitlab.Bool(d.Get("notify_only_broken_pipelines").(bool))
215208
opts.NotifyOnlyDefaultBranch = gitlab.Bool(d.Get("notify_only_default_branch").(bool))
216209
opts.BranchesToBeNotified = gitlab.String(d.Get("branches_to_be_notified").(string))
217-
opts.PushEvents = gitlab.Bool(d.Get("push_events").(bool))
218-
opts.PushChannel = gitlab.String(d.Get("push_channel").(string))
219-
opts.IssuesEvents = gitlab.Bool(d.Get("issues_events").(bool))
220-
opts.IssueChannel = gitlab.String(d.Get("issue_channel").(string))
221-
opts.ConfidentialIssuesEvents = gitlab.Bool(d.Get("confidential_issues_events").(bool))
210+
// TODO: Currently, go-gitlab doesn't implement this option yet.
211+
// see https://github.com/xanzy/go-gitlab/issues/1354
212+
// opts.CommitEvents = gitlab.Bool(d.Get("commit_events").(bool))
222213
opts.ConfidentialIssueChannel = gitlab.String(d.Get("confidential_issue_channel").(string))
223-
opts.MergeRequestsEvents = gitlab.Bool(d.Get("merge_requests_events").(bool))
214+
opts.ConfidentialIssuesEvents = gitlab.Bool(d.Get("confidential_issues_events").(bool))
215+
// TODO: Currently, GitLab ignores this option (not implemented yet?), so
216+
// there is no way to set it. Uncomment when this is fixed.
217+
// See: https://gitlab.com/gitlab-org/gitlab-ce/issues/49730
218+
// opts.ConfidentialNoteChannel = gitlab.String(d.Get("confidential_note_channel").(string))
219+
opts.ConfidentialNoteEvents = gitlab.Bool(d.Get("confidential_note_events").(bool))
220+
// TODO: Currently, GitLab doesn't correctly implement the API, so this is
221+
// impossible to implement here at the moment.
222+
// see https://gitlab.com/gitlab-org/gitlab/-/issues/28903
223+
// opts.DeploymentChannel = gitlab.String(d.Get("deployment_channel").(string))
224+
// opts.DeploymentEvents = gitlab.Bool(d.Get("deployment_events").(bool))
225+
opts.IssueChannel = gitlab.String(d.Get("issue_channel").(string))
226+
opts.IssuesEvents = gitlab.Bool(d.Get("issues_events").(bool))
227+
// TODO: Currently, go-gitlab doesn't implement this option yet.
228+
// see https://github.com/xanzy/go-gitlab/issues/1354
229+
// opts.JobEvents = gitlab.Bool(d.Get("job_events").(bool))
224230
opts.MergeRequestChannel = gitlab.String(d.Get("merge_request_channel").(string))
225-
opts.TagPushEvents = gitlab.Bool(d.Get("tag_push_events").(bool))
226-
opts.TagPushChannel = gitlab.String(d.Get("tag_push_channel").(string))
227-
opts.NoteEvents = gitlab.Bool(d.Get("note_events").(bool))
231+
opts.MergeRequestsEvents = gitlab.Bool(d.Get("merge_requests_events").(bool))
228232
opts.NoteChannel = gitlab.String(d.Get("note_channel").(string))
229-
opts.ConfidentialNoteEvents = gitlab.Bool(d.Get("confidential_note_events").(bool))
230-
opts.PipelineEvents = gitlab.Bool(d.Get("pipeline_events").(bool))
233+
opts.NoteEvents = gitlab.Bool(d.Get("note_events").(bool))
231234
opts.PipelineChannel = gitlab.String(d.Get("pipeline_channel").(string))
232-
opts.WikiPageEvents = gitlab.Bool(d.Get("wiki_page_events").(bool))
235+
opts.PipelineEvents = gitlab.Bool(d.Get("pipeline_events").(bool))
236+
opts.PushChannel = gitlab.String(d.Get("push_channel").(string))
237+
opts.PushEvents = gitlab.Bool(d.Get("push_events").(bool))
238+
opts.TagPushChannel = gitlab.String(d.Get("tag_push_channel").(string))
239+
opts.TagPushEvents = gitlab.Bool(d.Get("tag_push_events").(bool))
233240
opts.WikiPageChannel = gitlab.String(d.Get("wiki_page_channel").(string))
241+
opts.WikiPageEvents = gitlab.Bool(d.Get("wiki_page_events").(bool))
234242

235243
_, err := client.Services.SetSlackService(project, opts, gitlab.WithContext(ctx))
236244
if err != nil {
@@ -242,7 +250,7 @@ func resourceGitlabServiceSlackCreate(ctx context.Context, d *schema.ResourceDat
242250

243251
func resourceGitlabServiceSlackRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
244252
client := meta.(*gitlab.Client)
245-
project := d.Get("project").(string)
253+
project := d.Id()
246254

247255
log.Printf("[DEBUG] read gitlab slack service for project %s", project)
248256

@@ -256,9 +264,41 @@ func resourceGitlabServiceSlackRead(ctx context.Context, d *schema.ResourceData,
256264
return diag.FromErr(err)
257265
}
258266

259-
if err = resourceGitlabServiceSlackSetToState(d, service); err != nil {
260-
return diag.FromErr(err)
261-
}
267+
d.Set("project", project)
268+
d.Set("webhook", service.Properties.WebHook)
269+
d.Set("username", service.Properties.Username)
270+
d.Set("notify_only_broken_pipelines", bool(service.Properties.NotifyOnlyBrokenPipelines))
271+
d.Set("notify_only_default_branch", bool(service.Properties.NotifyOnlyDefaultBranch))
272+
d.Set("branches_to_be_notified", service.Properties.BranchesToBeNotified)
273+
d.Set("confidential_issue_channel", service.Properties.ConfidentialIssueChannel)
274+
d.Set("confidential_issues_events", service.ConfidentialIssuesEvents)
275+
// TODO: Currently, GitLab ignores this option (not implemented yet?), so
276+
// there is no way to set it. Uncomment when this is fixed.
277+
// See: https://gitlab.com/gitlab-org/gitlab-ce/issues/49730
278+
// d.Set("confidential_note_channel", service.Properties.ConfidentialNoteChannel)
279+
d.Set("confidential_note_events", service.ConfidentialNoteEvents)
280+
// TODO: Currently, GitLab doesn't correctly implement the API, so this is
281+
// impossible to implement here at the moment.
282+
// see https://gitlab.com/gitlab-org/gitlab/-/issues/28903
283+
// d.Set("deployment_channel", service.Properties.DeploymentChannel)
284+
// d.Set("deployment_events", service.DeploymentEvents)
285+
d.Set("issue_channel", service.Properties.IssueChannel)
286+
d.Set("issues_events", service.IssuesEvents)
287+
// TODO: Currently, go-gitlab doesn't implement this option yet.
288+
// see https://github.com/xanzy/go-gitlab/issues/1354
289+
d.Set("job_events", service.JobEvents)
290+
d.Set("merge_request_channel", service.Properties.MergeRequestChannel)
291+
d.Set("merge_requests_events", service.MergeRequestsEvents)
292+
d.Set("note_channel", service.Properties.NoteChannel)
293+
d.Set("note_events", service.NoteEvents)
294+
d.Set("pipeline_channel", service.Properties.PipelineChannel)
295+
d.Set("pipeline_events", service.PipelineEvents)
296+
d.Set("push_channel", service.Properties.PushChannel)
297+
d.Set("push_events", service.PushEvents)
298+
d.Set("tag_push_channel", service.Properties.TagPushChannel)
299+
d.Set("tag_push_events", service.TagPushEvents)
300+
d.Set("wiki_page_channel", service.Properties.WikiPageChannel)
301+
d.Set("wiki_page_events", service.WikiPageEvents)
262302

263303
return nil
264304
}
@@ -269,7 +309,7 @@ func resourceGitlabServiceSlackUpdate(ctx context.Context, d *schema.ResourceDat
269309

270310
func resourceGitlabServiceSlackDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
271311
client := meta.(*gitlab.Client)
272-
project := d.Get("project").(string)
312+
project := d.Id()
273313

274314
log.Printf("[DEBUG] delete gitlab slack service for project %s", project)
275315

@@ -280,9 +320,3 @@ func resourceGitlabServiceSlackDelete(ctx context.Context, d *schema.ResourceDat
280320

281321
return nil
282322
}
283-
284-
func resourceGitlabServiceSlackImportState(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
285-
d.Set("project", d.Id())
286-
287-
return []*schema.ResourceData{d}, nil
288-
}

0 commit comments

Comments
 (0)