This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_source_tc_route_table.go
173 lines (154 loc) · 4.53 KB
/
data_source_tc_route_table.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
/*
Provides details about a specific Route Table.
This resource can prove useful when a module accepts a Subnet id as an input variable and needs to, for example, add a route in the Route Table.
~> **NOTE:** It has been deprecated and replaced by tencentcloud_vpc_route_tables.
Example Usage
```hcl
variable "route_table_id" {}
data "tencentcloud_route_table" "selected" {
route_table_id = var.route_table_id
}
resource "tencentcloud_route_entry" "rtb_entry_instance" {
vpc_id = "{data.tencentcloud_route_table.selected.vpc_id}"
route_table_id = var.route_table_id
cidr_block = "10.4.8.0/24"
next_type = "instance"
next_hub = "10.16.1.7"
}
```
*/
package tencentcloud
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceTencentCloudRouteTable() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "This data source has been deprecated in Terraform TencentCloud provider version 1.10.0. Please use 'tencentcloud_vpc_route_tables' instead.",
Read: dataSourceTencentCloudRouteTableRead,
Schema: map[string]*schema.Schema{
"route_table_id": {
Type: schema.TypeString,
Required: true,
Description: "The Route Table ID.",
},
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) < 2 || len(value) > 60 {
errors = append(errors, fmt.Errorf("%s Cannot be less than 2 characters and cannot be longer than 60 characters", k))
}
return
},
Description: "The Route Table name.",
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Description: "The VPC ID.",
},
"subnet_num": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of associated subnets.",
},
"routes": {
Type: schema.TypeList,
Computed: true,
Description: "The information list of the VPC route table.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr_block": {
Type: schema.TypeString,
Computed: true,
Description: "The RouteEntry's target network segment.",
},
"next_type": {
Type: schema.TypeString,
Computed: true,
Description: "The `next_hub` type.",
},
"next_hub": {
Type: schema.TypeString,
Computed: true,
Description: "The RouteEntry's next hub.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The RouteEntry's description.",
},
},
},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time of routing table.",
},
},
}
}
func dataSourceTencentCloudRouteTableRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_route_table.read")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
var (
routeTableId string
name string
)
if temp, ok := d.GetOk("route_table_id"); ok {
tempStr := temp.(string)
if tempStr != "" {
routeTableId = tempStr
}
}
if temp, ok := d.GetOk("name"); ok {
tempStr := temp.(string)
if tempStr != "" {
name = tempStr
}
}
var infos, err = service.DescribeRouteTables(ctx, routeTableId, name, "", map[string]string{}, nil, "")
if err != nil {
return err
}
if len(infos) == 0 {
return fmt.Errorf("route table route_table_id=%s, name=%s not found", routeTableId, name)
}
routetable := infos[0]
d.SetId(routetable.routeTableId)
_ = d.Set("route_table_id", routetable.routeTableId)
_ = d.Set("vpc_id", routetable.vpcId)
_ = d.Set("name", routetable.name)
_ = d.Set("subnet_num", len(routetable.entryInfos))
_ = d.Set("create_time", routetable.createTime)
routes := make([]map[string]interface{}, 0, len(routetable.entryInfos))
for _, r := range routetable.entryInfos {
if strings.ToUpper(r.nextBub) == "LOCAL" {
continue
}
m := make(map[string]interface{})
nextType := ""
for k, v := range routeTypeNewMap {
if v == r.nextType {
nextType = k
break
}
}
m["next_type"] = nextType
m["next_hub"] = r.nextBub
m["cidr_block"] = r.destinationCidr
m["description"] = r.description
routes = append(routes, m)
}
if err := d.Set("routes", routes); err != nil {
return err
}
return nil
}