Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gitlab_pipeline_schedule_variable resource #204

Merged
merged 4 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,28 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"gitlab_branch_protection": resourceGitlabBranchProtection(),
"gitlab_tag_protection": resourceGitlabTagProtection(),
"gitlab_group": resourceGitlabGroup(),
"gitlab_project": resourceGitlabProject(),
"gitlab_label": resourceGitlabLabel(),
"gitlab_group_label": resourceGitlabGroupLabel(),
"gitlab_pipeline_schedule": resourceGitlabPipelineSchedule(),
"gitlab_pipeline_trigger": resourceGitlabPipelineTrigger(),
"gitlab_project_hook": resourceGitlabProjectHook(),
"gitlab_project_push_rules": resourceGitlabProjectPushRules(),
"gitlab_deploy_key": resourceGitlabDeployKey(),
"gitlab_deploy_key_enable": resourceGitlabDeployEnableKey(),
"gitlab_user": resourceGitlabUser(),
"gitlab_project_membership": resourceGitlabProjectMembership(),
"gitlab_group_membership": resourceGitlabGroupMembership(),
"gitlab_project_variable": resourceGitlabProjectVariable(),
"gitlab_group_variable": resourceGitlabGroupVariable(),
"gitlab_project_cluster": resourceGitlabProjectCluster(),
"gitlab_service_slack": resourceGitlabServiceSlack(),
"gitlab_service_jira": resourceGitlabServiceJira(),
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
"gitlab_branch_protection": resourceGitlabBranchProtection(),
"gitlab_tag_protection": resourceGitlabTagProtection(),
"gitlab_group": resourceGitlabGroup(),
"gitlab_project": resourceGitlabProject(),
"gitlab_label": resourceGitlabLabel(),
"gitlab_group_label": resourceGitlabGroupLabel(),
"gitlab_pipeline_schedule": resourceGitlabPipelineSchedule(),
"gitlab_pipeline_schedule_variable": resourceGitlabPipelineScheduleVariable(),
"gitlab_pipeline_trigger": resourceGitlabPipelineTrigger(),
"gitlab_project_hook": resourceGitlabProjectHook(),
"gitlab_project_push_rules": resourceGitlabProjectPushRules(),
"gitlab_deploy_key": resourceGitlabDeployKey(),
"gitlab_deploy_key_enable": resourceGitlabDeployEnableKey(),
"gitlab_user": resourceGitlabUser(),
"gitlab_project_membership": resourceGitlabProjectMembership(),
"gitlab_group_membership": resourceGitlabGroupMembership(),
"gitlab_project_variable": resourceGitlabProjectVariable(),
"gitlab_group_variable": resourceGitlabGroupVariable(),
"gitlab_project_cluster": resourceGitlabProjectCluster(),
"gitlab_service_slack": resourceGitlabServiceSlack(),
"gitlab_service_jira": resourceGitlabServiceJira(),
"gitlab_project_share_group": resourceGitlabProjectShareGroup(),
},

ConfigureFunc: providerConfigure,
Expand Down
130 changes: 130 additions & 0 deletions gitlab/resource_gitlab_pipeline_schedule_variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package gitlab

