Skip to content

Commit d569375

Browse files
shelley.besscehoffman
authored andcommitted
Modified Project to search by project id
Signed-off-by: shelley.bess <[email protected]>
1 parent 905ba5f commit d569375

File tree

2 files changed

+6
-24
lines changed

2 files changed

+6
-24
lines changed

gitlab/data_source_gitlab_project.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package gitlab
33
import (
44
"fmt"
55
"log"
6-
"strings"
76

87
"github.com/hashicorp/terraform/helper/schema"
98
"github.com/xanzy/go-gitlab"
@@ -14,43 +13,26 @@ func dataSourceGitlabProject() *schema.Resource {
1413
Read: dataSourceGitlabProjectRead,
1514

1615
Schema: map[string]*schema.Schema{
17-
"name": {
18-
Type: schema.TypeString,
16+
"id": {
17+
Type: schema.TypeInt,
1918
Required: true,
2019
},
2120
},
2221
}
2322
}
2423

25-
// Performs the lookup
2624
func dataSourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
2725
client := meta.(*gitlab.Client)
2826

2927
log.Printf("[INFO] Reading Gitlab project")
3028

31-
searchName := strings.ToLower(d.Get("name").(string))
29+
v, _ := d.GetOk("id")
3230

33-
o := &gitlab.ListProjectsOptions{
34-
Search: &searchName,
35-
}
36-
37-
projects, _, err := client.Projects.ListProjects(o)
31+
found, _, err := client.Projects.GetProject(v)
3832
if err != nil {
3933
return err
4034
}
4135

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-
}
5436
d.SetId(fmt.Sprintf("%d", found.ID))
5537
d.Set("name", found.Name)
5638
d.Set("path", found.Path)

gitlab/data_source_gitlab_project_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func testAccDataSourceGitlabProject(src, n string) resource.TestCheckFunc {
3939
return fmt.Errorf("Expected to get a project ID from Gitlab")
4040
}
4141

42-
testAttributes := []string{"id", "name", "visibility"}
42+
testAttributes := []string{"id", "Name", "Path", "Visibility", "Description"}
4343

4444
for _, attribute := range testAttributes {
4545
if searchResource[attribute] != projectResource[attribute] {
@@ -60,7 +60,7 @@ resource "gitlab_project" "test"{
6060
}
6161
6262
data "gitlab_project" "foo" {
63-
name = "${gitlab_project.test.name}"
63+
id = "${gitlab_project.test.id}"
6464
}
6565
`, projectname, projectname)
6666
}

0 commit comments

Comments
 (0)