-
Notifications
You must be signed in to change notification settings - Fork 533
/
data_source_alicloud_ess_notifications.go
129 lines (122 loc) · 3.81 KB
/
data_source_alicloud_ess_notifications.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
package alicloud
import (
"github.com/aliyun/alibaba-cloud-sdk-go/services/ess"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAlicloudEssNotifications() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudEssNotificationsRead,
Schema: map[string]*schema.Schema{
"scaling_group_id": {
Type: schema.TypeString,
Required: true,
},
"ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"notifications": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"notification_arn": {
Type: schema.TypeString,
Computed: true,
},
"notification_types": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"scaling_group_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudEssNotificationsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := ess.CreateDescribeNotificationConfigurationsRequest()
request.RegionId = client.RegionId
if scalingGroupId, ok := d.GetOk("scaling_group_id"); ok && scalingGroupId.(string) != "" {
request.ScalingGroupId = scalingGroupId.(string)
}
var allNotifications []ess.NotificationConfigurationModel
for {
raw, err := client.WithEssClient(func(essClient *ess.Client) (interface{}, error) {
return essClient.DescribeNotificationConfigurations(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_ess_notifications", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response := raw.(*ess.DescribeNotificationConfigurationsResponse)
if len(response.NotificationConfigurationModels.NotificationConfigurationModel) < 1 {
break
}
allNotifications = append(allNotifications, response.NotificationConfigurationModels.NotificationConfigurationModel...)
if len(response.NotificationConfigurationModels.NotificationConfigurationModel) < PageSizeLarge {
break
} else {
continue
}
}
var filteredNotifications = make([]ess.NotificationConfigurationModel, 0)
idsMap := make(map[string]string)
if ids, okIds := d.GetOk("ids"); okIds {
for _, i := range ids.([]interface{}) {
if i == nil {
continue
}
idsMap[i.(string)] = i.(string)
}
for _, n := range allNotifications {
if _, ok := idsMap[n.NotificationArn]; !ok {
continue
}
filteredNotifications = append(filteredNotifications, n)
}
} else {
filteredNotifications = allNotifications
}
return notificationsDescriptionAttribute(d, filteredNotifications, meta)
}
func notificationsDescriptionAttribute(d *schema.ResourceData, notifications []ess.NotificationConfigurationModel, meta interface{}) error {
var ids []string
var s = make([]map[string]interface{}, 0)
for _, n := range notifications {
mapping := map[string]interface{}{
"notification_arn": n.NotificationArn,
"notification_types": n.NotificationTypes.NotificationType,
"scaling_group_id": n.ScalingGroupId,
}
ids = append(ids, n.NotificationArn)
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("notifications", s); err != nil {
return WrapError(err)
}
if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}