-
Notifications
You must be signed in to change notification settings - Fork 26
/
resource_tunnel.go
132 lines (118 loc) · 3.67 KB
/
resource_tunnel.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
package aviatrix
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/AviatrixSystems/go-aviatrix/goaviatrix"
)
func resourceTunnel() *schema.Resource {
return &schema.Resource{
Create: resourceTunnelCreate,
Read: resourceTunnelRead,
Update: resourceTunnelUpdate,
Delete: resourceTunnelDelete,
Schema: map[string]*schema.Schema{
"vpc_name1": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"vpc_name2": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"over_aws_peering": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"peering_state": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"peering_hastatus": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"cluster": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"peering_link": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceTunnelCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
tunnel := &goaviatrix.Tunnel{
VpcName1: d.Get("vpc_name1").(string),
VpcName2: d.Get("vpc_name2").(string),
OverAwsPeering: d.Get("over_aws_peering").(string),
PeeringState: d.Get("peering_state").(string),
PeeringHaStatus: d.Get("peering_hastatus").(string),
Cluster: d.Get("cluster").(string),
PeeringLink: d.Get("peering_link").(string),
}
log.Printf("[INFO] Creating Aviatrix tunnel: %#v", tunnel)
err := client.CreateTunnel(tunnel)
if err != nil {
return fmt.Errorf("Failed to create Aviatrix Tunnel: %s", err)
}
d.SetId(tunnel.VpcName1 + "<->" + tunnel.VpcName2)
return resourceTunnelRead(d, meta)
}
func resourceTunnelRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
tunnel := &goaviatrix.Tunnel{
VpcName1: d.Get("vpc_name1").(string),
VpcName2: d.Get("vpc_name2").(string),
}
tun, err := client.GetTunnel(tunnel)
if err != nil {
return fmt.Errorf("Couldn't find Aviatrix Tunnel: %s", err)
}
log.Printf("[INFO] Found Aviatrix tunnel: %#v", tun)
d.Set("over_aws_peering", tun.OverAwsPeering)
d.Set("peering_state", tun.PeeringState)
d.Set("peering_hastatus", tun.PeeringHaStatus)
d.Set("cluster", tun.Cluster)
d.Set("peering_link", tun.PeeringLink)
d.SetId(tun.VpcName1 + "<->" + tun.VpcName2)
log.Printf("[INFO] Found tunnel: %#v", d)
return nil
}
func resourceTunnelUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
tunnel := &goaviatrix.Tunnel{
VpcName1: d.Get("vpc_name1").(string),
VpcName2: d.Get("vpc_name2").(string),
OverAwsPeering: d.Get("over_aws_peering").(string),
PeeringState: d.Get("peering_state").(string),
PeeringHaStatus: d.Get("peering_hastatus").(string),
Cluster: d.Get("cluster").(string),
PeeringLink: d.Get("peering_link").(string),
}
log.Printf("[INFO] Updating Aviatrix tunnel: %#v", tunnel)
err := client.UpdateTunnel(tunnel)
if err != nil {
return fmt.Errorf("Failed to update Aviatrix Tunnel: %s", err)
}
d.SetId(tunnel.VpcName1 + "<->" + tunnel.VpcName2)
return resourceTunnelRead(d, meta)
}
func resourceTunnelDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
tunnel := &goaviatrix.Tunnel{
VpcName1: d.Get("vpc_name1").(string),
VpcName2: d.Get("vpc_name2").(string),
}
log.Printf("[INFO] Deleting Aviatrix tunnel: %#v", tunnel)
err := client.DeleteTunnel(tunnel)
if err != nil {
return fmt.Errorf("Failed to delete Aviatrix Tunnel: %s", err)
}
return nil
}