Skip to content

Commit

Permalink
Adding support for project templates
Browse files Browse the repository at this point in the history
* template_name
* use_custom_template
* group_with_project_templates_id
  • Loading branch information
ahmet2mir committed Mar 12, 2020
1 parent f5149ec commit a204d8b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
Binary file removed JulienPivotto.gitlab-license.enc
Binary file not shown.
27 changes: 27 additions & 0 deletions gitlab/resource_gitlab_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"template_name": {
Type: schema.TypeString,
Optional: true,
},
"use_custom_template": {
Type: schema.TypeBool,
Optional: true,
},
"group_with_project_templates_id": {
Type: schema.TypeInt,
Optional: true,
},
}

func resourceGitlabProject() *schema.Resource {
Expand Down Expand Up @@ -302,6 +314,21 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
setProperties = append(setProperties, "initialize_with_readme")
}

if v, ok := d.GetOk("template_name"); ok {
options.TemplateName = gitlab.String(v.(string))
setProperties = append(setProperties, "template_name")
}

if v, ok := d.GetOk("use_custom_template"); ok {
options.UseCustomTemplate = gitlab.Bool(v.(bool))
setProperties = append(setProperties, "use_custom_template")
}

if v, ok := d.GetOk("group_with_project_templates_id"); ok {
options.GroupWithProjectTemplatesID = gitlab.Int(v.(int))
setProperties = append(setProperties, "group_with_project_templates_id")
}

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

project, _, err := client.Projects.CreateProject(options)
Expand Down
40 changes: 37 additions & 3 deletions gitlab/resource_gitlab_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,29 @@ func TestAccGitlabProject_initializeWithReadme(t *testing.T) {
Config: testAccGitlabProjectConfigInitializeWithReadme(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
testAccCheckGitlabProjectInitializeWithReadme(&project, &testAccGitlabProjectExpectedAttributes{
testAccCheckGitlabProjectDefaultBranch(&project, &testAccGitlabProjectExpectedAttributes{
DefaultBranch: "master",
}),
),
},
},
})
}

func TestAccGitlabProject_templateName(t *testing.T) {
var project gitlab.Project
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccGitlabProjectConfigTemplateName(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
testAccCheckGitlabProjectDefaultBranch(&project, &testAccGitlabProjectExpectedAttributes{
DefaultBranch: "master",
}),
),
Expand Down Expand Up @@ -429,10 +451,10 @@ func testAccCheckAggregateGitlabProject(expected, received *gitlab.Project) reso
return resource.ComposeAggregateTestCheckFunc(checks...)
}

func testAccCheckGitlabProjectInitializeWithReadme(project *gitlab.Project, want *testAccGitlabProjectExpectedAttributes) resource.TestCheckFunc {
func testAccCheckGitlabProjectDefaultBranch(project *gitlab.Project, want *testAccGitlabProjectExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {
if project.DefaultBranch != want.DefaultBranch {
return fmt.Errorf("got description %q; want %q", project.DefaultBranch, want.DefaultBranch)
return fmt.Errorf("got default branch %q; want %q", project.DefaultBranch, want.DefaultBranch)
}

return nil
Expand Down Expand Up @@ -665,3 +687,15 @@ resource "gitlab_project" "foo" {
}
`, rInt, rInt)
}

func testAccGitlabProjectConfigTemplateName(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "foo" {
name = "foo-%d"
path = "foo.%d"
description = "Terraform acceptance tests"
template_name = "spring"
default_branch = "master"
}
`, rInt, rInt)
}
Binary file removed vendor/github.com/ulikunitz/xz/fox.xz
Binary file not shown.
Binary file removed vendor/github.com/ulikunitz/xz/lzma/fox.lzma
Binary file not shown.
6 changes: 6 additions & 0 deletions website/docs/r/project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ The following arguments are supported:

* `initialize_with_readme` - (Optional) Create master branch with first commit containing a README.md file.

* `template_name` - (Optional) When used without use_custom_template, name of a built-in project template. When used with use_custom_template, name of a custom project template (enterprise edition).

* `use_custom_template` - (Optional) Use either custom instance or group (with group_with_project_templates_id) project template (enterprise edition).

* `group_with_project_templates_id` - (Optional) For group-level custom templates, specifies ID of group from which all the custom project templates are sourced. Leave empty for instance-level templates. Requires use_custom_template to be true (enterprise edition).

## Attributes Reference

The following additional attributes are exported:
Expand Down

0 comments on commit a204d8b

Please sign in to comment.