-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Log Rotation configuration (#339)
Add support for Log Rotation configuration #315 --------- Signed-off-by: flbla <[email protected]> Signed-off-by: Florian Blampey <[email protected]>
- Loading branch information
Showing
8 changed files
with
202 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Resource: harbor_purge_audit_log | ||
|
||
## Example Usage | ||
```hcl | ||
resource "harbor_purge_audit_log" "main" { | ||
schedule = "Daily" | ||
audit_retention_hour = 24 | ||
include_operations = "create,pull" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
The following arguments are supported: | ||
* **schedule** - (Required) Sets the schedule how often the Garbage Collection will run. Can be to `"Hourly"`, `"Daily"`, `"Weekly"` or can be a custom cron string ie, `"5 4 * * *"` | ||
|
||
* **audit_retention_hour** - (Required) to configure how long audit logs should be kept. For example, if you set this to 24 Harbor will only purge audit logs that are 24 or more hours old. | ||
|
||
* **include_operations** - (Required) valid values are `create` `delete` `pull`, thoses values can be comma separated. When Create, Delete, or Pull is set, Harbor will include audit logs for those operations in the purge. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package provider | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/goharbor/terraform-provider-harbor/client" | ||
"github.com/goharbor/terraform-provider-harbor/models" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourcePurgeAudit() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"schedule": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"audit_retention_hour": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
}, | ||
"include_operations": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateIncludeOperations, | ||
}, | ||
}, | ||
Create: resourcePurgeAuditCreate, | ||
Read: resourcePurgeAuditRead, | ||
Update: resourcePurgeAuditUpdate, | ||
Delete: resourcePurgeAuditDelete, | ||
} | ||
} | ||
|
||
func resourcePurgeAuditCreate(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*client.Client) | ||
err := apiClient.SetSchedule(d, "purgeaudit") | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId(models.PathPurgeAudit) | ||
return resourcePurgeAuditRead(d, m) | ||
} | ||
|
||
func resourcePurgeAuditRead(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*client.Client) | ||
|
||
resp, _, respCode, err := apiClient.SendRequest("GET", models.PathPurgeAudit, nil, 200) | ||
if respCode == 404 && err != nil { | ||
d.SetId("") | ||
return fmt.Errorf("resource not found %s", d.Id()) | ||
} | ||
if len(resp) == 0 { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
var jsonData models.SystemBody | ||
err = json.Unmarshal([]byte(resp), &jsonData) | ||
if err != nil { | ||
return err | ||
} | ||
jobParameters := jsonData.JobParameters | ||
|
||
var jsonJobParameters models.JobParameters | ||
err = json.Unmarshal([]byte(jobParameters), &jsonJobParameters) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
if jsonData.Schedule.Type == "Custom" { | ||
d.Set("schedule", jsonData.Schedule.Cron) | ||
} else { | ||
d.Set("schedule", jsonData.Schedule.Type) | ||
} | ||
d.Set("audit_retention_hour", jsonJobParameters.AuditRetentionHour) | ||
d.Set("include_operations", jsonJobParameters.IncludeOperations) | ||
return nil | ||
} | ||
|
||
func resourcePurgeAuditUpdate(d *schema.ResourceData, m interface{}) error { | ||
return resourcePurgeAuditCreate(d, m) | ||
} | ||
|
||
func resourcePurgeAuditDelete(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*client.Client) | ||
d.Set("schedule", "") | ||
err := apiClient.SetSchedule(d, "purgeaudit") | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func validateIncludeOperations(v interface{}, k string) (warns []string, errs []error) { | ||
includeOperations := v.(string) | ||
validValues := []string{"create", "pull", "delete"} | ||
|
||
ops := strings.Split(includeOperations, ",") | ||
for _, op := range ops { | ||
op = strings.TrimSpace(op) | ||
if !containsString(validValues, op) { | ||
errs = append(errs, fmt.Errorf("Invalid value %q in %q. Valid values are: create, pull, delete", op, k)) | ||
} | ||
} | ||
|
||
return warns, errs | ||
} | ||
|
||
func containsString(arr []string, value string) bool { | ||
for _, v := range arr { | ||
if v == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const resourcePurgeAuditMain = "harbor_purge_audit_log.main" | ||
|
||
func TestAccPurgeAuditUpdate(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
// CheckDestroy: testAccCheckLabelDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckPurgeAuditBasic(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckResourceExists(resourcePurgeAuditMain), | ||
resource.TestCheckResourceAttr( | ||
resourcePurgeAuditMain, "schedule", "Daily"), | ||
resource.TestCheckResourceAttr( | ||
resourcePurgeAuditMain, "audit_retention_hour", "24"), | ||
resource.TestCheckResourceAttr( | ||
resourcePurgeAuditMain, "include_operations", "create,pull"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPurgeAuditBasic() string { | ||
return fmt.Sprintf(` | ||
resource "harbor_purge_audit_log" "main" { | ||
schedule = "Daily" | ||
audit_retention_hour = 24 | ||
include_operations = "create,pull" | ||
} | ||
`) | ||
} |