Skip to content

fix(resource/pipeline): fix permit_restart_from_failed_steps = false #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codefresh/cfclient/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ type Spec struct {
RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"`
Hooks *Hooks `json:"hooks,omitempty"`
Options map[string]bool `json:"options,omitempty"`
PermitRestartFromFailedSteps bool `json:"permitRestartFromFailedSteps,omitempty"`
PermitRestartFromFailedSteps *bool `json:"permitRestartFromFailedSteps,omitempty"`
ExternalResources []ExternalResource `json:"externalResources,omitempty"`
}

Expand Down
1 change: 0 additions & 1 deletion codefresh/data_current_account_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func mapDataCurrentAccountUserToResource(currentAccount *cfclient.CurrentAccount

isFound := false


for _, user := range currentAccount.Users {
if (userAttributeName == "name" && user.UserName == userAttributeValue) || (userAttributeName == "email" && user.Email == userAttributeValue) {
isFound = true
Expand Down
61 changes: 52 additions & 9 deletions codefresh/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (

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

func ptrBool(b bool) *bool {
return &b
}

func resourcePipeline() *schema.Resource {
return &schema.Resource{
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.",
Expand Down Expand Up @@ -102,10 +106,20 @@ Or: <code>original_yaml_string = file("/path/to/my/codefresh.yml")</code>
Default: 0,
},
"permit_restart_from_failed_steps": {
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true",
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step (default: `true`).",
Type: schema.TypeBool,
Optional: true,
Default: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// If the user set the pipeline to use account settings, ignore the diff
return d.Get("spec.0.permit_restart_from_failed_steps_use_account_settings").(bool)
},
},
"permit_restart_from_failed_steps_use_account_settings": {
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.",
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"spec_template": {
Description: "The pipeline's spec template.",
Expand Down Expand Up @@ -915,7 +929,17 @@ func flattenSpec(spec cfclient.Spec) []map[string]interface{} {
m["concurrency"] = spec.Concurrency
m["branch_concurrency"] = spec.BranchConcurrency
m["trigger_concurrency"] = spec.TriggerConcurrency
m["permit_restart_from_failed_steps"] = spec.PermitRestartFromFailedSteps

if spec.PermitRestartFromFailedSteps == nil {
m["permit_restart_from_failed_steps"] = true // default value
m["permit_restart_from_failed_steps_use_account_settings"] = true
} else if *spec.PermitRestartFromFailedSteps {
m["permit_restart_from_failed_steps"] = true
m["permit_restart_from_failed_steps_use_account_settings"] = false
} else {
m["permit_restart_from_failed_steps"] = false
m["permit_restart_from_failed_steps_use_account_settings"] = false
}

m["priority"] = spec.Priority

Expand Down Expand Up @@ -1084,16 +1108,35 @@ func mapResourceToPipeline(d *schema.ResourceData) (*cfclient.Pipeline, error) {
OriginalYamlString: originalYamlString,
},
Spec: cfclient.Spec{
PackId: d.Get("spec.0.pack_id").(string),
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
Priority: d.Get("spec.0.priority").(int),
Concurrency: d.Get("spec.0.concurrency").(int),
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
PermitRestartFromFailedSteps: d.Get("spec.0.permit_restart_from_failed_steps").(bool),
PackId: d.Get("spec.0.pack_id").(string),
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
Priority: d.Get("spec.0.priority").(int),
Concurrency: d.Get("spec.0.concurrency").(int),
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
},
}

hasPermitRestartChanged := d.HasChange("spec.0.permit_restart_from_failed_steps")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@masontikhonov HasChange functions are normally used in custom diff functions. We should instead just check if permit_restart_from_failed_steps_use_account_settings is set to true and in such case set the property to nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

hasPermitRestartUseAccountChanged := d.HasChange("spec.0.permit_restart_from_failed_steps_use_account_settings")
if hasPermitRestartChanged || hasPermitRestartUseAccountChanged {
shouldPermitRestart := d.Get("spec.0.permit_restart_from_failed_steps").(bool)
shouldUseAccountSettings := d.Get("spec.0.permit_restart_from_failed_steps_use_account_settings").(bool)
switch shouldUseAccountSettings {
case true:
pipeline.Spec.PermitRestartFromFailedSteps = nil
default:
switch shouldPermitRestart {
case true:
pipeline.Spec.PermitRestartFromFailedSteps = ptrBool(true)
case false:
pipeline.Spec.PermitRestartFromFailedSteps = ptrBool(false)
default:
pipeline.Spec.PermitRestartFromFailedSteps = nil
}
}
}

if _, ok := d.GetOk("spec.0.spec_template"); ok {
pipeline.Spec.SpecTemplate = &cfclient.SpecTemplate{
Location: d.Get("spec.0.spec_template.0.location").(string),
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ Optional:
- `external_resource` (Block List) (see [below for nested schema](#nestedblock--spec--external_resource))
- `options` (Block List, Max: 1) The options for the pipeline. (see [below for nested schema](#nestedblock--spec--options))
- `pack_id` (String) SAAS pack (`5cd1746617313f468d669013` for Small; `5cd1746717313f468d669014` for Medium; `5cd1746817313f468d669015` for Large; `5cd1746817313f468d669017` for XL; `5cd1746817313f468d669018` for XXL); `5cd1746817313f468d669020` for 4XL).
- `permit_restart_from_failed_steps` (Boolean) Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true
- `permit_restart_from_failed_steps` (Boolean) Defines whether it is permitted to restart builds in this pipeline from failed step (default: `true`).
- `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.
- `priority` (Number) Helps to organize the order of builds execution in case of reaching the concurrency limit (default: `0`).
- `required_available_storage` (String) Minimum disk space required for build filesystem ( unit Gi is required).
- `runtime_environment` (Block List) The runtime environment for the pipeline. (see [below for nested schema](#nestedblock--spec--runtime_environment))
Expand Down
Loading