Skip to content

Commit

Permalink
feat(teo): [117892609] support realtime_log_delivery (#2697)
Browse files Browse the repository at this point in the history
* feat(teo): [117892609] support realtime_log_delivery

* feat: add changelog and doc
  • Loading branch information
gitmkn authored Jun 27, 2024
1 parent 7c93a93 commit 05c224a
Show file tree
Hide file tree
Showing 10 changed files with 1,564 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/2697.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
tencentcloud_teo_realtime_log_delivery
```
1 change: 1 addition & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,7 @@ func Provider() *schema.Provider {
"tencentcloud_teo_acceleration_domain": teo.ResourceTencentCloudTeoAccelerationDomain(),
"tencentcloud_teo_application_proxy": teo.ResourceTencentCloudTeoApplicationProxy(),
"tencentcloud_teo_application_proxy_rule": teo.ResourceTencentCloudTeoApplicationProxyRule(),
"tencentcloud_teo_realtime_log_delivery": teo.ResourceTencentCloudTeoRealtimeLogDelivery(),
"tencentcloud_tcm_mesh": tcm.ResourceTencentCloudTcmMesh(),
"tencentcloud_tcm_cluster_attachment": tcm.ResourceTencentCloudTcmClusterAttachment(),
"tencentcloud_tcm_prometheus_attachment": tcm.ResourceTencentCloudTcmPrometheusAttachment(),
Expand Down
1 change: 1 addition & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,7 @@ TencentCloud EdgeOne(TEO)
tencentcloud_teo_certificate_config
tencentcloud_teo_acceleration_domain
tencentcloud_teo_l4_proxy
tencentcloud_teo_realtime_log_delivery

TencentCloud ServiceMesh(TCM)
Data Source
Expand Down
1,065 changes: 1,065 additions & 0 deletions tencentcloud/services/teo/resource_tc_teo_realtime_log_delivery.go

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions tencentcloud/services/teo/resource_tc_teo_realtime_log_delivery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Provides a resource to create a teo teo_realtime_log_delivery

Example Usage

```hcl
resource "tencentcloud_teo_realtime_log_delivery" "teo_realtime_log_delivery" {
area = "overseas"
delivery_status = "disabled"
entity_list = [
"sid-2yvhjw98uaco",
]
fields = [
"ServiceID",
"ConnectTimeStamp",
"DisconnetTimeStamp",
"DisconnetReason",
"ClientRealIP",
"ClientRegion",
"EdgeIP",
"ForwardProtocol",
"ForwardPort",
"SentBytes",
"ReceivedBytes",
"LogTimeStamp",
]
log_type = "application"
sample = 0
task_name = "test"
task_type = "s3"
zone_id = "zone-2qtuhspy7cr6"
log_format {
field_delimiter = ","
format_type = "json"
record_delimiter = "\n"
record_prefix = "{"
record_suffix = "}"
}
s3 {
access_id = "xxxxxxxxxx"
access_key = "xxxxxxxxxx"
bucket = "test-1253833068"
compress_type = "gzip"
endpoint = "https://test-1253833068.cos.ap-nanjing.myqcloud.com"
region = "ap-nanjing"
}
}
```

Import

teo teo_realtime_log_delivery can be imported using the id, e.g.

```
terraform import tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery zoneId#taskId
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package teo

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
teo "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
)

func resourceTencentCloudTeoRealtimeLogDeliveryCreatePostHandleResponse0(ctx context.Context, resp *teo.CreateRealtimeLogDeliveryTaskResponse) error {
taskId := *resp.Response.TaskId
return checkRealtimeLogDeliveryStatus(ctx, taskId, "enabled")
}

func resourceTencentCloudTeoRealtimeLogDeliveryUpdatePostHandleResponse0(ctx context.Context, resp *teo.ModifyRealtimeLogDeliveryTaskResponse) error {
d := tccommon.ResourceDataFromContext(ctx)
if d == nil {
return fmt.Errorf("resource data can not be nil")
}
var taskId string
if v, ok := d.GetOk("task_id"); ok {
taskId = v.(string)
}
return checkRealtimeLogDeliveryStatus(ctx, taskId, "update")
}

func resourceTencentCloudTeoRealtimeLogDeliveryDeletePostHandleResponse0(ctx context.Context, resp *teo.DeleteRealtimeLogDeliveryTaskResponse) error {
d := tccommon.ResourceDataFromContext(ctx)
if d == nil {
return fmt.Errorf("resource data can not be nil")
}
var taskId string
if v, ok := d.GetOk("task_id"); ok {
taskId = v.(string)
}
return checkRealtimeLogDeliveryStatus(ctx, taskId, "deleted")
}

