Skip to content

Commit 9ff56b2

Browse files
committed
fix: use ENUM for permit_restart_from_failed_steps
1 parent 2adecb7 commit 9ff56b2

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

codefresh/resource_pipeline.go

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import (
1717

1818
var terminationPolicyOnCreateBranchAttributes = []string{"branchName", "ignoreTrigger", "ignoreBranch"}
1919

20+
const (
21+
PermitRestartPermit = "permit"
22+
PermitRestartForbid = "forbid"
23+
PermitRestartUseAccountSettings = "use-account-settings"
24+
)
25+
2026
func ptrBool(b bool) *bool {
2127
return &b
2228
}
@@ -106,10 +112,11 @@ Or: <code>original_yaml_string = file("/path/to/my/codefresh.yml")</code>
106112
Default: 0,
107113
},
108114
"permit_restart_from_failed_steps": {
109-
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true",
110-
Type: schema.TypeBool,
111-
Optional: true,
112-
Default: true,
115+
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step. Allowed values: 'permit'|'forbid'|'use-account-settings' (default: `use-account-settings`).",
116+
Type: schema.TypeString,
117+
Optional: true,
118+
ValidateFunc: validation.StringInSlice([]string{PermitRestartPermit, PermitRestartForbid, PermitRestartUseAccountSettings}, false),
119+
Default: PermitRestartUseAccountSettings,
113120
},
114121
"spec_template": {
115122
Description: "The pipeline's spec template.",
@@ -919,7 +926,14 @@ func flattenSpec(spec cfclient.Spec) []map[string]interface{} {
919926
m["concurrency"] = spec.Concurrency
920927
m["branch_concurrency"] = spec.BranchConcurrency
921928
m["trigger_concurrency"] = spec.TriggerConcurrency
922-
m["permit_restart_from_failed_steps"] = spec.PermitRestartFromFailedSteps
929+
930+
if spec.PermitRestartFromFailedSteps == nil {
931+
m["permit_restart_from_failed_steps"] = PermitRestartUseAccountSettings
932+
} else if *spec.PermitRestartFromFailedSteps {
933+
m["permit_restart_from_failed_steps"] = PermitRestartPermit
934+
} else {
935+
m["permit_restart_from_failed_steps"] = PermitRestartForbid
936+
}
923937

924938
m["priority"] = spec.Priority
925939

@@ -1088,16 +1102,27 @@ func mapResourceToPipeline(d *schema.ResourceData) (*cfclient.Pipeline, error) {
10881102
OriginalYamlString: originalYamlString,
10891103
},
10901104
Spec: cfclient.Spec{
1091-
PackId: d.Get("spec.0.pack_id").(string),
1092-
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
1093-
Priority: d.Get("spec.0.priority").(int),
1094-
Concurrency: d.Get("spec.0.concurrency").(int),
1095-
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
1096-
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
1097-
PermitRestartFromFailedSteps: ptrBool(d.Get("spec.0.permit_restart_from_failed_steps").(bool)),
1105+
PackId: d.Get("spec.0.pack_id").(string),
1106+
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
1107+
Priority: d.Get("spec.0.priority").(int),
1108+
Concurrency: d.Get("spec.0.concurrency").(int),
1109+
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
1110+
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
10981111
},
10991112
}
11001113

1114+
if hasPermitRestartChanged := d.HasChange("spec.0.permit_restart_from_failed_steps"); hasPermitRestartChanged {
1115+
permitRestart := d.Get("spec.0.permit_restart_from_failed_steps").(string)
1116+
switch permitRestart {
1117+
case PermitRestartPermit:
1118+
pipeline.Spec.PermitRestartFromFailedSteps = ptrBool(true)
1119+
case PermitRestartForbid:
1120+
pipeline.Spec.PermitRestartFromFailedSteps = ptrBool(false)
1121+
default:
1122+
pipeline.Spec.PermitRestartFromFailedSteps = nil
1123+
}
1124+
}
1125+
11011126
if _, ok := d.GetOk("spec.0.spec_template"); ok {
11021127
pipeline.Spec.SpecTemplate = &cfclient.SpecTemplate{
11031128
Location: d.Get("spec.0.spec_template.0.location").(string),

codefresh/resource_pipeline_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ func TestAccCodefreshPipeline_PremitRestartFromFailedSteps(t *testing.T) {
9090
CheckDestroy: testAccCheckCodefreshPipelineDestroy,
9191
Steps: []resource.TestStep{
9292
{
93-
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", ptrBool(true)),
93+
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", PermitRestartPermit),
9494
Check: resource.ComposeTestCheckFunc(
9595
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
96-
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", "true"),
96+
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", PermitRestartPermit),
9797
),
9898
},
9999
{
@@ -102,10 +102,22 @@ func TestAccCodefreshPipeline_PremitRestartFromFailedSteps(t *testing.T) {
102102
ImportStateVerify: true,
103103
},
104104
{
105-
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", ptrBool(false)),
105+
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", PermitRestartForbid),
106106
Check: resource.ComposeTestCheckFunc(
107107
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
108-
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", "false"),
108+
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", PermitRestartForbid),
109+
),
110+
},
111+
{
112+
ResourceName: resourceName,
113+
ImportState: true,
114+
ImportStateVerify: true,
115+
},
116+
{
117+
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", PermitRestartUseAccountSettings),
118+
Check: resource.ComposeTestCheckFunc(
119+
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
120+
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", PermitRestartUseAccountSettings),
109121
),
110122
},
111123
},
@@ -1070,7 +1082,7 @@ resource "codefresh_pipeline" "test" {
10701082
`, rName, repo, path, revision, context, concurrency, concurrencyBranch, concurrencyTrigger)
10711083
}
10721084

1073-
func testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(rName string, repo string, path string, revision string, context string, permitRestartFromFailedSteps *bool) string {
1085+
func testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(rName string, repo string, path string, revision string, context string, permitRestartFromFailedSteps string) string {
10741086
return fmt.Sprintf(`
10751087
resource "codefresh_pipeline" "test" {
10761088
@@ -1090,11 +1102,11 @@ resource "codefresh_pipeline" "test" {
10901102
context = %q
10911103
}
10921104
1093-
permit_restart_from_failed_steps = %t
1105+
permit_restart_from_failed_steps = %s
10941106
10951107
}
10961108
}
1097-
`, rName, repo, path, revision, context, *permitRestartFromFailedSteps)
1109+
`, rName, repo, path, revision, context, permitRestartFromFailedSteps)
10981110
}
10991111

11001112
func testAccCodefreshPipelineBasicConfigTriggers(

0 commit comments

Comments
 (0)