-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport.go
More file actions
113 lines (90 loc) · 2.54 KB
/
port.go
File metadata and controls
113 lines (90 loc) · 2.54 KB
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
package autonomisdk
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/intercloud/autonomi-sdk/models"
)
// CreatePhysicalPort creates a physical port in Autonomi platform.
func (c *Client) CreatePhysicalPort(ctx context.Context, payload models.CreatePhysicalPort) (*models.PhysicalPort, error) {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(&payload)
if err != nil {
return nil, err
}
if errV := c.validate.StructCtx(ctx, payload); errV != nil {
return nil, errV
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/accounts/%s/ports", c.hostURL, c.accountID), body)
if err != nil {
return nil, err
}
resp, err := c.doRequest(req)
if err != nil {
return nil, err
}
physicalPort := models.PhysicalPortSingleResponse{}
err = json.Unmarshal(resp, &physicalPort)
if err != nil {
return nil, err
}
return &physicalPort.Data, nil
}
func (c *Client) GetPhysicalPort(ctx context.Context, portID string) (*models.PhysicalPort, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/accounts/%s/ports/%s", c.hostURL, c.accountID, portID), nil)
if err != nil {
return nil, err
}
resp, err := c.doRequest(req)
if err != nil {
return nil, err
}
physicalPort := models.PhysicalPortSingleResponse{}
err = json.Unmarshal(resp, &physicalPort)
if err != nil {
return nil, err
}
return &physicalPort.Data, err
}
func (c *Client) ListPort(options ...OptionElement) (*[]models.PhysicalPort, error) {
// retrieve options from request
portOptions := &elementOptions{}
for _, o := range options {
o(portOptions)
}
// run request
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/accounts/%s/ports", c.hostURL, c.accountID), nil)
if err != nil {
return nil, err
}
// add query param if needed
if portOptions.administrativeState != "" {
q := req.URL.Query()
q.Add("state", portOptions.administrativeState.String())
req.URL.RawQuery = q.Encode()
}
resp, err := c.doRequest(req)
if err != nil {
return nil, err
}
ports := models.PhysicalPortListResponse{}
err = json.Unmarshal(resp, &ports)
if err != nil {
return nil, err
}
return &ports.Data, err
}
// DeletePhysicalPort creates a physical port in Autonomi platform. As
func (c *Client) DeletePhysicalPort(ctx context.Context, physicalPortID string) error {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/accounts/%s/ports/%s", c.hostURL, c.accountID, physicalPortID), nil)
if err != nil {
return err
}
_, err = c.doRequest(req)
if err != nil {
return err
}
return nil
}