/
data_source_apsarastack_route_entries.go
159 lines (147 loc) · 4.48 KB
/
data_source_apsarastack_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
package alibabacloudstack
import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/aliyun/terraform-provider-alibabacloudstack/alibabacloudstack/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strings"
)
func dataSourceAlibabacloudStackRouteEntries() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlibabacloudStackRouteEntriesRead,
Schema: map[string]*schema.Schema{
"route_table_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"instance_id": {
Type: schema.TypeString,
Optional: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
},
"cidr_block": {
Type: schema.TypeString,
Optional: true,
},
"entries": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_type": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
},
"route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"cidr_block": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlibabacloudStackRouteEntriesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AlibabacloudStackClient)
request := vpc.CreateDescribeRouteTablesRequest()
if strings.ToLower(client.Config.Protocol) == "https" {
request.Scheme = "https"
} else {
request.Scheme = "http"
}
request.RegionId = client.RegionId
request.Headers = map[string]string{"RegionId": client.RegionId}
request.QueryParams = map[string]string{"AccessKeySecret": client.SecretKey, "Product": "vpc", "Department": client.Department, "ResourceGroup": client.ResourceGroup}
request.PageSize = requests.NewInteger(PageSizeLarge)
request.PageNumber = requests.NewInteger(1)
request.RouteTableId = d.Get("route_table_id").(string)
var allRouteEntries []vpc.RouteEntry
invoker := NewInvoker()
for {
var raw interface{}
if err := invoker.Run(func() error {
response, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.DescribeRouteTables(request)
})
raw = response
return err
}); err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alibabacloudstack_route_entries", request.GetActionName(), AlibabacloudStackSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response, _ := raw.(*vpc.DescribeRouteTablesResponse)
if len(response.RouteTables.RouteTable) < 1 {
break
}
for _, entries := range response.RouteTables.RouteTable[0].RouteEntrys.RouteEntry {
if instance_id, ok := d.GetOk("instance_id"); ok && entries.InstanceId != instance_id.(string) {
continue
}
if route_entry_type, ok := d.GetOk("type"); ok && entries.Type != route_entry_type.(string) {
continue
}
if cidr_block, ok := d.GetOk("cidr_block"); ok && entries.DestinationCidrBlock != cidr_block.(string) {
continue
}
allRouteEntries = append(allRouteEntries, entries)
}
if len(response.RouteTables.RouteTable) < PageSizeLarge {
break
}
if page, err := getNextpageNumber(request.PageNumber); err != nil {
return WrapError(err)
} else {
request.PageNumber = page
}
}
return RouteEntriesDecriptionAttributes(d, allRouteEntries, meta)
}
func RouteEntriesDecriptionAttributes(d *schema.ResourceData, entries []vpc.RouteEntry, meta interface{}) error {
var ids []string
var s []map[string]interface{}
for _, entry := range entries {
mapping := map[string]interface{}{
"route_table_id": entry.RouteTableId,
"instance_id": entry.InstanceId,
"status": entry.Status,
"next_hop_type": entry.NextHopType,
"type": entry.Type,
"cidr_block": entry.DestinationCidrBlock,
}
ids = append(ids, entry.RouteTableId)
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("entries", s); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}