-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdata_source_tc_audits.go
127 lines (111 loc) · 3.55 KB
/
data_source_tc_audits.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package audit
import (
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
audit "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit/v20190319"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
)
func DataSourceTencentCloudAudits() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudAuditsRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the audits.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
// Computed values
"audit_list": {
Type: schema.TypeList,
Computed: true,
Description: "Information list of the dedicated audits.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the audit.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the audit.",
},
"cos_bucket": {
Type: schema.TypeString,
Computed: true,
Description: "Cos bucket name where audit save logs.",
},
"log_file_prefix": {
Type: schema.TypeString,
Computed: true,
Description: "Prefix of the log file of the audit.",
},
"audit_switch": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicate whether audit start logging or not.",
},
},
},
},
},
}
}
func dataSourceTencentCloudAuditsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_audits.read")()
logId := tccommon.GetLogId(tccommon.ContextNil)
name := d.Get("name").(string)
request := audit.NewListAuditsRequest()
var response *audit.ListAuditsResponse
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
ratelimit.Check(request.GetAction())
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAuditClient().ListAudits(request)
if e != nil {
log.Printf("[CRITAL]%s %s fail, reason:%s\n", logId, request.GetAction(), e.Error())
return tccommon.RetryError(e)
}
response = result
return nil
})
if err != nil {
return err
}
result := response.Response.AuditSummarys
ids := make([]string, 0, len(result))
auditList := make([]map[string]interface{}, 0, len(result))
for _, audit := range result {
if name != "" && name != *audit.AuditName {
continue
}
mapping := map[string]interface{}{
"id": audit.AuditName,
"name": audit.AuditName,
"audit_switch": int(*audit.AuditStatus) > 0,
"log_file_prefix": *audit.LogFilePrefix,
"cos_bucket": audit.CosBucketName,
}
auditList = append(auditList, mapping)
ids = append(ids, *audit.AuditName)
}
d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("audit_list", auditList); e != nil {
log.Printf("[CRITAL]%s provider set audit list fail, reason:%s\n", logId, e)
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), auditList); e != nil {
return e
}
}
return nil
}