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

Add inline cron triggers to codefresh_pipeline #122

Merged
merged 5 commits into from
Aug 18, 2023
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
20 changes: 20 additions & 0 deletions client/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ type Trigger struct {
Variables []Variable `json:"variables,omitempty"`
}

type CronTrigger struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Expression string `json:"expression,omitempty"`
Message string `json:"message,omitempty"`
GitTriggerId string `json:"gitTriggerId,omitempty"`
Branch string `json:"branch,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Options *TriggerOptions `json:"options,omitempty"`
RuntimeEnvironment *RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
Variables []Variable `json:"variables,omitempty"`
}

type TriggerOptions struct {
NoCache bool `json:"noCache,omitempty"`
NoCfCache bool `json:"noCfCache,omitempty"`
Expand All @@ -83,10 +96,17 @@ func (t *Trigger) SetVariables(variables map[string]interface{}) {
}
}

func (t *CronTrigger) SetVariables(variables map[string]interface{}) {
for key, value := range variables {
t.Variables = append(t.Variables, Variable{Key: key, Value: value.(string)})
}
}

type Spec struct {
Variables []Variable `json:"variables,omitempty"`
SpecTemplate *SpecTemplate `json:"specTemplate,omitempty"`
Triggers []Trigger `json:"triggers,omitempty"`
CronTriggers []CronTrigger `json:"cronTriggers,omitempty"`
Priority int `json:"priority,omitempty"`
Concurrency int `json:"concurrency,omitempty"`
BranchConcurrency int `json:"branchConcurrency,omitempty"`
Expand Down
323 changes: 277 additions & 46 deletions codefresh/resource_pipeline.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions codefresh/resource_pipeline_cron_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

func resourcePipelineCronTrigger() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "This resource is deprecated and will be removed in a future version of the Codefresh Terraform provider. Please use the cron_triggers attribute of the codefresh_pipeline resource instead.",
Description: "This resource is used to create cron-based triggers for pipeilnes.",
Create: resourcePipelineCronTriggerCreate,
Read: resourcePipelineCronTriggerRead,
Expand Down
229 changes: 229 additions & 0 deletions codefresh/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,125 @@ func TestAccCodefreshPipeline_Triggers(t *testing.T) {
})
}

func TestAccCodefreshPipeline_CronTriggers(t *testing.T) {
name := pipelineNamePrefix + acctest.RandString(10)
resourceName := "codefresh_pipeline.test"
var pipeline cfClient.Pipeline

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCodefreshPipelineDestroy,
Steps: []resource.TestStep{
{
Config: testAccCodefreshPipelineBasicConfigCronTriggers(
name,
"codefresh-contrib/react-sample-app",
"./codefresh.yml",
"master",
"git",
"cT1",
"first",
"0 0/1 * 1/1 * *",
"64abd1550f02a62699b10df7",
"runtime1",
"100mb",
"1cpu",
"1gb",
"1gb",
"cT2",
"second",
"0 0/1 * 1/1 * *",
"64abd1550f02a62699b10df7",
true,
true,
true,
true,
"MY_VAR",
"test",
),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.#", "2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.name", "cT1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.message", "first"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.expression", "0 0/1 * 1/1 * *"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.name", "runtime1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.memory", "100mb"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.cpu", "1cpu"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.dind_storage", "1gb"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.required_available_storage", "1gb"),

resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.#", "2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.name", "cT2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.message", "second"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.expression", "0 0/1 * 1/1 * *"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.git_trigger_id", "64abd1550f02a62699b10df7"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.no_cache", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.no_cf_cache", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.reset_volume", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.enable_notifications", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccCodefreshPipelineBasicConfigCronTriggers(
name,
"codefresh-contrib/react-sample-app",
"./codefresh.yml",
"master",
"git",
"cT1",
"first-1",
"0 0/1 * 1/1 * *",
"00abd1550f02a62699b10df7",
"runtime2",
"500mb",
"2cpu",
"2gb",
"3gb",
"cT2",
"second",
"0 1/1 * 1/1 * *",
"00abd1550f02a62699b10df7",
true,
true,
false,
false,
"MY_VAR",
"test",
),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.#", "2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.name", "cT1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.message", "first-1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.expression", "0 0/1 * 1/1 * *"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.name", "runtime2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.memory", "500mb"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.cpu", "2cpu"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.dind_storage", "2gb"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.0.runtime_environment.0.required_available_storage", "3gb"),

resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.#", "2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.name", "cT2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.message", "second"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.expression", "0 1/1 * 1/1 * *"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.git_trigger_id", "00abd1550f02a62699b10df7"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.no_cache", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.no_cf_cache", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.reset_volume", "false"),
resource.TestCheckResourceAttr(resourceName, "spec.0.cron_trigger.1.options.0.enable_notifications", "false"),
),
},
},
})
}

func TestAccCodefreshPipeline_Revision(t *testing.T) {
name := pipelineNamePrefix + acctest.RandString(10)
resourceName := "codefresh_pipeline.test"
Expand Down Expand Up @@ -901,6 +1020,116 @@ resource "codefresh_pipeline" "test" {
trigger2CommitStatusTitle)
}

func testAccCodefreshPipelineBasicConfigCronTriggers(
rName,
repo,
path,
revision,
context,
cronTrigger1Name string,
cronTrigger1Message string,
cronTrigger1Expression string,
cronTrigger1GitTriggerId string,
cronTrigger1REName string,
cronTrigger1REMemory string,
cronTrigger1RECpu string,
cronTrigger1REDindStorage string,
cronTrigger1RERequiredAvailableStorage string,
cronTrigger2Name string,
cronTrigger2Message string,
cronTrigger2Expression string,
cronTrigger2GitTriggerId string,
cronTrigger2NoCache bool,
cronTrigger2NoCfCache bool,
cronTrigger2ResetVolume bool,
cronTrigger2EnableNotifications bool,
cronTrigger2VarName string,
cronTrigger2VarValue string,
) 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
}

cron_trigger {
name = %q
type = "cron"
branch = "main"
message = %q
expression = %q
git_trigger_id = %q
disabled = true
runtime_environment {
name = %q
memory = %q
cpu = %q
dind_storage = %q
required_available_storage = %q
}
}

cron_trigger {
name = %q
type = "cron"
branch = "master"
message = %q
expression = %q
git_trigger_id = %q
disabled = false
options {
no_cache = %t
no_cf_cache = %t
reset_volume = %t
enable_notifications = %t
}
variables = {
%q = %q
}
}
}
}
`,
rName,
repo,
path,
revision,
context,
cronTrigger1Name,
cronTrigger1Message,
cronTrigger1Expression,
cronTrigger1GitTriggerId,
cronTrigger1REName,
cronTrigger1REMemory,
cronTrigger1RECpu,
cronTrigger1REDindStorage,
cronTrigger1RERequiredAvailableStorage,
cronTrigger2Name,
cronTrigger2Message,
cronTrigger2Expression,
cronTrigger2GitTriggerId,
cronTrigger2NoCache,
cronTrigger2NoCfCache,
cronTrigger2ResetVolume,
cronTrigger2EnableNotifications,
cronTrigger2VarName,
cronTrigger2VarValue,
)
}

