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_ons_groups.go
136 lines (119 loc) · 3.47 KB
/
data_source_alicloud_ons_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
package alicloud
import (
"regexp"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ons"
"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 dataSourceAlicloudOnsGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudOnsGroupsRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"group_id_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateRegexp,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
// Computed values
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"owner": {
Type: schema.TypeString,
Computed: true,
},
"independent_naming": {
Type: schema.TypeBool,
Computed: true,
},
"remark": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudOnsGroupsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
onsService := OnsService{client}
request := ons.CreateOnsGroupListRequest()
request.RegionId = client.RegionId
request.InstanceId = d.Get("instance_id").(string)
raw, err := onsService.client.WithOnsClient(func(onsClient *ons.Client) (interface{}, error) {
return onsClient.OnsGroupList(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_ons_groups", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response, _ := raw.(*ons.OnsGroupListResponse)
var filteredGroups []ons.SubscribeInfoDo
nameRegex, ok := d.GetOk("group_id_regex")
if ok && nameRegex.(string) != "" {
var r *regexp.Regexp
if nameRegex != "" {
r = regexp.MustCompile(nameRegex.(string))
}
for _, group := range response.Data.SubscribeInfoDo {
if r != nil && !r.MatchString(group.GroupId) {
continue
}
filteredGroups = append(filteredGroups, group)
}
} else {
filteredGroups = response.Data.SubscribeInfoDo
}
return onsGroupsDecriptionAttributes(d, filteredGroups, meta)
}
func onsGroupsDecriptionAttributes(d *schema.ResourceData, topicsInfo []ons.SubscribeInfoDo, meta interface{}) error {
var ids []string
var s []map[string]interface{}
for _, item := range topicsInfo {
mapping := map[string]interface{}{
"id": item.GroupId,
"owner": item.Owner,
"independent_naming": item.IndependentNaming,
"remark": item.Remark,
}
ids = append(ids, item.GroupId)
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}
if err := d.Set("groups", s); 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), s)
}
return nil
}