-
Notifications
You must be signed in to change notification settings - Fork 48
/
aws_tgw_peering.go
81 lines (71 loc) · 2.35 KB
/
aws_tgw_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
package goaviatrix
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
)
type AwsTgwPeering struct {
Action string `form:"action,omitempty"`
CID string `form:"CID,omitempty"`
TgwName1 string `form:"tgw_name1,omitempty" json:"tgw_name1,omitempty"`
TgwName2 string `form:"tgw_name2,omitempty" json:"tgw_name2,omitempty"`
Async bool `form:"async,omitempty"`
}
type AwsTgwPeeringAPIResp struct {
Return bool `json:"return"`
Results []string `json:"results"`
Reason string `json:"reason"`
}
func (c *Client) CreateAwsTgwPeering(awsTgwPeering *AwsTgwPeering) error {
awsTgwPeering.CID = c.CID
awsTgwPeering.Action = "add_tgw_peering"
awsTgwPeering.Async = true
return c.PostAsyncAPI(awsTgwPeering.Action, awsTgwPeering, BasicCheck)
}
func (c *Client) GetAwsTgwPeering(awsTgwPeering *AwsTgwPeering) error {
var data AwsTgwPeeringAPIResp
form := map[string]string{
"CID": c.CID,
"action": "list_peered_tgw_names",
"tgw_name": awsTgwPeering.TgwName1,
}
check := func(action, method, reason string, ret bool) error {
if !ret {
if strings.Contains(reason, "does not exist") {
return ErrNotFound
}
return fmt.Errorf("rest API %s %s failed: %s", action, method, reason)
}
return nil
}
err := c.GetAPI(&data, form["action"], form, check)
if err != nil {
return err
}
if len(data.Results) == 0 {
log.Errorf("Aws tgw peering with tgw: %s and tgw: %s not found", awsTgwPeering.TgwName1, awsTgwPeering.TgwName2)
return ErrNotFound
}
peeringList := data.Results
for i := range peeringList {
if peeringList[i] == awsTgwPeering.TgwName2 {
return nil
}
}
return ErrNotFound
}
func (c *Client) DeleteAwsTgwPeering(awsTgwPeering *AwsTgwPeering) error {
awsTgwPeering.CID = c.CID
awsTgwPeering.Action = "delete_tgw_peering"
awsTgwPeering.Async = true
return c.PostAsyncAPI(awsTgwPeering.Action, awsTgwPeering, BasicCheck)
}
func DiffSuppressFuncAwsTgwPeeringTgwName1(k, old, new string, d *schema.ResourceData) bool {
tgwName2Old, _ := d.GetChange("tgw_name2")
return old == d.Get("tgw_name2").(string) && new == tgwName2Old.(string)
}
func DiffSuppressFuncAwsTgwPeeringTgwName2(k, old, new string, d *schema.ResourceData) bool {
tgwName1Old, _ := d.GetChange("tgw_name1")
return old == d.Get("tgw_name1").(string) && new == tgwName1Old.(string)
}