Skip to content

Commit dd0da2b

Browse files
Implement configuration of the integration "Microsoft Teams" (#308) (#784)
* Implement configuration of the integration "Microsoft Teams" (#308) * Make use of schema.ImportStatePassthrough Fixes #784 (comment) * Get rid of obsolete `ImportStateIdFunc` Fixes #784 (comment) * Resolve tfproviderlint issue `AT002` Fixes #784 (comment) * Align handling of attributes with more of the exiting implementations. Before it mostly was just copy-paste-adapt of the jira service. * Remove superfluous attribute `title` and unify order of attributes in all locations. Fixes #784 (comment)
1 parent 912b647 commit dd0da2b

File tree

4 files changed

+411
-0
lines changed

4 files changed

+411
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# gitlab\_service\_microsoft\_teams
2+
3+
This resource allows you to manage Microsoft Teams integration.
4+
5+
## Example Usage
6+
7+
```hcl
8+
resource "gitlab_project" "awesome_project" {
9+
name = "awesome_project"
10+
description = "My awesome project."
11+
visibility_level = "public"
12+
}
13+
14+
resource "gitlab_service_microsoft_teams" "teams" {
15+
project = gitlab_project.awesome_project.id
16+
webhook = "https://testurl.com/?token=XYZ"
17+
push_events = true
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
* `project` - (Required) ID of the project you want to activate integration on.
26+
* `webhook` - (Required) The Microsoft Teams webhook. For example, https://outlook.office.com/webhook/...
27+
* `notify_only_broken_pipelines` - (Optional) Send notifications for broken pipelines
28+
* `branches_to_be_notified` - (Optional) Branches to send notifications for. Valid options are “all”, “default”, “protected”, and “default_and_protected”. The default value is “default”
29+
* `push_events` - (Optional) Enable notifications for push events
30+
* `issues_events` - (Optional) Enable notifications for issue events
31+
* `confidential_issues_events` - (Optional) Enable notifications for confidential issue events
32+
* `merge_requests_events` - (Optional) Enable notifications for merge request events
33+
* `tag_push_events` - (Optional) Enable notifications for tag push events
34+
* `note_events` - (Optional) Enable notifications for note events
35+
* `confidential_note_events` - (Optional) Enable notifications for confidential note events
36+
* `pipeline_events` - (Optional) Enable notifications for pipeline events
37+
* `wiki_page_events` - (Optional) Enable notifications for wiki page events
38+
39+
## Importing Microsoft Teams service
40+
41+
You can import a service_microsoft_teams state using `terraform import <resource> <project_id>`:
42+
43+
```bash
44+
$ terraform import gitlab_service_microsoft_teams.teams 1
45+
```

gitlab/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func Provider() terraform.ResourceProvider {
8585
"gitlab_project_cluster": resourceGitlabProjectCluster(),
8686
"gitlab_service_slack": resourceGitlabServiceSlack(),
8787
"gitlab_service_jira": resourceGitlabServiceJira(),
88+
"gitlab_service_microsoft_teams": resourceGitlabServiceMicrosoftTeams(),
8889
"gitlab_service_github": resourceGitlabServiceGithub(),
8990
"gitlab_service_pipelines_email": resourceGitlabServicePipelinesEmail(),
9091
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
gitlab "github.com/xanzy/go-gitlab"
10+
)
11+
12+
func resourceGitlabServiceMicrosoftTeams() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceGitlabServiceMicrosoftTeamsCreate,
15+
Read: resourceGitlabServiceMicrosoftTeamsRead,
16+
Update: resourceGitlabServiceMicrosoftTeamsUpdate,
17+
Delete: resourceGitlabServiceMicrosoftTeamsDelete,
18+
Importer: &schema.ResourceImporter{
19+
State: schema.ImportStatePassthrough,
20+
},
21+
22+
Schema: map[string]*schema.Schema{
23+
"project": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
ForceNew: true,
27+
},
28+
"created_at": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"updated_at": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"active": {
37+
Type: schema.TypeBool,
38+
Computed: true,
39+
},
40+
"webhook": {
41+
Type: schema.TypeString,
42+
Required: true,
43+
ValidateFunc: validateURLFunc,
44+
},
45+
"notify_only_broken_pipelines": {
46+
Type: schema.TypeBool,
47+
Optional: true,
48+
},
49+
"branches_to_be_notified": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
},
53+
"push_events": {
54+
Type: schema.TypeBool,
55+
Optional: true,
56+
},
57+
"issues_events": {
58+
Type: schema.TypeBool,
59+
Optional: true,
60+
},
61+
"confidential_issues_events": {
62+
Type: schema.TypeBool,
63+
Optional: true,
64+
},
65+
"merge_requests_events": {
66+
Type: schema.TypeBool,
67+
Optional: true,
68+
},
69+
"tag_push_events": {
70+
Type: schema.TypeBool,
71+
Optional: true,
72+
},
73+
"note_events": {
74+
Type: schema.TypeBool,
75+
Optional: true,
76+
},
77+
"confidential_note_events": {
78+
Type: schema.TypeBool,
79+
Optional: true,
80+
},
81+
"pipeline_events": {
82+
Type: schema.TypeBool,
83+
Optional: true,
84+
},
85+
"wiki_page_events": {
86+
Type: schema.TypeBool,
87+
Optional: true,
88+
},
89+
},
90+
}
91+
}
92+
93+
func resourceGitlabServiceMicrosoftTeamsCreate(d *schema.ResourceData, meta interface{}) error {
94+
client := meta.(*gitlab.Client)
95+
project := d.Get("project").(string)
96+
d.SetId(project)
97+
98+
options := &gitlab.SetMicrosoftTeamsServiceOptions{
99+
WebHook: gitlab.String(d.Get("webhook").(string)),
100+
NotifyOnlyBrokenPipelines: gitlab.Bool(d.Get("notify_only_broken_pipelines").(bool)),
101+
BranchesToBeNotified: gitlab.String(d.Get("branches_to_be_notified").(string)),
102+
PushEvents: gitlab.Bool(d.Get("push_events").(bool)),
103+
IssuesEvents: gitlab.Bool(d.Get("issues_events").(bool)),
104+
ConfidentialIssuesEvents: gitlab.Bool(d.Get("confidential_issues_events").(bool)),
105+
MergeRequestsEvents: gitlab.Bool(d.Get("merge_requests_events").(bool)),
106+
TagPushEvents: gitlab.Bool(d.Get("tag_push_events").(bool)),
107+
NoteEvents: gitlab.Bool(d.Get("note_events").(bool)),
108+
ConfidentialNoteEvents: gitlab.Bool(d.Get("confidential_note_events").(bool)),
109+
PipelineEvents: gitlab.Bool(d.Get("pipeline_events").(bool)),
110+
WikiPageEvents: gitlab.Bool(d.Get("wiki_page_events").(bool)),
111+
}
112+
113+
log.Printf("[DEBUG] Create Gitlab Microsoft Teams service")
114+
115+
if _, err := client.Services.SetMicrosoftTeamsService(project, options); err != nil {
116+
return fmt.Errorf("couldn't create Gitlab Microsoft Teams service: %w", err)
117+
}
118+
119+
return resourceGitlabServiceMicrosoftTeamsRead(d, meta)
120+
}
121+
122+
func resourceGitlabServiceMicrosoftTeamsRead(d *schema.ResourceData, meta interface{}) error {
123+
client := meta.(*gitlab.Client)
124+
project := d.Id()
125+
126+
p, resp, err := client.Projects.GetProject(project, nil)
127+
if err != nil {
128+
if resp != nil && resp.StatusCode == http.StatusNotFound {
129+
log.Printf("[DEBUG] Removing Gitlab Microsoft Teams service %s because project %s not found", d.Id(), p.Name)
130+
d.SetId("")
131+
return nil
132+
}
133+
return err
134+
}
135+
136+
log.Printf("[DEBUG] Read Gitlab Microsoft Teams service for project %s", d.Id())
137+
138+
teamsService, _, err := client.Services.GetMicrosoftTeamsService(project)
139+
if err != nil {
140+
return err
141+
}
142+
143+
d.Set("project", project)
144+
d.Set("created_at", teamsService.CreatedAt.String())
145+
d.Set("updated_at", teamsService.UpdatedAt.String())
146+
d.Set("active", teamsService.Active)
147+
d.Set("webhook", teamsService.Properties.WebHook)
148+
d.Set("notify_only_broken_pipelines", teamsService.Properties.NotifyOnlyBrokenPipelines)
149+
d.Set("branches_to_be_notified", teamsService.Properties.BranchesToBeNotified)
150+
d.Set("push_events", teamsService.PushEvents)
151+
d.Set("issues_events", teamsService.IssuesEvents)
152+
d.Set("confidential_issues_events", teamsService.ConfidentialIssuesEvents)
153+
d.Set("merge_requests_events", teamsService.MergeRequestsEvents)
154+
d.Set("tag_push_events", teamsService.TagPushEvents)
155+
d.Set("note_events", teamsService.NoteEvents)
156+
d.Set("confidential_note_events", teamsService.ConfidentialNoteEvents)
157+
d.Set("pipeline_events", teamsService.PipelineEvents)
158+
d.Set("wiki_page_events", teamsService.WikiPageEvents)
159+
160+
return nil
161+
}
162+
163+
func resourceGitlabServiceMicrosoftTeamsUpdate(d *schema.ResourceData, meta interface{}) error {
164+
return resourceGitlabServiceMicrosoftTeamsCreate(d, meta)
165+
}
166+
167+
func resourceGitlabServiceMicrosoftTeamsDelete(d *schema.ResourceData, meta interface{}) error {
168+
client := meta.(*gitlab.Client)
169+
project := d.Id()
170+
171+
log.Printf("[DEBUG] Delete Gitlab Microsoft Teams service for project %s", d.Id())
172+
173+
_, err := client.Services.DeleteMicrosoftTeamsService(project)
174+
return err
175+
}

0 commit comments

Comments
 (0)