Skip to content

Commit

Permalink
feat(dds): support to delete audit log (#5841)
Browse files Browse the repository at this point in the history
  • Loading branch information
saf3dfsa authored Nov 13, 2024
1 parent 8d2d321 commit 3035d16
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/resources/dds_audit_log_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
subcategory: "Document Database Service (DDS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_dds_audit_log_delete"
description: |-
Manages a DDS audit log delete resource within HuaweiCloud.
---

# huaweicloud_dds_audit_log_delete

Manages a DDS audit log delete resource within HuaweiCloud.

## Example Usage

```hcl
variable "instance_id" {}
variable "file_names" {}
resource "huaweicloud_dds_audit_log_delete" "test" {
instance_id = var.instance_id
file_names = var.file_names
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
If omitted, the provider-level region will be used.
Changing this creates a new resource.

* `instance_id` - (Required, String, ForceNew) Specifies the instance ID.
Changing this creates a new resource.

* `file_names` - (Required, List, ForceNew) Specifies the audit log file names.
Changing this creates a new resource.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The resource ID.

## Timeouts

This resource provides the following timeouts configuration options:

* `create` - Default is 50 minutes.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,7 @@ func Provider() *schema.Provider {
"huaweicloud_dds_backup": dds.ResourceDdsBackup(),
"huaweicloud_dds_parameter_template": dds.ResourceDdsParameterTemplate(),
"huaweicloud_dds_audit_log_policy": dds.ResourceDdsAuditLogPolicy(),
"huaweicloud_dds_audit_log_delete": dds.ResourceDDSAuditLogDelete(),
"huaweicloud_dds_lts_log": dds.ResourceDdsLtsLog(),
"huaweicloud_dds_instance_restart": dds.ResourceDDSInstanceRestart(),
"huaweicloud_dds_instance_internal_ip_modify": dds.ResourceDDSInstanceModifyIP(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dds

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDDSAuditLogDelete_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckDDSInstanceID(t)
acceptance.TestAccPreCheckDDSTimeRange(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccDDSAuditLogDelete_basic(),
},
},
})
}

func testAccDDSAuditLogDelete_basic() string {
return fmt.Sprintf(`
data "huaweicloud_dds_audit_logs" "test" {
instance_id = "%[1]s"
start_time = "%[2]s"
end_time = "%[3]s"
}
resource "huaweicloud_dds_audit_log_delete" "test" {
instance_id = "%[1]s"
file_names = [try(data.huaweicloud_dds_audit_logs.test.audit_logs[0].name, "")]
lifecycle {
ignore_changes = [
file_names,
]
}
}`, acceptance.HW_DDS_INSTANCE_ID, acceptance.HW_DDS_START_TIME, acceptance.HW_DDS_END_TIME)
}
125 changes: 125 additions & 0 deletions huaweicloud/services/dds/resource_huaweicloud_dds_audit_log_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package dds

import (
"context"
"net/http"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

// @API DDS DELETE /v3/{project_id}/instances/{instance_id}/auditlog
// @API DDS GET /v3/{project_id}/instances
func ResourceDDSAuditLogDelete() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDDSAuditLogDeleteCreate,
ReadContext: resourceDDSAuditLogDeleteRead,
DeleteContext: resourceDDSAuditLogDeleteDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(50 * time.Minute),
},

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"file_names": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func resourceDDSAuditLogDeleteCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := meta.(*config.Config)
client, err := conf.DdsV3Client(conf.GetRegion(d))
if err != nil {
return diag.Errorf("error creating DDS client: %s", err)
}

instId := d.Get("instance_id").(string)

deleteHttpUrl := "v3/{project_id}/instances/{instance_id}/auditlog"
deletePath := client.Endpoint + deleteHttpUrl
deletePath = strings.ReplaceAll(deletePath, "{project_id}", client.ProjectID)
deletePath = strings.ReplaceAll(deletePath, "{instance_id}", instId)

deleteOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: utils.RemoveNil(buildAuditLogDeleteBodyParams(d)),
}

// retry
retryFunc := func() (interface{}, bool, error) {
resp, err := client.Request("DELETE", deletePath, &deleteOpt)
retry, err := handleMultiOperationsError(err)
return resp, retry, err
}
deleteResp, err := common.RetryContextWithWaitForState(&common.RetryContextWithWaitForStateParam{
Ctx: ctx,
RetryFunc: retryFunc,
WaitFunc: ddsInstanceStateRefreshFunc(client, instId),
WaitTarget: []string{"normal"},
Timeout: d.Timeout(schema.TimeoutCreate),
DelayTimeout: 10 * time.Second,
PollInterval: 10 * time.Second,
})
if err != nil {
return diag.Errorf("error deleting audit log: %s", err)
}

// job ID is the ID for background task that are not perceived by users
deleteRespBody, err := utils.FlattenResponse(deleteResp.(*http.Response))
if err != nil {
return diag.Errorf("error flattening response: %s", err)
}
jobID := utils.PathSearch("job_id", deleteRespBody, "").(string)
if jobID == "" {
return diag.Errorf("unable to find job ID from API response")
}

d.SetId(jobID)

return nil
}

func buildAuditLogDeleteBodyParams(d *schema.ResourceData) map[string]interface{} {
return map[string]interface{}{
"file_names": d.Get("file_names"),
}
}

func resourceDDSAuditLogDeleteRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

func resourceDDSAuditLogDeleteDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
errorMsg := "Deleting audit log delete resource is not supported. The delete resource is only removed from the state," +
" the instance remains in the cloud."
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: errorMsg,
},
}
}

0 comments on commit 3035d16

Please sign in to comment.