Skip to content

Commit 2945191

Browse files
fix(resource/pipeline): fix permit_restart_from_failed_steps = false (#160)
## What This fixes `permit_restart_from_failed_steps` behavior in `pipeline` resource. Before this fix, setting `permit_restart_from_failed_steps = false` or omitting this key resulted in “Permit restart from failed step: Use account settings” for both cases. New syntax/behavior is: `permit_restart_from_failed_steps = [true|false]` with the default value `true` (“Permit”). If `false`, the policy is set to “Forbid”. New flag is added: `permit_restart_from_failed_steps_use_account_settings = [false|true]` with the default value `false` (“do not use account settings”). If `true`, `permit_restart_from_failed_steps` will be ignored and pipeline policy will be set to “Use account settings”. > [!WARNING] > BREAKING CHANGES! > * Previously, `permit_restart_from_failed_steps = false` resulted in “Permit restart from failed step: Use account settings”. > From now on, setting `permit_restart_from_failed_steps = false` will result in “Permit restart from failed step: Forbid” Fixes #CR-26963 ## Notes <!-- Add any notes here --> ## Checklist * [x] _I have read [CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._ * [ ] _I have [allowed changes to my fork to be made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._ * [x] _I have added tests, assuming new tests are warranted_. * [x] _I understand that the `/test` comment will be ignored by the CI trigger [unless it is made by a repo admin or collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._
1 parent 736e7ad commit 2945191

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

codefresh/cfclient/pipeline.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ type Spec struct {
133133
RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"`
134134
Hooks *Hooks `json:"hooks,omitempty"`
135135
Options map[string]bool `json:"options,omitempty"`
136-
PermitRestartFromFailedSteps bool `json:"permitRestartFromFailedSteps,omitempty"`
136+
PermitRestartFromFailedSteps *bool `json:"permitRestartFromFailedSteps,omitempty"`
137137
ExternalResources []ExternalResource `json:"externalResources,omitempty"`
138138
}
139139

codefresh/data_current_account_user.go

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ func mapDataCurrentAccountUserToResource(currentAccount *cfclient.CurrentAccount
6868

6969
isFound := false
7070

71-
7271
for _, user := range currentAccount.Users {
7372
if (userAttributeName == "name" && user.UserName == userAttributeValue) || (userAttributeName == "email" && user.Email == userAttributeValue) {
7473
isFound = true

codefresh/resource_pipeline.go

+48-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717

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

20+
func ptrBool(b bool) *bool {
21+
return &b
22+
}
23+
2024
func resourcePipeline() *schema.Resource {
2125
return &schema.Resource{
2226
Description: "The central component of the Codefresh Platform. Pipelines are workflows that contain individual steps. Each step is responsible for a specific action in the process.",
@@ -102,10 +106,20 @@ Or: <code>original_yaml_string = file("/path/to/my/codefresh.yml")</code>
102106
Default: 0,
103107
},
104108
"permit_restart_from_failed_steps": {
105-
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true",
109+
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step (default: `true`).",
106110
Type: schema.TypeBool,
107111
Optional: true,
108112
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,
109123
},
110124
"spec_template": {
111125
Description: "The pipeline's spec template.",
@@ -915,7 +929,17 @@ func flattenSpec(spec cfclient.Spec) []map[string]interface{} {
915929
m["concurrency"] = spec.Concurrency
916930
m["branch_concurrency"] = spec.BranchConcurrency
917931
m["trigger_concurrency"] = spec.TriggerConcurrency
918-
m["permit_restart_from_failed_steps"] = spec.PermitRestartFromFailedSteps
932+
933+
if spec.PermitRestartFromFailedSteps == nil {
934+
m["permit_restart_from_failed_steps"] = true // default value
935+
m["permit_restart_from_failed_steps_use_account_settings"] = true
936+
} else if *spec.PermitRestartFromFailedSteps {
937+
m["permit_restart_from_failed_steps"] = true
938+
m["permit_restart_from_failed_steps_use_account_settings"] = false
939+
} else {
940+
m["permit_restart_from_failed_steps"] = false
941+
m["permit_restart_from_failed_steps_use_account_settings"] = false
942+
}
919943

920944
m["priority"] = spec.Priority
921945

@@ -1084,16 +1108,31 @@ func mapResourceToPipeline(d *schema.ResourceData) (*cfclient.Pipeline, error) {
10841108
OriginalYamlString: originalYamlString,
10851109
},
10861110
Spec: cfclient.Spec{
1087-
PackId: d.Get("spec.0.pack_id").(string),
1088-
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
1089-
Priority: d.Get("spec.0.priority").(int),
1090-
Concurrency: d.Get("spec.0.concurrency").(int),
1091-
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
1092-
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
1093-
PermitRestartFromFailedSteps: d.Get("spec.0.permit_restart_from_failed_steps").(bool),
1111+
PackId: d.Get("spec.0.pack_id").(string),
1112+
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
1113+
Priority: d.Get("spec.0.priority").(int),
1114+
Concurrency: d.Get("spec.0.concurrency").(int),
1115+
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
1116+
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
10941117
},
10951118
}
10961119

1120+
shouldPermitRestart := d.Get("spec.0.permit_restart_from_failed_steps").(bool)
1121+
shouldUseAccountSettings := d.Get("spec.0.permit_restart_from_failed_steps_use_account_settings").(bool)
1122+
switch shouldUseAccountSettings {
1123+
case true:
1124+
pipeline.Spec.PermitRestartFromFailedSteps = nil
1125+
default:
1126+
switch shouldPermitRestart {
1127+
case true:
1128+
pipeline.Spec.PermitRestartFromFailedSteps = ptrBool(true)
1129+
case false:
1130+
pipeline.Spec.PermitRestartFromFailedSteps = ptrBool(false)
1131+
default:
1132+
pipeline.Spec.PermitRestartFromFailedSteps = nil
1133+
}
1134+
}
1135+
10971136
if _, ok := d.GetOk("spec.0.spec_template"); ok {
10981137
pipeline.Spec.SpecTemplate = &cfclient.SpecTemplate{
10991138
Location: d.Get("spec.0.spec_template.0.location").(string),

docs/resources/pipeline.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ Optional:
132132
- `external_resource` (Block List) (see [below for nested schema](#nestedblock--spec--external_resource))
133133
- `options` (Block List, Max: 1) The options for the pipeline. (see [below for nested schema](#nestedblock--spec--options))
134134
- `pack_id` (String) SAAS pack (`5cd1746617313f468d669013` for Small; `5cd1746717313f468d669014` for Medium; `5cd1746817313f468d669015` for Large; `5cd1746817313f468d669017` for XL; `5cd1746817313f468d669018` for XXL); `5cd1746817313f468d669020` for 4XL).
135-
- `permit_restart_from_failed_steps` (Boolean) Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true
135+
- `permit_restart_from_failed_steps` (Boolean) Defines whether it is permitted to restart builds in this pipeline from failed step (default: `true`).
136+
- `permit_restart_from_failed_steps_use_account_settings` (Boolean) 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.
136137
- `priority` (Number) Helps to organize the order of builds execution in case of reaching the concurrency limit (default: `0`).
137138
- `required_available_storage` (String) Minimum disk space required for build filesystem ( unit Gi is required).
138139
- `runtime_environment` (Block List) The runtime environment for the pipeline. (see [below for nested schema](#nestedblock--spec--runtime_environment))

0 commit comments

Comments
 (0)