blob: 00419c679893d72c089101ba6d16f6a22145dbbf [file] [log] [blame]
Michal Kobusf6113582019-09-09 15:58:21 +02001package ports
2
3import (
4 "fmt"
5
6 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
7 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
8)
9
10// ListOptsBuilder allows extensions to add additional parameters to the
11// List request.
12type ListOptsBuilder interface {
13 ToPortListQuery() (string, error)
14 ToPortListDetailQuery() (string, error)
15}
16
17// ListOpts allows the filtering and sorting of paginated collections through
18// the API. Filtering is achieved by passing in struct field values that map to
19// the node attributes you want to see returned. Marker and Limit are used
20// for pagination.
21type ListOpts struct {
22 // Filter the list by the name or uuid of the Node
23 Node string `q:"node"`
24
25 // Filter the list by the Node uuid
26 NodeUUID string `q:"node_uuid"`
27
28 // Filter the list with the specified Portgroup (name or UUID)
29 PortGroup string `q:"portgroup"`
30
31 // Filter the list with the specified physical hardware address, typically MAC
32 Address string `q:"address"`
33
34 // One or more fields to be returned in the response.
35 Fields []string `q:"fields"`
36
37 // Requests a page size of items.
38 Limit int `q:"limit"`
39
40 // The ID of the last-seen item
41 Marker string `q:"marker"`
42
43 // Sorts the response by the requested sort direction.
44 // Valid value is asc (ascending) or desc (descending). Default is asc.
45 SortDir string `q:"sort_dir"`
46
47 // Sorts the response by the this attribute value. Default is id.
48 SortKey string `q:"sort_key"`
49}
50
51// ToPortListQuery formats a ListOpts into a query string.
52func (opts ListOpts) ToPortListQuery() (string, error) {
53 q, err := gophercloud.BuildQueryString(opts)
54 return q.String(), err
55}
56
57// List makes a request against the API to list ports accessible to you.
58func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
59 url := listURL(client)
60 if opts != nil {
61 query, err := opts.ToPortListQuery()
62 if err != nil {
63 return pagination.Pager{Err: err}
64 }
65 url += query
66 }
67 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
68 return PortPage{pagination.LinkedPageBase{PageResult: r}}
69 })
70}
71
72// ToPortListDetailQuery formats a ListOpts into a query string for the list details API.
73func (opts ListOpts) ToPortListDetailQuery() (string, error) {
74 // Detail endpoint can't filter by Fields
75 if len(opts.Fields) > 0 {
76 return "", fmt.Errorf("fields is not a valid option when getting a detailed listing of ports")
77 }
78
79 q, err := gophercloud.BuildQueryString(opts)
80 return q.String(), err
81}
82
83// ListDetail - Return a list ports with complete details.
84// Some filtering is possible by passing in flags in "ListOpts",
85// but you cannot limit by the fields returned.
86func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
87 url := listDetailURL(client)
88 if opts != nil {
89 query, err := opts.ToPortListDetailQuery()
90 if err != nil {
91 return pagination.Pager{Err: err}
92 }
93 url += query
94 }
95 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
96 return PortPage{pagination.LinkedPageBase{PageResult: r}}
97 })
98}
99
100// Get - requests the details off a port, by ID.
101func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
102 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
103 OkCodes: []int{200},
104 })
105 return
106}
107
108// CreateOptsBuilder allows extensions to add additional parameters to the
109// Create request.
110type CreateOptsBuilder interface {
111 ToPortCreateMap() (map[string]interface{}, error)
112}
113
114// CreateOpts specifies port creation parameters.
115type CreateOpts struct {
116 // UUID of the Node this resource belongs to.
117 NodeUUID string `json:"node_uuid,omitempty"`
118
119 // Physical hardware address of this network Port,
120 // typically the hardware MAC address.
121 Address string `json:"address,omitempty"`
122
123 // UUID of the Portgroup this resource belongs to.
124 PortGroupUUID string `json:"portgroup_uuid,omitempty"`
125
126 // The Port binding profile. If specified, must contain switch_id (only a MAC
127 // address or an OpenFlow based datapath_id of the switch are accepted in this
128 // field) and port_id (identifier of the physical port on the switch to which
129 // node’s port is connected to) fields. switch_info is an optional string
130 // field to be used to store any vendor-specific information.
131 LocalLinkConnection map[string]interface{} `json:"local_link_connection,omitempty"`
132
133 // Indicates whether PXE is enabled or disabled on the Port.
134 PXEEnabled *bool `json:"pxe_enabled,omitempty"`
135
136 // The name of the physical network to which a port is connected. May be empty.
137 PhysicalNetwork string `json:"physical_network,omitempty"`
138
139 // A set of one or more arbitrary metadata key and value pairs.
140 Extra map[string]interface{} `json:"extra,omitempty"`
141
142 // Indicates whether the Port is a Smart NIC port.
143 IsSmartNIC *bool `json:"is_smartnic,omitempty"`
144}
145
146// ToPortCreateMap assembles a request body based on the contents of a CreateOpts.
147func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
148 body, err := gophercloud.BuildRequestBody(opts, "")
149 if err != nil {
150 return nil, err
151 }
152
153 return body, nil
154}
155
156// Create - requests the creation of a port
157func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
158 reqBody, err := opts.ToPortCreateMap()
159 if err != nil {
160 r.Err = err
161 return
162 }
163
164 _, r.Err = client.Post(createURL(client), reqBody, &r.Body, nil)
165 return
166}
167
168// TODO Update
169type Patch interface {
170 ToPortUpdateMap() map[string]interface{}
171}
172
173// UpdateOpts is a slice of Patches used to update a port
174type UpdateOpts []Patch
175
176type UpdateOp string
177
178const (
179 ReplaceOp UpdateOp = "replace"
180 AddOp UpdateOp = "add"
181 RemoveOp UpdateOp = "remove"
182)
183
184type UpdateOperation struct {
185 Op UpdateOp `json:"op,required"`
186 Path string `json:"path,required"`
187 Value interface{} `json:"value,omitempty"`
188}
189
190func (opts UpdateOperation) ToPortUpdateMap() map[string]interface{} {
191 return map[string]interface{}{
192 "op": opts.Op,
193 "path": opts.Path,
194 "value": opts.Value,
195 }
196}
197
198// Update - requests the update of a port
199func Update(client *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
200 body := make([]map[string]interface{}, len(opts))
201 for i, patch := range opts {
202 body[i] = patch.ToPortUpdateMap()
203 }
204
205 _, r.Err = client.Patch(updateURL(client, id), body, &r.Body, &gophercloud.RequestOpts{
206 JSONBody: &body,
207 OkCodes: []int{200},
208 })
209 return
210}
211
212// Delete - requests the deletion of a port
213func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
214 _, r.Err = client.Delete(deleteURL(client, id), nil)
215 return
216}