Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 1 | package ports |
| 2 | |
| 3 | import ( |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 11 | type commonResult struct { |
| 12 | gophercloud.CommonResult |
| 13 | } |
| 14 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 15 | // Extract is a function that accepts a result and extracts a port resource. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 16 | func (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 Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 33 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 34 | type CreateResult struct { |
| 35 | commonResult |
| 36 | } |
| 37 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 38 | // GetResult represents the result of a get operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 39 | type GetResult struct { |
| 40 | commonResult |
| 41 | } |
| 42 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 43 | // UpdateResult represents the result of an update operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 44 | type UpdateResult struct { |
| 45 | commonResult |
| 46 | } |
| 47 | |
Jamie Hannaford | f311483 | 2014-09-24 11:00:43 +0200 | [diff] [blame] | 48 | // DeleteResult represents the result of a delete operation. |
Jamie Hannaford | d903642 | 2014-09-23 17:50:24 +0200 | [diff] [blame] | 49 | type DeleteResult commonResult |
| 50 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 51 | // IP is a sub-struct that represents an individual IP. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 52 | type IP struct { |
| 53 | SubnetID string `mapstructure:"subnet_id" json:"subnet_id"` |
Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 54 | IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"` |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 57 | // Port represents a Neutron port. See package documentation for a top-level |
| 58 | // description of what this is. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 59 | type Port struct { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 60 | // 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 Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 86 | // PortPage is the page returned by a pager when traversing over a collection |
| 87 | // of network ports. |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 88 | type PortPage struct { |
| 89 | pagination.LinkedPageBase |
| 90 | } |
| 91 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 92 | // 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. |
| 95 | func (p PortPage) NextPageURL() (string, error) { |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 96 | 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 Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 104 | err := mapstructure.Decode(p.Body, &r) |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 105 | 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 Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 122 | // IsEmpty checks whether a PortPage struct is empty. |
| 123 | func (p PortPage) IsEmpty() (bool, error) { |
| 124 | is, err := ExtractPorts(p) |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 125 | if err != nil { |
| 126 | return true, nil |
| 127 | } |
| 128 | return len(is) == 0, nil |
| 129 | } |
| 130 | |
Jamie Hannaford | 686c496 | 2014-09-23 10:46:20 +0200 | [diff] [blame] | 131 | // 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 Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 134 | func 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 | } |