forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/gitlab: Add
gitlab_group
resource
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
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |