Skip to content

Commit 27c1c28

Browse files
authored
Merge pull request #311 from tptee/resource_gitlab_service_github
Add resource for GitHub integration
2 parents c28275f + 0b06446 commit 27c1c28

8 files changed

+381
-26
lines changed

gitlab/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func Provider() terraform.ResourceProvider {
8484
"gitlab_project_cluster": resourceGitlabProjectCluster(),
8585
"gitlab_service_slack": resourceGitlabServiceSlack(),
8686
"gitlab_service_jira": resourceGitlabServiceJira(),
87+
"gitlab_service_github": resourceGitlabServiceGithub(),
8788
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
8889
"gitlab_group_cluster": resourceGitlabGroupCluster(),
8990
"gitlab_group_ldap_link": resourceGitlabGroupLdapLink(),

gitlab/provider_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package gitlab
22

33
import (
4+
"errors"
45
"os"
6+
"strings"
57
"testing"
68

79
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
810
"github.com/hashicorp/terraform-plugin-sdk/terraform"
11+
"github.com/xanzy/go-gitlab"
912
)
1013

1114
var testAccProviders map[string]terraform.ResourceProvider
@@ -18,6 +21,26 @@ func init() {
1821
}
1922
}
2023

24+
func isRunningInEE() (bool, error) {
25+
if conn, ok := testAccProvider.Meta().(*gitlab.Client); ok {
26+
version, _, err := conn.Version.GetVersion()
27+
if err != nil {
28+
return false, err
29+
}
30+
if strings.Contains(version.String(), "-ee") {
31+
return true, nil
32+
}
33+
} else {
34+
return false, errors.New("Provider not initialized, unable to get GitLab connection")
35+
}
36+
return false, nil
37+
}
38+
39+
func isRunningInCE() (bool, error) {
40+
isEE, err := isRunningInEE()
41+
return !isEE, err
42+
}
43+
2144
func TestProvider(t *testing.T) {
2245
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
2346
t.Fatalf("err: %s", err)

gitlab/resource_gitlab_project_push_rules_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package gitlab
22

33
import (
4-
"errors"
54
"fmt"
6-
"strings"
75
"testing"
86

97
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -13,26 +11,6 @@ import (
1311
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
1412
)
1513

16-
func isRunningInEE() (bool, error) {
17-
if conn, ok := testAccProvider.Meta().(*gitlab.Client); ok {
18-
version, _, err := conn.Version.GetVersion()
19-
if err != nil {
20-
return false, err
21-
}
22-
if strings.Contains(version.String(), "-ee") {
23-
return true, nil
24-
}
25-
} else {
26-
return false, errors.New("Provider not initialized, unable to get GitLab connection")
27-
}
28-
return false, nil
29-
}
30-
31-
func isRunningInCE() (bool, error) {
32-
isEE, err := isRunningInEE()
33-
return !isEE, err
34-
}
35-
3614
func TestAccGitlabProjectPushRules_basic(t *testing.T) {
3715
var pushRules gitlab.ProjectPushRules
3816
rInt := acctest.RandInt()
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
gitlab "github.com/xanzy/go-gitlab"
9+
)
10+
11+
func resourceGitlabServiceGithub() *schema.Resource {
12+
return &schema.Resource{
13+
Create: resourceGitlabServiceGithubCreate,
14+
Read: resourceGitlabServiceGithubRead,
15+
Update: resourceGitlabServiceGithubUpdate,
16+
Delete: resourceGitlabServiceGithubDelete,
17+
Importer: &schema.ResourceImporter{
18+
State: resourceGitlabServiceGithubImportState,
19+
},
20+
21+
Schema: map[string]*schema.Schema{
22+
"project": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
"token": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
Sensitive: true,
30+
},
31+
"repository_url": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
},
35+
"static_context": {
36+
Type: schema.TypeBool,
37+
Optional: true,
38+
Default: true,
39+
},
40+
41+
// Computed from the GitLab API. Omitted event fields because they're always true in Github.
42+
"title": {
43+
Type: schema.TypeString,
44+
Computed: true,
45+
},
46+
"created_at": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
50+
"updated_at": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
},
54+
"active": {
55+
Type: schema.TypeBool,
56+
Computed: true,
57+
},
58+
},
59+
}
60+
}
61+
62+
func resourceGitlabServiceGithubSetToState(d *schema.ResourceData, service *gitlab.GithubService) {
63+
d.SetId(fmt.Sprintf("%d", service.ID))
64+
d.Set("repository_url", service.Properties.RepositoryURL)
65+
d.Set("static_context", service.Properties.StaticContext)
66+
67+
d.Set("title", service.Title)
68+
d.Set("created_at", service.CreatedAt.String())
69+
d.Set("updated_at", service.UpdatedAt.String())
70+
d.Set("active", service.Active)
71+
}
72+
73+
func resourceGitlabServiceGithubCreate(d *schema.ResourceData, meta interface{}) error {
74+
client := meta.(*gitlab.Client)
75+
project := d.Get("project").(string)
76+
77+
log.Printf("[DEBUG] create gitlab github service for project %s", project)
78+
79+
opts := &gitlab.SetGithubServiceOptions{
80+
Token: gitlab.String(d.Get("token").(string)),
81+
RepositoryURL: gitlab.String(d.Get("repository_url").(string)),
82+
StaticContext: gitlab.Bool(d.Get("static_context").(bool)),
83+
}
84+
85+
_, err := client.Services.SetGithubService(project, opts)
86+
if err != nil {
87+
return err
88+
}
89+
90+
return resourceGitlabServiceGithubRead(d, meta)
91+
}
92+
93+
func resourceGitlabServiceGithubRead(d *schema.ResourceData, meta interface{}) error {
94+
client := meta.(*gitlab.Client)
95+
project := d.Get("project").(string)
96+
97+
log.Printf("[DEBUG] read gitlab github service for project %s", project)
98+
99+
service, _, err := client.Services.GetGithubService(project)
100+
if err != nil {
101+
return err
102+
}
103+
104+
resourceGitlabServiceGithubSetToState(d, service)
105+
106+
return nil
107+
}
108+
109+
func resourceGitlabServiceGithubUpdate(d *schema.ResourceData, meta interface{}) error {
110+
return resourceGitlabServiceGithubCreate(d, meta)
111+
}
112+
113+
func resourceGitlabServiceGithubDelete(d *schema.ResourceData, meta interface{}) error {
114+
client := meta.(*gitlab.Client)
115+
project := d.Get("project").(string)
116+
117+
log.Printf("[DEBUG] delete gitlab github service for project %s", project)
118+
119+
_, err := client.Services.DeleteGithubService(project)
120+
return err
121+
}
122+
123+
func resourceGitlabServiceGithubImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
124+
d.Set("project", d.Id())
125+
126+
return []*schema.ResourceData{d}, nil
127+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10+
gitlab "github.com/xanzy/go-gitlab"
11+
)
12+
13+
func TestAccGitlabServiceGithub_basic(t *testing.T) {
14+
var githubService gitlab.GithubService
15+
rInt := acctest.RandInt()
16+
githubResourceName := "gitlab_service_github.github"
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testAccCheckGitlabServiceGithubDestroy,
22+
Steps: []resource.TestStep{
23+
// Create a project and a github service
24+
{
25+
SkipFunc: isRunningInCE,
26+
Config: testAccGitlabServiceGithubConfig(rInt),
27+
Check: resource.ComposeTestCheckFunc(
28+
testAccCheckGitlabServiceGithubExists(githubResourceName, &githubService),
29+
resource.TestCheckResourceAttr(githubResourceName, "repository_url", "https://github.com/terraform-providers/terraform-provider-gitlab"),
30+
resource.TestCheckResourceAttr(githubResourceName, "static_context", "true"),
31+
),
32+
},
33+
// Update the github service
34+
{
35+
SkipFunc: isRunningInCE,
36+
Config: testAccGitlabServiceGithubUpdateConfig(rInt),
37+
Check: resource.ComposeTestCheckFunc(
38+
testAccCheckGitlabServiceGithubExists(githubResourceName, &githubService),
39+
resource.TestCheckResourceAttr(githubResourceName, "repository_url", "https://github.com/terraform-providers/terraform-provider-github"),
40+
resource.TestCheckResourceAttr(githubResourceName, "static_context", "false"),
41+
),
42+
},
43+
// Update the github service to get back to previous settings
44+
{
45+
SkipFunc: isRunningInCE,
46+
Config: testAccGitlabServiceGithubConfig(rInt),
47+
Check: resource.ComposeTestCheckFunc(
48+
testAccCheckGitlabServiceGithubExists(githubResourceName, &githubService),
49+
resource.TestCheckResourceAttr(githubResourceName, "repository_url", "https://github.com/terraform-providers/terraform-provider-gitlab"),
50+
resource.TestCheckResourceAttr(githubResourceName, "static_context", "true"),
51+
),
52+
},
53+
},
54+
})
55+
}
56+
57+
func TestAccGitlabServiceGithub_import(t *testing.T) {
58+
githubResourceName := "gitlab_service_github.github"
59+
rInt := acctest.RandInt()
60+
61+
resource.Test(t, resource.TestCase{
62+
PreCheck: func() { testAccPreCheck(t) },
63+
Providers: testAccProviders,
64+
CheckDestroy: testAccCheckGitlabServiceGithubDestroy,
65+
Steps: []resource.TestStep{
66+
{
67+
SkipFunc: isRunningInCE,
68+
Config: testAccGitlabServiceGithubConfig(rInt),
69+
},
70+
{
71+
SkipFunc: isRunningInCE,
72+
ResourceName: githubResourceName,
73+
ImportStateIdFunc: getGithubProjectID(githubResourceName),
74+
ImportState: true,
75+
ImportStateVerify: true,
76+
ImportStateVerifyIgnore: []string{
77+
"token",
78+
},
79+
},
80+
},
81+
})
82+
}
83+
84+
func testAccCheckGitlabServiceGithubExists(n string, service *gitlab.GithubService) resource.TestCheckFunc {
85+
return func(s *terraform.State) error {
86+
rs, ok := s.RootModule().Resources[n]
87+
if !ok {
88+
return fmt.Errorf("Not Found: %s", n)
89+
}
90+
91+
project := rs.Primary.Attributes["project"]
92+
if project == "" {
93+
return fmt.Errorf("No project ID is set")
94+
}
95+
conn := testAccProvider.Meta().(*gitlab.Client)
96+
97+
githubService, _, err := conn.Services.GetGithubService(project)
98+
if err != nil {
99+
return fmt.Errorf("Github service does not exist in project %s: %v", project, err)
100+
}
101+
*service = *githubService
102+
103+
return nil
104+
}
105+
}
106+
107+
func testAccCheckGitlabServiceGithubDestroy(s *terraform.State) error {
108+
conn := testAccProvider.Meta().(*gitlab.Client)
109+
110+
for _, rs := range s.RootModule().Resources {
111+
if rs.Type != "gitlab_project" {
112+
continue
113+
}
114+
115+
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID, nil)
116+
if err == nil {
117+
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
118+
if gotRepo.MarkedForDeletionAt == nil {
119+
return fmt.Errorf("Repository still exists")
120+
}
121+
}
122+
}
123+
if resp.StatusCode != 404 {
124+
return err
125+
}
126+
return nil
127+
}
128+
return nil
129+
}
130+
131+
func getGithubProjectID(n string) resource.ImportStateIdFunc {
132+
return func(s *terraform.State) (string, error) {
133+
rs, ok := s.RootModule().Resources[n]
134+
if !ok {
135+
return "", fmt.Errorf("Not Found: %s", n)
136+
}
137+
138+
project := rs.Primary.Attributes["project"]
139+
if project == "" {
140+
return "", fmt.Errorf("No project ID is set")
141+
}
142+
143+
return project, nil
144+
}
145+
}
146+
147+
func testAccGitlabServiceGithubConfig(rInt int) string {
148+
return fmt.Sprintf(`
149+
# Requires billing with Silver or above
150+
resource "gitlab_project" "foo" {
151+
name = "foo-%d"
152+
description = "Terraform acceptance tests"
153+
}
154+
155+
resource "gitlab_service_github" "github" {
156+
project = "${gitlab_project.foo.id}"
157+
token = "test"
158+
repository_url = "https://github.com/terraform-providers/terraform-provider-gitlab"
159+
}
160+
`, rInt)
161+
}
162+
163+
func testAccGitlabServiceGithubUpdateConfig(rInt int) string {
164+
return fmt.Sprintf(`
165+
# Requires billing with Silver or above
166+
resource "gitlab_project" "foo" {
167+
name = "foo-%d"
168+
description = "Terraform acceptance tests"
169+
}
170+
171+
resource "gitlab_service_github" "github" {
172+
project = "${gitlab_project.foo.id}"
173+
token = "test"
174+
repository_url = "https://github.com/terraform-providers/terraform-provider-github"
175+
static_context = false
176+
}
177+
`, rInt)
178+
}

0 commit comments

Comments
 (0)