Skip to content

Commit a2c27f5

Browse files
authored
Merge pull request #375 from jeremad/jrm/pipeline_emails
Add resource for pipelines email integration
2 parents ef97dc9 + da42d52 commit a2c27f5

11 files changed

+333
-6
lines changed

gitlab/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func Provider() terraform.ResourceProvider {
8686
"gitlab_service_slack": resourceGitlabServiceSlack(),
8787
"gitlab_service_jira": resourceGitlabServiceJira(),
8888
"gitlab_service_github": resourceGitlabServiceGithub(),
89+
"gitlab_service_pipelines_email": resourceGitlabServicePipelinesEmail(),
8990
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
9091
"gitlab_group_cluster": resourceGitlabGroupCluster(),
9192
"gitlab_group_ldap_link": resourceGitlabGroupLdapLink(),

gitlab/resource_gitlab_service_github.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func resourceGitlabServiceGithub() *schema.Resource {
2222
"project": {
2323
Type: schema.TypeString,
2424
Required: true,
25+
ForceNew: true,
2526
},
2627
"token": {
2728
Type: schema.TypeString,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package gitlab
2+
3+
import (
4+
"log"
5+
"strings"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
9+
gitlab "github.com/xanzy/go-gitlab"
10+
)
11+
12+
func resourceGitlabServicePipelinesEmail() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceGitlabServicePipelinesEmailCreate,
15+
Read: resourceGitlabServicePipelinesEmailRead,
16+
Update: resourceGitlabServicePipelinesEmailCreate,
17+
Delete: resourceGitlabServicePipelinesEmailDelete,
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+
"recipients": {
29+
Type: schema.TypeSet,
30+
Required: true,
31+
Elem: &schema.Schema{Type: schema.TypeString},
32+
},
33+
"notify_only_broken_pipelines": {
34+
Type: schema.TypeBool,
35+
Optional: true,
36+
Default: true,
37+
},
38+
"branches_to_be_notified": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
ValidateFunc: validation.StringInSlice([]string{"all", "default", "protected", "default_and_protected"}, true),
42+
Default: "default",
43+
},
44+
},
45+
}
46+
}
47+
48+
func resourceGitlabServicePipelinesEmailSetToState(d *schema.ResourceData, service *gitlab.PipelinesEmailService) {
49+
d.Set("recipients", strings.Split(service.Properties.Recipients, ","))
50+
d.Set("notify_only_broken_pipelines", service.Properties.NotifyOnlyBrokenPipelines)
51+
d.Set("branches_to_be_notified", service.Properties.BranchesToBeNotified)
52+
}
53+
54+
func resourceGitlabServicePipelinesEmailCreate(d *schema.ResourceData, meta interface{}) error {
55+
client := meta.(*gitlab.Client)
56+
project := d.Get("project").(string)
57+
d.SetId(project)
58+
options := &gitlab.SetPipelinesEmailServiceOptions{
59+
Recipients: gitlab.String(strings.Join(*stringSetToStringSlice(d.Get("recipients").(*schema.Set)), ",")),
60+
NotifyOnlyBrokenPipelines: gitlab.Bool(d.Get("notify_only_broken_pipelines").(bool)),
61+
BranchesToBeNotified: gitlab.String(d.Get("branches_to_be_notified").(string)),
62+
}
63+
64+
log.Printf("[DEBUG] create gitlab pipelines emails service for project %s", project)
65+
66+
_, err := client.Services.SetPipelinesEmailService(project, options)
67+
if err != nil {
68+
return err
69+
}
70+
71+
return resourceGitlabServicePipelinesEmailRead(d, meta)
72+
}
73+
74+
func resourceGitlabServicePipelinesEmailRead(d *schema.ResourceData, meta interface{}) error {
75+
client := meta.(*gitlab.Client)
76+
project := d.Id()
77+
78+
log.Printf("[DEBUG] read gitlab pipelines emails service for project %s", project)
79+
80+
service, _, err := client.Services.GetPipelinesEmailService(project)
81+
if err != nil {
82+
return err
83+
}
84+
85+
d.Set("project", project)
86+
resourceGitlabServicePipelinesEmailSetToState(d, service)
87+
return nil
88+
}
89+
90+
func resourceGitlabServicePipelinesEmailDelete(d *schema.ResourceData, meta interface{}) error {
91+
client := meta.(*gitlab.Client)
92+
project := d.Id()
93+
94+
log.Printf("[DEBUG] delete gitlab pipelines email service for project %s", project)
95+
96+
_, err := client.Services.DeletePipelinesEmailService(project)
97+
return err
98+
}
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+
"sort"
6+
"strings"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
12+
gitlab "github.com/xanzy/go-gitlab"
13+
)
14+
15+
func TestAccGitlabServicePipelinesEmail_basic(t *testing.T) {
16+
var pipelinesEmailService gitlab.PipelinesEmailService
17+
rInt := acctest.RandInt()
18+
pipelinesEmailResourceName := "gitlab_service_pipelines_email.email"
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { testAccPreCheck(t) },
22+
Providers: testAccProviders,
23+
CheckDestroy: testAccCheckGitlabServicePipelinesEmailDestroy,
24+
Steps: []resource.TestStep{
25+
// Create a project and a pipelines email service
26+
{
27+
Config: testAccGitlabServicePipelinesEmailConfig(rInt),
28+
Check: resource.ComposeTestCheckFunc(
29+
testAccCheckGitlabServicePipelinesEmailExists(pipelinesEmailResourceName, &pipelinesEmailService),
30+
testRecipients(&pipelinesEmailService, []string{"[email protected]"}),
31+
resource.TestCheckResourceAttr(pipelinesEmailResourceName, "notify_only_broken_pipelines", "true"),
32+
resource.TestCheckResourceAttr(pipelinesEmailResourceName, "branches_to_be_notified", "default"),
33+
),
34+
},
35+
// Update the pipelinesEmail service
36+
{
37+
Config: testAccGitlabServicePipelinesEmailUpdateConfig(rInt),
38+
Check: resource.ComposeTestCheckFunc(
39+
testAccCheckGitlabServicePipelinesEmailExists(pipelinesEmailResourceName, &pipelinesEmailService),
40+
testRecipients(&pipelinesEmailService, []string{"[email protected]", "[email protected]"}),
41+
resource.TestCheckResourceAttr(pipelinesEmailResourceName, "notify_only_broken_pipelines", "false"),
42+
resource.TestCheckResourceAttr(pipelinesEmailResourceName, "branches_to_be_notified", "all"),
43+
),
44+
},
45+
// Update the pipelinesEmail service to get back to previous settings
46+
{
47+
Config: testAccGitlabServicePipelinesEmailConfig(rInt),
48+
Check: resource.ComposeTestCheckFunc(
49+
testAccCheckGitlabServicePipelinesEmailExists(pipelinesEmailResourceName, &pipelinesEmailService),
50+
testRecipients(&pipelinesEmailService, []string{"[email protected]"}),
51+
resource.TestCheckResourceAttr(pipelinesEmailResourceName, "notify_only_broken_pipelines", "true"),
52+
resource.TestCheckResourceAttr(pipelinesEmailResourceName, "branches_to_be_notified", "default"),
53+
),
54+
},
55+
},
56+
})
57+
}
58+
59+
func TestAccGitlabServicePipelinesEmail_import(t *testing.T) {
60+
pipelinesEmailResourceName := "gitlab_service_pipelines_email.email"
61+
rInt := acctest.RandInt()
62+
63+
resource.Test(t, resource.TestCase{
64+
PreCheck: func() { testAccPreCheck(t) },
65+
Providers: testAccProviders,
66+
CheckDestroy: testAccCheckGitlabServicePipelinesEmailDestroy,
67+
Steps: []resource.TestStep{
68+
{
69+
Config: testAccGitlabServicePipelinesEmailConfig(rInt),
70+
},
71+
{
72+
ResourceName: pipelinesEmailResourceName,
73+
ImportState: true,
74+
ImportStateVerify: true,
75+
},
76+
},
77+
})
78+
}
79+
80+
func testAccCheckGitlabServicePipelinesEmailExists(n string, service *gitlab.PipelinesEmailService) resource.TestCheckFunc {
81+
return func(s *terraform.State) error {
82+
rs, ok := s.RootModule().Resources[n]
83+
if !ok {
84+
return fmt.Errorf("Not Found: %s", n)
85+
}
86+
87+
project := rs.Primary.Attributes["project"]
88+
if project == "" {
89+
return fmt.Errorf("No project ID is set")
90+
}
91+
conn := testAccProvider.Meta().(*gitlab.Client)
92+
93+
pipelinesEmailService, _, err := conn.Services.GetPipelinesEmailService(project)
94+
if err != nil {
95+
return fmt.Errorf("PipelinesEmail service does not exist in project %s: %v", project, err)
96+
}
97+
*service = *pipelinesEmailService
98+
99+
return nil
100+
}
101+
}
102+
103+
func testRecipients(service *gitlab.PipelinesEmailService, expected []string) resource.TestCheckFunc {
104+
return func(s *terraform.State) error {
105+
res_string := service.Properties.Recipients
106+
res := strings.Split(res_string, ",")
107+
if len(res) != len(expected) {
108+
return fmt.Errorf("'recipients' field does not have the correct size expected: %d, found: %d", len(expected), len(res))
109+
}
110+
sort.Strings(res)
111+
sort.Strings(expected)
112+
for i, r := range res {
113+
e := expected[i]
114+
if r != e {
115+
return fmt.Errorf("expected: %s, found: %s", r, e)
116+
}
117+
118+
}
119+
return nil
120+
}
121+
}
122+
123+
func testAccCheckGitlabServicePipelinesEmailDestroy(s *terraform.State) error {
124+
conn := testAccProvider.Meta().(*gitlab.Client)
125+
126+
for _, rs := range s.RootModule().Resources {
127+
if rs.Type != "gitlab_project" {
128+
continue
129+
}
130+
131+
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
132+
if err == nil {
133+
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
134+
if gotRepo.MarkedForDeletionAt == nil {
135+
return fmt.Errorf("Repository still exists")
136+
}
137+
}
138+
}
139+
if resp.StatusCode != 404 {
140+
return err
141+
}
142+
return nil
143+
}
144+
return nil
145+
}
146+
147+
func testAccGitlabServicePipelinesEmailConfig(rInt int) string {
148+
return fmt.Sprintf(`
149+
resource "gitlab_project" "foo" {
150+
name = "foo-%d"
151+
description = "Terraform acceptance tests"
152+
}
153+
154+
resource "gitlab_service_pipelines_email" "email" {
155+
project = gitlab_project.foo.id
156+
recipients = ["[email protected]"]
157+
}
158+
`, rInt)
159+
}
160+
161+
func testAccGitlabServicePipelinesEmailUpdateConfig(rInt int) string {
162+
return fmt.Sprintf(`
163+
resource "gitlab_project" "foo" {
164+
name = "foo-%d"
165+
description = "Terraform acceptance tests"
166+
}
167+
168+
resource "gitlab_service_pipelines_email" "email" {
169+
project = gitlab_project.foo.id
170+
171+
notify_only_broken_pipelines = false
172+
branches_to_be_notified = "all"
173+
}
174+
`, rInt)
175+
}

