Skip to content

Commit 70ec669

Browse files
gitmknanonymous
andauthored
feat: support tat (#1665)
Co-authored-by: anonymous <anonymous@mail.org>
1 parent 147ef42 commit 70ec669

25 files changed

+1925
-2
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ require (
7373
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssm v1.0.199
7474
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts v1.0.524
7575
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag v1.0.199
76-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat v1.0.538
76+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat v1.0.634
7777
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199
7878
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcm v1.0.547
7979
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.593

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag v1.0.199 h1:/s979h2
617617
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag v1.0.199/go.mod h1:sX14+NSvMjOhNFaMtP2aDy6Bss8PyFXij21gpY6+DAs=
618618
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat v1.0.538 h1:BkEs83oSi7mPbLjDc1rD1GPaIvzRyoFNAsOYZYnRs6E=
619619
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat v1.0.538/go.mod h1:mCkpKMR0T6th92NQbG0gnylJmq2BPRryQiMIgot1qtU=
620+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat v1.0.634 h1:GJDzXxKloZeM8fN+qlIspPnZbUw1lOZGe7jGqfFbQMM=
621+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat v1.0.634/go.mod h1:yX1elLeYvjmtPvgBVCOyYxyy1u2XEKfXaEiZXIWRCKw=
620622
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199 h1:i17zUWDw6iN7EMkQMGDXIXpur73vwUvbZrX4M5S0xhQ=
621623
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199/go.mod h1:PUgbrkzA9IaKBj1urk+W4L6Jr5TuBhQ4xB/96QvLf/U=
622624
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcm v1.0.547 h1:6bukohygmfu4riewOMCuYYZSkg3vTad8PCjpGyWD0Gs=

tencentcloud/basic_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ const (
859859
// TAT
860860
const (
861861
defaultInstanceId = "ins-881b1c8w"
862+
defaultCommandId = "cmd-rxbs7f5z"
862863
)
863864

864865
// End of TAT
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*
2+
Use this data source to query detailed information of tat agent
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_tat_agent" "agent" {
8+
# instance_ids = ["ins-f9jr4bd2"]
9+
filters {
10+
name = "environment"
11+
values = ["Linux"]
12+
}
13+
}
14+
```
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
21+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
22+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
23+
tat "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat/v20201028"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
25+
)
26+
27+
func dataSourceTencentCloudTatAgent() *schema.Resource {
28+
return &schema.Resource{
29+
Read: dataSourceTencentCloudTatAgentRead,
30+
Schema: map[string]*schema.Schema{
31+
"instance_ids": {
32+
Optional: true,
33+
Type: schema.TypeSet,
34+
Elem: &schema.Schema{
35+
Type: schema.TypeString,
36+
},
37+
Description: "List of instance IDs for the query.",
38+
},
39+
40+
"filters": {
41+
Optional: true,
42+
Type: schema.TypeList,
43+
Description: "Filter conditions. agent-status - String - Required: No - (Filter condition) Filter by agent status. Valid values: Online, Offline. environment - String - Required: No - (Filter condition) Filter by the agent environment. Valid value: Linux. instance-id - String - Required: No - (Filter condition) Filter by the instance ID. Up to 10 Filters allowed in one request. For each filter, five Filter.Values can be specified. InstanceIds and Filters cannot be specified at the same time.",
44+
Elem: &schema.Resource{
45+
Schema: map[string]*schema.Schema{
46+
"name": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
Description: "Field to be filtered.",
50+
},
51+
"values": {
52+
Type: schema.TypeSet,
53+
Elem: &schema.Schema{
54+
Type: schema.TypeString,
55+
},
56+
Required: true,
57+
Description: "Filter values of the field.",
58+
},
59+
},
60+
},
61+
},
62+
63+
"automation_agent_set": {
64+
Computed: true,
65+
Type: schema.TypeList,
66+
Description: "List of agent message.",
67+
Elem: &schema.Resource{
68+
Schema: map[string]*schema.Schema{
69+
"instance_id": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: "InstanceId.",
73+
},
74+
"version": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
Description: "Agent version.",
78+
},
79+
"last_heartbeat_time": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
Description: "Time of last heartbeat.",
83+
},
84+
"agent_status": {
85+
Type: schema.TypeString,
86+
Computed: true,
87+
Description: "Agent status.Ranges:&lt;li&gt; Online:Online&lt;li&gt; Offline:Offline.",
88+
},
89+
"environment": {
90+
Type: schema.TypeString,
91+
Computed: true,
92+
Description: "Environment for Agent.Ranges:&lt;li&gt; Linux:Linux instance&lt;li&gt; Windows:Windows instance.",
93+
},
94+
"support_features": {
95+
Type: schema.TypeSet,
96+
Elem: &schema.Schema{
97+
Type: schema.TypeString,
98+
},
99+
Computed: true,
100+
Description: "List of feature Agent support.",
101+
},
102+
},
103+
},
104+
},
105+
106+
"result_output_file": {
107+
Type: schema.TypeString,
108+
Optional: true,
109+
Description: "Used to save results.",
110+
},
111+
},
112+
}
113+
}
114+
115+
func dataSourceTencentCloudTatAgentRead(d *schema.ResourceData, meta interface{}) error {
116+
defer logElapsed("data_source.tencentcloud_tat_agent.read")()
117+
defer inconsistentCheck(d, meta)()
118+
119+
logId := getLogId(contextNil)
120+
121+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
122+
123+
paramMap := make(map[string]interface{})
124+
if v, ok := d.GetOk("instance_ids"); ok {
125+
instanceIdsSet := v.(*schema.Set).List()
126+
paramMap["InstanceIds"] = helper.InterfacesStringsPoint(instanceIdsSet)
127+
}
128+
129+
if v, ok := d.GetOk("filters"); ok {
130+
filtersSet := v.([]interface{})
131+
tmpSet := make([]*tat.Filter, 0, len(filtersSet))
132+
133+
for _, item := range filtersSet {
134+
filter := tat.Filter{}
135+
filterMap := item.(map[string]interface{})
136+
137+
if v, ok := filterMap["name"]; ok {
138+
filter.Name = helper.String(v.(string))
139+
}
140+
if v, ok := filterMap["values"]; ok {
141+
valuesSet := v.(*schema.Set).List()
142+
filter.Values = helper.InterfacesStringsPoint(valuesSet)
143+
}
144+
tmpSet = append(tmpSet, &filter)
145+
}
146+
paramMap["filters"] = tmpSet
147+
}
148+
149+
service := TatService{client: meta.(*TencentCloudClient).apiV3Conn}
150+
151+
var automationAgentSet []*tat.AutomationAgentInfo
152+
153+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
154+
result, e := service.DescribeTatAgentByFilter(ctx, paramMap)
155+
if e != nil {
156+
return retryError(e)
157+
}
158+
automationAgentSet = result
159+
return nil
160+
})
161+
if err != nil {
162+
return err
163+
}
164+
165+
ids := make([]string, 0, len(automationAgentSet))
166+
tmpList := make([]map[string]interface{}, 0, len(automationAgentSet))
167+
168+
if automationAgentSet != nil {
169+
for _, automationAgentInfo := range automationAgentSet {
170+
automationAgentInfoMap := map[string]interface{}{}
171+
172+
if automationAgentInfo.InstanceId != nil {
173+
automationAgentInfoMap["instance_id"] = automationAgentInfo.InstanceId
174+
}
175+
176+
if automationAgentInfo.Version != nil {
177+
automationAgentInfoMap["version"] = automationAgentInfo.Version
178+
}
179+
180+
if automationAgentInfo.LastHeartbeatTime != nil {
181+
automationAgentInfoMap["last_heartbeat_time"] = automationAgentInfo.LastHeartbeatTime
182+
}
183+
184+
if automationAgentInfo.AgentStatus != nil {
185+
automationAgentInfoMap["agent_status"] = automationAgentInfo.AgentStatus
186+
}
187+
188+
if automationAgentInfo.Environment != nil {
189+
automationAgentInfoMap["environment"] = automationAgentInfo.Environment
190+
}
191+
192+
if automationAgentInfo.SupportFeatures != nil {
193+
automationAgentInfoMap["support_features"] = automationAgentInfo.SupportFeatures
194+
}
195+
196+
ids = append(ids, *automationAgentInfo.InstanceId)
197+
tmpList = append(tmpList, automationAgentInfoMap)
198+
}
199+
200+
_ = d.Set("automation_agent_set", tmpList)
201+
}
202+
203+
d.SetId(helper.DataResourceIdsHash(ids))
204+
output, ok := d.GetOk("result_output_file")
205+
if ok && output.(string) != "" {
206+
if e := writeToFile(output.(string), tmpList); e != nil {
207+
return e
208+
}
209+
}
210+
return nil
211+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudTatAgentDataSource_basic -v
10+
func TestAccTencentCloudTatAgentDataSource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccTatAgentDataSource,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_tat_agent.agent"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_tat_agent.agent", "automation_agent_set.0.agent_status"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_tat_agent.agent", "automation_agent_set.0.environment"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_tat_agent.agent", "automation_agent_set.0.instance_id"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_tat_agent.agent", "automation_agent_set.0.last_heartbeat_time"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_tat_agent.agent", "automation_agent_set.0.support_features.#"),
27+
resource.TestCheckResourceAttrSet("data.tencentcloud_tat_agent.agent", "automation_agent_set.0.version"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
const testAccTatAgentDataSource = `
35+
36+
data "tencentcloud_tat_agent" "agent" {
37+
# instance_ids = ["ins-f9jr4bd2"]
38+
filters {
39+
name = "environment"
40+
values = ["Linux"]
41+
}
42+
}
43+
44+
`

0 commit comments

Comments
 (0)