blob: 91118a42e2faee5fa4d7fc3f2bd24ad58d2b3301 [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
Jamie Hannafordd9036422014-09-23 17:50:24 +02004 "fmt"
5
Jamie Hannaford548d3402014-09-18 15:50:08 +02006 "github.com/mitchellh/mapstructure"
Jamie Hannafordd9036422014-09-23 17:50:24 +02007 "github.com/rackspace/gophercloud"
Jamie Hannaford548d3402014-09-18 15:50:08 +02008 "github.com/rackspace/gophercloud/pagination"
9)
10
Jamie Hannafordd9036422014-09-23 17:50:24 +020011type commonResult struct {
12 gophercloud.CommonResult
13}
14
Jamie Hannafordf3114832014-09-24 11:00:43 +020015// Extract is a function that accepts a result and extracts a port resource.
Jamie Hannafordd9036422014-09-23 17:50:24 +020016func (r commonResult) Extract() (*Port, error) {
17 if r.Err != nil {
18 return nil, r.Err
19 }
20
21 var res struct {
22 Port *Port `json:"port"`
23 }
24
25 err := mapstructure.Decode(r.Resp, &res)
26 if err != nil {
27 return nil, fmt.Errorf("Error decoding Neutron port: %v", err)
28 }
29
30 return res.Port, nil
31}
32
Jamie Hannafordf3114832014-09-24 11:00:43 +020033// CreateResult represents the result of a create operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020034type CreateResult struct {
35 commonResult
36}
37
Jamie Hannafordf3114832014-09-24 11:00:43 +020038// GetResult represents the result of a get operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020039type GetResult struct {
40 commonResult
41}
42
Jamie Hannafordf3114832014-09-24 11:00:43 +020043// UpdateResult represents the result of an update operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020044type UpdateResult struct {
45 commonResult
46}
47
Jamie Hannafordf3114832014-09-24 11:00:43 +020048// DeleteResult represents the result of a delete operation.
Jamie Hannafordd9036422014-09-23 17:50:24 +020049type DeleteResult commonResult
50
Jamie Hannaford686c4962014-09-23 10:46:20 +020051// IP is a sub-struct that represents an individual IP.
Jamie Hannaford548d3402014-09-18 15:50:08 +020052type IP struct {
53 SubnetID string `mapstructure:"subnet_id" json:"subnet_id"`
Jamie Hannaford2a0492a2014-09-22 12:02:11 +020054 IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020055}
56
Jamie Hannaford686c4962014-09-23 10:46:20 +020057// Port represents a Neutron port. See package documentation for a top-level
58// description of what this is.
Jamie Hannaford548d3402014-09-18 15:50:08 +020059type Port struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +020060 // UUID for the port.
61 ID string `mapstructure:"id" json:"id"`
62 // Network that this port is associated with.
63 NetworkID string `mapstructure:"network_id" json:"network_id"`
64 // Human-readable name for the port. Might not be unique.
65 Name string `mapstructure:"name" json:"name"`
66 // Administrative state of port. If false (down), port does not forward packets.
67 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
68 // Indicates whether network is currently operational. Possible values include
69 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
70 Status string `mapstructure:"status" json:"status"`
71 // Mac address to use on this port.
72 MACAddress string `mapstructure:"mac_address" json:"mac_address"`
73 // Specifies IP addresses for the port thus associating the port itself with
74 // the subnets where the IP addresses are picked from
75 FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"`
76 // Owner of network. Only admin users can specify a tenant_id other than its own.
77 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
78 // Identifies the entity (e.g.: dhcp agent) using this port.
79 DeviceOwner string `mapstructure:"device_owner" json:"device_owner"`
80 // Specifies the IDs of any security groups associated with a port.
81 SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"`
82 // Identifies the device (e.g., virtual server) using this port.
83 DeviceID string `mapstructure:"device_id" json:"device_id"`
Jamie Hannaford548d3402014-09-18 15:50:08 +020084}
85
Jamie Hannaford686c4962014-09-23 10:46:20 +020086// PortPage is the page returned by a pager when traversing over a collection
87// of network ports.
Jamie Hannaford548d3402014-09-18 15:50:08 +020088type PortPage struct {
89 pagination.LinkedPageBase
90}
91
Jamie Hannaford686c4962014-09-23 10:46:20 +020092// NextPageURL is invoked when a paginated collection of ports has reached
93// the end of a page and the pager seeks to traverse over a new one. In order
94// to do this, it needs to construct the next page's URL.
95func (p PortPage) NextPageURL() (string, error) {
Jamie Hannaford548d3402014-09-18 15:50:08 +020096 type resp struct {
97 Links []struct {
98 Href string `mapstructure:"href"`
99 Rel string `mapstructure:"rel"`
100 } `mapstructure:"ports_links"`
101 }
102
103 var r resp
Jamie Hannaford686c4962014-09-23 10:46:20 +0200104 err := mapstructure.Decode(p.Body, &r)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200105 if err != nil {
106 return "", err
107 }
108
109 var url string
110 for _, l := range r.Links {
111 if l.Rel == "next" {
112 url = l.Href
113 }
114 }
115 if url == "" {
116 return "", nil
117 }
118
119 return url, nil
120}
121
Jamie Hannaford686c4962014-09-23 10:46:20 +0200122// IsEmpty checks whether a PortPage struct is empty.
123func (p PortPage) IsEmpty() (bool, error) {
124 is, err := ExtractPorts(p)
Jamie Hannaford548d3402014-09-18 15:50:08 +0200125 if err != nil {
126 return true, nil
127 }
128 return len(is) == 0, nil
129}
130
Jamie Hannaford686c4962014-09-23 10:46:20 +0200131// ExtractPorts accepts a Page struct, specifically a PortPage struct,
132// and extracts the elements into a slice of Port structs. In other words,
133// a generic collection is mapped into a relevant slice.
Jamie Hannaford548d3402014-09-18 15:50:08 +0200134func ExtractPorts(page pagination.Page) ([]Port, error) {
135 var resp struct {
136 Ports []Port `mapstructure:"ports" json:"ports"`
137 }
138
139 err := mapstructure.Decode(page.(PortPage).Body, &resp)
140 if err != nil {
141 return nil, err
142 }
143
144 return resp.Ports, nil
145}