forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_alicloud_dns_groups.go
118 lines (101 loc) · 2.83 KB
/
data_source_alicloud_dns_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
package alicloud
import (
"fmt"
"log"
"regexp"
"github.com/denverdino/aliyungo/dns"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func dataSourceAlicloudDnsGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudDnsGroupsRead,
Schema: map[string]*schema.Schema{
"name_regex": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
// Computed values
"groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"group_id": {
Type: schema.TypeString,
Computed: true,
},
"group_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudDnsGroupsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
args := &dns.DescribeDomainGroupsArgs{}
var allGroups []dns.DomainGroupType
pagination := getPagination(1, 50)
for {
args.Pagination = pagination
raw, err := client.WithDnsClient(func(dnsClient *dns.Client) (interface{}, error) {
return dnsClient.DescribeDomainGroups(args)
})
if err != nil {
return err
}
groups, _ := raw.([]dns.DomainGroupType)
allGroups = append(allGroups, groups...)
if len(groups) < pagination.PageSize {
break
}
pagination.PageNumber += 1
}
var filteredGroups []dns.DomainGroupType
if v, ok := d.GetOk("name_regex"); ok && v.(string) != "" {
r := regexp.MustCompile(v.(string))
for _, group := range allGroups {
if r.MatchString(group.GroupName) {
filteredGroups = append(filteredGroups, group)
}
}
} else {
filteredGroups = allGroups[:]
}
if len(filteredGroups) < 1 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
log.Printf("[DEBUG] alicloud_dns_groups - Groups found: %#v", allGroups)
return groupsDecriptionAttributes(d, filteredGroups, meta)
}
func groupsDecriptionAttributes(d *schema.ResourceData, groupTypes []dns.DomainGroupType, meta interface{}) error {
var ids []string
var s []map[string]interface{}
for _, group := range groupTypes {
mapping := map[string]interface{}{
"group_id": group.GroupId,
"group_name": group.GroupName,
}
log.Printf("[DEBUG] alicloud_dns_groups - adding group: %v", mapping)
ids = append(ids, group.GroupId)
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("groups", s); err != nil {
return 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
}