diff --git a/codefresh/cfclient/pipeline.go b/codefresh/cfclient/pipeline.go index 0b7c9a9..c15bd69 100644 --- a/codefresh/cfclient/pipeline.go +++ b/codefresh/cfclient/pipeline.go @@ -90,6 +90,17 @@ type RuntimeEnvironment struct { RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"` } +type ExternalResource struct { + ID string `json:"id,omitempty"` + Type string `json:"type"` + Source string `json:"source"` + Context string `json:"context"` + Destination string `json:"destination"` + IsFolder bool `json:"isFolder"` + Repo string `json:"repo"` + Revision string `json:"revision"` +} + func (t *Trigger) SetVariables(variables map[string]interface{}, encrypted bool) { for key, value := range variables { t.Variables = append(t.Variables, Variable{Key: key, Value: value.(string), Encrypted: encrypted}) @@ -123,6 +134,7 @@ type Spec struct { Hooks *Hooks `json:"hooks,omitempty"` Options map[string]bool `json:"options,omitempty"` PermitRestartFromFailedSteps bool `json:"permitRestartFromFailedSteps,omitempty"` + ExternalResources []ExternalResource `json:"externalResources,omitempty"` } type Steps struct { @@ -149,7 +161,6 @@ func (d Hooks) MarshalJSON() ([]byte, error) { bytes := []byte(d.Hooks) return bytes, nil } - func (d *Steps) UnmarshalJSON(data []byte) error { d.Steps = string(data) return nil diff --git a/codefresh/resource_pipeline.go b/codefresh/resource_pipeline.go index 0da0da6..3fb36cc 100644 --- a/codefresh/resource_pipeline.go +++ b/codefresh/resource_pipeline.go @@ -641,6 +641,58 @@ Pipeline concurrency policy: Builds on 'Pending Approval' state should be: }, }, }, + "external_resource": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id" : { + Type: schema.TypeString, + Computed: true, + }, + "type" : { + Type: schema.TypeString, + Optional: true, + Description: "Type of the external resource. Currently only git is supported", + ValidateFunc: validation.StringInSlice([]string{ + "git", + }, false), + Default: "git", + }, + "repo" : { + Type: schema.TypeString, + Required: true, + Description: "git repository url", + }, + "context" : { + Type: schema.TypeString, + Required: true, + Description: "Context name for the git repository", + }, + "revision": { + Type: schema.TypeString, + Required: true, + Description: "Revision/branch in the git repository", + }, + "is_folder": { + Type: schema.TypeBool, + Description: "Whether or not the resource specified in source_path is a folder", + Optional: true, + Default: false, + }, + "source_path": { + Type: schema.TypeString, + Description: "The source folder in the repository (use relative path)", + Required: true, + }, + "target_path": { + Type: schema.TypeString, + Description: "The target folder in the pipeline workspace where the file/folder will be copied to (use absolute path)", + Required: true, + }, + }, + }, + }, }, }, }, @@ -832,6 +884,10 @@ func flattenSpec(spec cfclient.Spec) []map[string]interface{} { m["runtime_environment"] = flattenSpecRuntimeEnvironment(spec.RuntimeEnvironment) } + if len(spec.ExternalResources) > 0 { + m["external_resource"] = flattenExternalResources(spec.ExternalResources) + } + if len(spec.TerminationPolicy) > 0 { m["termination_policy"] = flattenSpecTerminationPolicy(spec.TerminationPolicy) } @@ -988,6 +1044,25 @@ func flattenCronTriggers(cronTriggers []cfclient.CronTrigger) []map[string]inter return res } +func flattenExternalResources(externalResources []cfclient.ExternalResource) []map[string]interface{} { + var res = make([]map[string]interface{}, len(externalResources)) + for i, externalResource := range externalResources { + m := make(map[string]interface{}) + m["type"] = externalResource.Type + m["repo"] = externalResource.Repo + m["context"] = externalResource.Context + m["source_path"] = externalResource.Source + m["target_path"] = externalResource.Destination + m["revision"] = externalResource.Revision + m["is_folder"] = externalResource.IsFolder + m["id"] = externalResource.ID + + res[i] = m + } + + return res +} + func mapResourceToPipeline(d *schema.ResourceData) (*cfclient.Pipeline, error) { tags := d.Get("tags").(*schema.Set).List() @@ -1146,6 +1221,23 @@ func mapResourceToPipeline(d *schema.ResourceData) (*cfclient.Pipeline, error) { } } + if externalResources, ok := d.GetOk("spec.0.external_resource"); ok { + for idx := range externalResources.([]interface{}) { + codefreshExternalResource := cfclient.ExternalResource{ + Type: d.Get(fmt.Sprintf("spec.0.external_resource.%v.type", idx)).(string), + Repo: d.Get(fmt.Sprintf("spec.0.external_resource.%v.repo", idx)).(string), + Revision: d.Get(fmt.Sprintf("spec.0.external_resource.%v.revision", idx)).(string), + Context: d.Get(fmt.Sprintf("spec.0.external_resource.%v.context", idx)).(string), + Source: d.Get(fmt.Sprintf("spec.0.external_resource.%v.source_path", idx)).(string), + Destination: d.Get(fmt.Sprintf("spec.0.external_resource.%v.target_path", idx)).(string), + IsFolder: d.Get(fmt.Sprintf("spec.0.external_resource.%v.is_folder", idx)).(bool), + ID: d.Get(fmt.Sprintf("spec.0.external_resource.%v.id", idx)).(string), + } + + pipeline.Spec.ExternalResources = append(pipeline.Spec.ExternalResources, codefreshExternalResource) + } + } + var codefreshTerminationPolicy []map[string]interface{} if _, ok := d.GetOk("spec.0.termination_policy.0.on_create_branch"); ok { diff --git a/codefresh/resource_project_test.go b/codefresh/resource_project_test.go index e94de0a..f035a4d 100644 --- a/codefresh/resource_project_test.go +++ b/codefresh/resource_project_test.go @@ -83,9 +83,9 @@ func TestAccCodefreshProject_Variables(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, ImportStateVerifyIgnore: []string{"encrypted_variables"}, }, { @@ -170,7 +170,7 @@ resource "codefresh_project" "test" { `, rName, tag1, tag2) } -func testAccCodefreshProjectBasicConfigVariables(rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name,encrytedVar1Value string) string { +func testAccCodefreshProjectBasicConfigVariables(rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name, encrytedVar1Value string) string { return fmt.Sprintf(` resource "codefresh_project" "test" { name = "%s" @@ -183,5 +183,5 @@ resource "codefresh_project" "test" { %q = %q } } -`, rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name,encrytedVar1Value) +`, rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name, encrytedVar1Value) }