gitlab/resource_gitlab_service_slack.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func resourceGitlabServiceSlack() *schema.Resource {
2222
"project": {
2323
Type: schema.TypeString,
2424
Required: true,
25+
ForceNew: true,
2526
},
2627
"webhook": {
2728
Type: schema.TypeString,

website/docs/r/project_share_group.html.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ This resource allows you to share a project with a group
1414

1515
```hcl
1616
resource "gitlab_project_share_group" "test" {
17-
project_id = "12345"
18-
group_id = 1337
19-
access_level = "guest"
17+
project_id = "12345"
18+
group_id = 1337
19+
access_level = "guest"
2020
}
2121
```
2222

website/docs/r/service_github.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ resource "gitlab_project" "awesome_project" {
2222
}
2323
2424
resource "gitlab_service_github" "github" {
25-
project = "${gitlab_project.awesome_project.id}"
25+
project = gitlab_project.awesome_project.id
2626
token = "REDACTED"
2727
repository_url = "https://github.com/gitlabhq/terraform-provider-gitlab"
2828
}

website/docs/r/service_jira.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ resource "gitlab_project" "awesome_project" {
2020
}
2121
2222
resource "gitlab_service_jira" "jira" {
23-
project = "${gitlab_project.awesome_project.id}"
23+
project = gitlab_project.awesome_project.id
2424
url = "https://jira.example.com"
2525
username = "user"
2626
password = "mypass"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
layout: "gitlab"
3+
page_title: "GitLab: gitlab_service_pipelines_email"
4+
sidebar_current: "docs-gitlab-resource-service_pipelines_email"
5+
description: |-
6+
Manage Pipelines email integration that emails the pipeline status to a list of recipients.
7+
---
8+
9+
# gitlab\_service\_pipelines_email
10+
11+
This resource manages a [Pipelines email integration](https://docs.gitlab.com/ee/user/project/integrations/overview.html#integrations-listing) that emails the pipeline status to a list of recipients.
12+
13+
## Example Usage
14+
15+
```hcl
16+
resource "gitlab_project" "awesome_project" {
17+
name = "awesome_project"
18+
description = "My awesome project."
19+
visibility_level = "public"
20+
}
21+
22+
resource "gitlab_service_pipelines_email" "email" {
23+
project = gitlab_project.awesome_project.id
24+
recipients = ["[email protected]"]
25+
notify_only_broken_pipelines = true
26+
branches_to_be_notified = "all"
27+
}
28+
```
29+
30+
## Argument Reference
31+
32+
The following arguments are supported:
33+
34+
* `project` - (Required, string) ID of the project you want to activate integration on.
35+
36+
* `recipients` - (Required, set(string)) email addresses where notifications are sent.
37+
38+
* `notify_only_broken_pipelines` - (Optional, bool) Notify only broken pipelines. Default is true.
39+
40+
* `branches_to_be_notified` - (Optional, string) Branches to send notifications for. Valid options are `all`, `default`, `protected`, and `default_and_protected`. Default is `default`
41+
42+
## Importing Pipelines email service
43+
44+
You can import a service_pipelines_email state using `terraform import <resource> <project_id>`:
45+
46+
```bash
47+
$ terraform import gitlab_service_pipelines_email.email 1
48+
```

website/docs/r/service_slack.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ resource "gitlab_project" "awesome_project" {
2020
}
2121
2222
resource "gitlab_service_slack" "slack" {
23-
project = "${gitlab_project.awesome_project.id}"
23+
project = gitlab_project.awesome_project.id
2424
webhook = "https://webhook.com"
2525
username = "myuser"
2626
push_events = true

0 commit comments

Comments
 (0)