forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_vroute_entry.go
145 lines (121 loc) · 3.62 KB
/
resource_alicloud_vroute_entry.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
package alicloud
import (
"fmt"
"github.com/denverdino/aliyungo/ecs"
"github.com/hashicorp/terraform/helper/schema"
"strings"
)
func resourceAliyunRouteEntry() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunRouteEntryCreate,
Read: resourceAliyunRouteEntryRead,
Delete: resourceAliyunRouteEntryDelete,
Schema: map[string]*schema.Schema{
"router_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"route_table_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"destination_cidrblock": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"nexthop_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateRouteEntryNextHopType,
},
"nexthop_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceAliyunRouteEntryCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).ecsconn
rtId := d.Get("route_table_id").(string)
rId := d.Get("router_id").(string)
cidr := d.Get("destination_cidrblock").(string)
nt := d.Get("nexthop_type").(string)
ni := d.Get("nexthop_id").(string)
args, err := buildAliyunRouteEntryArgs(d, meta)
if err != nil {
return err
}
err = conn.CreateRouteEntry(args)
if err != nil {
return err
}
// route_table_id:router_id:destination_cidrblock:nexthop_type:nexthop_id
d.SetId(rtId + ":" + rId + ":" + cidr + ":" + nt + ":" + ni)
d.Set("router_id", rId)
if err := conn.WaitForAllRouteEntriesAvailable(rId, rtId, defaultTimeout); err != nil {
return fmt.Errorf("WaitFor route entry got error: %#v", err)
}
return resourceAliyunRouteEntryRead(d, meta)
}
func resourceAliyunRouteEntryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
parts := strings.Split(d.Id(), ":")
rtId := parts[0]
//rId := parts[1]
cidr := parts[2]
nexthop_type := parts[3]
nexthop_id := parts[4]
en, err := client.QueryRouteEntry(rtId, cidr, nexthop_type, nexthop_id)
if err != nil {
if notFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error route entry: %#v", err)
}
d.Set("route_table_id", en.RouteTableId)
d.Set("destination_cidrblock", en.DestinationCidrBlock)
d.Set("nexthop_type", en.NextHopType)
d.Set("nexthop_id", en.InstanceId)
return nil
}
func resourceAliyunRouteEntryDelete(d *schema.ResourceData, meta interface{}) error {
con := meta.(*AliyunClient).ecsconn
args, err := buildAliyunRouteEntryDeleteArgs(d, meta)
if err != nil {
return err
}
return con.DeleteRouteEntry(args)
}
func buildAliyunRouteEntryArgs(d *schema.ResourceData, meta interface{}) (*ecs.CreateRouteEntryArgs, error) {
args := &ecs.CreateRouteEntryArgs{
RouteTableId: d.Get("route_table_id").(string),
DestinationCidrBlock: d.Get("destination_cidrblock").(string),
}
if v := d.Get("nexthop_type").(string); v != "" {
args.NextHopType = ecs.NextHopType(v)
}
if v := d.Get("nexthop_id").(string); v != "" {
args.NextHopId = v
}
return args, nil
}
func buildAliyunRouteEntryDeleteArgs(d *schema.ResourceData, meta interface{}) (*ecs.DeleteRouteEntryArgs, error) {
args := &ecs.DeleteRouteEntryArgs{
RouteTableId: d.Get("route_table_id").(string),
DestinationCidrBlock: d.Get("destination_cidrblock").(string),
}
if v := d.Get("destination_cidrblock").(string); v != "" {
args.DestinationCidrBlock = v
}
if v := d.Get("nexthop_id").(string); v != "" {
args.NextHopId = v
}
return args, nil
}