generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mapped_host.go
58 lines (51 loc) · 1.6 KB
/
mapped_host.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
package client
import (
"context"
"encoding/json"
"fmt"
)
type MappedHost struct {
MappedHost string `json:"mapped_host"`
Name string `json:"name,omitempty"`
}
// ReqBody returns a body with mapped_host only because the name of the mapped host should be in the path params only
func (mh *MappedHost) ReqBody() ([]byte, error) {
mh.Name = ""
return json.Marshal(mh)
}
func parseMappedHost(resp []byte) (*MappedHost, error) {
mh := &MappedHost{}
err := json.Unmarshal(resp, mh)
if err != nil {
return nil, fmt.Errorf("could not parse network element response: %v", err)
}
return mh, nil
}
func SetMappedHost(ctx context.Context, c *Client, neID string, mappedHost *MappedHost) (*MappedHost, error) {
url := fmt.Sprintf("%s/%s/%s/mapped_hosts/%s", c.BaseURL, networkElementsEndpoint, neID, mappedHost.Name)
body, err := mappedHost.ReqBody()
if err != nil {
return nil, fmt.Errorf("could not convert MappedHost to json")
}
resp, err := c.Put(ctx, url, body)
if err != nil {
return nil, err
}
return parseMappedHost(resp)
}
func GetMappedHost(ctx context.Context, c *Client, neID string, mappedHost *MappedHost) (*MappedHost, error) {
url := fmt.Sprintf("%s/%s/%s/mapped_hosts/%s", c.BaseURL, networkElementsEndpoint, neID, mappedHost.Name)
resp, err := c.Get(ctx, url, nil)
if err != nil {
return nil, err
}
return parseMappedHost(resp)
}
func DeleteMappedHost(ctx context.Context, c *Client, neID, name string) error {
url := fmt.Sprintf("%s/%s/%s/mapped_hosts/%s", c.BaseURL, networkElementsEndpoint, neID, name)
_, err := c.Delete(ctx, url, nil)
if err != nil {
return err
}
return nil
}