Skip to content

Commit 25ebbc3

Browse files
shelley.besscehoffman
authored andcommitted
Project data source and test
Signed-off-by: shelley.bess <[email protected]>
1 parent 803978b commit 25ebbc3

File tree

5 files changed

+260
-0
lines changed

5 files changed

+260
-0
lines changed

gitlab/data_source_gitlab_project.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
"github.com/xanzy/go-gitlab"
10+
)
11+
12+
func dataSourceGitlabProject() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceGitlabProjectRead,
15+
16+
Schema: map[string]*schema.Schema{
17+
"name": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
},
21+
},
22+
}
23+
}
24+
25+
// Performs the lookup
26+
func dataSourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
27+
client := meta.(*gitlab.Client)
28+
29+
log.Printf("[INFO] Reading Gitlab project")
30+
31+
searchName := strings.ToLower(d.Get("name").(string))
32+
33+
o := &gitlab.ListProjectsOptions{
34+
Search: &searchName,
35+
}
36+
37+
projects, _, err := client.Projects.ListProjects(o)
38+
if err != nil {
39+
return err
40+
}
41+
42+
var found *gitlab.Project
43+
44+
for _, project := range projects {
45+
if strings.ToLower(project.Name) == searchName {
46+
found = project
47+
break
48+
}
49+
}
50+
51+
if found == nil {
52+
return fmt.Errorf("Unable to locate any project with the name: %s", searchName)
53+
}
54+
d.SetId(fmt.Sprintf("%d", found.ID))
55+
d.Set("name", found.Name)
56+
d.Set("path", found.Path)
57+
d.Set("description", found.Description)
58+
d.Set("default_branch", found.DefaultBranch)
59+
d.Set("issues_enabled", found.IssuesEnabled)
60+
d.Set("merge_requests_enabled", found.MergeRequestsEnabled)
61+
d.Set("wiki_enabled", found.WikiEnabled)
62+
d.Set("snippets_enabled", found.SnippetsEnabled)
63+
d.Set("visibility_level", string(found.Visibility))
64+
d.Set("namespace_id", found.Namespace.ID)
65+
d.Set("ssh_url_to_repo", found.SSHURLToRepo)
66+
d.Set("http_url_to_repo", found.HTTPURLToRepo)
67+
d.Set("web_url", found.WebURL)
68+
return nil
69+
}
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", "visibility"}
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+
name = "${gitlab_project.test.name}"
64+
}
65+
`, projectname, projectname)
66+
}

gitlab/data_source_gitlab_user.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
gitlab "github.com/xanzy/go-gitlab"
10+
)
11+
12+
// Search by email required
13+
func dataSourceGitlabUser() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceGitlabUserRead,
16+
Schema: map[string]*schema.Schema{
17+
//Search option
18+
"email": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
},
22+
},
23+
}
24+
}
25+
26+
// Performs the lookup
27+
func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
28+
client := meta.(*gitlab.Client)
29+
// Create the query, grab the email for the query and set it for use
30+
var query *gitlab.ListUsersOptions
31+
email := strings.ToLower(d.Get("email").(string))
32+
*query.Search = email
33+
// Query to find the email. Returns a list
34+
users, _, err := client.Users.ListUsers(query)
35+
if err != nil {
36+
return err
37+
}
38+
39+
// Create a user to save userdata to
40+
var user *gitlab.User
41+
// Grab User data out of list
42+
for _, a := range users {
43+
if a.Email == email {
44+
user = a
45+
break
46+
}
47+
}
48+
if user == nil {
49+
return fmt.Errorf("The email '%s' does not match any user email", email)
50+
}
51+
d.SetId(strconv.Itoa(user.ID))
52+
d.Set("name", user.Name)
53+
d.Set("username", user.Username)
54+
d.Set("email", user.Email)
55+
return nil
56+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
srcR := s.RootModule().Resources[src]
33+
srcA := srcR.Primary.Attributes
34+
35+
r := s.RootModule().Resources[n]
36+
a := r.Primary.Attributes
37+
38+
if a["id"] == "" {
39+
return fmt.Errorf("Expected to get a user email from Gitlab")
40+
}
41+
42+
testAtts := []string{"id", "email"}
43+
44+
for _, att := range testAtts {
45+
if a[att] != srcA[att] {
46+
return fmt.Errorf("Expected the user %s to be: %s, but got: %s", att, srcA[att], a[att])
47+
}
48+
}
49+
return nil
50+
}
51+
}
52+
53+
func testAccDataGitlabUserConfig(userEmail string) string {
54+
return fmt.Sprintf(`
55+
resource "gitlab_user" "foo"{
56+
email = "%s"
57+
}
58+
59+
data "gitlab_user" "foo" {
60+
name = "${gitlab_user.foo.email}"
61+
}
62+
`, userEmail)
63+
}

gitlab/provider.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ 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{
4450
"gitlab_group": resourceGitlabGroup(),
4551
"gitlab_project": resourceGitlabProject(),

0 commit comments

Comments
 (0)