import (
"fmt"
"log"
"strconv"

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

func resourceGitlabPipelineScheduleVariable() *schema.Resource {
return &schema.Resource{
Create: resourceGitlabPipelineScheduleVariableCreate,
Read: resourceGitlabPipelineScheduleVariableRead,
Update: resourceGitlabPipelineScheduleVariableUpdate,
Delete: resourceGitlabPipelineScheduleVariableDelete,

Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"pipeline_schedule_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func resourceGitlabPipelineScheduleVariableCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
project := d.Get("project").(string)
scheduleID := d.Get("pipeline_schedule_id").(int)

options := &gitlab.CreatePipelineScheduleVariableOptions{
Key: gitlab.String(d.Get("key").(string)),
Value: gitlab.String(d.Get("value").(string)),
}

log.Printf("[DEBUG] create gitlab PipelineScheduleVariable %s:%s", *options.Key, *options.Value)

scheduleVar, _, err := client.PipelineSchedules.CreatePipelineScheduleVariable(project, scheduleID, options)
if err != nil {
return err
}

id := strconv.Itoa(scheduleID)
d.SetId(buildTwoPartID(&id, &scheduleVar.Key))

return resourceGitlabPipelineScheduleVariableRead(d, meta)
}

func resourceGitlabPipelineScheduleVariableRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
project := d.Get("project").(string)
scheduleID := d.Get("pipeline_schedule_id").(int)
pipelineVariableKey := d.Get("key").(string)

log.Printf("[DEBUG] read gitlab PipelineSchedule %s/%d", project, scheduleID)

pipelineSchedule, _, err := client.PipelineSchedules.GetPipelineSchedule(project, scheduleID)
if err != nil {
return err
}

found := false
for _, pipelineVariable := range pipelineSchedule.Variables {
if pipelineVariable.Key == pipelineVariableKey {
d.Set("project", project)
d.Set("key", pipelineVariable.Key)
d.Set("value", pipelineVariable.Value)
d.Set("pipeline_schedule_id", scheduleID)
found = true
break
}
}
if !found {
return fmt.Errorf("PipelineScheduleVariable %s can not be found", pipelineVariableKey)
}

return nil
}

func resourceGitlabPipelineScheduleVariableUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
project := d.Get("project").(string)
variableKey := d.Get("key").(string)
scheduleID := d.Get("pipeline_schedule_id").(int)

if d.HasChange("value") {
options := &gitlab.EditPipelineScheduleVariableOptions{
Value: gitlab.String(d.Get("value").(string)),
}

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

_, _, err := client.PipelineSchedules.EditPipelineScheduleVariable(project, scheduleID, variableKey, options)
if err != nil {
return err
}
}

return resourceGitlabPipelineScheduleVariableRead(d, meta)
}

func resourceGitlabPipelineScheduleVariableDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gitlab.Client)
project := d.Get("project").(string)
variableKey := d.Get("key").(string)
scheduleID := d.Get("pipeline_schedule_id").(int)

_, resp, err := client.PipelineSchedules.DeletePipelineScheduleVariable(project, scheduleID, variableKey)
if err != nil {
return fmt.Errorf("%s failed to delete pipeline schedule variable: %s", d.Id(), resp.Status)
}
return err
}
153 changes: 153 additions & 0 deletions gitlab/resource_gitlab_pipeline_schedule_variable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package gitlab

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
gitlab "github.com/xanzy/go-gitlab"
)

func TestAccGitlabPipelineScheduleVariable_basic(t *testing.T) {
var variable gitlab.PipelineVariable
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccGitlabPipelineScheduleVariableConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabPipelineScheduleVariableExists("gitlab_pipeline_schedule_variable.schedule_var", &variable),
testAccCheckGitlabPipelineScheduleVariableAttributes(&variable, &testAccGitlabPipelineScheduleVariableExpectedAttributes{
Key: "TERRAFORMED_TEST_VALUE",
Value: "test",
}),
),
},
{
Config: testAccGitlabPipelineScheduleVariableUpdateConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabPipelineScheduleVariableExists("gitlab_pipeline_schedule_variable.schedule_var", &variable),
testAccCheckGitlabPipelineScheduleVariableAttributes(&variable, &testAccGitlabPipelineScheduleVariableExpectedAttributes{
Key: "TERRAFORMED_TEST_VALUE",
Value: "test_updated",
}),
),
},
{
Config: testAccGitlabPipelineScheduleVariableConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabPipelineScheduleVariableExists("gitlab_pipeline_schedule_variable.schedule_var", &variable),
testAccCheckGitlabPipelineScheduleVariableAttributes(&variable, &testAccGitlabPipelineScheduleVariableExpectedAttributes{
Key: "TERRAFORMED_TEST_VALUE",
Value: "test",
}),
),
},
},
})
}

func testAccCheckGitlabPipelineScheduleVariableExists(n string, variable *gitlab.PipelineVariable) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not Found: %s", n)
}

project := rs.Primary.Attributes["project"]
scheduleID, err := strconv.Atoi(rs.Primary.Attributes["pipeline_schedule_id"])
if err != nil {
return fmt.Errorf("failed to convert PipelineSchedule.ID to int")
}

