-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathresource_gateway.go
126 lines (112 loc) · 3.27 KB
/
resource_gateway.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
package aviatrix
import (
"fmt"
"log"
"github.com/AviatrixSystems/go-aviatrix/goaviatrix"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAviatrixGateway() *schema.Resource {
return &schema.Resource{
Create: resourceAviatrixGatewayCreate,
Read: resourceAviatrixGatewayRead,
Update: resourceAviatrixGatewayUpdate,
Delete: resourceAviatrixGatewayDelete,
Schema: map[string]*schema.Schema{
"cloud_type": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"account_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"gw_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"vpc_reg": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"vpc_size": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"vpc_net": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceAviatrixGatewayCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
gateway := &goaviatrix.Gateway{
CloudType: d.Get("cloud_type").(int),
AccountName: d.Get("account_name").(string),
GwName: d.Get("gw_name").(string),
VpcID: d.Get("vpc_id").(string),
VpcRegion: d.Get("vpc_reg").(string),
VpcSize: d.Get("vpc_size").(string),
VpcNet: d.Get("vpc_net").(string),
}
log.Printf("[INFO] Creating Aviatrix gateway: %#v", gateway)
err := client.CreateGateway(gateway)
if err != nil {
return fmt.Errorf("Failed to create Aviatrix Gateway: %s", err)
}
d.SetId(gateway.GwName)
return nil
//return resourceAviatrixGatewayRead(d, meta)
}
func resourceAviatrixGatewayRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
gateway := &goaviatrix.Gateway{
AccountName: d.Get("account_name").(string),
GwName: d.Get("gw_name").(string),
}
gw, err := client.GetGateway(gateway)
if err != nil {
return fmt.Errorf("Couldn't find Aviatrix Gateway: %s", err)
}
if (gw != nil) {
d.Set("account_name", gw.AccountName)
d.Set("gw_name", gw.GwName)
d.Set("vpc_id", gw.VpcID)
d.Set("vpc_reg", gw.VpcRegion)
d.Set("vpc_size", gw.VpcSize)
d.Set("vpc_net", gw.VpcNet)
}
return nil
}
func resourceAviatrixGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
gateway := &goaviatrix.Gateway{
GwName: d.Get("gw_name").(string),
GwSize: d.Get("vpc_size").(string),
}
log.Printf("[INFO] Updating Aviatrix gateway: %#v", gateway)
err := client.UpdateGateway(gateway)
if err != nil {
return fmt.Errorf("Failed to update Aviatrix Gateway: %s", err)
}
d.SetId(gateway.GwName)
return resourceAviatrixGatewayRead(d, meta)
}
func resourceAviatrixGatewayDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
gateway := &goaviatrix.Gateway{
CloudType: d.Get("cloud_type").(int),
GwName: d.Get("gw_name").(string),
}
log.Printf("[INFO] Deleting Aviatrix gateway: %#v", gateway)
err := client.DeleteGateway(gateway)
if err != nil {
return fmt.Errorf("Failed to delete Aviatrix Gateway: %s", err)
}
return nil
}