Skip to content

Commit

Permalink
Add trigger options
Browse files Browse the repository at this point in the history
Include pipeline caching behaviors and notification setting
  • Loading branch information
Nguyen, Tung authored and Nguyen, Tung committed Jul 30, 2021
1 parent 4df8740 commit c228913
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 6 deletions.
8 changes: 8 additions & 0 deletions client/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Trigger struct {
ModifiedFilesGlob string `json:"modifiedFilesGlob,omitempty"`
Provider string `json:"provider,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Options *TriggerOptions `json:"options,omitempty"`
PullRequestAllowForkEvents bool `json:"pullRequestAllowForkEvents,omitempty"`
CommitStatusTitle string `json:"commitStatusTitle,omitempty"`
Context string `json:"context,omitempty"`
Expand All @@ -56,6 +57,13 @@ type Trigger struct {
Variables []Variable `json:"variables,omitempty"`
}

type TriggerOptions struct {
NoCache string `json:"noCache,omitempty"`
NoCfCache string `json:"noCfCache,omitempty"`
ResetVolume string `json:"resetVolume,omitempty"`
EnableNotifications string `json:"enableNotifications,omitempty"`
}

type RuntimeEnvironment struct {
Name string `json:"name,omitempty"`
Memory string `json:"memory,omitempty"`
Expand Down
51 changes: 51 additions & 0 deletions codefresh/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ func resourcePipeline() *schema.Resource {
Optional: true,
Default: false,
},
"options": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"no_cache": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"no_cf_cache": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"reset_volume": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"enable_notifications": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},
"pull_request_allow_fork_events": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -541,6 +569,17 @@ func flattenSpecRuntimeEnvironment(spec cfClient.RuntimeEnvironment) []map[strin
}
}

func flattenTriggerOptions(options cfClient.TriggerOptions) []map[string]interface{} {
return []map[string]interface{}{
{
"no_cache": options.NoCache,
"no_cf_cache": options.NoCfCache,
"reset_volume": options.ResetVolume,
"enable_notifications": options.EnableNotifications,
},
}
}

func flattenTriggers(triggers []cfClient.Trigger) []map[string]interface{} {
var res = make([]map[string]interface{}, len(triggers))
for i, trigger := range triggers {
Expand All @@ -556,6 +595,9 @@ func flattenTriggers(triggers []cfClient.Trigger) []map[string]interface{} {
m["comment_regex"] = trigger.CommentRegex
m["modified_files_glob"] = trigger.ModifiedFilesGlob
m["disabled"] = trigger.Disabled
if trigger.Options != nil {
m["options"] = flattenTriggerOptions(*trigger.Options)
}
m["pull_request_allow_fork_events"] = trigger.PullRequestAllowForkEvents
m["commit_status_title"] = trigger.CommitStatusTitle
m["provider"] = trigger.Provider
Expand Down Expand Up @@ -649,6 +691,15 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
}
variables := d.Get(fmt.Sprintf("spec.0.trigger.%v.variables", idx)).(map[string]interface{})
codefreshTrigger.SetVariables(variables)
if _, ok := d.GetOk(fmt.Sprintf("spec.0.trigger.%v.options", idx)); ok {
options := cfClient.TriggerOptions{
NoCache: d.Get(fmt.Sprintf("spec.0.trigger.%v.options.0.no_cache", idx)).(string),
NoCfCache: d.Get(fmt.Sprintf("spec.0.trigger.%v.options.0.no_cf_cache", idx)).(string),
ResetVolume: d.Get(fmt.Sprintf("spec.0.trigger.%v.options.0.reset_volume", idx)).(string),
EnableNotifications: d.Get(fmt.Sprintf("spec.0.trigger.%v.options.0.enable_notifications", idx)).(string),
}
codefreshTrigger.Options = &options
}
if _, ok := d.GetOk(fmt.Sprintf("spec.0.trigger.%v.runtime_environment", idx)); ok {
triggerRuntime := cfClient.RuntimeEnvironment{
Name: d.Get(fmt.Sprintf("spec.0.trigger.%v.runtime_environment.0.name", idx)).(string),
Expand Down
43 changes: 37 additions & 6 deletions codefresh/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
"tags",
"git",
"shared_context2",
true,
true,
true,
true,
"push.tags",
"codefresh-contrib/react-sample-app",
"triggerTestVar",
Expand All @@ -284,6 +288,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.name", "commits"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.name", "tags"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.contexts.0", "shared_context2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.options.0.no_cache", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.options.0.no_cf_cache", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.options.0.reset_volume", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.options.0.enable_notifications", "true"),
),
},
{
Expand All @@ -310,6 +318,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
"tags",
"git",
"shared_context2_update",
true,
true,
false,
false,
"push.tags",
"codefresh-contrib/react-sample-app",
"triggerTestVar",
Expand All @@ -324,6 +336,10 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.0.comment_regex", "/PR comment2/gi"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.variables.triggerTestVar", "triggerTestValue"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.contexts.0", "shared_context2_update"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.options.0.no_cache", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.options.0.no_cf_cache", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.options.0.reset_volume", "false"),
resource.TestCheckResourceAttr(resourceName, "spec.0.trigger.1.options.0.enable_notifications", "false"),
),
},
},
Expand Down Expand Up @@ -703,12 +719,17 @@ func testAccCodefreshPipelineBasicConfigTriggers(
trigger1Repo,
trigger2Name,
trigger2Context,
trigger2Contexts,
trigger2Contexts string,
trigger2NoCache,
trigger2NoCfCache,
trigger2ResetVolume,
trigger2EnableNotifications bool,
trigger2Event,
trigger2Repo,
trigger2VarName,
trigger2VarValue,
trigger2CommitStatusTitle string) string {
trigger2CommitStatusTitle string,
) string {
return fmt.Sprintf(`
resource "codefresh_pipeline" "test" {
Expand Down Expand Up @@ -753,12 +774,18 @@ resource "codefresh_pipeline" "test" {
trigger {
name = %q
branch_regex = "/.*/gi"
context = %q
contexts = [
%q
]
context = %q
contexts = [
%q
]
description = ""
disabled = false
options = {
no_cache = %t
no_cf_cache = %t
reset_volume = %t
enable_notifications = %t
}
events = [
%q
]
Expand Down Expand Up @@ -794,6 +821,10 @@ resource "codefresh_pipeline" "test" {
trigger2Name,
trigger2Context,
trigger2Contexts,
trigger2NoCache,
trigger2NoCfCache,
trigger2ResetVolume,
trigger2EnableNotifications,
trigger2Event,
trigger2Repo,
trigger2VarName,
Expand Down
10 changes: 10 additions & 0 deletions docs/resources/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ resource "codefresh_pipeline" "test" {
- `pull_request_allow_fork_events` - (Optional) Boolean. If this trigger is also applicable to Git forks.
- `contexts` - (Optional) A list of strings representing the contexts ([shared_configuration](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/shared-configuration/)) to be loaded when the trigger is executed
- `runtime_environment` - (Optional) A collection of `runtime_environment` blocks as documented below.
- `options`: (Optional) A collection `option` blocks as documented below.
---

`runtime_environment` supports the following:
Expand All @@ -153,6 +154,15 @@ resource "codefresh_pipeline" "test" {

---

`options` supports the following:

- `no_cache` - (Required) Boolean. If true, docker layer cache is disabled. Default false
- `no_cf_cache` - (Optional) Boolean. If true, extra Codefresh caching is disabled. Default false
- `reset_volume` - (Optional) Boolean. If true, all files on volume will be deleted before each execution. Default false
- `enable_notifications` - (Optional) Boolean. If false the pipeline will not send notifications to Slack and status updates back to the Git provider. Default false

---

`termination_policy` supports the following:

- `on_create_branch` - (Optional) A `on_create_branch` block as documented below.
Expand Down

0 comments on commit c228913

Please sign in to comment.