Skip to content

Commit

Permalink
provider/gitlab: Add gitlab_group resource
Browse files Browse the repository at this point in the history
This adds a gitlab_group resource.

This combined with hashicorp#14483 should allow you to create projects in a
group (untested)

The implementation of this is a little ugly as go-gitlab doesn't have a
function already present for EditGroup, so we hack it into being as part
of the implementation of  resourceGitlabGroupUpdate
  • Loading branch information
richardc committed May 15, 2017
1 parent 994284f commit dd436aa
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin/providers/gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Provider() terraform.ResourceProvider {
},
},
ResourcesMap: map[string]*schema.Resource{
"gitlab_group": resourceGitlabGroup(),
"gitlab_project": resourceGitlabProject(),
"gitlab_project_hook": resourceGitlabProjectHook(),
},
Expand Down
140 changes: 140 additions & 0 deletions builtin/providers/gitlab/resource_gitlab_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package gitlab

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
gitlab "github.com/xanzy/go-gitlab"
)

func resourceGitlabGroup() *schema.Resource {
return &schema.Resource{
Create: resourceGitlabGroupCreate,
Read: resourceGitlabGroupRead,
Update: resourceGitlabGroupUpdate,
Delete: resourceGitlabGroupDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"path": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"visibility_level": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true),
Default: "private",
},
},
}
}

func resourceGitlabGroupCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
options := &gitlab.CreateGroupOptions{
Name: gitlab.String(d.Get("name").(string)),
}

if v, ok := d.GetOk("path"); ok {
options.Path = gitlab.String(v.(string))
}

if v, ok := d.GetOk("description"); ok {
options.Description = gitlab.String(v.(string))
}

if v, ok := d.GetOk("visibility_level"); ok {
options.VisibilityLevel = stringToVisibilityLevel(v.(string))
}

log.Printf("[DEBUG] create gitlab group %q", options.Name)

group, _, err := client.Groups.CreateGroup(options)
if err != nil {
return err
}

d.SetId(fmt.Sprintf("%d", group.ID))

return resourceGitlabGroupRead(d, meta)
}

func resourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
log.Printf("[DEBUG] read gitlab group %s", d.Id())

group, response, err := client.Groups.GetGroup(d.Id())
if err != nil {
if response.StatusCode == 404 {
log.Printf("[WARN] removing group %s from state because it no longer exists in gitlab", d.Id())
d.SetId("")
return nil
}

return err
}

d.Set("name", group.Name)
d.Set("path", group.Path)
d.Set("description", group.Description)

return nil
}

func resourceGitlabGroupUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)

// XXX This should be an EditGroupOptions, but gitlab has an omission
options := &gitlab.CreateGroupOptions{}

if d.HasChange("name") {
options.Name = gitlab.String(d.Get("name").(string))
}

if d.HasChange("path") {
options.Description = gitlab.String(d.Get("description").(string))
}

if d.HasChange("description") {
options.Description = gitlab.String(d.Get("description").(string))
}

if d.HasChange("visibility_level") {
options.VisibilityLevel = stringToVisibilityLevel(d.Get("visibility_level").(string))
}

log.Printf("[DEBUG] update gitlab group %s", d.Id())

// TODO(richardc): get this added to go-gitlab as EditGroup
path := fmt.Sprintf("groups/%s", d.Id())
req, err := client.NewRequest("PUT", path, options, []gitlab.OptionFunc{})
if err != nil {
return err
}

g := new(gitlab.Group)
_, err = client.Do(req, g)
if err != nil {
return err
}

return resourceGitlabGroupRead(d, meta)
}

func resourceGitlabGroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
log.Printf("[DEBUG] Delete gitlab group %s", d.Id())

_, err := client.Groups.DeleteGroup(d.Id())
return err
}

0 comments on commit dd436aa

Please sign in to comment.