forked from aliyun/terraform-provider-alicloud
/
data_source_alicloud_cms_alarm_contact_groups.go
164 lines (152 loc) · 4.25 KB
/
data_source_alicloud_cms_alarm_contact_groups.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
package alicloud
import (
"regexp"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cms"
"github.com/gunnerliu/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func dataSourceAlicloudCmsAlarmContactGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudCmsAlarmContactGroupsRead,
Schema: map[string]*schema.Schema{
"name_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateRegexp,
ForceNew: true,
},
"names": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"ids": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"alarm_contact_group_name": {
Type: schema.TypeString,
Computed: true,
},
"contacts": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"describe": {
Type: schema.TypeString,
Computed: true,
},
"enable_subscribed": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudCmsAlarmContactGroupsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := cms.CreateDescribeContactGroupListRequest()
request.PageSize = requests.NewInteger(PageSizeLarge)
request.PageNumber = requests.NewInteger(1)
var objects []cms.ContactGroup
var alarmContactGroupNameRegex *regexp.Regexp
if v, ok := d.GetOk("name_regex"); ok {
r, err := regexp.Compile(v.(string))
if err != nil {
return WrapError(err)
}
alarmContactGroupNameRegex = r
}
idsMap := make(map[string]string)
if v, ok := d.GetOk("ids"); ok {
for _, vv := range v.([]interface{}) {
if vv == nil {
continue
}
idsMap[vv.(string)] = vv.(string)
}
}
var response *cms.DescribeContactGroupListResponse
for {
raw, err := client.WithCmsClient(func(cmsClient *cms.Client) (interface{}, error) {
return cmsClient.DescribeContactGroupList(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_cms_alarm_contact_groups", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
response, _ = raw.(*cms.DescribeContactGroupListResponse)
for _, item := range response.ContactGroupList.ContactGroup {
if alarmContactGroupNameRegex != nil {
if !alarmContactGroupNameRegex.MatchString(item.Name) {
continue
}
}
if len(idsMap) > 0 {
if _, ok := idsMap[item.Name]; !ok {
continue
}
}
objects = append(objects, item)
}
if len(response.ContactGroupList.ContactGroup) < PageSizeLarge {
break
}
page, err := getNextpageNumber(request.PageNumber)
if err != nil {
return WrapError(err)
}
request.PageNumber = page
}
ids := make([]string, 0)
names := make([]string, 0)
s := make([]map[string]interface{}, 0)
for _, object := range objects {
mapping := map[string]interface{}{
"id": object.Name,
"alarm_contact_group_name": object.Name,
"contacts": object.Contacts.Contact,
"describe": object.Describe,
"enable_subscribed": object.EnableSubscribed,
}
ids = append(ids, object.Name)
names = append(names, object.Name)
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}
if err := d.Set("names", names); err != nil {
return WrapError(err)
}
if err := d.Set("groups", s); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}