conn := testAccProvider.Meta().(*gitlab.Client)
pipelineSchedule, _, err := conn.PipelineSchedules.GetPipelineSchedule(project, scheduleID)
if err != nil {
return err
}

for _, pipelineVariable := range pipelineSchedule.Variables {
if pipelineVariable.Key == rs.Primary.Attributes["key"] {
*variable = *pipelineVariable
return nil
}
}
return fmt.Errorf("PipelineScheduleVariable %s does not exist", variable.Key)
}
}

type testAccGitlabPipelineScheduleVariableExpectedAttributes struct {
Key string
Value string
}

func testAccCheckGitlabPipelineScheduleVariableAttributes(variable *gitlab.PipelineVariable, want *testAccGitlabPipelineScheduleVariableExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {
if variable.Key != want.Key {
return fmt.Errorf("got key %q; want %q", variable.Key, want.Key)
}

return nil
}
}

func testAccGitlabPipelineScheduleVariableConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "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"
}

resource "gitlab_pipeline_schedule" "schedule" {
project = "${gitlab_project.foo.id}"
description = "Pipeline Schedule"
ref = "master"
cron = "0 1 * * *"
}

resource "gitlab_pipeline_schedule_variable" "schedule_var" {
project = "${gitlab_project.foo.id}"
pipeline_schedule_id = "${gitlab_pipeline_schedule.schedule.id}"
key = "TERRAFORMED_TEST_VALUE"
value = "test"
}
`, rInt)
}

func testAccGitlabPipelineScheduleVariableUpdateConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_project" "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"
}

resource "gitlab_pipeline_schedule" "schedule" {
project = "${gitlab_project.foo.id}"
description = "Pipeline Schedule"
ref = "master"
cron = "0 1 * * *"
}

resource "gitlab_pipeline_schedule_variable" "schedule_var" {
project = "${gitlab_project.foo.id}"
pipeline_schedule_id = "${gitlab_pipeline_schedule.schedule.id}"
key = "TERRAFORMED_TEST_VALUE"
value = "test_updated"
}
`, rInt)
}
41 changes: 41 additions & 0 deletions website/docs/r/pipeline_schedule_variable.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
layout: "gitlab"
page_title: "GitLab: gitlab_pipeline_schedule_variable"
sidebar_current: "docs-gitlab-resource-pipeline-schedule-variable"
description: |-
Creates and manages pipeline schedule variables for GitLab projects
---

# gitlab\_pipeline\_schedule\_variable

This resource allows you to create and manage variables for pipeline schedules.

## Example Usage

```hcl
resource "gitlab_pipeline_schedule" "example" {
project = "12345"
description = "Used to schedule builds"
ref = "master"
cron = "0 1 * * *"
}

resource "gitlab_pipeline_schedule_variable" "example" {
project = "${gitlab_pipeline_schedule.project}"
pipeline_schedule_id = "${gitlab_pipeline_schedule.id}"
key = "EXAMPLE_KEY"
value = "example"
}
```

## Argument Reference

The following arguments are supported:

* `project` - (Required, string) The id of the project to add the schedule to.

* `pipeline_schedule_id` - (Required, string) The id of the pipeline schedule.

* `key` - (Required, string) Name of the variable.

* `value` - (Required, string) Value of the variable.
3 changes: 3 additions & 0 deletions website/gitlab.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<li<%= sidebar_current("docs-gitlab-resource-pipeline-schedule") %>>
<a href="/docs/providers/gitlab/r/pipeline_schedule.html">gitlab_pipeline_schedule</a>
</li>
<li<%= sidebar_current("docs-gitlab-resource-pipeline-schedule-variable") %>>
<a href="/docs/providers/gitlab/r/pipeline_schedule_variable.html">gitlab_pipeline_schedule_variable</a>
</li>
<li<%= sidebar_current("docs-gitlab-resource-pipeline-trigger") %>>
<a href="/docs/providers/gitlab/r/pipeline_trigger.html">gitlab_pipeline_trigger</a>
</li>
Expand Down