From 5dd94a6f6d6b6c60917225f09cb7fd7f7b7d45a9 Mon Sep 17 00:00:00 2001 From: Matej Velikonja Date: Tue, 7 May 2019 08:36:55 +0200 Subject: [PATCH 1/6] add some computed fields to gitlab group resource --- gitlab/resource_gitlab_group.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gitlab/resource_gitlab_group.go b/gitlab/resource_gitlab_group.go index 8f713c47f..32cab39e2 100644 --- a/gitlab/resource_gitlab_group.go +++ b/gitlab/resource_gitlab_group.go @@ -30,6 +30,18 @@ func resourceGitlabGroup() *schema.Resource { Type: schema.TypeString, Required: true, }, + "full_path": { + Type: schema.TypeString, + Computed: true, + }, + "full_name": { + Type: schema.TypeString, + Computed: true, + }, + "web_url": { + Type: schema.TypeString, + Computed: true, + }, "description": { Type: schema.TypeString, Optional: true, @@ -108,6 +120,9 @@ func resourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error { d.SetId(fmt.Sprintf("%d", group.ID)) d.Set("name", group.Name) d.Set("path", group.Path) + d.Set("full_path", group.FullPath) + d.Set("full_name", group.FullName) + d.Set("web_url", group.WebURL) d.Set("description", group.Description) d.Set("lfs_enabled", group.LFSEnabled) d.Set("request_access_enabled", group.RequestAccessEnabled) From c78677cf59455b0fd1944698992b428ee05ee6e0 Mon Sep 17 00:00:00 2001 From: Matej Velikonja Date: Tue, 7 May 2019 08:40:44 +0200 Subject: [PATCH 2/6] update gitlab group resource documentation --- website/docs/r/group.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/website/docs/r/group.html.markdown b/website/docs/r/group.html.markdown index 630fab876..0c6505ac5 100644 --- a/website/docs/r/group.html.markdown +++ b/website/docs/r/group.html.markdown @@ -56,6 +56,12 @@ The resource exports the following attributes: * `id` - The unique id assigned to the group by the GitLab server. Serves as a namespace id where one is needed. + +* `full_path` - The full path of the group. + +* `full_name` - The full name of the group. + +* `web_url` - Web URL of the group. ## Importing groups From 5a70aa43e5579786f27e00cea504a69a40057cca Mon Sep 17 00:00:00 2001 From: Matej Velikonja Date: Tue, 7 May 2019 08:46:08 +0200 Subject: [PATCH 3/6] add gitlab group data resource --- gitlab/data_source_gitlab_group.go | 112 ++++++++++++++++++++++++ gitlab/data_source_gitlab_group_test.go | 111 +++++++++++++++++++++++ gitlab/provider.go | 1 + 3 files changed, 224 insertions(+) create mode 100644 gitlab/data_source_gitlab_group.go create mode 100644 gitlab/data_source_gitlab_group_test.go diff --git a/gitlab/data_source_gitlab_group.go b/gitlab/data_source_gitlab_group.go new file mode 100644 index 000000000..7efc3c0ff --- /dev/null +++ b/gitlab/data_source_gitlab_group.go @@ -0,0 +1,112 @@ +package gitlab + +import ( + "fmt" + "github.com/hashicorp/terraform/helper/schema" + "github.com/xanzy/go-gitlab" + "log" +) + +func dataSourceGitlabGroup() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGitlabGroupRead, + Schema: map[string]*schema.Schema{ + "group_id": { + Type: schema.TypeInt, + Computed: true, + Optional: true, + ConflictsWith: []string{ + "full_path", + }, + }, + "full_path": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ConflictsWith: []string{ + "name", + }, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "full_name": { + Type: schema.TypeString, + Computed: true, + }, + "web_url": { + Type: schema.TypeString, + Computed: true, + }, + "path": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "lfs_enabled": { + Type: schema.TypeBool, + Computed: true, + }, + "request_access_enabled": { + Type: schema.TypeBool, + Computed: true, + }, + "visibility_level": { + Type: schema.TypeString, + Computed: true, + }, + "parent_id": { + Type: schema.TypeInt, + Computed: true, + }, + }, + } +} + +func dataSourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*gitlab.Client) + + var group *gitlab.Group + var err error + + log.Printf("[INFO] Reading Gitlab group") + + groupIDData, groupIDOk := d.GetOk("group_id") + fullPathData, fullPathOk := d.GetOk("full_path") + + if groupIDOk { + // Get group by id + group, _, err = client.Groups.GetGroup(groupIDData.(int)) + if err != nil { + return err + } + } else if fullPathOk { + // Get group by full path + group, _, err = client.Groups.GetGroup(fullPathData.(string)) + if err != nil { + return err + } + } else { + return fmt.Errorf("one and only one of group_id or full_path must be set") + } + + d.Set("group_id", group.ID) + d.Set("full_path", group.FullPath) + d.Set("name", group.Name) + d.Set("full_name", group.FullName) + d.Set("web_url", group.WebURL) + d.Set("path", group.Path) + d.Set("description", group.Description) + d.Set("lfs_enabled", group.LFSEnabled) + d.Set("request_access_enabled", group.RequestAccessEnabled) + d.Set("visibility_level", group.Visibility) + d.Set("parent_id", group.ParentID) + + d.SetId(fmt.Sprintf("%d", group.ID)) + + return nil +} diff --git a/gitlab/data_source_gitlab_group_test.go b/gitlab/data_source_gitlab_group_test.go new file mode 100644 index 000000000..b9f8a6e7e --- /dev/null +++ b/gitlab/data_source_gitlab_group_test.go @@ -0,0 +1,111 @@ +package gitlab + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceGitlabGroup_basic(t *testing.T) { + rString := fmt.Sprintf("%s", acctest.RandString(5)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + // Get group using its ID + { + Config: testAccDataGitlabGroupByID(rString), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceGitlabGroup("gitlab_group.foo", "data.gitlab_group.foo"), + ), + }, + // Get group using its full path + { + Config: testAccDataGitlabGroupByFullPath(rString), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceGitlabGroup("gitlab_group.sub_foo", "data.gitlab_group.sub_foo"), + ), + }, + }, + }) +} + +func testAccDataSourceGitlabGroup(src, n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + + group := s.RootModule().Resources[src] + groupResource := group.Primary.Attributes + + search := s.RootModule().Resources[n] + searchResource := search.Primary.Attributes + + testAttributes := []string{ + "id", + "full_path", + "name", + "full_name", + "web_url", + "path", + "description", + "lfs_enabled", + "request_access_enabled", + "visibility_level", + "parent_id", + } + + for _, attribute := range testAttributes { + if searchResource[attribute] != groupResource[attribute] { + return fmt.Errorf("expected group's parameter `%s` to be: %s, but got: `%s`", attribute, groupResource[attribute], searchResource[attribute]) + } + } + + return nil + } +} + +func testAccDataGitlabGroupByID(rString string) string { + return fmt.Sprintf(` +%s + +data "gitlab_group" "foo" { + group_id = "${gitlab_group.foo.id}" +} +`, testAccDataGitlabGroupSetup(rString)) +} + +func testAccDataGitlabGroupByFullPath(rString string) string { + return fmt.Sprintf(` +%s + +data "gitlab_group" "sub_foo" { + full_path = "${gitlab_group.foo.path}/${gitlab_group.sub_foo.path}" +} +`, testAccDataGitlabGroupSetup(rString)) +} + +func testAccDataGitlabGroupSetup(rString string) string { + return fmt.Sprintf(` +resource "gitlab_group" "foo" { + name = "foo-name-%[1]s" + path = "foo-path-%[1]s" + + # So that acceptance tests can be run in a gitlab organization + # with no billing + visibility_level = "public" +} + +resource "gitlab_group" "sub_foo" { + name = "sub-foo-name-%[1]s" + path = "sub-foo-path-%[1]s" + parent_id = "${gitlab_group.foo.id}" + + # So that acceptance tests can be run in a gitlab organization + # with no billing + visibility_level = "public" +} + `, rString) +} diff --git a/gitlab/provider.go b/gitlab/provider.go index 786cc955c..f80a53786 100644 --- a/gitlab/provider.go +++ b/gitlab/provider.go @@ -42,6 +42,7 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ + "gitlab_group": dataSourceGitlabGroup(), "gitlab_project": dataSourceGitlabProject(), "gitlab_user": dataSourceGitlabUser(), "gitlab_users": dataSourceGitlabUsers(), From 435ba8d35d65557984193f1d0dc9088110bcdb98 Mon Sep 17 00:00:00 2001 From: Matej Velikonja Date: Tue, 7 May 2019 09:16:04 +0200 Subject: [PATCH 4/6] add documentation for the gitlab group data resource --- website/docs/d/group.html.markdown | 67 ++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 website/docs/d/group.html.markdown diff --git a/website/docs/d/group.html.markdown b/website/docs/d/group.html.markdown new file mode 100644 index 000000000..c38ded112 --- /dev/null +++ b/website/docs/d/group.html.markdown @@ -0,0 +1,67 @@ +--- +layout: "gitlab" +page_title: "GitLab: gitlab_group" +sidebar_current: "docs-gitlab-data-source-group" +description: |- + Looks up a gitlab group +--- + +# gitlab\_group + +Provides details about a specific group in the gitlab provider. + +## Example Usage + +**By group's ID** + +```hcl +data "gitlab_group" "foo" { + group_id = 123 +} +``` + +**By group's full path** + +```hcl +data "gitlab_group" "foo" { + full_path = "foo/bar" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `group_id` - (Optional) The ID of the group. + +* `full_path` - (Optional) The full path of the group. + +**Note**: exactly one of group_id or full_path must be provided. + +## Attributes Reference + +The resource exports the following attributes: + +* `id` - The unique ID assigned to the group. + +* `name` - The name of this group. + +* `path` - The path of the group. + +* `description` - The description of the group. + +* `lfs_enabled` - Boolean, is LFS enabled for projects in this group. + +* `request_access_enabled` - Boolean, is request for access enabled to the group. + +* `visibility_level` - Visibility level of the group. Possible values are `private`, `internal`, `public`. + +* `parent_id` - Integer, ID of the parent group. + +* `full_path` - The full path of the group. + +* `full_name` - The full name of the group. + +* `web_url` - Web URL of the group. + +[doc]: https://docs.gitlab.com/ee/api/groups.html#details-of-a-group From 94360d73dea2136d213150a070bef1aac8db9c94 Mon Sep 17 00:00:00 2001 From: Matej Velikonja Date: Tue, 14 May 2019 16:16:28 +0200 Subject: [PATCH 5/6] fix wrong conflicts with --- gitlab/data_source_gitlab_group.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitlab/data_source_gitlab_group.go b/gitlab/data_source_gitlab_group.go index 7efc3c0ff..bd07f0c98 100644 --- a/gitlab/data_source_gitlab_group.go +++ b/gitlab/data_source_gitlab_group.go @@ -24,7 +24,7 @@ func dataSourceGitlabGroup() *schema.Resource { Computed: true, Optional: true, ConflictsWith: []string{ - "name", + "group_id", }, }, "name": { From 8705fed8d21f72b1d3b2c8fb0f81ac137a76ecd2 Mon Sep 17 00:00:00 2001 From: Matej Velikonja Date: Tue, 14 May 2019 16:17:57 +0200 Subject: [PATCH 6/6] add gitlab_group data resource to side menu --- website/gitlab.erb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/gitlab.erb b/website/gitlab.erb index 6589806b7..76f62aa91 100644 --- a/website/gitlab.erb +++ b/website/gitlab.erb @@ -13,6 +13,9 @@ > Data Sources