From c5b54268b2fbd00af9d0cd112390df53ddc6fef8 Mon Sep 17 00:00:00 2001 From: Brett Wright Date: Mon, 13 Dec 2021 13:07:58 +0100 Subject: [PATCH 1/4] Fixed the import of replharbor_replication --- docs/resources/replication.md | 2 +- models/replications.go | 3 ++- provider/resource_replication.go | 28 +++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/docs/resources/replication.md b/docs/resources/replication.md index 93c7a05..abccfdc 100644 --- a/docs/resources/replication.md +++ b/docs/resources/replication.md @@ -76,5 +76,5 @@ In addition to all argument, the following attributes are exported: Harbor project can be imported using the `replication id` eg, ` -terraform import harbor_project.main /registries/7 +terraform import harbor_project.main /replication/policies/1 ` diff --git a/models/replications.go b/models/replications.go index 99f1443..26f7c78 100644 --- a/models/replications.go +++ b/models/replications.go @@ -5,6 +5,7 @@ var PathReplication = "/replication/policies" type ReplicationBody struct { Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` + ID int `json:"id"` SrcRegistry struct { ID int `json:"id,omitempty"` } `json:"src_registry,omitempty"` @@ -18,7 +19,7 @@ type ReplicationBody struct { Cron string `json:"cron,omitempty"` } `json:"trigger_settings,omitempty"` } `json:"trigger,omitempty"` - Enabled bool `json:"enabled,omitempty"` + Enabled bool `json:"enabled"` Deletion bool `json:"deletion,omitempty"` Override bool `json:"override,omitempty"` Filters []ReplicationFilters `json:"filters,omitempty"` diff --git a/provider/resource_replication.go b/provider/resource_replication.go index 175d92a..d325f1d 100644 --- a/provider/resource_replication.go +++ b/provider/resource_replication.go @@ -89,6 +89,9 @@ func resourceReplication() *schema.Resource { Read: resourceReplicationRead, Update: resourceReplicationUpdate, Delete: resourceReplicationDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, } } @@ -126,7 +129,30 @@ func resourceReplicationRead(d *schema.ResourceData, m interface{}) error { return fmt.Errorf("Resource not found %s", d.Id()) } - d.Set("replication_policy_id", jsonData.ID) + var jsonDataReplication models.ReplicationBody + err = json.Unmarshal([]byte(resp), &jsonDataReplication) + if err != nil { + return fmt.Errorf("Resource not found %s", d.Id()) + } + + destRegistryID := jsonDataReplication.DestRegistry.ID + + if destRegistryID == 0 { + d.Set("action", "pull") + d.Set("registry_id", jsonDataReplication.SrcRegistry.ID) + + } else { + d.Set("action", "push") + d.Set("registry_id", destRegistryID) + } + + d.Set("replication_policy_id", jsonDataReplication.ID) + d.Set("enabled", jsonDataReplication.Enabled) + d.Set("name", jsonDataReplication.Name) + d.Set("deletion", jsonDataReplication.Deletion) + d.Set("override", jsonDataReplication.Override) + d.Set("schedule", jsonDataReplication.Trigger.Type) + return nil } From c876a7fb8bf52177933255b1e1c753399097aeaa Mon Sep 17 00:00:00 2001 From: Brett Wright Date: Mon, 13 Dec 2021 13:30:35 +0100 Subject: [PATCH 2/4] fixed set schedule --- provider/resource_replication.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/provider/resource_replication.go b/provider/resource_replication.go index d325f1d..8ca5d5d 100644 --- a/provider/resource_replication.go +++ b/provider/resource_replication.go @@ -146,12 +146,17 @@ func resourceReplicationRead(d *schema.ResourceData, m interface{}) error { d.Set("registry_id", destRegistryID) } + if jsonDataReplication.Trigger.Type != "manual" { + d.Set("schedule", jsonDataReplication.Trigger.TriggerSettings.Cron) + } else { + d.Set("schedule", "manual") + } + d.Set("replication_policy_id", jsonDataReplication.ID) d.Set("enabled", jsonDataReplication.Enabled) d.Set("name", jsonDataReplication.Name) d.Set("deletion", jsonDataReplication.Deletion) d.Set("override", jsonDataReplication.Override) - d.Set("schedule", jsonDataReplication.Trigger.Type) return nil } From 42db7ea8462ad99a734955989739a42f383214b8 Mon Sep 17 00:00:00 2001 From: Brett Wright Date: Mon, 13 Dec 2021 13:39:28 +0100 Subject: [PATCH 3/4] hmmmm --- provider/resource_replication.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/resource_replication.go b/provider/resource_replication.go index 8ca5d5d..c222711 100644 --- a/provider/resource_replication.go +++ b/provider/resource_replication.go @@ -146,7 +146,7 @@ func resourceReplicationRead(d *schema.ResourceData, m interface{}) error { d.Set("registry_id", destRegistryID) } - if jsonDataReplication.Trigger.Type != "manual" { + if jsonDataReplication.Trigger.Type == "scheduled" { d.Set("schedule", jsonDataReplication.Trigger.TriggerSettings.Cron) } else { d.Set("schedule", "manual") From da18ba62db76117e93db5c280f3e0d4e01f5794c Mon Sep 17 00:00:00 2001 From: Brett Wright Date: Mon, 13 Dec 2021 13:51:37 +0100 Subject: [PATCH 4/4] updated test --- provider/resource_replication_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/resource_replication_test.go b/provider/resource_replication_test.go index d4c9733..52c837a 100644 --- a/provider/resource_replication_test.go +++ b/provider/resource_replication_test.go @@ -102,7 +102,7 @@ func testAccCheckReplicationUpdate() string { } func TestDestinationNamespace(t *testing.T) { - var scheduleType = "event_based" + var scheduleType = "* 0/15 * * * *" var destNamepace = "gcp-project" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) },