This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 27
/
data_source_alicloud_fc_triggers.go
192 lines (179 loc) · 4.8 KB
/
data_source_alicloud_fc_triggers.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package alicloud
import (
"regexp"
"github.com/aliyun/fc-go-sdk"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func dataSourceAlicloudFcTriggers() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudFcTriggersRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
},
"function_name": {
Type: schema.TypeString,
Required: true,
},
"name_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateRegexp,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
// Computed values
"triggers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"source_arn": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"invocation_role": {
Type: schema.TypeString,
Computed: true,
},
"config": {
Type: schema.TypeString,
Computed: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
"last_modification_time": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudFcTriggersRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
serviceName := d.Get("service_name").(string)
functionName := d.Get("function_name").(string)
var ids []string
var names []string
var triggerMappings []map[string]interface{}
nextToken := ""
// ids
idsMap := make(map[string]string)
if v, ok := d.GetOk("ids"); ok {
for _, vv := range v.([]interface{}) {
idsMap[vv.(string)] = vv.(string)
}
}
for {
request := fc.NewListTriggersInput(serviceName, functionName)
if nextToken != "" {
request.NextToken = &nextToken
}
var requestInfo *fc.Client
raw, err := client.WithFcClient(func(fcClient *fc.Client) (interface{}, error) {
requestInfo = fcClient
return fcClient.ListTriggers(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_fc_triggers", "ListTriggers", FcGoSdk)
}
addDebug("ListTriggers", raw, requestInfo, request)
response, _ := raw.(*fc.ListTriggersOutput)
if len(response.Triggers) < 1 {
break
}
for _, trigger := range response.Triggers {
var sourceARN, invocationRole string
if trigger.SourceARN != nil {
sourceARN = *trigger.SourceARN
}
if trigger.InvocationRole != nil {
invocationRole = *trigger.InvocationRole
}
mapping := map[string]interface{}{
"id": *trigger.TriggerID,
"name": *trigger.TriggerName,
"source_arn": sourceARN,
"type": *trigger.TriggerType,
"invocation_role": invocationRole,
"config": string(trigger.RawTriggerConfig),
"creation_time": *trigger.CreatedTime,
"last_modification_time": *trigger.LastModifiedTime,
}
nameRegex, ok := d.GetOk("name_regex")
if ok && nameRegex.(string) != "" {
var r *regexp.Regexp
if nameRegex != "" {
r = regexp.MustCompile(nameRegex.(string))
}
if r != nil && !r.MatchString(mapping["name"].(string)) {
continue
}
}
if len(idsMap) > 0 {
if _, ok := idsMap[*trigger.TriggerID]; !ok {
continue
}
}
triggerMappings = append(triggerMappings, mapping)
ids = append(ids, *trigger.TriggerID)
names = append(names, *trigger.TriggerName)
}
nextToken = ""
if response.NextToken != nil {
nextToken = *response.NextToken
}
if nextToken == "" {
break
}
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("triggers", triggerMappings); err != nil {
return WrapError(err)
}
if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}
if err := d.Set("names", names); 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), triggerMappings)
}
return nil
}