forked from aliyun/terraform-provider-alicloud
/
data_source_alicloud_actiontrail.go
130 lines (118 loc) · 3.45 KB
/
data_source_alicloud_actiontrail.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
128
129
130
package alicloud
import (
"regexp"
"github.com/aliyun/alibaba-cloud-sdk-go/services/actiontrail"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func dataSourceAlicloudActiontrails() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudActionTrailsRead,
Schema: map[string]*schema.Schema{
"names": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"name_regex": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"actiontrails": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"event_rw": {
Type: schema.TypeString,
Computed: true,
},
"oss_bucket_name": {
Type: schema.TypeString,
Computed: true,
},
"oss_key_prefix": {
Type: schema.TypeString,
Computed: true,
},
"role_name": {
Type: schema.TypeString,
Computed: true,
},
"sls_project_arn": {
Type: schema.TypeString,
Computed: true,
},
"sls_write_role_arn": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudActionTrailsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := actiontrail.CreateDescribeTrailsRequest()
raw, err := client.WithActionTrailClient(func(actiontrailClient *actiontrail.Client) (interface{}, error) {
return actiontrailClient.DescribeTrails(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_actiontrails", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
response, _ := raw.(*actiontrail.DescribeTrailsResponse)
var filteredTrailList []actiontrail.TrailListItem
if nameRegex, ok := d.GetOk("name_regex"); ok && nameRegex.(string) != "" {
var r *regexp.Regexp
if nameRegex != "" {
r = regexp.MustCompile(nameRegex.(string))
}
for _, trail := range response.TrailList {
if r != nil && !r.MatchString(trail.Name) {
continue
}
filteredTrailList = append(filteredTrailList, trail)
}
} else {
filteredTrailList = response.TrailList
}
var ids []string
var filteredActionTrail []map[string]interface{}
for _, trail := range filteredTrailList {
mapping := map[string]interface{}{
"name": trail.Name,
"event_rw": trail.EventRW,
"oss_bucket_name": trail.OssBucketName,
"oss_key_prefix": trail.OssKeyPrefix,
"role_name": trail.RoleName,
"sls_project_arn": trail.SlsProjectArn,
"sls_write_role_arn": trail.SlsWriteRoleArn,
}
ids = append(ids, trail.Name)
filteredActionTrail = append(filteredActionTrail, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("actiontrails", filteredActionTrail); err != nil {
return WrapError(err)
}
if err := d.Set("names", ids); err != nil {
return WrapError(err)
}
// create a json file in current directory and write data source to it.
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), filteredActionTrail)
}
return nil
}