func testAccCodefreshPipelineBasicConfigRuntimeEnvironment(rName, repo, path, revision, context, runtimeName string) string {
return fmt.Sprintf(`
resource "codefresh_pipeline" "test" {
Expand Down
46 changes: 46 additions & 0 deletions docs/resources/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The central component of the Codefresh Platform. Pipelines are workflows that co

See the [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/introduction-to-codefresh-pipelines/) for the details.

~> **NOTE:** `cron_trigger` conflicts with the deprecated [codefresh_pipeline_cron_trigger](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline_cron_trigger) resource.

## Example Usage

```hcl
Expand Down Expand Up @@ -125,6 +127,7 @@ Optional:
- `branch_concurrency` (Number) The maximum amount of concurrent builds that may run for each branch. Zero is unlimited (default: `0`).
- `concurrency` (Number) The maximum amount of concurrent builds. Zero is unlimited (default: `0`).
- `contexts` (List of String) 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.
- `cron_trigger` (Block List) The pipeline's cron triggers. Conflicts with the deprecated [codefresh_pipeline_cron_trigger](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline_cron_trigger) resource. (see [below for nested schema](#nestedblock--spec--cron_trigger))
- `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).
- `priority` (Number) Helps to organize the order of builds execution in case of reaching the concurrency limit (default: `0`).
Expand All @@ -136,6 +139,49 @@ Optional:
- `trigger_concurrency` (Number) The maximum amount of concurrent builds that may run for each trigger (default: `0`).
- `variables` (Map of String) The pipeline's variables.

<a id="nestedblock--spec--cron_trigger"></a>
### Nested Schema for `spec.cron_trigger`

Required:

- `expression` (String)
- `message` (String)
- `name` (String) The name of the cron trigger.

Optional:

- `branch` (String) Branch that should be passed for build triggered by this cron trigger.
- `disabled` (Boolean) Flag to disable the trigger.
- `git_trigger_id` (String) Related git-trigger id. Will by used to take all possible git information by branch.
- `options` (Block List) The trigger's options. (see [below for nested schema](#nestedblock--spec--cron_trigger--options))
- `runtime_environment` (Block List) The runtime environment for the trigger. (see [below for nested schema](#nestedblock--spec--cron_trigger--runtime_environment))
- `type` (String) The type of the trigger (default: `cron`; see notes above).
- `variables` (Map of String) Trigger variables.

<a id="nestedblock--spec--cron_trigger--options"></a>
### Nested Schema for `spec.cron_trigger.options`

Optional:

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


<a id="nestedblock--spec--cron_trigger--runtime_environment"></a>
### Nested Schema for `spec.cron_trigger.runtime_environment`

Optional:

- `cpu` (String) The CPU allocated to the runtime environment.
- `dind_storage` (String) The storage allocated to the runtime environment.
- `memory` (String) The memory allocated to the runtime environment.
- `name` (String) The name of the runtime environment.
- `required_available_storage` (String) Minimum disk space required for build filesystem ( unit Gi is required).



<a id="nestedblock--spec--options"></a>
### Nested Schema for `spec.options`

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/pipeline_cron_trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This resource is used to create cron-based triggers for pipeilnes.

See the [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/cron-triggers/).

~> **DEPRECATED:** This resource is being deprecated in favor of the `cron_trigger` attribute of the [codefresh_pipeline](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline) resource.

## Example usage

```hcl
Expand Down
2 changes: 2 additions & 0 deletions templates/resources/pipeline.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ description: |-

See the [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/introduction-to-codefresh-pipelines/) for the details.

~> **NOTE:** `cron_trigger` conflicts with the deprecated [codefresh_pipeline_cron_trigger](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline_cron_trigger) resource.

## Example Usage

```hcl
Expand Down
2 changes: 2 additions & 0 deletions templates/resources/pipeline_cron_trigger.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ description: |-

See the [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/cron-triggers/).

~> **DEPRECATED:** This resource is being deprecated in favor of the `cron_trigger` attribute of the [codefresh_pipeline](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline) resource.

## Example usage

```hcl
Expand Down