2016-10-19 16:25:12 +02:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-04-26 19:44:05 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/validation"
|
2016-10-19 16:25:12 +02:00
|
|
|
gitlab "github.com/xanzy/go-gitlab"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceGitlabProject() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceGitlabProjectCreate,
|
|
|
|
Read: resourceGitlabProjectRead,
|
|
|
|
Update: resourceGitlabProjectUpdate,
|
|
|
|
Delete: resourceGitlabProjectDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-04-26 19:44:05 +02:00
|
|
|
"name": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"description": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"default_branch": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"issues_enabled": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"merge_requests_enabled": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"wiki_enabled": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"snippets_enabled": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"visibility_level": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2017-04-26 19:44:05 +02:00
|
|
|
ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true),
|
2016-10-19 16:25:12 +02:00
|
|
|
Default: "private",
|
|
|
|
},
|
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
"ssh_url_to_repo": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"http_url_to_repo": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-04-26 19:44:05 +02:00
|
|
|
"web_url": {
|
2016-10-19 16:25:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Project) {
|
2016-10-19 16:25:12 +02:00
|
|
|
d.Set("name", project.Name)
|
|
|
|
d.Set("description", project.Description)
|
|
|
|
d.Set("default_branch", project.DefaultBranch)
|
|
|
|
d.Set("issues_enabled", project.IssuesEnabled)
|
|
|
|
d.Set("merge_requests_enabled", project.MergeRequestsEnabled)
|
|
|
|
d.Set("wiki_enabled", project.WikiEnabled)
|
|
|
|
d.Set("snippets_enabled", project.SnippetsEnabled)
|
|
|
|
d.Set("visibility_level", visibilityLevelToString(project.VisibilityLevel))
|
|
|
|
|
|
|
|
d.Set("ssh_url_to_repo", project.SSHURLToRepo)
|
|
|
|
d.Set("http_url_to_repo", project.HTTPURLToRepo)
|
|
|
|
d.Set("web_url", project.WebURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*gitlab.Client)
|
|
|
|
options := &gitlab.CreateProjectOptions{
|
2017-04-26 19:44:05 +02:00
|
|
|
Name: gitlab.String(d.Get("name").(string)),
|
|
|
|
IssuesEnabled: gitlab.Bool(d.Get("issues_enabled").(bool)),
|
|
|
|
MergeRequestsEnabled: gitlab.Bool(d.Get("merge_requests_enabled").(bool)),
|
|
|
|
WikiEnabled: gitlab.Bool(d.Get("wiki_enabled").(bool)),
|
|
|
|
SnippetsEnabled: gitlab.Bool(d.Get("snippets_enabled").(bool)),
|
2016-10-19 16:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 project %q", options.Name)
|
|
|
|
|
|
|
|
project, _, err := client.Projects.CreateProject(options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(fmt.Sprintf("%d", project.ID))
|
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
return resourceGitlabProjectRead(d, meta)
|
2016-10-19 16:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*gitlab.Client)
|
|
|
|
log.Printf("[DEBUG] read gitlab project %s", d.Id())
|
|
|
|
|
|
|
|
project, response, err := client.Projects.GetProject(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
if response.StatusCode == 404 {
|
|
|
|
log.Printf("[WARN] removing project %s from state because it no longer exists in gitlab", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
resourceGitlabProjectSetToState(d, project)
|
2016-10-19 16:25:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*gitlab.Client)
|
|
|
|
|
|
|
|
options := &gitlab.EditProjectOptions{}
|
|
|
|
|
|
|
|
if d.HasChange("name") {
|
|
|
|
options.Name = gitlab.String(d.Get("name").(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("description") {
|
|
|
|
options.Description = gitlab.String(d.Get("description").(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("default_branch") {
|
|
|
|
options.DefaultBranch = gitlab.String(d.Get("description").(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("visibility_level") {
|
|
|
|
options.VisibilityLevel = stringToVisibilityLevel(d.Get("visibility_level").(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("issues_enabled") {
|
|
|
|
options.IssuesEnabled = gitlab.Bool(d.Get("issues_enabled").(bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("merge_requests_enabled") {
|
|
|
|
options.MergeRequestsEnabled = gitlab.Bool(d.Get("merge_requests_enabled").(bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("wiki_enabled") {
|
|
|
|
options.WikiEnabled = gitlab.Bool(d.Get("wiki_enabled").(bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("snippets_enabled") {
|
|
|
|
options.SnippetsEnabled = gitlab.Bool(d.Get("snippets_enabled").(bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] update gitlab project %s", d.Id())
|
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
_, _, err := client.Projects.EditProject(d.Id(), options)
|
2016-10-19 16:25:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 19:44:05 +02:00
|
|
|
return resourceGitlabProjectRead(d, meta)
|
2016-10-19 16:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGitlabProjectDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*gitlab.Client)
|
2017-04-26 19:44:05 +02:00
|
|
|
log.Printf("[DEBUG] Delete gitlab project %s", d.Id())
|
2016-10-19 16:25:12 +02:00
|
|
|
|
|
|
|
_, err := client.Projects.DeleteProject(d.Id())
|
|
|
|
return err
|
|
|
|
}
|