Skip to content

Commit

Permalink
Implement configuration of the integration "Microsoft Teams" (#308) (#…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
MichelHartmann committed Jan 19, 2022
1 parent 912b647 commit dd0da2b
Show file tree
Hide file tree
Showing 4 changed files with 411 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/resources/service_microsoft_teams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# gitlab\_service\_microsoft\_teams

This resource allows you to manage Microsoft Teams integration.

## Example Usage

```hcl
resource "gitlab_project" "awesome_project" {
name = "awesome_project"
description = "My awesome project."
visibility_level = "public"
}
resource "gitlab_service_microsoft_teams" "teams" {
project = gitlab_project.awesome_project.id
webhook = "https://testurl.com/?token=XYZ"
push_events = true
}
```

## Argument Reference

The following arguments are supported:

* `project` - (Required) ID of the project you want to activate integration on.
* `webhook` - (Required) The Microsoft Teams webhook. For example, https://outlook.office.com/webhook/...
* `notify_only_broken_pipelines` - (Optional) Send notifications for broken pipelines
* `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”
* `push_events` - (Optional) Enable notifications for push events
* `issues_events` - (Optional) Enable notifications for issue events
* `confidential_issues_events` - (Optional) Enable notifications for confidential issue events
* `merge_requests_events` - (Optional) Enable notifications for merge request events
* `tag_push_events` - (Optional) Enable notifications for tag push events
* `note_events` - (Optional) Enable notifications for note events
* `confidential_note_events` - (Optional) Enable notifications for confidential note events
* `pipeline_events` - (Optional) Enable notifications for pipeline events
* `wiki_page_events` - (Optional) Enable notifications for wiki page events

## Importing Microsoft Teams service

You can import a service_microsoft_teams state using `terraform import <resource> <project_id>`:

```bash
$ terraform import gitlab_service_microsoft_teams.teams 1
```
1 change: 1 addition & 0 deletions gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func Provider() terraform.ResourceProvider {
"gitlab_project_cluster": resourceGitlabProjectCluster(),
"gitlab_service_slack": resourceGitlabServiceSlack(),
"gitlab_service_jira": resourceGitlabServiceJira(),
"gitlab_service_microsoft_teams": resourceGitlabServiceMicrosoftTeams(),
"gitlab_service_github": resourceGitlabServiceGithub(),
"gitlab_service_pipelines_email": resourceGitlabServicePipelinesEmail(),
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
Expand Down
175 changes: 175 additions & 0 deletions gitlab/resource_gitlab_service_microsoft_teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package gitlab

import (
"fmt"
"log"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
gitlab "github.com/xanzy/go-gitlab"
)

func resourceGitlabServiceMicrosoftTeams() *schema.Resource {
return &schema.Resource{
Create: resourceGitlabServiceMicrosoftTeamsCreate,
Read: resourceGitlabServiceMicrosoftTeamsRead,
Update: resourceGitlabServiceMicrosoftTeamsUpdate,
Delete: resourceGitlabServiceMicrosoftTeamsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
"active": {
Type: schema.TypeBool,
Computed: true,
},
"webhook": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateURLFunc,
},
"notify_only_broken_pipelines": {
Type: schema.TypeBool,
Optional: true,
},
"branches_to_be_notified": {
Type: schema.TypeString,
Optional: true,
},
"push_events": {
Type: schema.TypeBool,
Optional: true,
},
"issues_events": {
Type: schema.TypeBool,
Optional: true,
},
"confidential_issues_events": {
Type: schema.TypeBool,
Optional: true,
},
"merge_requests_events": {
Type: schema.TypeBool,
Optional: true,
},
"tag_push_events": {
Type: schema.TypeBool,
Optional: true,
},
"note_events": {
Type: schema.TypeBool,
Optional: true,
},
"confidential_note_events": {
Type: schema.TypeBool,
Optional: true,
},
"pipeline_events": {
Type: schema.TypeBool,
Optional: true,
},
"wiki_page_events": {
Type: schema.TypeBool,
Optional: true,
},
},
}
}

func resourceGitlabServiceMicrosoftTeamsCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
project := d.Get("project").(string)
d.SetId(project)

options := &gitlab.SetMicrosoftTeamsServiceOptions{
WebHook: gitlab.String(d.Get("webhook").(string)),
NotifyOnlyBrokenPipelines: gitlab.Bool(d.Get("notify_only_broken_pipelines").(bool)),
BranchesToBeNotified: gitlab.String(d.Get("branches_to_be_notified").(string)),
PushEvents: gitlab.Bool(d.Get("push_events").(bool)),
IssuesEvents: gitlab.Bool(d.Get("issues_events").(bool)),
ConfidentialIssuesEvents: gitlab.Bool(d.Get("confidential_issues_events").(bool)),
MergeRequestsEvents: gitlab.Bool(d.Get("merge_requests_events").(bool)),
TagPushEvents: gitlab.Bool(d.Get("tag_push_events").(bool)),
NoteEvents: gitlab.Bool(d.Get("note_events").(bool)),
ConfidentialNoteEvents: gitlab.Bool(d.Get("confidential_note_events").(bool)),
PipelineEvents: gitlab.Bool(d.Get("pipeline_events").(bool)),
WikiPageEvents: gitlab.Bool(d.Get("wiki_page_events").(bool)),
}

log.Printf("[DEBUG] Create Gitlab Microsoft Teams service")

if _, err := client.Services.SetMicrosoftTeamsService(project, options); err != nil {
return fmt.Errorf("couldn't create Gitlab Microsoft Teams service: %w", err)
}

return resourceGitlabServiceMicrosoftTeamsRead(d, meta)
}

func resourceGitlabServiceMicrosoftTeamsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
project := d.Id()

p, resp, err := client.Projects.GetProject(project, nil)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
log.Printf("[DEBUG] Removing Gitlab Microsoft Teams service %s because project %s not found", d.Id(), p.Name)
d.SetId("")
return nil
}
return err
}

log.Printf("[DEBUG] Read Gitlab Microsoft Teams service for project %s", d.Id())

teamsService, _, err := client.Services.GetMicrosoftTeamsService(project)
if err != nil {
return err
}

d.Set("project", project)
d.Set("created_at", teamsService.CreatedAt.String())
d.Set("updated_at", teamsService.UpdatedAt.String())
d.Set("active", teamsService.Active)
d.Set("webhook", teamsService.Properties.WebHook)
d.Set("notify_only_broken_pipelines", teamsService.Properties.NotifyOnlyBrokenPipelines)
d.Set("branches_to_be_notified", teamsService.Properties.BranchesToBeNotified)
d.Set("push_events", teamsService.PushEvents)
d.Set("issues_events", teamsService.IssuesEvents)
d.Set("confidential_issues_events", teamsService.ConfidentialIssuesEvents)
d.Set("merge_requests_events", teamsService.MergeRequestsEvents)
d.Set("tag_push_events", teamsService.TagPushEvents)
d.Set("note_events", teamsService.NoteEvents)
d.Set("confidential_note_events", teamsService.ConfidentialNoteEvents)
d.Set("pipeline_events", teamsService.PipelineEvents)
d.Set("wiki_page_events", teamsService.WikiPageEvents)

return nil
}

func resourceGitlabServiceMicrosoftTeamsUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceGitlabServiceMicrosoftTeamsCreate(d, meta)
}

func resourceGitlabServiceMicrosoftTeamsDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
project := d.Id()

log.Printf("[DEBUG] Delete Gitlab Microsoft Teams service for project %s", d.Id())

_, err := client.Services.DeleteMicrosoftTeamsService(project)
return err
}

0 comments on commit dd0da2b

Please sign in to comment.