Skip to content

Commit

Permalink
Feat/cat resource (#2143)
Browse files Browse the repository at this point in the history
* feat: support cat

* faet: support task

* fix: Modify the interface DescribeProbeNodes to DescribeNodes

* fix: modify sdk

* fix: modify sdk

* fix: modify test

* fix: modify test

* fix: modify test

* fix: modify test

* fix: modify test

* fix: modify provider

* fix: modify provider

* fix: modify provider

* fix: modify provider

* fix: modify provider

* fix: modify go mod

* fix: modify go mod

* fix: add doc

* fix: add changelog

* Update 2143.txt

changelog update

---------

Co-authored-by: andrewjiang <[email protected]>
  • Loading branch information
gitmkn and andrew-tx authored Oct 8, 2023
1 parent f2e1fb2 commit 98dec7a
Show file tree
Hide file tree
Showing 19 changed files with 1,028 additions and 225 deletions.
11 changes: 11 additions & 0 deletions .changelog/2143.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:new-data-source
tencentcloud_cat_metric_data
```

```release-note:enhancement
datasource/tencentcloud_cat_node: Add computed `task_types`
```

```release-note:enhancement
resource/tencentcloud_cat_task_set: Support `suspend` and `resume` for dial test tasks
```
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm v1.0.624
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.0.756
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.409
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.520
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.760
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.591
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb v1.0.699
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn v1.0.539
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwch v1.0.745
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs v1.0.627
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfw v1.0.759
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs v1.0.600
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ciam v1.0.695
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.748
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.693
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.711
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.759
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.760
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.589
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.692
Expand Down Expand Up @@ -93,16 +94,14 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tsf v1.0.674
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.755
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.759
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata v1.0.725
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfw v1.0.759
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.759
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
gopkg.in/yaml.v2 v2.4.0
)


require (
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
4d63.com/gochecknoglobals v0.2.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.409 h1:ToZpNh7
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.409/go.mod h1:U24yUxCDruJLayOsP/onO2E/7+9ljeNsNO+phu+PeiM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.520 h1:n4FN0PI+1MVWi+RGQbD/cElXjquZQK0K1h1Z1nNWNWw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.520/go.mod h1:gzI+2Qd/iUfPPQQjW30k0G3mJ3m7tXeXrydJMm8jsOo=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.760 h1:oGY4IigfIw0iQKh3/cOY29KBeEeFbvJft69e0beyfdI=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.760/go.mod h1:SoBOZr6fklP8zeHaq7XOnCd5xBypU2R94nOPafYX1J4=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.591 h1:TPGLjH6wqkA5Iirl6xCxFkWwrtCZ8ZmeF7ASzqUbsZA=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.591/go.mod h1:nCAGfVTXZkVnurkbjF3b0FM6RwGztE9t8D9Ms1unaKo=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.592 h1:xowULQ7Mca1TjHyjAGe8RYjg37yhWpne5Q+TihFNHVA=
Expand Down Expand Up @@ -878,6 +880,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.758 h1:T7Ry
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.758/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.759 h1:flWgFybB3MYWFxwRO4yXbdiPT3SNwjSLuXCXsfs6kN4=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.759/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.760 h1:OyUPYgXBZNh3erZZ4jhpFH+aGtcCB1TUUtYX4WbR+xg=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.760/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624 h1:nEZqsoqt1pEoaP9JjkHQy3/H00suCfzlHW1qOm2nYD8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624/go.mod h1:+TXSVyeKwt1IhZRqKPbTREteBcP+K07Q846/ilNzLWA=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.589 h1:LZihgirMH0vsaGScYexxwY0fTss9vHaSZs/YOQUVESg=
Expand Down
154 changes: 154 additions & 0 deletions tencentcloud/data_source_tc_cat_metric_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
Use this data source to query detailed information of cat metric_data
Example Usage
```hcl
data "tencentcloud_cat_metric_data" "metric_data" {
analyze_task_type = "AnalyzeTaskType_Network"
metric_type = "gauge"
field = "avg(\"ping_time\")"
filters = [
"\"host\" = 'www.qq.com'",
"time >= now()-1h",
]
}
```
*/
package tencentcloud

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cat "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat/v20180409"
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func dataSourceTencentCloudCatMetricData() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCatMetricDataRead,
Schema: map[string]*schema.Schema{
"analyze_task_type": {
Required: true,
Type: schema.TypeString,
Description: "Analysis of task type, supported types: `AnalyzeTaskType_Network`: network quality, `AnalyzeTaskType_Browse`: page performance, `AnalyzeTaskType_Transport`: port performance, `AnalyzeTaskType_UploadDownload`: file transport, `AnalyzeTaskType_MediaStream`: audiovisual experience.",
},

"metric_type": {
Required: true,
Type: schema.TypeString,
Description: "Metric type, metrics queries are passed with gauge by default.",
},

"field": {
Required: true,
Type: schema.TypeString,
Description: "Detailed fields of metrics, specified metrics can be passed or aggregate metrics, such as avg(ping_time) means entire delay.",
},

"filter": {
Optional: true,
Type: schema.TypeString,
Description: "Filter conditions can be passed as a single filter or multiple parameters concatenated together.",
},

"group_by": {
Optional: true,
Type: schema.TypeString,
Description: "Aggregation time, such as 1m, 1d, 30d, and so on.",
},

"filters": {
Required: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Multiple condition filtering, supports combining multiple filtering conditions for query.",
},

"metric_set": {
Computed: true,
Type: schema.TypeString,
Description: "Return JSON string.",
},

"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func dataSourceTencentCloudCatMetricDataRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_cat_metric_data.read")()
defer inconsistentCheck(d, meta)()

logId := getLogId(contextNil)

ctx := context.WithValue(context.TODO(), logIdKey, logId)

paramMap := make(map[string]interface{})
if v, ok := d.GetOk("analyze_task_type"); ok {
paramMap["AnalyzeTaskType"] = helper.String(v.(string))
}

if v, ok := d.GetOk("metric_type"); ok {
paramMap["MetricType"] = helper.String(v.(string))
}

if v, ok := d.GetOk("field"); ok {
paramMap["Field"] = helper.String(v.(string))
}

if v, ok := d.GetOk("filter"); ok {
paramMap["Filter"] = helper.String(v.(string))
}

if v, ok := d.GetOk("group_by"); ok {
paramMap["GroupBy"] = helper.String(v.(string))
}

if v, ok := d.GetOk("filters"); ok {
filtersSet := v.(*schema.Set).List()
paramMap["Filters"] = helper.InterfacesStringsPoint(filtersSet)
}

service := CatService{client: meta.(*TencentCloudClient).apiV3Conn}

var metric *cat.DescribeProbeMetricDataResponseParams
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeCatMetricDataByFilter(ctx, paramMap)
if e != nil {
if sdkError, ok := e.(*sdkErrors.TencentCloudSDKError); ok {
if sdkError.Code == "FailedOperation.DbQueryFailed" {
return resource.NonRetryableError(e)
}
}
return retryError(e)
}
metric = result
return nil
})
if err != nil {
return err
}

var metricSet string
if metric != nil && metric.MetricSet != nil {
metricSet = *metric.MetricSet
_ = d.Set("metric_set", metric.MetricSet)
}

d.SetId(helper.DataResourceIdsHash([]string{metricSet}))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := writeToFile(output.(string), metricSet); e != nil {
return e
}
}
return nil
}
83 changes: 83 additions & 0 deletions tencentcloud/data_source_tc_cat_metric_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tencentcloud

import (
"testing"

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

// go test -test.run TestAccTencentCloudCatMetricDataDataSource_basic -v
func TestAccTencentCloudCatMetricDataDataSource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCatMetricDataDataSource,
Check: resource.ComposeTestCheckFunc(
testAccCheckTencentCloudDataSourceID("data.tencentcloud_cat_metric_data.metric_data"),
resource.TestCheckResourceAttrSet("data.tencentcloud_cat_metric_data.metric_data", "metric_set"),
),
},
},
})
}

const testAccCatMetricDataDataSource = `
resource "tencentcloud_cat_task_set" "task_set" {
interval = 1
nodes = [
"12136",
"12137",
]
parameters = jsonencode(
{
blackList = ""
filterIp = 0
grabBag = 0
ipType = 0
netDigOn = 1
netDnsNs = ""
netDnsOn = 1
netDnsQuerymethod = 1
netDnsServer = 2
netDnsTimeout = 5
netIcmpActivex = 0
netIcmpActivexStr = ""
netIcmpDataCut = 1
netIcmpInterval = 0.5
netIcmpNum = 20
netIcmpOn = 1
netIcmpSize = 32
netIcmpTimeout = 20
netTracertNum = 30
netTracertOn = 1
netTracertTimeout = 60
whiteList = ""
}
)
tags = {}
task_category = 1
task_type = 5
batch_tasks {
name = "terraform-test"
target_address = "www.baidu.com"
}
}
data "tencentcloud_cat_metric_data" "metric_data" {
analyze_task_type = "AnalyzeTaskType_Network"
metric_type = "gauge"
field = "avg(\"ping_time\")"
filters = [
"\"host\" = 'www.baidu.com'",
"time >= now()-1h",
]
depends_on = [ tencentcloud_cat_task_set.task_set ]
}
`
32 changes: 31 additions & 1 deletion tencentcloud/data_source_tc_cat_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func dataSourceTencentCloudCatNode() *schema.Resource {
Computed: true,
Description: "Node status: 1=running, 2=offline.",
},
"task_types": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeInt},
Computed: true,
Description: "The task types supported by the node. `1`: page performance, `2`: file upload, `3`: file download, `4`: port performance, `5`: network quality, `6`: audio and video experience.",
},
},
},
},
Expand Down Expand Up @@ -158,7 +164,7 @@ func dataSourceTencentCloudCatNodeRead(d *schema.ResourceData, meta interface{})

var nodeSets []*cat.NodeDefine
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
results, e := catService.DescribeCatNodeByFilter(ctx, paramMap)
results, e := catService.DescribeCatProbeNodeByFilter(ctx, paramMap)
if e != nil {
return retryError(e)
}
Expand All @@ -170,6 +176,20 @@ func dataSourceTencentCloudCatNodeRead(d *schema.ResourceData, meta interface{})
return err
}

var nodeSetExt []*cat.NodeDefineExt
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
results, e := catService.DescribeCatNodeByFilter(ctx, paramMap)
if e != nil {
return retryError(e)
}
nodeSetExt = results
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read Cat nodeSet failed, reason:%+v", logId, err)
return err
}

ids := make([]string, 0, len(nodeSets))
nodeSetList := make([]map[string]interface{}, 0, len(nodeSets))
if nodeSets != nil {
Expand Down Expand Up @@ -205,6 +225,16 @@ func dataSourceTencentCloudCatNodeRead(d *schema.ResourceData, meta interface{})
if nodeSet.NodeDefineStatus != nil {
nodeSetMap["node_define_status"] = nodeSet.NodeDefineStatus
}

for _, node := range nodeSetExt {
if *node.Code == *nodeSet.Code {
if node.TaskTypes != nil {
nodeSetMap["task_types"] = node.TaskTypes
}
break
}
}

ids = append(ids, *nodeSet.Name)
nodeSetList = append(nodeSetList, nodeSetMap)
}
Expand Down
Loading

0 comments on commit 98dec7a

Please sign in to comment.