Sync baremetal openstack with upstream
Change-Id: I125fc08e2cc4433aeaa470de48823dd4434c2030
Related-PROD: PROD-33018
diff --git a/openstack/baremetal/v1/ports/doc.go b/openstack/baremetal/v1/ports/doc.go
new file mode 100644
index 0000000..eb0579b
--- /dev/null
+++ b/openstack/baremetal/v1/ports/doc.go
@@ -0,0 +1,85 @@
+/*
+ Package ports contains the functionality to Listing, Searching, Creating, Updating,
+ and Deleting of bare metal Port resources
+
+ API reference: https://developer.openstack.org/api-ref/baremetal/#ports-ports
+
+
+Example to List Ports with Detail
+
+ ports.ListDetail(client, nil).EachPage(func(page pagination.Page) (bool, error) {
+ portList, err := ports.ExtractPorts(page)
+ if err != nil {
+ return false, err
+ }
+
+ for _, n := range portList {
+ // Do something
+ }
+
+ return true, nil
+ })
+
+Example to List Ports
+
+ listOpts := ports.ListOpts{
+ Limit: 10,
+ }
+
+ ports.List(client, listOpts).EachPage(func(page pagination.Page) (bool, error) {
+ portList, err := ports.ExtractPorts(page)
+ if err != nil {
+ return false, err
+ }
+
+ for _, n := range portList {
+ // Do something
+ }
+
+ return true, nil
+ })
+
+Example to Create a Port
+
+ createOpts := ports.CreateOpts{
+ NodeUUID: "e8920409-e07e-41bb-8cc1-72acb103e2dd",
+ Address: "00:1B:63:84:45:E6",
+ PhysicalNetwork: "my-network",
+ }
+
+ createPort, err := ports.Create(client, createOpts).Extract()
+ if err != nil {
+ panic(err)
+ }
+
+Example to Get a Port
+
+ showPort, err := ports.Get(client, "c9afd385-5d89-4ecb-9e1c-68194da6b474").Extract()
+ if err != nil {
+ panic(err)
+ }
+
+Example to Update a Port
+
+ updateOpts := ports.UpdateOpts{
+ ports.UpdateOperation{
+ Op: ReplaceOp,
+ Path: "/address",
+ Value: "22:22:22:22:22:22",
+ },
+ }
+
+ updatePort, err := ports.Update(client, "c9afd385-5d89-4ecb-9e1c-68194da6b474", updateOpts).Extract()
+ if err != nil {
+ panic(err)
+ }
+
+Example to Delete a Port
+
+ err = ports.Delete(client, "c9afd385-5d89-4ecb-9e1c-68194da6b474").ExtractErr()
+ if err != nil {
+ panic(err)
+ }
+
+*/
+package ports
diff --git a/openstack/baremetal/v1/ports/requests.go b/openstack/baremetal/v1/ports/requests.go
new file mode 100644
index 0000000..00419c6
--- /dev/null
+++ b/openstack/baremetal/v1/ports/requests.go
@@ -0,0 +1,216 @@
+package ports
+
+import (
+ "fmt"
+
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git"
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git/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) {
+ _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
+ OkCodes: []int{200},
+ })
+ 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
+ }
+
+ _, r.Err = client.Post(createURL(client), reqBody, &r.Body, nil)
+ 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"`
+ Path string `json:"path,required"`
+ 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()
+ }
+
+ _, r.Err = client.Patch(updateURL(client, id), body, &r.Body, &gophercloud.RequestOpts{
+ JSONBody: &body,
+ OkCodes: []int{200},
+ })
+ return
+}
+
+// Delete - requests the deletion of a port
+func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
+ _, r.Err = client.Delete(deleteURL(client, id), nil)
+ return
+}
diff --git a/openstack/baremetal/v1/ports/results.go b/openstack/baremetal/v1/ports/results.go
new file mode 100644
index 0000000..3b26811
--- /dev/null
+++ b/openstack/baremetal/v1/ports/results.go
@@ -0,0 +1,131 @@
+package ports
+
+import (
+ "time"
+
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git"
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
+)
+
+type portResult struct {
+ gophercloud.Result
+}
+
+func (r portResult) Extract() (*Port, error) {
+ var s Port
+ err := r.ExtractInto(&s)
+ return &s, err
+}
+
+func (r portResult) ExtractInto(v interface{}) error {
+ return r.Result.ExtractIntoStructPtr(v, "")
+}
+
+func ExtractPortsInto(r pagination.Page, v interface{}) error {
+ return r.(PortPage).Result.ExtractIntoSlicePtr(v, "ports")
+}
+
+// Port represents a port in the OpenStack Bare Metal API.
+type Port struct {
+ // UUID for the resource.
+ UUID string `json:"uuid"`
+
+ // Physical hardware address of this network Port,
+ // typically the hardware MAC address.
+ Address string `json:"address"`
+
+ // UUID of the Node this resource belongs to.
+ NodeUUID string `json:"node_uuid"`
+
+ // UUID of the Portgroup this resource belongs to.
+ PortGroupUUID string `json:"portgroup_uuid"`
+
+ // 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"`
+
+ // Indicates whether PXE is enabled or disabled on the Port.
+ PXEEnabled bool `json:"pxe_enabled"`
+
+ // The name of the physical network to which a port is connected.
+ // May be empty.
+ PhysicalNetwork string `json:"physical_network"`
+
+ // Internal metadata set and stored by the Port. This field is read-only.
+ InternalInfo map[string]interface{} `json:"internal_info"`
+
+ // A set of one or more arbitrary metadata key and value pairs.
+ Extra map[string]interface{} `json:"extra"`
+
+ // The UTC date and time when the resource was created, ISO 8601 format.
+ CreatedAt time.Time `json:"created_at"`
+
+ // The UTC date and time when the resource was updated, ISO 8601 format.
+ // May be “null”.
+ UpdatedAt time.Time `json:"updated_at"`
+
+ // A list of relative links. Includes the self and bookmark links.
+ Links []interface{} `json:"links"`
+
+ // Indicates whether the Port is a Smart NIC port.
+ IsSmartNIC bool `json:"is_smartnic"`
+}
+
+// PortPage abstracts the raw results of making a List() request against
+// the API.
+type PortPage struct {
+ pagination.LinkedPageBase
+}
+
+// IsEmpty returns true if a page contains no Port results.
+func (r PortPage) IsEmpty() (bool, error) {
+ s, err := ExtractPorts(r)
+ return len(s) == 0, err
+}
+
+// NextPageURL uses the response's embedded link reference to navigate to the
+// next page of results.
+func (r PortPage) NextPageURL() (string, error) {
+ var s struct {
+ Links []gophercloud.Link `json:"ports_links"`
+ }
+ err := r.ExtractInto(&s)
+ if err != nil {
+ return "", err
+ }
+ return gophercloud.ExtractNextURL(s.Links)
+}
+
+// ExtractPorts interprets the results of a single page from a List() call,
+// producing a slice of Port entities.
+func ExtractPorts(r pagination.Page) ([]Port, error) {
+ var s []Port
+ err := ExtractPortsInto(r, &s)
+ return s, err
+}
+
+// GetResult is the response from a Get operation. Call its Extract
+// method to interpret it as a Port.
+type GetResult struct {
+ portResult
+}
+
+// CreateResult is the response from a Create operation.
+type CreateResult struct {
+ portResult
+}
+
+// UpdateResult is the response from an Update operation. Call its Extract
+// method to interpret it as a Port.
+type UpdateResult struct {
+ portResult
+}
+
+// DeleteResult is the response from a Delete operation. Call its ExtractErr
+// method to determine if the call succeeded or failed.
+type DeleteResult struct {
+ gophercloud.ErrResult
+}
diff --git a/openstack/baremetal/v1/ports/testing/doc.go b/openstack/baremetal/v1/ports/testing/doc.go
new file mode 100644
index 0000000..bf82f4e
--- /dev/null
+++ b/openstack/baremetal/v1/ports/testing/doc.go
@@ -0,0 +1,2 @@
+// ports unit tests
+package testing
diff --git a/openstack/baremetal/v1/ports/testing/fixtures.go b/openstack/baremetal/v1/ports/testing/fixtures.go
new file mode 100644
index 0000000..2fc7278
--- /dev/null
+++ b/openstack/baremetal/v1/ports/testing/fixtures.go
@@ -0,0 +1,252 @@
+package testing
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+ "time"
+
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/baremetal/v1/ports"
+ th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper"
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client"
+)
+
+// PortListBody contains the canned body of a ports.List response, without detail.
+const PortListBody = `
+{
+ "ports": [
+ {
+ "uuid": "3abe3f36-9708-4e9f-b07e-0f898061d3a7",
+ "links": [
+ {
+ "href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7",
+ "rel": "self"
+ },
+ {
+ "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7",
+ "rel": "bookmark"
+ }
+ ],
+ "address": "52:54:00:0a:af:d1"
+ },
+ {
+ "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "links": [
+ {
+ "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "rel": "self"
+ },
+ {
+ "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "rel": "bookmark"
+ }
+ ],
+ "address": "52:54:00:4d:87:e6"
+ }
+ ]
+}
+`
+
+// PortListDetailBody contains the canned body of a port.ListDetail response.
+const PortListDetailBody = `
+{
+ "ports": [
+ {
+ "local_link_connection": {},
+ "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
+ "uuid": "3abe3f36-9708-4e9f-b07e-0f898061d3a7",
+ "links": [
+ {
+ "href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7",
+ "rel": "self"
+ },
+ {
+ "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7",
+ "rel": "bookmark"
+ }
+ ],
+ "extra": {},
+ "pxe_enabled": true,
+ "portgroup_uuid": null,
+ "updated_at": "2019-02-15T09:55:19+00:00",
+ "physical_network": null,
+ "address": "52:54:00:0a:af:d1",
+ "internal_info": {
+
+ },
+ "created_at": "2019-02-15T09:52:23+00:00"
+ },
+ {
+ "local_link_connection": {},
+ "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
+ "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "links": [
+ {
+ "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "rel": "self"
+ },
+ {
+ "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "rel": "bookmark"
+ }
+ ],
+ "extra": {},
+ "pxe_enabled": true,
+ "portgroup_uuid": null,
+ "updated_at": "2019-02-15T09:55:19+00:00",
+ "physical_network": null,
+ "address": "52:54:00:4d:87:e6",
+ "internal_info": {},
+ "created_at": "2019-02-15T09:52:24+00:00"
+ }
+ ]
+}
+`
+
+// SinglePortBody is the canned body of a Get request on an existing port.
+const SinglePortBody = `
+{
+ "local_link_connection": {
+
+ },
+ "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
+ "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "links": [
+ {
+ "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "rel": "self"
+ },
+ {
+ "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ "rel": "bookmark"
+ }
+ ],
+ "extra": {
+
+ },
+ "pxe_enabled": true,
+ "portgroup_uuid": null,
+ "updated_at": "2019-02-15T09:55:19+00:00",
+ "physical_network": null,
+ "address": "52:54:00:4d:87:e6",
+ "internal_info": {
+
+ },
+ "created_at": "2019-02-15T09:52:24+00:00"
+}
+`
+
+var (
+ fooCreated, _ = time.Parse(time.RFC3339, "2019-02-15T09:52:24+00:00")
+ fooUpdated, _ = time.Parse(time.RFC3339, "2019-02-15T09:55:19+00:00")
+ BarCreated, _ = time.Parse(time.RFC3339, "2019-02-15T09:52:23+00:00")
+ BarUpdated, _ = time.Parse(time.RFC3339, "2019-02-15T09:55:19+00:00")
+ PortFoo = ports.Port{
+ UUID: "f2845e11-dbd4-4728-a8c0-30d19f48924a",
+ NodeUUID: "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
+ Address: "52:54:00:4d:87:e6",
+ PXEEnabled: true,
+ LocalLinkConnection: map[string]interface{}{},
+ InternalInfo: map[string]interface{}{},
+ Extra: map[string]interface{}{},
+ CreatedAt: fooCreated,
+ UpdatedAt: fooUpdated,
+ Links: []interface{}{map[string]interface{}{"href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", "rel": "self"}, map[string]interface{}{"href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", "rel": "bookmark"}},
+ }
+
+ PortBar = ports.Port{
+ UUID: "3abe3f36-9708-4e9f-b07e-0f898061d3a7",
+ NodeUUID: "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
+ Address: "52:54:00:0a:af:d1",
+ PXEEnabled: true,
+ LocalLinkConnection: map[string]interface{}{},
+ InternalInfo: map[string]interface{}{},
+ Extra: map[string]interface{}{},
+ CreatedAt: BarCreated,
+ UpdatedAt: BarUpdated,
+ Links: []interface{}{map[string]interface{}{"href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", "rel": "self"}, map[string]interface{}{"rel": "bookmark", "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7"}},
+ }
+)
+
+// HandlePortListSuccessfully sets up the test server to respond to a port List request.
+func HandlePortListSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ w.Header().Add("Content-Type", "application/json")
+ r.ParseForm()
+
+ marker := r.Form.Get("marker")
+ switch marker {
+ case "":
+ fmt.Fprintf(w, PortListBody)
+
+ case "f2845e11-dbd4-4728-a8c0-30d19f48924a":
+ fmt.Fprintf(w, `{ "ports": [] }`)
+ default:
+ t.Fatalf("/ports invoked with unexpected marker=[%s]", marker)
+ }
+ })
+}
+
+// HandlePortListSuccessfully sets up the test server to respond to a port List request.
+func HandlePortListDetailSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/ports/detail", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ w.Header().Add("Content-Type", "application/json")
+ r.ParseForm()
+
+ fmt.Fprintf(w, PortListDetailBody)
+ })
+}
+
+// HandleSPortCreationSuccessfully sets up the test server to respond to a port creation request
+// with a given response.
+func HandlePortCreationSuccessfully(t *testing.T, response string) {
+ th.Mux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ th.TestJSONRequest(t, r, `{
+ "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
+ "address": "52:54:00:4d:87:e6",
+ "pxe_enabled": true
+ }`)
+
+ w.WriteHeader(http.StatusAccepted)
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, response)
+ })
+}
+
+// HandlePortDeletionSuccessfully sets up the test server to respond to a port deletion request.
+func HandlePortDeletionSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "DELETE")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.WriteHeader(http.StatusNoContent)
+ })
+}
+
+func HandlePortGetSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+
+ fmt.Fprintf(w, SinglePortBody)
+ })
+}
+
+func HandlePortUpdateSuccessfully(t *testing.T, response string) {
+ th.Mux.HandleFunc("/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "PATCH")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestJSONRequest(t, r, `[{"op": "replace", "path": "/address", "value": "22:22:22:22:22:22"}]`)
+
+ fmt.Fprintf(w, response)
+ })
+}
diff --git a/openstack/baremetal/v1/ports/testing/requests_test.go b/openstack/baremetal/v1/ports/testing/requests_test.go
new file mode 100644
index 0000000..db1f133
--- /dev/null
+++ b/openstack/baremetal/v1/ports/testing/requests_test.go
@@ -0,0 +1,144 @@
+package testing
+
+import (
+ "testing"
+
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/baremetal/v1/ports"
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
+ th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper"
+ "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client"
+)
+
+func TestListDetailPorts(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePortListDetailSuccessfully(t)
+
+ pages := 0
+ err := ports.ListDetail(client.ServiceClient(), ports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
+ pages++
+
+ actual, err := ports.ExtractPorts(page)
+ if err != nil {
+ return false, err
+ }
+
+ if len(actual) != 2 {
+ t.Fatalf("Expected 2 ports, got %d", len(actual))
+ }
+ th.CheckDeepEquals(t, PortBar, actual[0])
+ th.CheckDeepEquals(t, PortFoo, actual[1])
+
+ return true, nil
+ })
+
+ th.AssertNoErr(t, err)
+
+ if pages != 1 {
+ t.Errorf("Expected 1 page, saw %d", pages)
+ }
+}
+
+func TestListPorts(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePortListSuccessfully(t)
+
+ pages := 0
+ err := ports.List(client.ServiceClient(), ports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
+ pages++
+
+ actual, err := ports.ExtractPorts(page)
+ if err != nil {
+ return false, err
+ }
+
+ if len(actual) != 2 {
+ t.Fatalf("Expected 2 ports, got %d", len(actual))
+ }
+ th.AssertEquals(t, "3abe3f36-9708-4e9f-b07e-0f898061d3a7", actual[0].UUID)
+ th.AssertEquals(t, "f2845e11-dbd4-4728-a8c0-30d19f48924a", actual[1].UUID)
+
+ return true, nil
+ })
+
+ th.AssertNoErr(t, err)
+
+ if pages != 1 {
+ t.Errorf("Expected 1 page, saw %d", pages)
+ }
+}
+
+func TestListOpts(t *testing.T) {
+ // Detail cannot take Fields
+ opts := ports.ListOpts{
+ Fields: []string{"uuid", "address"},
+ }
+
+ _, err := opts.ToPortListDetailQuery()
+ th.AssertEquals(t, err.Error(), "fields is not a valid option when getting a detailed listing of ports")
+
+ // Regular ListOpts can
+ query, err := opts.ToPortListQuery()
+ th.AssertEquals(t, query, "?fields=uuid&fields=address")
+ th.AssertNoErr(t, err)
+}
+
+func TestCreatePort(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePortCreationSuccessfully(t, SinglePortBody)
+
+ iTrue := true
+ actual, err := ports.Create(client.ServiceClient(), ports.CreateOpts{
+ NodeUUID: "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
+ Address: "52:54:00:4d:87:e6",
+ PXEEnabled: &iTrue,
+ }).Extract()
+ th.AssertNoErr(t, err)
+
+ th.CheckDeepEquals(t, PortFoo, *actual)
+}
+
+func TestDeletePort(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePortDeletionSuccessfully(t)
+
+ res := ports.Delete(client.ServiceClient(), "3abe3f36-9708-4e9f-b07e-0f898061d3a7")
+ th.AssertNoErr(t, res.Err)
+}
+
+func TestGetPort(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePortGetSuccessfully(t)
+
+ c := client.ServiceClient()
+ actual, err := ports.Get(c, "f2845e11-dbd4-4728-a8c0-30d19f48924a").Extract()
+ if err != nil {
+ t.Fatalf("Unexpected Get error: %v", err)
+ }
+
+ th.CheckDeepEquals(t, PortFoo, *actual)
+}
+
+func TestUpdatePort(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandlePortUpdateSuccessfully(t, SinglePortBody)
+
+ c := client.ServiceClient()
+ actual, err := ports.Update(c, "f2845e11-dbd4-4728-a8c0-30d19f48924a", ports.UpdateOpts{
+ ports.UpdateOperation{
+ Op: ports.ReplaceOp,
+ Path: "/address",
+ Value: "22:22:22:22:22:22",
+ },
+ }).Extract()
+ if err != nil {
+ t.Fatalf("Unexpected Update error: %v", err)
+ }
+
+ th.CheckDeepEquals(t, PortFoo, *actual)
+}
diff --git a/openstack/baremetal/v1/ports/urls.go b/openstack/baremetal/v1/ports/urls.go
new file mode 100644
index 0000000..80b86b3
--- /dev/null
+++ b/openstack/baremetal/v1/ports/urls.go
@@ -0,0 +1,31 @@
+package ports
+
+import "gerrit.mcp.mirantis.net/debian/gophercloud.git"
+
+func createURL(client *gophercloud.ServiceClient) string {
+ return client.ServiceURL("ports")
+}
+
+func listURL(client *gophercloud.ServiceClient) string {
+ return createURL(client)
+}
+
+func listDetailURL(client *gophercloud.ServiceClient) string {
+ return client.ServiceURL("ports", "detail")
+}
+
+func resourceURL(client *gophercloud.ServiceClient, id string) string {
+ return client.ServiceURL("ports", id)
+}
+
+func deleteURL(client *gophercloud.ServiceClient, id string) string {
+ return resourceURL(client, id)
+}
+
+func getURL(client *gophercloud.ServiceClient, id string) string {
+ return resourceURL(client, id)
+}
+
+func updateURL(client *gophercloud.ServiceClient, id string) string {
+ return resourceURL(client, id)
+}