Skip to content
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

feat: Support Pending Approval options in pipelines #50

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions client/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Spec struct {
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
TerminationPolicy []map[string]interface{} `json:"terminationPolicy,omitempty"`
Hooks *Hooks `json:"hooks,omitempty"`
Options map[string]bool `json:"options,omitempty"`
}

type Steps struct {
Expand Down
44 changes: 44 additions & 0 deletions codefresh/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ func resourcePipeline() *schema.Resource {
},
},
},
"options": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"keep_pvcs_for_pending_approval": {
Type: schema.TypeBool,
Optional: true,
},
"pending_approval_concurrency_applied": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -445,6 +462,21 @@ func flattenSpec(spec cfClient.Spec) []interface{} {
m["termination_policy"] = flattenSpecTerminationPolicy(spec.TerminationPolicy)
}

if len(spec.Options) > 0 {
var resOptions []map[string]bool
options := map[string]bool{}
for keyOption, valueOption := range spec.Options {
switch {
case keyOption == "keepPVCsForPendingApproval":
options["keep_pvcs_for_pending_approval"] = valueOption
case keyOption == "pendingApprovalConcurrencyApplied":
options["pending_approval_concurrency_applied"] = valueOption
}
}
resOptions = append(resOptions, options)
m["options"] = resOptions
}

m["concurrency"] = spec.Concurrency
m["branch_concurrency"] = spec.BranchConcurrency
m["trigger_concurrency"] = spec.TriggerConcurrency
Expand Down Expand Up @@ -647,6 +679,18 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
onTerminateAnnotationPolicy["key"] = "cf_predecessor"
codefreshTerminationPolicy = append(codefreshTerminationPolicy, onTerminateAnnotationPolicy)
}
if _, ok := d.GetOk("spec.0.options"); ok {
pipelineSpecOption := make(map[string]bool)
if keepPVCs, ok := d.GetOkExists("spec.0.options.0.keep_pvcs_for_pending_approval"); ok {
pipelineSpecOption["keepPVCsForPendingApproval"] = keepPVCs.(bool)
}
if pendingApprovalConcurrencyApplied, ok := d.GetOkExists("spec.0.options.0.pending_approval_concurrency_applied"); ok {
pipelineSpecOption["pendingApprovalConcurrencyApplied"] = pendingApprovalConcurrencyApplied.(bool)
}
pipeline.Spec.Options = pipelineSpecOption
} else {
pipeline.Spec.Options = nil
}

pipeline.Spec.TerminationPolicy = codefreshTerminationPolicy

Expand Down
63 changes: 63 additions & 0 deletions codefresh/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,41 @@ func TestAccCodefreshPipelineOnCreateBranchIgnoreTrigger(t *testing.T) {
})
}

func TestAccCodefreshPipelineOptions(t *testing.T) {
name := pipelineNamePrefix + acctest.RandString(10)
resourceName := "codefresh_pipeline.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCodefreshPipelineDestroy,
Steps: []resource.TestStep{
{
Config: testAccCodefreshPipelineOptions(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", true, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodefreshPipelineExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "spec.0.options.0.keep_pvcs_for_pending_approval", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.options.0.pending_approval_concurrency_applied", "false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccCodefreshPipelineBasicConfig(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git"),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodefreshPipelineExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckNoResourceAttr(resourceName, "spec.0.options"),
),
},
},
})
}

func testAccCheckCodefreshPipelineExists(resource string) resource.TestCheckFunc {
return func(state *terraform.State) error {

Expand Down Expand Up @@ -848,6 +883,34 @@ func TestAccCodefreshPipeline_Contexts(t *testing.T) {
})
}

func testAccCodefreshPipelineOptions(rName, repo, path, revision, context string, keepPVCsForPendingApproval, pendingApprovalConcurrencyApplied bool) string {
return fmt.Sprintf(`
resource "codefresh_pipeline" "test" {

lifecycle {
ignore_changes = [
revision
]
}

name = "%s"

spec {
spec_template {
repo = %q
path = %q
revision = %q
context = %q
}
options {
keep_pvcs_for_pending_approval = %t
pending_approval_concurrency_applied = %t
}
}
}
`, rName, repo, path, revision, context, keepPVCsForPendingApproval, pendingApprovalConcurrencyApplied)
}

func testAccCodefreshPipelineOnCreateBranchIgnoreTrigger(rName, repo, path, revision, context string, ignoreTrigger bool) string {
return fmt.Sprintf(`
resource "codefresh_pipeline" "test" {
Expand Down
13 changes: 13 additions & 0 deletions docs/resources/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ resource "codefresh_pipeline" "test" {
- `runtime_environment` - (Optional) A collection of `runtime_environment` blocks as documented below.
- `contexts` - (Optional) A list of strings representing the contexts ([shared_configuration](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/shared-configuration/)) to be configured for the pipeline
- `termination_policy` - (Optional) A `termination_policy` block as documented below.
- `options` - (Optional) A `options` block as documented below.

---

Expand Down Expand Up @@ -178,6 +179,18 @@ The following table presents how to configure this block based on the options av
| Once a build is created, terminate all other running builds | From the SAME trigger | Defined | N/A | false | true |
| Once a build is created, terminate all other running builds | From ANY trigger | Defined | N/A | true | true |

---

`options` supports the following:

- `keep_pvcs_for_pending_approval` - (Optional) Boolean for the Settings under pending approval: `When build enters "Pending Approval" state, volume should`:
* Default (attribute not specified): "Use Setting accounts"
* true: "Remain (build remains active)"
* false: "Be removed"
- `pending_approval_concurrency_applied` - (Optional) Boolean for the Settings under pending approval: `Pipeline concurrency policy: Builds on "Pending Approval" state should be`:
* Default (attribute not specified): "Use Setting accounts"
* true: "Included in concurrency"
* false: "Not included in concurrency"

## Attributes Reference

Expand Down