-
Notifications
You must be signed in to change notification settings - Fork 510
/
requests.go
219 lines (179 loc) · 6.71 KB
/
requests.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package ports
import (
"fmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToPortListQuery() (string, error)
ToPortListDetailQuery() (string, error)
}
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the node attributes you want to see returned. Marker and Limit are used
// for pagination.
type ListOpts struct {
// Filter the list by the name or uuid of the Node
Node string `q:"node"`
// Filter the list by the Node uuid
NodeUUID string `q:"node_uuid"`
// Filter the list with the specified Portgroup (name or UUID)
PortGroup string `q:"portgroup"`
// Filter the list with the specified physical hardware address, typically MAC
Address string `q:"address"`
// One or more fields to be returned in the response.
Fields []string `q:"fields"`
// Requests a page size of items.
Limit int `q:"limit"`
// The ID of the last-seen item
Marker string `q:"marker"`
// Sorts the response by the requested sort direction.
// Valid value is asc (ascending) or desc (descending). Default is asc.
SortDir string `q:"sort_dir"`
// Sorts the response by the this attribute value. Default is id.
SortKey string `q:"sort_key"`
}
// ToPortListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToPortListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List makes a request against the API to list ports accessible to you.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToPortListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return PortPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// ToPortListDetailQuery formats a ListOpts into a query string for the list details API.
func (opts ListOpts) ToPortListDetailQuery() (string, error) {
// Detail endpoint can't filter by Fields
if len(opts.Fields) > 0 {
return "", fmt.Errorf("fields is not a valid option when getting a detailed listing of ports")
}
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// ListDetail - Return a list ports with complete details.
// Some filtering is possible by passing in flags in "ListOpts",
// but you cannot limit by the fields returned.
func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listDetailURL(client)
if opts != nil {
query, err := opts.ToPortListDetailQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return PortPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Get - requests the details off a port, by ID.
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToPortCreateMap() (map[string]interface{}, error)
}
// CreateOpts specifies port creation parameters.
type CreateOpts struct {
// UUID of the Node this resource belongs to.
NodeUUID string `json:"node_uuid,omitempty"`
// Physical hardware address of this network Port,
// typically the hardware MAC address.
Address string `json:"address,omitempty"`
// UUID of the Portgroup this resource belongs to.
PortGroupUUID string `json:"portgroup_uuid,omitempty"`
// The Port binding profile. If specified, must contain switch_id (only a MAC
// address or an OpenFlow based datapath_id of the switch are accepted in this
// field) and port_id (identifier of the physical port on the switch to which
// node’s port is connected to) fields. switch_info is an optional string
// field to be used to store any vendor-specific information.
LocalLinkConnection map[string]interface{} `json:"local_link_connection,omitempty"`
// Indicates whether PXE is enabled or disabled on the Port.
PXEEnabled *bool `json:"pxe_enabled,omitempty"`
// The name of the physical network to which a port is connected. May be empty.
PhysicalNetwork string `json:"physical_network,omitempty"`
// A set of one or more arbitrary metadata key and value pairs.
Extra map[string]interface{} `json:"extra,omitempty"`
// Indicates whether the Port is a Smart NIC port.
IsSmartNIC *bool `json:"is_smartnic,omitempty"`
}
// ToPortCreateMap assembles a request body based on the contents of a CreateOpts.
func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
body, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return body, nil
}
// Create - requests the creation of a port
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
reqBody, err := opts.ToPortCreateMap()
if err != nil {
r.Err = err
return
}
resp, err := client.Post(createURL(client), reqBody, &r.Body, nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// TODO Update
type Patch interface {
ToPortUpdateMap() map[string]interface{}
}
// UpdateOpts is a slice of Patches used to update a port
type UpdateOpts []Patch
type UpdateOp string
const (
ReplaceOp UpdateOp = "replace"
AddOp UpdateOp = "add"
RemoveOp UpdateOp = "remove"
)
type UpdateOperation struct {
Op UpdateOp `json:"op" required:"true"`
Path string `json:"path" required:"true"`
Value interface{} `json:"value,omitempty"`
}
func (opts UpdateOperation) ToPortUpdateMap() map[string]interface{} {
return map[string]interface{}{
"op": opts.Op,
"path": opts.Path,
"value": opts.Value,
}
}
// Update - requests the update of a port
func Update(client *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
body := make([]map[string]interface{}, len(opts))
for i, patch := range opts {
body[i] = patch.ToPortUpdateMap()
}
resp, err := client.Patch(updateURL(client, id), body, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
// Delete - requests the deletion of a port
func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
resp, err := client.Delete(deleteURL(client, id), nil)
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}