generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
egress_route.go
91 lines (79 loc) · 2.51 KB
/
egress_route.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
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const egressRouteEndpoint = "v1/egress_routes"
type EgressRoute struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description"`
Destinations []string `json:"destinations"`
Enabled bool `json:"enabled"`
ExemptSources []string `json:"exempt_sources"`
Sources []string `json:"sources"`
Via string `json:"via"`
}
func NewEgressRoute(d *schema.ResourceData) *EgressRoute {
res := &EgressRoute{}
if d.HasChange("name") {
res.Name = d.Get("name").(string)
}
res.Description = d.Get("description").(string)
res.Enabled = d.Get("enabled").(bool)
res.Destinations = ConfigToStringSlice("destinations", d)
res.ExemptSources = ConfigToStringSlice("exempt_sources", d)
res.Sources = ConfigToStringSlice("sources", d)
res.Via = d.Get("via").(string)
return res
}
func parseEgressRoute(resp []byte) (*EgressRoute, error) {
e := &EgressRoute{}
err := json.Unmarshal(resp, e)
if err != nil {
return nil, fmt.Errorf("could not parse egress route response: %v", err)
}
return e, nil
}
func CreateEgressRoute(ctx context.Context, c *Client, e *EgressRoute) (*EgressRoute, error) {
url := fmt.Sprintf("%s/%s", c.BaseURL, egressRouteEndpoint)
body, err := json.Marshal(e)
if err != nil {
return nil, fmt.Errorf("could not convert egress route to json: %v", err)
}
resp, err := c.Post(ctx, url, body)
if err != nil {
return nil, err
}
return parseEgressRoute(resp)
}
func GetEgressRoute(ctx context.Context, c *Client, eID string) (*EgressRoute, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, egressRouteEndpoint, eID)
resp, err := c.Get(ctx, url, nil)
if err != nil {
return nil, err
}
return parseEgressRoute(resp)
}
func UpdateEgressRoute(ctx context.Context, c *Client, eID string, e *EgressRoute) (*EgressRoute, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, egressRouteEndpoint, eID)
body, err := json.Marshal(e)
if err != nil {
return nil, fmt.Errorf("could not convert egress route to json: %v", err)
}
resp, err := c.Patch(ctx, url, body)
if err != nil {
return nil, err
}
return parseEgressRoute(resp)
}
func DeleteEgressRoute(ctx context.Context, c *Client, mID string) (*EgressRoute, error) {
url := fmt.Sprintf("%s/%s/%s", c.BaseURL, egressRouteEndpoint, mID)
resp, err := c.Delete(ctx, url, nil)
if err != nil {
return nil, err
}
return parseEgressRoute(resp)
}