Skip to content

Commit bdb1e5f

Browse files
authored
Merge pull request #43 from VertivSRE/project-membership
Project membership resource
2 parents af02e06 + 6022d26 commit bdb1e5f

11 files changed

+724
-6
lines changed

gitlab/data_source_gitlab_project.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform/helper/schema"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
func dataSourceGitlabProject() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGitlabProjectRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"id": {
17+
Type: schema.TypeInt,
18+
Required: true,
19+
},
20+
},
21+
}
22+
}
23+
24+
func dataSourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
25+
client := meta.(*gitlab.Client)
26+
27+
log.Printf("[INFO] Reading Gitlab project")
28+
29+
v, _ := d.GetOk("id")
30+
31+
found, _, err := client.Projects.GetProject(v)
32+
if err != nil {
33+
return err
34+
}
35+
36+
d.SetId(fmt.Sprintf("%d", found.ID))
37+
d.Set("name", found.Name)
38+
d.Set("path", found.Path)
39+
d.Set("description", found.Description)
40+
d.Set("default_branch", found.DefaultBranch)
41+
d.Set("issues_enabled", found.IssuesEnabled)
42+
d.Set("merge_requests_enabled", found.MergeRequestsEnabled)
43+
d.Set("wiki_enabled", found.WikiEnabled)
44+
d.Set("snippets_enabled", found.SnippetsEnabled)
45+
d.Set("visibility_level", string(found.Visibility))
46+
d.Set("namespace_id", found.Namespace.ID)
47+
d.Set("ssh_url_to_repo", found.SSHURLToRepo)
48+
d.Set("http_url_to_repo", found.HTTPURLToRepo)
49+
d.Set("web_url", found.WebURL)
50+
return nil
51+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
)
11+
12+
func TestAccDataGitlabProject_basic(t *testing.T) {
13+
projectname := fmt.Sprintf("tf-%s", acctest.RandString(5))
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccDataGitlabProjectConfig(projectname),
21+
Check: resource.ComposeTestCheckFunc(
22+
testAccDataSourceGitlabProject("gitlab_project.test", "data.gitlab_project.foo"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccDataSourceGitlabProject(src, n string) resource.TestCheckFunc {
30+
return func(s *terraform.State) error {
31+
32+
project := s.RootModule().Resources[src]
33+
projectResource := project.Primary.Attributes
34+
35+
search := s.RootModule().Resources[n]
36+
searchResource := search.Primary.Attributes
37+
38+
if searchResource["id"] == "" {
39+
return fmt.Errorf("Expected to get a project ID from Gitlab")
40+
}
41+
42+
testAttributes := []string{"id", "Name", "Path", "Visibility", "Description"}
43+
44+
for _, attribute := range testAttributes {
45+
if searchResource[attribute] != projectResource[attribute] {
46+
return fmt.Errorf("Expected the project %s to be: %s, but got: %s", attribute, projectResource[attribute], searchResource[attribute])
47+
}
48+
}
49+
return nil
50+
}
51+
}
52+
53+
func testAccDataGitlabProjectConfig(projectname string) string {
54+
return fmt.Sprintf(`
55+
resource "gitlab_project" "test"{
56+
name = "%s"
57+
path = "%s"
58+
description = "Terraform acceptance tests"
59+
visibility_level = "public"
60+
}
61+
62+
data "gitlab_project" "foo" {
63+
id = "${gitlab_project.test.id}"
64+
}
65+
`, projectname, projectname)
66+
}

gitlab/data_source_gitlab_user.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
gitlab "github.com/xanzy/go-gitlab"
10+
)
11+
12+
func dataSourceGitlabUser() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceGitlabUserRead,
15+
Schema: map[string]*schema.Schema{
16+
"email": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
},
20+
},
21+
}
22+
}
23+
24+
func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
25+
client := meta.(*gitlab.Client)
26+
27+
log.Printf("[INFO] Reading Gitlab user")
28+
29+
searchEmail := strings.ToLower(d.Get("email").(string))
30+
query := &gitlab.ListUsersOptions{
31+
Search: &searchEmail,
32+
}
33+
users, _, err := client.Users.ListUsers(query)
34+
if err != nil {
35+
return err
36+
}
37+
38+
var found *gitlab.User
39+
40+
for _, user := range users {
41+
if strings.ToLower(user.Email) == searchEmail {
42+
found = user
43+
break
44+
}
45+
}
46+
if found == nil {
47+
return fmt.Errorf("The email '%s' does not match any user email", searchEmail)
48+
}
49+
d.SetId(fmt.Sprintf("%d", found.ID))
50+
d.Set("name", found.Name)
51+
d.Set("username", found.Username)
52+
d.Set("email", found.Email)
53+
return nil
54+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
)
11+
12+
func TestAccDataGitlabUser_basic(t *testing.T) {
13+
userEmail := fmt.Sprintf("tf-%s", acctest.RandString(5))
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccDataGitlabUserConfig(userEmail),
21+
Check: resource.ComposeTestCheckFunc(
22+
testAccDataSourceGitlabUser("gitlab_user.foo", "data.gitlab_user.foo"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccDataSourceGitlabUser(src, n string) resource.TestCheckFunc {
30+
return func(s *terraform.State) error {
31+
32+
user := s.RootModule().Resources[src]
33+
userResource := user.Primary.Attributes
34+
35+
search := s.RootModule().Resources[n]
36+
searchResource := search.Primary.Attributes
37+
38+
if searchResource["email"] == "" {
39+
return fmt.Errorf("Expected to get user email from Gitlab")
40+
}
41+
42+
testAttributes := []string{"email"}
43+
44+
for _, attribute := range testAttributes {
45+
if searchResource[attribute] != userResource[attribute] {
46+
return fmt.Errorf("Expected the user %s to be: %s, but got: %s", attribute, userResource[attribute], searchResource[attribute])
47+
}
48+
}
49+
return nil
50+
}
51+
}
52+
53+
func testAccDataGitlabUserConfig(userEmail string) string {
54+
return fmt.Sprintf(`
55+
resource "gitlab_user" "foo" {
56+
name = "foo %s"
57+
username = "listest%s"
58+
password = "test%stt"
59+
email = "listest%[email protected]"
60+
}
61+
62+
data "gitlab_user" "foo" {
63+
email = "${gitlab_user.foo.email}"
64+
}
65+
`, userEmail, userEmail, userEmail, userEmail)
66+
}

gitlab/provider.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,20 @@ func Provider() terraform.ResourceProvider {
4040
Description: descriptions["insecure"],
4141
},
4242
},
43+
44+
DataSourcesMap: map[string]*schema.Resource{
45+
"gitlab_project": dataSourceGitlabProject(),
46+
"gitlab_user": dataSourceGitlabUser(),
47+
},
48+
4349
ResourcesMap: map[string]*schema.Resource{
44-
"gitlab_group": resourceGitlabGroup(),
45-
"gitlab_project": resourceGitlabProject(),
46-
"gitlab_label": resourceGitlabLabel(),
47-
"gitlab_project_hook": resourceGitlabProjectHook(),
48-
"gitlab_deploy_key": resourceGitlabDeployKey(),
49-
"gitlab_user": resourceGitlabUser(),
50+
"gitlab_group": resourceGitlabGroup(),
51+
"gitlab_project": resourceGitlabProject(),
52+
"gitlab_label": resourceGitlabLabel(),
53+
"gitlab_project_hook": resourceGitlabProjectHook(),
54+
"gitlab_deploy_key": resourceGitlabDeployKey(),
55+
"gitlab_user": resourceGitlabUser(),
56+
"gitlab_project_membership": resourceGitlabProjectMembership(),
5057
},
5158

5259
ConfigureFunc: providerConfigure,

0 commit comments

Comments
 (0)