Skip to content

Commit

Permalink
impelement external resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-medvedev-codefresh committed Mar 20, 2024
1 parent 843163f commit 2fb0793
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
13 changes: 12 additions & 1 deletion codefresh/cfclient/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
92 changes: 92 additions & 0 deletions codefresh/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions codefresh/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
{
Expand Down Expand Up @@ -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"
Expand All @@ -183,5 +183,5 @@ resource "codefresh_project" "test" {
%q = %q
}
}
`, rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name,encrytedVar1Value)
`, rName, var1Name, var1Value, var2Name, var2Value, encrytedVar1Name, encrytedVar1Value)
}

0 comments on commit 2fb0793

Please sign in to comment.