diff --git a/client/replication.go b/client/replication.go index f95e6fe..7502a2a 100755 --- a/client/replication.go +++ b/client/replication.go @@ -80,3 +80,13 @@ func GetReplicationBody(d *schema.ResourceData) models.ReplicationBody { log.Println(body) return body } + +func GetExecutionBody(d *schema.ResourceData) models.ExecutionBody { + + body := models.ExecutionBody{ + PolicyID: d.Get("policy_id").(int), + } + + log.Println(body) + return body +} diff --git a/docs/resources/replication.md b/docs/resources/replication.md index 1464f6b..db17fe4 100644 --- a/docs/resources/replication.md +++ b/docs/resources/replication.md @@ -73,6 +73,7 @@ The following arguments are supported: * **deletion** - (Optional) Specify whether to delete the remote resources when locally deleted. Can be set to `true` or `false` (Default: `false`) * **filters** - (Optional) A collection of `filters` block as documented below. +* **execute_on_changed** - (Optional) Specify whether to execute the replication rule if new or modified. Can be set to `true` or `false` (Default: `false`) --- diff --git a/models/replications.go b/models/replications.go index 2e70b91..53c81d8 100644 --- a/models/replications.go +++ b/models/replications.go @@ -1,6 +1,7 @@ package models var PathReplication = "/replication/policies" +var PathExecution = "/replication/executions" type ReplicationBody struct { Name string `json:"name,omitempty"` @@ -32,3 +33,7 @@ type ReplicationFilters struct { Value interface{} `json:"value,omitempty"` Decoration string `json:"decoration,omitempty"` } + +type ExecutionBody struct { + PolicyID int `json:"policy_id"` +} diff --git a/provider/resource_replication.go b/provider/resource_replication.go index 3961361..173b108 100644 --- a/provider/resource_replication.go +++ b/provider/resource_replication.go @@ -3,6 +3,9 @@ package provider import ( "encoding/json" "fmt" + "path/filepath" + "strconv" + "strings" "github.com/goharbor/terraform-provider-harbor/client" "github.com/goharbor/terraform-provider-harbor/models" @@ -98,6 +101,11 @@ func resourceReplication() *schema.Resource { Optional: true, Default: -1, }, + "execute_on_changed": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, }, Create: resourceReplicationCreate, Read: resourceReplicationRead, @@ -119,12 +127,27 @@ func resourceReplicationCreate(d *schema.ResourceData, m interface{}) error { return err } - id, err := client.GetID(headers) + location, err := client.GetID(headers) if err != nil { return err } - d.SetId(id) + if d.Get("execute_on_changed").(bool) { + + policyId, _ := strconv.Atoi(location[strings.LastIndex(location, "/")+1:]) + + body := models.ExecutionBody{ + PolicyID: policyId, + } + + _, _, _, err := apiClient.SendRequest("POST", models.PathExecution, body, 201) + + if err != nil { + return err + } + } + + d.SetId(location) return resourceReplicationRead(d, m) } @@ -188,6 +211,21 @@ func resourceReplicationUpdate(d *schema.ResourceData, m interface{}) error { return err } + if d.Get("execute_on_changed").(bool) { + + policyId, _ := strconv.Atoi(filepath.Base(d.Id())) + + body := models.ExecutionBody{ + PolicyID: policyId, + } + + _, _, _, err := apiClient.SendRequest("POST", models.PathExecution, body, 201) + + if err != nil { + return err + } + } + return resourceReplicationRead(d, m) }