Skip to content

Commit 4f10ee5

Browse files
committed
fix: add permit_restart_from_failed_steps_use_account_settings
1 parent 9ff56b2 commit 4f10ee5

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

codefresh/resource_pipeline.go

+36-22
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ 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-
2620
func ptrBool(b bool) *bool {
2721
return &b
2822
}
@@ -112,11 +106,20 @@ Or: <code>original_yaml_string = file("/path/to/my/codefresh.yml")</code>
112106
Default: 0,
113107
},
114108
"permit_restart_from_failed_steps": {
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,
109+
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step (default: `true`).",
110+
Type: schema.TypeBool,
111+
Optional: true,
112+
Default: true,
113+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
114+
// If the user set the pipeline to use account settings, ignore the diff
115+
return d.Get("spec.0.permit_restart_from_failed_steps_use_account_settings").(bool)
116+
},
117+
},
118+
"permit_restart_from_failed_steps_use_account_settings": {
119+
Description: "Defines whether `permit_restart_from_failed_steps` should be set to “Use account settings” (default: `false`). If set, `permit_restart_from_failed_steps` will be ignored.",
120+
Type: schema.TypeBool,
121+
Optional: true,
122+
Default: false,
120123
},
121124
"spec_template": {
122125
Description: "The pipeline's spec template.",
@@ -928,11 +931,14 @@ func flattenSpec(spec cfclient.Spec) []map[string]interface{} {
928931
m["trigger_concurrency"] = spec.TriggerConcurrency
929932

930933
if spec.PermitRestartFromFailedSteps == nil {
931-
m["permit_restart_from_failed_steps"] = PermitRestartUseAccountSettings
934+
m["permit_restart_from_failed_steps"] = true // default value
935+
m["permit_restart_from_failed_steps_use_account_settings"] = true
932936
} else if *spec.PermitRestartFromFailedSteps {
933-
m["permit_restart_from_failed_steps"] = PermitRestartPermit
937+
m["permit_restart_from_failed_steps"] = true
938+
m["permit_restart_from_failed_steps_use_account_settings"] = false
934939
} else {
935-
m["permit_restart_from_failed_steps"] = PermitRestartForbid
940+
m["permit_restart_from_failed_steps"] = false
941+
m["permit_restart_from_failed_steps_use_account_settings"] = false
936942
}
937943

938944
m["priority"] = spec.Priority
@@ -1111,15 +1117,23 @@ func mapResourceToPipeline(d *schema.ResourceData) (*cfclient.Pipeline, error) {
11111117
},
11121118
}
11131119

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:
1120+
hasPermitRestartChanged := d.HasChange("spec.0.permit_restart_from_failed_steps")
1121+
hasPermitRestartUseAccountChanged := d.HasChange("spec.0.permit_restart_from_failed_steps_use_account_settings")
1122+
if hasPermitRestartChanged || hasPermitRestartUseAccountChanged {
1123+
shouldPermitRestart := d.Get("spec.0.permit_restart_from_failed_steps").(bool)
1124+
shouldUseAccountSettings := d.Get("spec.0.permit_restart_from_failed_steps_use_account_settings").(bool)
1125+
switch shouldUseAccountSettings {
1126+
case true:
11221127
pipeline.Spec.PermitRestartFromFailedSteps = nil
1128+
default:
1129+
switch shouldPermitRestart {
1130+
case true:
1131+
pipeline.Spec.PermitRestartFromFailedSteps = ptrBool(true)
1132+
case false:
1133+
pipeline.Spec.PermitRestartFromFailedSteps = ptrBool(false)
1134+
default:
1135+
pipeline.Spec.PermitRestartFromFailedSteps = nil
1136+
}
11231137
}
11241138
}
11251139

codefresh/resource_pipeline_test.go

+6-18
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", PermitRestartPermit),
93+
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", true),
9494
Check: resource.ComposeTestCheckFunc(
9595
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
96-
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", PermitRestartPermit),
96+
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", "true"),
9797
),
9898
},
9999
{
@@ -102,22 +102,10 @@ func TestAccCodefreshPipeline_PremitRestartFromFailedSteps(t *testing.T) {
102102
ImportStateVerify: true,
103103
},
104104
{
105-
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", PermitRestartForbid),
105+
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", false),
106106
Check: resource.ComposeTestCheckFunc(
107107
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
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),
108+
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", "false"),
121109
),
122110
},
123111
},
@@ -1082,7 +1070,7 @@ resource "codefresh_pipeline" "test" {
10821070
`, rName, repo, path, revision, context, concurrency, concurrencyBranch, concurrencyTrigger)
10831071
}
10841072

1085-
func testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(rName string, repo string, path string, revision string, context string, permitRestartFromFailedSteps string) string {
1073+
func testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(rName string, repo string, path string, revision string, context string, permitRestartFromFailedSteps bool) string {
10861074
return fmt.Sprintf(`
10871075
resource "codefresh_pipeline" "test" {
10881076
@@ -1102,7 +1090,7 @@ resource "codefresh_pipeline" "test" {
11021090
context = %q
11031091
}
11041092
1105-
permit_restart_from_failed_steps = %s
1093+
permit_restart_from_failed_steps = %t
11061094
11071095
}
11081096
}

0 commit comments

Comments
 (0)