-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from kirecek/add/r/gitlab_pipeline_schedule_v…
…ariable Add gitlab_pipeline_schedule_variable resource
- Loading branch information
Showing
5 changed files
with
349 additions
and
21 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,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
153
gitlab/resource_gitlab_pipeline_schedule_variable_test.go
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,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) | ||
} |
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,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. |
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