diff --git a/builtin/providers/gitlab/provider.go b/builtin/providers/gitlab/provider.go index f4389d694..4dfe07f6c 100644 --- a/builtin/providers/gitlab/provider.go +++ b/builtin/providers/gitlab/provider.go @@ -11,13 +11,13 @@ func Provider() terraform.ResourceProvider { // The actual provider return &schema.Provider{ Schema: map[string]*schema.Schema{ - "token": &schema.Schema{ + "token": { Type: schema.TypeString, Required: true, DefaultFunc: schema.EnvDefaultFunc("GITLAB_TOKEN", nil), Description: descriptions["token"], }, - "base_url": &schema.Schema{ + "base_url": { Type: schema.TypeString, Optional: true, DefaultFunc: schema.EnvDefaultFunc("GITLAB_BASE_URL", ""), diff --git a/builtin/providers/gitlab/resource_gitlab_project.go b/builtin/providers/gitlab/resource_gitlab_project.go index b4824f36a..e76c2b1ad 100644 --- a/builtin/providers/gitlab/resource_gitlab_project.go +++ b/builtin/providers/gitlab/resource_gitlab_project.go @@ -5,6 +5,7 @@ import ( "log" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" gitlab "github.com/xanzy/go-gitlab" ) @@ -16,54 +17,54 @@ func resourceGitlabProject() *schema.Resource { Delete: resourceGitlabProjectDelete, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, }, - "description": &schema.Schema{ + "description": { Type: schema.TypeString, Optional: true, }, - "default_branch": &schema.Schema{ + "default_branch": { Type: schema.TypeString, Optional: true, }, - "issues_enabled": &schema.Schema{ + "issues_enabled": { Type: schema.TypeBool, Optional: true, Default: true, }, - "merge_requests_enabled": &schema.Schema{ + "merge_requests_enabled": { Type: schema.TypeBool, Optional: true, Default: true, }, - "wiki_enabled": &schema.Schema{ + "wiki_enabled": { Type: schema.TypeBool, Optional: true, Default: true, }, - "snippets_enabled": &schema.Schema{ + "snippets_enabled": { Type: schema.TypeBool, Optional: true, Default: true, }, - "visibility_level": &schema.Schema{ + "visibility_level": { Type: schema.TypeString, Optional: true, - ValidateFunc: validateValueFunc([]string{"private", "internal", "public"}), + ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true), Default: "private", }, - "ssh_url_to_repo": &schema.Schema{ + "ssh_url_to_repo": { Type: schema.TypeString, Computed: true, }, - "http_url_to_repo": &schema.Schema{ + "http_url_to_repo": { Type: schema.TypeString, Computed: true, }, - "web_url": &schema.Schema{ + "web_url": { Type: schema.TypeString, Computed: true, }, @@ -71,7 +72,7 @@ func resourceGitlabProject() *schema.Resource { } } -func resourceGitlabProjectUpdateFromAPI(d *schema.ResourceData, project *gitlab.Project) { +func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Project) { d.Set("name", project.Name) d.Set("description", project.Description) d.Set("default_branch", project.DefaultBranch) @@ -89,29 +90,17 @@ func resourceGitlabProjectUpdateFromAPI(d *schema.ResourceData, project *gitlab. func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*gitlab.Client) options := &gitlab.CreateProjectOptions{ - Name: gitlab.String(d.Get("name").(string)), + 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)), } if v, ok := d.GetOk("description"); ok { options.Description = gitlab.String(v.(string)) } - if v, ok := d.GetOk("issues_enabled"); ok { - options.IssuesEnabled = gitlab.Bool(v.(bool)) - } - - if v, ok := d.GetOk("merge_requests_enabled"); ok { - options.MergeRequestsEnabled = gitlab.Bool(v.(bool)) - } - - if v, ok := d.GetOk("wiki_enabled"); ok { - options.WikiEnabled = gitlab.Bool(v.(bool)) - } - - if v, ok := d.GetOk("snippets_enabled"); ok { - options.SnippetsEnabled = gitlab.Bool(v.(bool)) - } - if v, ok := d.GetOk("visibility_level"); ok { options.VisibilityLevel = stringToVisibilityLevel(v.(string)) } @@ -125,9 +114,7 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error d.SetId(fmt.Sprintf("%d", project.ID)) - resourceGitlabProjectUpdateFromAPI(d, project) - - return nil + return resourceGitlabProjectRead(d, meta) } func resourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error { @@ -145,7 +132,7 @@ func resourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error { return err } - resourceGitlabProjectUpdateFromAPI(d, project) + resourceGitlabProjectSetToState(d, project) return nil } @@ -188,19 +175,17 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error log.Printf("[DEBUG] update gitlab project %s", d.Id()) - project, _, err := client.Projects.EditProject(d.Id(), options) + _, _, err := client.Projects.EditProject(d.Id(), options) if err != nil { return err } - resourceGitlabProjectUpdateFromAPI(d, project) - - return nil + return resourceGitlabProjectRead(d, meta) } func resourceGitlabProjectDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*gitlab.Client) - log.Printf("[DEBUG] update gitlab project %s", d.Id()) + log.Printf("[DEBUG] Delete gitlab project %s", d.Id()) _, err := client.Projects.DeleteProject(d.Id()) return err diff --git a/builtin/providers/gitlab/resource_gitlab_project_test.go b/builtin/providers/gitlab/resource_gitlab_project_test.go index d4b53e476..52083b0be 100644 --- a/builtin/providers/gitlab/resource_gitlab_project_test.go +++ b/builtin/providers/gitlab/resource_gitlab_project_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "github.com/xanzy/go-gitlab" @@ -11,6 +12,7 @@ import ( func TestAccGitlabProject_basic(t *testing.T) { var project gitlab.Project + rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -18,12 +20,12 @@ func TestAccGitlabProject_basic(t *testing.T) { CheckDestroy: testAccCheckGitlabProjectDestroy, Steps: []resource.TestStep{ // Create a project with all the features on - resource.TestStep{ - Config: testAccGitlabProjectConfig, + { + Config: testAccGitlabProjectConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckGitlabProjectExists("gitlab_project.foo", &project), testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{ - Name: "foo", + Name: fmt.Sprintf("foo-%d", rInt), Description: "Terraform acceptance tests", IssuesEnabled: true, MergeRequestsEnabled: true, @@ -34,24 +36,24 @@ func TestAccGitlabProject_basic(t *testing.T) { ), }, // Update the project to turn the features off - resource.TestStep{ - Config: testAccGitlabProjectUpdateConfig, + { + Config: testAccGitlabProjectUpdateConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckGitlabProjectExists("gitlab_project.foo", &project), testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{ - Name: "foo", + Name: fmt.Sprintf("foo-%d", rInt), Description: "Terraform acceptance tests!", VisibilityLevel: 20, }), ), }, - // Update the project to turn the features on again - resource.TestStep{ - Config: testAccGitlabProjectConfig, + //Update the project to turn the features on again + { + Config: testAccGitlabProjectConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckGitlabProjectExists("gitlab_project.foo", &project), testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{ - Name: "foo", + Name: fmt.Sprintf("foo-%d", rInt), Description: "Terraform acceptance tests", IssuesEnabled: true, MergeRequestsEnabled: true, @@ -157,20 +159,23 @@ func testAccCheckGitlabProjectDestroy(s *terraform.State) error { return nil } -const testAccGitlabProjectConfig = ` +func testAccGitlabProjectConfig(rInt int) string { + return fmt.Sprintf(` resource "gitlab_project" "foo" { - name = "foo" + name = "foo-%d" description = "Terraform acceptance tests" # So that acceptance tests can be run in a gitlab organization # with no billing visibility_level = "public" } -` + `, rInt) +} -const testAccGitlabProjectUpdateConfig = ` +func testAccGitlabProjectUpdateConfig(rInt int) string { + return fmt.Sprintf(` resource "gitlab_project" "foo" { - name = "foo" + name = "foo-%d" description = "Terraform acceptance tests!" # So that acceptance tests can be run in a gitlab organization @@ -182,4 +187,5 @@ resource "gitlab_project" "foo" { wiki_enabled = false snippets_enabled = false } -` + `, rInt) +} diff --git a/website/source/docs/providers/gitlab/index.html.markdown b/website/source/docs/providers/gitlab/index.html.markdown index cf6fd431a..ed0831f5b 100644 --- a/website/source/docs/providers/gitlab/index.html.markdown +++ b/website/source/docs/providers/gitlab/index.html.markdown @@ -17,7 +17,7 @@ Use the navigation to the left to read about the available resources. ## Example Usage -``` +```hcl # Configure the GitLab Provider provider "gitlab" { token = "${var.github_token}" diff --git a/website/source/docs/providers/gitlab/r/project.html.markdown b/website/source/docs/providers/gitlab/r/project.html.markdown index 4d807c960..9a1eae738 100644 --- a/website/source/docs/providers/gitlab/r/project.html.markdown +++ b/website/source/docs/providers/gitlab/r/project.html.markdown @@ -14,7 +14,7 @@ GitLab organization. ## Example Usage -``` +```hcl resource "gitlab_repository" "example" { name = "example" description = "My awesome codebase" diff --git a/website/source/layouts/docs.erb b/website/source/layouts/docs.erb index 5ffc5e230..87d5b9dc7 100644 --- a/website/source/layouts/docs.erb +++ b/website/source/layouts/docs.erb @@ -272,6 +272,10 @@ GitHub + > + Gitlab + + > Google Cloud diff --git a/website/source/layouts/gitlab.erb b/website/source/layouts/gitlab.erb new file mode 100644 index 000000000..25bf6e530 --- /dev/null +++ b/website/source/layouts/gitlab.erb @@ -0,0 +1,26 @@ +<% wrap_layout :inner do %> + <% content_for :sidebar do %> + + <% end %> + + <%= yield %> + <% end %>