forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_alicloud_cen_route_entries.go
213 lines (187 loc) · 5.84 KB
/
data_source_alicloud_cen_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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package alicloud
import (
"fmt"
"log"
"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 dataSourceAlicloudCenRouteEntries() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudCenPublishedRouteEntriesRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"route_table_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"cidr_block": {
Type: schema.TypeString,
Optional: 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{
"route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"cidr_block": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_id": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_type": {
Type: schema.TypeString,
Computed: true,
},
"route_type": {
Type: schema.TypeString,
Computed: true,
},
"operational_mode": {
Type: schema.TypeBool,
Computed: true,
},
"publish_status": {
Type: schema.TypeString,
Computed: true,
},
// Complex computed value
"conflicts": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr_block": {
Type: schema.TypeString,
Computed: true,
},
"region_id": {
Type: schema.TypeString,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
},
"instance_type": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
}
}
func dataSourceAlicloudCenPublishedRouteEntriesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
cenService := CenService{client}
args := cbn.CreateDescribePublishedRouteEntriesRequest()
args.CenId = d.Get("instance_id").(string)
args.ChildInstanceRouteTableId = d.Get("route_table_id").(string)
if v, ok := d.GetOk("cidr_block"); ok {
args.DestinationCidrBlock = v.(string)
}
childInstanceId, childInstanceType, err := cenService.CreateCenRouteEntryParas(args.ChildInstanceRouteTableId)
if err != nil {
return fmt.Errorf("Query route entry encounter an error, CEN %s vtb %s region_id %s, error info: %#v.",
args.CenId, args.ChildInstanceRouteTableId, client.RegionId, err)
}
args.ChildInstanceId = childInstanceId
args.ChildInstanceType = childInstanceType
args.ChildInstanceRegionId = client.RegionId
args.PageSize = requests.NewInteger(PageSizeLarge)
var allPublishedRouteEntries []cbn.PublishedRouteEntry
for pageNumber := 1; ; pageNumber++ {
args.PageNumber = requests.NewInteger(pageNumber)
raw, err := client.WithCenClient(func(cbnClient *cbn.Client) (interface{}, error) {
return cbnClient.DescribePublishedRouteEntries(args)
})
if err != nil {
return err
}
resp, _ := raw.(*cbn.DescribePublishedRouteEntriesResponse)
if resp == nil || len(resp.PublishedRouteEntries.PublishedRouteEntry) < 1 {
break
}
allPublishedRouteEntries = append(allPublishedRouteEntries, resp.PublishedRouteEntries.PublishedRouteEntry...)
if len(resp.PublishedRouteEntries.PublishedRouteEntry) < PageSizeLarge {
break
}
}
if len(allPublishedRouteEntries) < 1 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
log.Printf("[DEBUG] alicloud_cen_route_entries - PublishedRouteEntries found: %#v", allPublishedRouteEntries)
return cenPublishedRouteEntriesAttributes(d, allPublishedRouteEntries)
}
func cenPublishedRouteEntriesAttributes(d *schema.ResourceData, allPublishedRouteEntries []cbn.PublishedRouteEntry) error {
var ids []string
var s []map[string]interface{}
for _, routeEntry := range allPublishedRouteEntries {
mapping := map[string]interface{}{
"route_table_id": routeEntry.ChildInstanceRouteTableId,
"cidr_block": routeEntry.DestinationCidrBlock,
"next_hop_type": routeEntry.NextHopType,
"next_hop_id": routeEntry.NextHopId,
"operational_mode": routeEntry.OperationalMode,
"publish_status": routeEntry.PublishStatus,
"route_type": routeEntry.RouteType,
// Complex types get their own functions
"conflicts": routeConflictsMappings(routeEntry.Conflicts.Conflict),
}
id := routeEntry.ChildInstanceRouteTableId + COLON_SEPARATED + routeEntry.DestinationCidrBlock
ids = append(ids, id)
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("entries", 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
}
// Returns a set of route conflicts mappings.
func routeConflictsMappings(m []cbn.Conflict) []map[string]interface{} {
var s []map[string]interface{}
for _, v := range m {
mapping := map[string]interface{}{
"cidr_block": v.DestinationCidrBlock,
"region_id": v.RegionId,
"instance_id": v.InstanceId,
"instance_type": v.InstanceType,
"status": v.Status,
}
s = append(s, mapping)
}
return s
}