Skip to content

Commit

Permalink
Add namespace flattening options to replication body. (goharbor#253)
Browse files Browse the repository at this point in the history
Add namespace flattening options to replication body. Default is 0 aka No Flattening.

Signed-off-by: Andrew Wood <[email protected]>

Signed-off-by: Andrew Wood <[email protected]>
Signed-off-by: André Krüger <[email protected]>
  • Loading branch information
4n3w authored and 1337andre committed Nov 8, 2022
1 parent 601211e commit b4110fe
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
13 changes: 7 additions & 6 deletions client/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ func GetReplicationBody(d *schema.ResourceData) models.ReplicationBody {
schedule := d.Get("schedule").(string)

body := models.ReplicationBody{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Override: d.Get("override").(bool),
Enabled: d.Get("enabled").(bool),
Deletion: d.Get("deletion").(bool),
DestNamespace: d.Get("dest_namespace").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Override: d.Get("override").(bool),
Enabled: d.Get("enabled").(bool),
Deletion: d.Get("deletion").(bool),
DestNamespace: d.Get("dest_namespace").(string),
DestNamespaceReplace: d.Get("dest_namespace_replace").(int),
}

if action == "push" {
Expand Down
5 changes: 3 additions & 2 deletions docs/resources/replication.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ The following arguments are supported:

* **action** - (Required)

* **schedule** - (Optional) The scheduled time of when the container register will be push / pull. In cron base format. Hourly `"0 0 * * * *"`, Daily `"0 0 0 * * *"`, Monthly `"0 0 0 * * 0"`. Can be one of the follow. `event_based`, `manual`, `cron format` (Default: `manual`)
* **schedule** - (Optional) The scheduled time of when the container register will be push / pull. In cron base format. Hourly `"0 0 * * * *"`, Daily `"0 0 0 * * *"`, Monthly `"0 0 0 * * 0"`. Can be one of the following: `event_based`, `manual`, `cron format` (Default: `manual`)
* **override** - (Optional) Specify whether to override the resources at the destination if a resources with the same name exist. Can be set to `true` or `false` (Default: `true`)
* **enabled** - (Optional) Specify whether the replication is enabled. Can be set to `true` or `false` (Default: `true`)
* **description** - (Optional) Write about description of the replication policy.
* **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)
* **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
9 changes: 5 additions & 4 deletions models/replications.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type ReplicationBody struct {
DestRegistry struct {
ID int `json:"id,omitempty"`
} `json:"dest_registry,omitempty"`
DestNamespace string `json:"dest_namespace,omitempty"`
Trigger struct {
DestNamespace string `json:"dest_namespace,omitempty"`
DestNamespaceReplace int `json:"dest_namespace_replace_count"`
Trigger struct {
Type string `json:"type,omitempty"`
TriggerSettings struct {
Cron string `json:"cron,omitempty"`
Expand All @@ -26,7 +27,7 @@ type ReplicationBody struct {
}

type ReplicationFilters struct {
Type string `json:"type,omitempty"`
Value interface{} `json:"value,omitempty"`
Type string `json:"type,omitempty"`
Value interface{} `json:"value,omitempty"`
Decoration string `json:"decoration,omitempty"`
}
5 changes: 5 additions & 0 deletions provider/resource_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func resourceReplication() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"dest_namespace_replace": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
"override": {
Type: schema.TypeBool,
Optional: true,
Expand Down
46 changes: 45 additions & 1 deletion provider/resource_replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func testAccCheckReplicationDestroy(s *terraform.State) error {

resp, _, _, err := apiClient.SendRequest("GET", rs.Primary.ID, nil, 404)
if err != nil {
return fmt.Errorf("Resouse was not delete \n %s", resp)
return fmt.Errorf("Resource was not deleted \n %s", resp)
}

}
Expand Down Expand Up @@ -124,6 +124,29 @@ func TestDestinationNamespace(t *testing.T) {
})
}

func TestDestinationNamespaceReplaceCount(t *testing.T) {
var scheduleType = "* 0/15 * * * *"
var destNamespaceReplaceCount = 0

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
// CheckDestroy: testAccCheckLabelDestroy,
Steps: []resource.TestStep{
{
Config: testReplicationPolicyDestinationNamespaceWithReplaceCount(scheduleType, destNamespaceReplaceCount),
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists(harborReplicationPull),
resource.TestCheckResourceAttr(
harborReplicationPull, "schedule", scheduleType),
resource.TestCheckResourceAttr(
harborReplicationPull, "dest_namespace_replace", fmt.Sprintf("%d", destNamespaceReplaceCount)),
),
},
},
})
}

func testReplicationPolicyDestinationNamespace(scheduleType string, destNamepace string) string {
// endpoint := os.Getenv("HARBOR_REPLICATION_ENDPOINT")
endpoint := "https://hub.docker.com"
Expand All @@ -143,3 +166,24 @@ func testReplicationPolicyDestinationNamespace(scheduleType string, destNamepace
}
`, endpoint, scheduleType, destNamepace)
}

func testReplicationPolicyDestinationNamespaceWithReplaceCount(scheduleType string, destNamepace int) string {
// endpoint := os.Getenv("HARBOR_REPLICATION_ENDPOINT")
endpoint := "https://hub.docker.com"
return fmt.Sprintf(`
resource "harbor_registry" "main" {
provider_name = "docker-hub"
name = "docker-hub-test-rep-pol"
endpoint_url = "%s"
}
resource "harbor_replication" "pull" {
name = "test_pull"
action = "pull"
registry_id = harbor_registry.main.registry_id
schedule = "%s"
dest_namespace = "nobody_cares"
dest_namespace_replace = "%d"
}
`, endpoint, scheduleType, destNamepace)
}

0 comments on commit b4110fe

Please sign in to comment.