Skip to content

Commit

Permalink
Issue #258 : Support harbor_replication execution (#398)
Browse files Browse the repository at this point in the history
Fix issue #258

Signed-off-by: Djamox <[email protected]>
Co-authored-by: Florian Blampey <[email protected]>
  • Loading branch information
gaglimax and flbla authored Dec 11, 2023
1 parent 81a9b9e commit 3233577
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
10 changes: 10 additions & 0 deletions client/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions docs/resources/replication.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

---

Expand Down
5 changes: 5 additions & 0 deletions models/replications.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

var PathReplication = "/replication/policies"
var PathExecution = "/replication/executions"

type ReplicationBody struct {
Name string `json:"name,omitempty"`
Expand Down Expand Up @@ -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"`
}
42 changes: 40 additions & 2 deletions provider/resource_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 3233577

Please sign in to comment.