func checkRealtimeLogDeliveryStatus(ctx context.Context, taskId string, expectedStatuses ...string) error {
d := tccommon.ResourceDataFromContext(ctx)
if d == nil {
return fmt.Errorf("resource data can not be nil")
}
meta := tccommon.ProviderMetaFromContext(ctx)
if meta == nil {
return fmt.Errorf("provider meta can not be nil")
}

var zoneId string
if v, ok := d.GetOk("zone_id"); ok {
zoneId = v.(string)
}

if v, ok := d.GetOk("delivery_status"); ok && expectedStatuses[0] == "update" {
expectedStatuses = append(expectedStatuses, v.(string))
}

service := TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
return resource.Retry(6*tccommon.ReadRetryTimeout, func() *resource.RetryError {
instance, errRet := service.DescribeTeoRealtimeLogDeliveryById(ctx, zoneId, taskId)
if errRet != nil {
return tccommon.RetryError(errRet, tccommon.InternalError)
}

if instance == nil {
if expectedStatuses[0] != "deleted" {
return resource.NonRetryableError(fmt.Errorf("RealtimeLogDeliveryTask data not found, taskId: %v", taskId))
}
return nil
}

for _, s := range expectedStatuses {
if s == *instance.DeliveryStatus {
return nil
}
}

return resource.RetryableError(fmt.Errorf("RealtimeLogDeliveryTask status is %v, retry...", *instance.DeliveryStatus))
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package teo_test

import (
"testing"

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

tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
)

func TestAccTencentCloudTeoRealtimeLogDeliveryResource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
tcacctest.AccPreCheck(t)
},
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{
{
Config: testAccTeoRealtimeLogDelivery,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "id"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "area", "overseas"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "delivery_status", "enabled"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "entity_list.#", "1"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "fields.#"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "log_type", "application"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "sample", "0"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "task_name", "test"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "task_type", "s3"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "log_format.#", "1"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.#", "1"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.access_id"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.access_key"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.bucket"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.compress_type"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.endpoint"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.region"),
),
},
{
ResourceName: "tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccTeoRealtimeLogDeliveryUp,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "id"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "area", "overseas"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "delivery_status", "disabled"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "entity_list.#", "1"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "fields.#"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "log_type", "application"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "sample", "0"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "task_name", "test"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "task_type", "s3"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "log_format.#", "1"),
resource.TestCheckResourceAttr("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.#", "1"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.access_id"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.access_key"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.bucket"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.compress_type"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.endpoint"),
resource.TestCheckResourceAttrSet("tencentcloud_teo_realtime_log_delivery.teo_realtime_log_delivery", "s3.0.region"),
),
},
},
})
}

const testAccTeoRealtimeLogDelivery = `
resource "tencentcloud_teo_realtime_log_delivery" "teo_realtime_log_delivery" {
area = "overseas"
delivery_status = "enabled"
entity_list = [
"sid-2yvhjw98uaco",
]
fields = [
"ServiceID",
"ConnectTimeStamp",
"DisconnetTimeStamp",
"DisconnetReason",
"ClientRealIP",
"ClientRegion",
"EdgeIP",
"ForwardProtocol",
"ForwardPort",
"SentBytes",
"ReceivedBytes",
"LogTimeStamp",
]
log_type = "application"
sample = 0
task_name = "test"
task_type = "s3"
zone_id = "zone-2qtuhspy7cr6"
log_format {
field_delimiter = ","
format_type = "json"
record_delimiter = "\n"
record_prefix = "{"
record_suffix = "}"
}
s3 {
access_id = "xxxxxxxxxx"
access_key = "xxxxxxxxxx"
bucket = "test-1253833068"
compress_type = "gzip"
endpoint = "https://test-1253833068.cos.ap-nanjing.myqcloud.com"
region = "ap-nanjing"
}
}
`

const testAccTeoRealtimeLogDeliveryUp = `
resource "tencentcloud_teo_realtime_log_delivery" "teo_realtime_log_delivery" {
area = "overseas"
delivery_status = "disabled"
entity_list = [
"sid-2yvhjw98uaco",
]
fields = [
"ServiceID",
"ConnectTimeStamp",
"DisconnetTimeStamp",
"DisconnetReason",
"ClientRealIP",
"ClientRegion",
"EdgeIP",
"ForwardProtocol",
"ForwardPort",
"SentBytes",
"ReceivedBytes",
"LogTimeStamp",
]
log_type = "application"
sample = 0
task_name = "test"
task_type = "s3"
zone_id = "zone-2qtuhspy7cr6"
log_format {
field_delimiter = ","
format_type = "json"
record_delimiter = "\n"
record_prefix = "{"
record_suffix = "}"
}
s3 {
access_id = "xxxxxxxxxx"
access_key = "xxxxxxxxxx"
bucket = "test-1253833068"
compress_type = "gzip"
endpoint = "https://test-1253833068.cos.ap-nanjing.myqcloud.com"
region = "ap-nanjing"
}
}
`
34 changes: 34 additions & 0 deletions tencentcloud/services/teo/service_tencentcloud_teo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,3 +1104,37 @@ func (me *TeoService) DescribeTeoL4ProxyById(ctx context.Context, zoneId string,
ret = response.Response.L4Proxies[0]
return
}

func (me *TeoService) DescribeTeoRealtimeLogDeliveryById(ctx context.Context, zoneId string, taskId string) (ret *teo.RealtimeLogDeliveryTask, errRet error) {
logId := tccommon.GetLogId(ctx)

request := teo.NewDescribeRealtimeLogDeliveryTasksRequest()
request.ZoneId = helper.String(zoneId)
advancedFilter := &teo.AdvancedFilter{
Name: helper.String("task-id"),
Values: []*string{helper.String(taskId)},
}
request.Filters = append(request.Filters, advancedFilter)

defer func() {
if errRet != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
}
}()

ratelimit.Check(request.GetAction())

response, err := me.client.UseTeoClient().DescribeRealtimeLogDeliveryTasks(request)
if err != nil {
errRet = err
return
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

if len(response.Response.RealtimeLogDeliveryTasks) < 1 {
return
}

ret = response.Response.RealtimeLogDeliveryTasks[0]
return
}
Loading

0 comments on commit 05c224a

Please sign in to comment.