Skip to content

Commit

Permalink
Fix creation of replications with dest_namespace_replace = 0 (#314)
Browse files Browse the repository at this point in the history
- Remove `omitempty` tag from `DestNamespaceReplace`

When the value of `dest_namespace_replace` is set to `0`, Harbor
actually creates a replication rule with `dest_namespace_replace_count`
of `-1` (flatten all levels).

It happens because `DestNamespaceReplace` field has `omitempty` tag,
which filters out the field when its value is set to `0`. Therefore
Harbor falls back to its default value of `-1`
([docs](https://github.com/goharbor/harbor/blob/cb0749c7abef5783e4f8857f1f564377f64337c1/api/v2.0/swagger.yaml#L7167-L7172)).

All other, non-zero values work fine.

Also, I see that `omitempty` tag was set some time ago by #282.

Thank you

---------

Signed-off-by: Dmitry G <[email protected]>
Co-authored-by: Florian Blampey <[email protected]>
  • Loading branch information
dmitry-g and flbla authored Jun 5, 2023
1 parent 07dc042 commit dd5d6a8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/resources/replication.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The following arguments are supported:
* **enabled** - (Optional) Specify whether the replication is enabled. Can be set to `true` or `false` (Default: `true`)
* **description** - (Optional) Description of the replication policy.
* **dest_namespace** - (Optional) Specify the destination namespace. if empty, the resource will be put under the same namespace as the source.
* **dest_namespace_replace** - (Optional) Specify the destination namespace flattening policy. Integers from `-1` to `3` are valid values in the harbor API. A value of `-1` will 'Flatten All Levels', `0` means 'No Flattening', `1` 'Flatten 1 Level', `2` 'Flatten 2 Levels', `3` 'Flatten 3 Levels' (Default: `0`, see [Replication Rules](https://goharbor.io/docs/latest/administration/configuring-replication/create-replication-rules/) for more details)
* **dest_namespace_replace** - (Optional) Specify the destination namespace flattening policy. Integers from `-1` to `3` are valid values in the harbor API. A value of `-1` will 'Flatten All Levels', `0` means 'No Flattening', `1` 'Flatten 1 Level', `2` 'Flatten 2 Levels', `3` 'Flatten 3 Levels' (Default: `-1`, see [Replication Rules](https://goharbor.io/docs/latest/administration/configuring-replication/create-replication-rules/) for more details)
* **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.
Expand Down
2 changes: 1 addition & 1 deletion models/replications.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ReplicationBody struct {
ID int `json:"id,omitempty"`
} `json:"dest_registry,omitempty"`
DestNamespace string `json:"dest_namespace,omitempty"`
DestNamespaceReplace int `json:"dest_namespace_replace_count,omitempty"`
DestNamespaceReplace int `json:"dest_namespace_replace_count"`
Trigger struct {
Type string `json:"type,omitempty"`
TriggerSettings struct {
Expand Down
4 changes: 3 additions & 1 deletion provider/resource_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func resourceReplication() *schema.Resource {
"speed": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
Default: -1,
},
},
Create: resourceReplicationCreate,
Expand Down Expand Up @@ -173,6 +173,8 @@ func resourceReplicationRead(d *schema.ResourceData, m interface{}) error {
d.Set("name", jsonDataReplication.Name)
d.Set("deletion", jsonDataReplication.Deletion)
d.Set("override", jsonDataReplication.Override)
d.Set("dest_namespace", jsonDataReplication.DestNamespace)
d.Set("dest_namespace_replace", jsonDataReplication.DestNamespaceReplace)

return nil
}
Expand Down

0 comments on commit dd5d6a8

Please sign in to comment.