From 9ebe23a59cfa7378532e76955ec741b0db2d9e86 Mon Sep 17 00:00:00 2001 From: Andrea Berlingieri Date: Tue, 22 Oct 2024 17:46:28 +0200 Subject: [PATCH] Add support for setting options types (file or text) --- rundeck/job.go | 3 ++ rundeck/resource_job.go | 13 +++++++ rundeck/resource_job_test.go | 74 ++++++++++++++++++++++++++++++++++++ website/docs/r/job.html.md | 4 +- 4 files changed, 93 insertions(+), 1 deletion(-) diff --git a/rundeck/job.go b/rundeck/job.go index 202376ee2..c0fb55c27 100644 --- a/rundeck/job.go +++ b/rundeck/job.go @@ -211,6 +211,9 @@ type JobOption struct { // Option should be hidden from job run page Hidden bool `xml:"hidden,omitempty"` + + // Type of the option. One of: text, file + Type string `xml:"type,attr,omitempty"` } // JobValueChoices is a specialization of []string representing a sequence of predefined values diff --git a/rundeck/resource_job.go b/rundeck/resource_job.go index 2716b7cc3..cd83426e4 100644 --- a/rundeck/resource_job.go +++ b/rundeck/resource_job.go @@ -319,6 +319,11 @@ func resourceRundeckJob() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "type": { + Type: schema.TypeString, + Optional: true, + Default: "text", + }, }, }, }, @@ -820,6 +825,7 @@ func jobFromResourceData(d *schema.ResourceData) (*JobDetail, error) { ValueIsExposedToScripts: optionMap["exposed_to_scripts"].(bool), StoragePath: optionMap["storage_path"].(string), Hidden: optionMap["hidden"].(bool), + Type: optionMap["type"].(string), } if option.StoragePath != "" && !option.ObscureInput { return nil, fmt.Errorf("argument \"obscure_input\" must be set to `true` when \"storage_path\" is not empty") @@ -835,6 +841,9 @@ func jobFromResourceData(d *schema.ResourceData) (*JobDetail, error) { option.ValueChoices = append(option.ValueChoices, iv.(string)) } + if option.Type != "" && option.Type != "text" && option.Type != "file" { + return nil, fmt.Errorf("argument \"type\" cannot have \"%s\" as a value; allowed values: \"text\", \"file\"", option.Type) + } optionsConfig.Options = append(optionsConfig.Options, option) } job.OptionsConfig = optionsConfig @@ -1065,6 +1074,10 @@ func jobToResourceData(job *JobDetail, d *schema.ResourceData) error { "exposed_to_scripts": option.ValueIsExposedToScripts, "storage_path": option.StoragePath, "hidden": option.Hidden, + "type": option.Type, + } + if optionConfigI["type"] == "" { + optionConfigI["type"] = "text" } optionConfigsI = append(optionConfigsI, optionConfigI) } diff --git a/rundeck/resource_job_test.go b/rundeck/resource_job_test.go index 8fbd93606..90a0e289c 100644 --- a/rundeck/resource_job_test.go +++ b/rundeck/resource_job_test.go @@ -319,6 +319,39 @@ func TestAccJobOptions_secure_choice(t *testing.T) { }) } +func TestAccJobOptions_option_type(t *testing.T) { + var job JobDetail + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccJobCheckDestroy(&job), + Steps: []resource.TestStep{ + { + Config: testAccJobOptions_option_type, + Check: resource.ComposeTestCheckFunc( + testAccJobCheckExists("rundeck_job.test", &job), + func(s *terraform.State) error { + fileOption := job.OptionsConfig.Options[0] + if expected := "file"; fileOption.Type != expected { + return fmt.Errorf("wrong option type; expected %v, got %v", expected, fileOption.Type) + } + filenameOption := job.OptionsConfig.Options[1] + if expected := "text"; filenameOption.Type != expected { + return fmt.Errorf("wrong option type; expected %v, got %v", expected, filenameOption.Type) + } + fileextensionOption := job.OptionsConfig.Options[2] + if expected := "text"; fileextensionOption.Type != expected { + return fmt.Errorf("wrong option type; expected %v, got %v", expected, fileextensionOption.Type) + } + return nil + }, + ), + }, + }, + }) +} + func TestAccJob_plugins(t *testing.T) { var job JobDetail @@ -702,6 +735,47 @@ resource "rundeck_job" "test" { } ` +const testAccJobOptions_option_type = ` +resource "rundeck_project" "test" { + name = "terraform-acc-test-job-option-option-type" + description = "parent project for job acceptance tests" + + resource_model_source { + type = "file" + config = { + format = "resourcexml" + file = "/tmp/terraform-acc-tests.xml" + } + } +} +resource "rundeck_job" "test" { + project_name = "${rundeck_project.test.name}" + name = "basic-job" + description = "A basic job" + + preserve_options_order = true + + option { + name = "input_file" + type = "file" + } + + option { + name = "output_file_name" + } + + option { + name = "output_file_extension" + type = "text" + } + + command { + description = "Prints the contents of the input file" + shell_command = "cat $${file.input_file} > $${option.output_file_name}.$${option.output_file_extension}" + } +} +` + const testOchestration_maxperecent = ` resource "rundeck_project" "test" { name = "terraform-acc-test-job" diff --git a/website/docs/r/job.html.md b/website/docs/r/job.html.md index 7934fd9c2..35a776f1a 100644 --- a/website/docs/r/job.html.md +++ b/website/docs/r/job.html.md @@ -228,6 +228,8 @@ The following arguments are supported: * `hidden`: (Optional) Boolean controlling whether this option should be hidden from the UI on the job run page. Defaults to `false`. +* `type`: (Optional) Option type. One of: `file`, `text`. Defaults to `text`. + `command` blocks must have any one of the following combinations of arguments as contents: * `description`: (Optional) gives a description to the command block. @@ -342,4 +344,4 @@ Rundeck job can be imported using the project and job uuid, e.g. $ terraform import rundeck_job.my_job project_name/JOB-UUID ``` -It is also possible to use `import` blocks to generate job config from existing jobs. [See Hashi Docs here](https://developer.hashicorp.com/terraform/language/import/generating-configuration) \ No newline at end of file +It is also possible to use `import` blocks to generate job config from existing jobs. [See Hashi Docs here](https://developer.hashicorp.com/terraform/language/import/generating-configuration)