forked from aliyun/terraform-provider-alicloud
/
data_source_alicloud_cen_region_route_entries.go
124 lines (107 loc) · 3.46 KB
/
data_source_alicloud_cen_region_route_entries.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
package alicloud
import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cbn"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func dataSourceAlicloudCenRegionRouteEntries() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudCenRegionDomainRouteEntriesRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"region_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
// Computed values
"entries": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr_block": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_id": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_type": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_region_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudCenRegionDomainRouteEntriesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := cbn.CreateDescribeCenRegionDomainRouteEntriesRequest()
request.CenId = d.Get("instance_id").(string)
request.CenRegionId = d.Get("region_id").(string)
request.PageSize = requests.NewInteger(PageSizeLarge)
var allCenRouteEntries []cbn.CenRouteEntry
for pageNumber := 1; ; pageNumber++ {
raw, err := client.WithCenClient(func(cbnClient *cbn.Client) (interface{}, error) {
return cbnClient.DescribeCenRegionDomainRouteEntries(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_cen_region_route_entries", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
response, _ := raw.(*cbn.DescribeCenRegionDomainRouteEntriesResponse)
if len(response.CenRouteEntries.CenRouteEntry) < 1 {
break
}
allCenRouteEntries = append(allCenRouteEntries, response.CenRouteEntries.CenRouteEntry...)
if len(response.CenRouteEntries.CenRouteEntry) < PageSizeLarge {
break
}
request.PageNumber = requests.NewInteger(pageNumber)
}
return cenRegionDomainRouteEntriesAttributes(d, allCenRouteEntries)
}
func cenRegionDomainRouteEntriesAttributes(d *schema.ResourceData, allCenRouteEntries []cbn.CenRouteEntry) error {
var s []map[string]interface{}
for _, cenRouteEntry := range allCenRouteEntries {
mapping := map[string]interface{}{
"cidr_block": cenRouteEntry.DestinationCidrBlock,
"type": cenRouteEntry.Type,
"next_hop_id": cenRouteEntry.NextHopInstanceId,
"next_hop_type": cenRouteEntry.NextHopType,
"next_hop_region_id": cenRouteEntry.NextHopRegionId,
}
s = append(s, mapping)
}
id := d.Get("instance_id").(string) + COLON_SEPARATED + d.Get("region_id").(string)
d.SetId(id)
if err := d.Set("entries", 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
}