-
Notifications
You must be signed in to change notification settings - Fork 26
/
resource_transitive_peering.go
91 lines (78 loc) · 2.51 KB
/
resource_transitive_peering.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
package aviatrix
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/AviatrixSystems/go-aviatrix/goaviatrix"
)
func resourceTranspeer() *schema.Resource {
return &schema.Resource{
Create: resourceTranspeerCreate,
Read: resourceTranspeerRead,
Update: resourceTranspeerUpdate,
Delete: resourceTranspeerDelete,
Schema: map[string]*schema.Schema{
"source": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"nexthop": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"reachable_cidr": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceTranspeerCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
transpeer := &goaviatrix.Transpeer{
Source: d.Get("source").(string),
Nexthop: d.Get("nexthop").(string),
ReachableCidr: d.Get("reachable_cidr").(string),
}
log.Printf("[INFO] Creating Aviatrix transitive peering: %#v", transpeer)
err := client.CreateTranspeer(transpeer)
if err != nil {
return fmt.Errorf("Failed to create Aviatrix Transitive peering: %s", err)
}
d.SetId(transpeer.Source+transpeer.Nexthop+transpeer.ReachableCidr)
//return nil
return resourceTranspeerRead(d, meta)
}
func resourceTranspeerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
transpeer := &goaviatrix.Transpeer{
Source: d.Get("source").(string),
Nexthop: d.Get("nexthop").(string),
ReachableCidr: d.Get("reachable_cidr").(string),
}
transpeer, err := client.GetTranspeer(transpeer)
if err != nil {
return fmt.Errorf("Couldn't find Aviatrix Transitive peering: %s", err)
}
d.Set("source", transpeer.Source)
d.Set("nexthop", transpeer.Nexthop)
d.Set("reachable_cidr", transpeer.ReachableCidr)
return nil
}
func resourceTranspeerUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Aviatrix transitive peering cannot be updated - delete and create new one")
}
func resourceTranspeerDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
transpeer := &goaviatrix.Transpeer{
Source: d.Get("source").(string),
Nexthop: d.Get("nexthop").(string),
ReachableCidr: d.Get("reachable_cidr").(string),
}
log.Printf("[INFO] Deleting Aviatrix transpeer: %#v", transpeer)
err := client.DeleteTranspeer(transpeer)
if err != nil {
return fmt.Errorf("Failed to delete Aviatrix Transpeer: %s", err)
}
return nil
}