Skip to content

Commit 5bc995c

Browse files
sandrogattusoGattuso, Sandro
and
Gattuso, Sandro
authored
Support for shared configuration in pipelines (#22)
* Support for shared configuration in pipelines * Adding documentation for contexts in pipelines Co-authored-by: Gattuso, Sandro <[email protected]>
1 parent af5e3b9 commit 5bc995c

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

codefresh/provider_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package codefresh
22

33
import (
4-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5-
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
64
"os"
75
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
)
99

1010
var testAccProvider *schema.Provider
11-
var testAccProviders map[string]terraform.ResourceProvider
11+
var testAccProviders map[string]*schema.Provider
1212

1313
func init() {
14-
testAccProvider = Provider().(*schema.Provider)
15-
testAccProviders = map[string]terraform.ResourceProvider{
14+
testAccProvider = Provider()
15+
testAccProviders = map[string]*schema.Provider{
1616
"codefresh": testAccProvider,
1717
}
1818
}
1919

2020
func TestProvider(t *testing.T) {
21-
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
21+
if err := Provider().InternalValidate(); err != nil {
2222
t.Fatalf("err: %s", err)
2323
}
2424
}

codefresh/resource_pipeline.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package codefresh
22

33
import (
44
"fmt"
5+
"strings"
6+
57
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
68
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7-
"strings"
89
)
910

1011
func resourcePipeline() *schema.Resource {
@@ -28,7 +29,6 @@ func resourcePipeline() *schema.Resource {
2829
"project_id": {
2930
Type: schema.TypeString,
3031
Computed: true,
31-
3232
},
3333
"revision": {
3434
Type: schema.TypeInt,
@@ -158,6 +158,13 @@ func resourcePipeline() *schema.Resource {
158158
},
159159
},
160160
},
161+
"contexts": {
162+
Type: schema.TypeList,
163+
Optional: true,
164+
Elem: &schema.Schema{
165+
Type: schema.TypeString,
166+
},
167+
},
161168
"runtime_environment": {
162169
Type: schema.TypeList,
163170
Optional: true,
@@ -316,6 +323,8 @@ func flattenSpec(spec cfClient.Spec) []interface{} {
316323

317324
m["priority"] = spec.Priority
318325

326+
m["contexts"] = spec.Contexts
327+
319328
res = append(res, m)
320329
return res
321330
}
@@ -407,6 +416,9 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
407416
}
408417
}
409418

419+
contexts := d.Get("spec.0.contexts").([]interface{})
420+
pipeline.Spec.Contexts = contexts
421+
410422
variables := d.Get("spec.0.variables").(map[string]interface{})
411423
pipeline.SetVariables(variables)
412424

codefresh/resource_pipeline_test.go

+67-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package codefresh
22

33
import (
44
"fmt"
5+
"regexp"
6+
"testing"
7+
58
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
69
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
710
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
811
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9-
"regexp"
10-
"testing"
1112
)
1213

1314
var pipelineNamePrefix = "TerraformAccTest_"
@@ -382,6 +383,36 @@ resource "codefresh_pipeline" "test" {
382383
`, rName, repo, path, revision, context, var1Name, var1Value, var2Name, var2Value)
383384
}
384385

386+
func testAccCodefreshPipelineBasicConfigContexts(rName, repo, path, revision, context, sharedContext1, sharedContext2 string) string {
387+
return fmt.Sprintf(`
388+
resource "codefresh_pipeline" "test" {
389+
390+
lifecycle {
391+
ignore_changes = [
392+
revision
393+
]
394+
}
395+
396+
name = "%s"
397+
398+
spec {
399+
spec_template {
400+
repo = %q
401+
path = %q
402+
revision = %q
403+
context = %q
404+
}
405+
406+
contexts = [
407+
%q,
408+
%q
409+
]
410+
411+
}
412+
}
413+
`, rName, repo, path, revision, context, sharedContext1, sharedContext2)
414+
}
415+
385416
func testAccCodefreshPipelineBasicConfigTriggers(
386417
rName,
387418
repo,
@@ -517,3 +548,37 @@ resource "codefresh_pipeline" "test" {
517548
}
518549
`, rName, originalYamlString)
519550
}
551+
552+
func TestAccCodefreshPipeline_Contexts(t *testing.T) {
553+
name := pipelineNamePrefix + acctest.RandString(10)
554+
resourceName := "codefresh_pipeline.test"
555+
556+
resource.ParallelTest(t, resource.TestCase{
557+
PreCheck: func() { testAccPreCheck(t) },
558+
Providers: testAccProviders,
559+
CheckDestroy: testAccCheckCodefreshPipelineDestroy,
560+
Steps: []resource.TestStep{
561+
{
562+
Config: testAccCodefreshPipelineBasicConfigContexts(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", "context1", "context2"),
563+
Check: resource.ComposeTestCheckFunc(
564+
testAccCheckCodefreshPipelineExists(resourceName),
565+
resource.TestCheckResourceAttr(resourceName, "spec.0.contexts.0", "context1"),
566+
resource.TestCheckResourceAttr(resourceName, "spec.0.contexts.1", "context2"),
567+
),
568+
},
569+
{
570+
ResourceName: resourceName,
571+
ImportState: true,
572+
ImportStateVerify: true,
573+
},
574+
{
575+
Config: testAccCodefreshPipelineBasicConfigContexts(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", "context1_updated", "context2_updated"),
576+
Check: resource.ComposeTestCheckFunc(
577+
testAccCheckCodefreshPipelineExists(resourceName),
578+
resource.TestCheckResourceAttr(resourceName, "spec.0.contexts.0", "context1_updated"),
579+
resource.TestCheckResourceAttr(resourceName, "spec.0.contexts.1", "context2_updated"),
580+
),
581+
},
582+
},
583+
})
584+
}

docs/resources/pipeline.md

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ resource "codefresh_pipeline" "test" {
3636
context = "git"
3737
}
3838
39+
contexts = [
40+
"context1-name",
41+
"context2-name",
42+
]
43+
3944
trigger {
4045
branch_regex = "/.*/gi"
4146
context = "git"
@@ -94,6 +99,7 @@ resource "codefresh_pipeline" "test" {
9499
- `trigger` - (Optional) A collection of `trigger` blocks as documented below. Triggers [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/git-triggers/).
95100
- `spec_template` - (Optional) A collection of `spec_template` blocks as documented below.
96101
- `runtime_environment` - (Optional) A collection of `runtime_environment` blocks as documented below.
102+
- `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
97103

98104
---
99105

examples/pipelines.md

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ resource "codefresh_pipeline" "test" {
4242
context = "git"
4343
}
4444
45+
contexts = [
46+
"context1-name",
47+
"context2-name",
48+
]
49+
4550
trigger {
4651
branch_regex = "/.*/gi"
4752
context = "git"

0 commit comments

Comments
 (0)