diff --git a/codefresh/provider_test.go b/codefresh/provider_test.go index 957593ef..ac007c8e 100644 --- a/codefresh/provider_test.go +++ b/codefresh/provider_test.go @@ -1,24 +1,24 @@ package codefresh import ( - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "os" "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) var testAccProvider *schema.Provider -var testAccProviders map[string]terraform.ResourceProvider +var testAccProviders map[string]*schema.Provider func init() { - testAccProvider = Provider().(*schema.Provider) - testAccProviders = map[string]terraform.ResourceProvider{ + testAccProvider = Provider() + testAccProviders = map[string]*schema.Provider{ "codefresh": testAccProvider, } } func TestProvider(t *testing.T) { - if err := Provider().(*schema.Provider).InternalValidate(); err != nil { + if err := Provider().InternalValidate(); err != nil { t.Fatalf("err: %s", err) } } diff --git a/codefresh/resource_pipeline.go b/codefresh/resource_pipeline.go index 54e5d4e9..79dfc39c 100644 --- a/codefresh/resource_pipeline.go +++ b/codefresh/resource_pipeline.go @@ -2,9 +2,10 @@ package codefresh import ( "fmt" + "strings" + cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "strings" ) func resourcePipeline() *schema.Resource { @@ -28,7 +29,6 @@ func resourcePipeline() *schema.Resource { "project_id": { Type: schema.TypeString, Computed: true, - }, "revision": { Type: schema.TypeInt, @@ -158,6 +158,13 @@ func resourcePipeline() *schema.Resource { }, }, }, + "contexts": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "runtime_environment": { Type: schema.TypeList, Optional: true, @@ -316,6 +323,8 @@ func flattenSpec(spec cfClient.Spec) []interface{} { m["priority"] = spec.Priority + m["contexts"] = spec.Contexts + res = append(res, m) return res } @@ -407,6 +416,9 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline { } } + contexts := d.Get("spec.0.contexts").([]interface{}) + pipeline.Spec.Contexts = contexts + variables := d.Get("spec.0.variables").(map[string]interface{}) pipeline.SetVariables(variables) diff --git a/codefresh/resource_pipeline_test.go b/codefresh/resource_pipeline_test.go index 0e58377a..448bb5d1 100644 --- a/codefresh/resource_pipeline_test.go +++ b/codefresh/resource_pipeline_test.go @@ -2,12 +2,13 @@ package codefresh import ( "fmt" + "regexp" + "testing" + cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - "regexp" - "testing" ) var pipelineNamePrefix = "TerraformAccTest_" @@ -382,6 +383,36 @@ resource "codefresh_pipeline" "test" { `, rName, repo, path, revision, context, var1Name, var1Value, var2Name, var2Value) } +func testAccCodefreshPipelineBasicConfigContexts(rName, repo, path, revision, context, sharedContext1, sharedContext2 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 + } + + contexts = [ + %q, + %q + ] + + } +} +`, rName, repo, path, revision, context, sharedContext1, sharedContext2) +} + func testAccCodefreshPipelineBasicConfigTriggers( rName, repo, @@ -517,3 +548,37 @@ resource "codefresh_pipeline" "test" { } `, rName, originalYamlString) } + +func TestAccCodefreshPipeline_Contexts(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: testAccCodefreshPipelineBasicConfigContexts(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", "context1", "context2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckCodefreshPipelineExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "spec.0.contexts.0", "context1"), + resource.TestCheckResourceAttr(resourceName, "spec.0.contexts.1", "context2"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccCodefreshPipelineBasicConfigContexts(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", "context1_updated", "context2_updated"), + Check: resource.ComposeTestCheckFunc( + testAccCheckCodefreshPipelineExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "spec.0.contexts.0", "context1_updated"), + resource.TestCheckResourceAttr(resourceName, "spec.0.contexts.1", "context2_updated"), + ), + }, + }, + }) +} diff --git a/docs/resources/pipeline.md b/docs/resources/pipeline.md index aef9b09f..36ec9032 100644 --- a/docs/resources/pipeline.md +++ b/docs/resources/pipeline.md @@ -36,6 +36,11 @@ resource "codefresh_pipeline" "test" { context = "git" } + contexts = [ + "context1-name", + "context2-name", + ] + trigger { branch_regex = "/.*/gi" context = "git" @@ -94,6 +99,7 @@ resource "codefresh_pipeline" "test" { - `trigger` - (Optional) A collection of `trigger` blocks as documented below. Triggers [documentation](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/git-triggers/). - `spec_template` - (Optional) A collection of `spec_template` blocks as documented below. - `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 --- diff --git a/examples/pipelines.md b/examples/pipelines.md index db6a17a0..9ba47b43 100644 --- a/examples/pipelines.md +++ b/examples/pipelines.md @@ -42,6 +42,11 @@ resource "codefresh_pipeline" "test" { context = "git" } + contexts = [ + "context1-name", + "context2-name", + ] + trigger { branch_regex = "/.*/gi" context = "git"