Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 1 | package ports |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 6 | "github.com/racker/perigee" |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/openstack/utils" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
| 12 | type ListOpts struct { |
| 13 | Status string |
| 14 | Name string |
| 15 | AdminStateUp *bool |
| 16 | NetworkID string |
| 17 | TenantID string |
| 18 | DeviceOwner string |
| 19 | MACAddress string |
| 20 | ID string |
| 21 | SecurityGroups string |
| 22 | DeviceID string |
| 23 | BindingHostID string |
| 24 | BindingVIFType string |
| 25 | BindingVNICType string |
| 26 | Limit int |
| 27 | Page string |
| 28 | PerPage string |
Jamie Hannaford | d0f090c | 2014-09-22 13:44:34 +0200 | [diff] [blame] | 29 | SortKey string |
| 30 | SortDir string |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
| 34 | // Build query parameters |
| 35 | q := make(map[string]string) |
| 36 | if opts.Status != "" { |
| 37 | q["status"] = opts.Status |
| 38 | } |
| 39 | if opts.Name != "" { |
| 40 | q["name"] = opts.Name |
| 41 | } |
| 42 | if opts.AdminStateUp != nil { |
| 43 | q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp) |
| 44 | } |
| 45 | if opts.NetworkID != "" { |
| 46 | q["network_id"] = opts.NetworkID |
| 47 | } |
| 48 | if opts.TenantID != "" { |
| 49 | q["tenant_id"] = opts.TenantID |
| 50 | } |
| 51 | if opts.DeviceOwner != "" { |
| 52 | q["device_owner"] = opts.DeviceOwner |
| 53 | } |
| 54 | if opts.MACAddress != "" { |
| 55 | q["mac_address"] = opts.MACAddress |
| 56 | } |
| 57 | if opts.ID != "" { |
| 58 | q["id"] = opts.ID |
| 59 | } |
| 60 | if opts.SecurityGroups != "" { |
| 61 | q["security_groups"] = opts.SecurityGroups |
| 62 | } |
| 63 | if opts.DeviceID != "" { |
| 64 | q["device_id"] = opts.DeviceID |
| 65 | } |
| 66 | if opts.BindingHostID != "" { |
| 67 | q["binding:host_id"] = opts.BindingHostID |
| 68 | } |
| 69 | if opts.BindingVIFType != "" { |
| 70 | q["binding:vif_type"] = opts.BindingVIFType |
| 71 | } |
| 72 | if opts.BindingVNICType != "" { |
| 73 | q["binding:vnic_type"] = opts.BindingVNICType |
| 74 | } |
| 75 | if opts.NetworkID != "" { |
| 76 | q["network_id"] = opts.NetworkID |
| 77 | } |
| 78 | if opts.Limit != 0 { |
| 79 | q["limit"] = strconv.Itoa(opts.Limit) |
| 80 | } |
| 81 | if opts.Page != "" { |
| 82 | q["page"] = opts.Page |
| 83 | } |
| 84 | if opts.PerPage != "" { |
| 85 | q["per_page"] = opts.PerPage |
| 86 | } |
Jamie Hannaford | d0f090c | 2014-09-22 13:44:34 +0200 | [diff] [blame] | 87 | if opts.SortKey != "" { |
| 88 | q["sort_key"] = opts.SortKey |
| 89 | } |
| 90 | if opts.SortDir != "" { |
| 91 | q["sort_dir"] = opts.SortDir |
| 92 | } |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 93 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 94 | u := listURL(c) + utils.BuildQuery(q) |
Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 95 | return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page { |
| 96 | return PortPage{pagination.LinkedPageBase(r)} |
| 97 | }) |
| 98 | } |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 99 | |
| 100 | func Get(c *gophercloud.ServiceClient, id string) (*Port, error) { |
| 101 | var p Port |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 102 | _, err := perigee.Request("GET", getURL(c, id), perigee.Options{ |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame] | 103 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 104 | Results: &struct { |
| 105 | Port *Port `json:"port"` |
| 106 | }{&p}, |
| 107 | OkCodes: []int{200}, |
| 108 | }) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | return &p, nil |
| 113 | } |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 114 | |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 115 | func maybeString(original string) *string { |
| 116 | if original != "" { |
| 117 | return &original |
| 118 | } |
| 119 | return nil |
| 120 | } |
| 121 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 122 | type CreateOpts struct { |
| 123 | NetworkID string |
| 124 | Name string |
| 125 | AdminStateUp *bool |
| 126 | MACAddress string |
| 127 | FixedIPs interface{} |
| 128 | DeviceID string |
| 129 | DeviceOwner string |
| 130 | TenantID string |
| 131 | SecurityGroups []string |
| 132 | } |
| 133 | |
| 134 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*Port, error) { |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 135 | type port struct { |
| 136 | NetworkID string `json:"network_id,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 137 | Name *string `json:"name,omitempty"` |
| 138 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 139 | MACAddress *string `json:"mac_address,omitempty"` |
| 140 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 141 | DeviceID *string `json:"device_id,omitempty"` |
| 142 | DeviceOwner *string `json:"device_owner,omitempty"` |
| 143 | TenantID *string `json:"tenant_id,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 144 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 145 | } |
| 146 | type request struct { |
| 147 | Port port `json:"port"` |
| 148 | } |
| 149 | |
| 150 | // Validate |
| 151 | if opts.NetworkID == "" { |
| 152 | return nil, ErrNetworkIDRequired |
| 153 | } |
| 154 | |
| 155 | // Populate request body |
| 156 | reqBody := request{Port: port{ |
| 157 | NetworkID: opts.NetworkID, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 158 | Name: maybeString(opts.Name), |
| 159 | AdminStateUp: opts.AdminStateUp, |
| 160 | TenantID: maybeString(opts.TenantID), |
| 161 | MACAddress: maybeString(opts.MACAddress), |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 162 | DeviceID: maybeString(opts.DeviceID), |
| 163 | DeviceOwner: maybeString(opts.DeviceOwner), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 164 | }} |
| 165 | |
| 166 | if opts.FixedIPs != nil { |
| 167 | reqBody.Port.FixedIPs = opts.FixedIPs |
| 168 | } |
| 169 | |
| 170 | if opts.SecurityGroups != nil { |
| 171 | reqBody.Port.SecurityGroups = opts.SecurityGroups |
| 172 | } |
| 173 | |
| 174 | // Response |
| 175 | type response struct { |
| 176 | Port *Port `json:"port"` |
| 177 | } |
| 178 | var res response |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 179 | _, err := perigee.Request("POST", createURL(c), perigee.Options{ |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 180 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 181 | ReqBody: &reqBody, |
| 182 | Results: &res, |
| 183 | OkCodes: []int{201}, |
Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 184 | DumpReqJson: true, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 185 | }) |
| 186 | if err != nil { |
| 187 | return nil, err |
| 188 | } |
| 189 | |
| 190 | return res.Port, nil |
| 191 | } |
| 192 | |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 193 | type UpdateOpts struct { |
| 194 | Name string |
| 195 | AdminStateUp *bool |
| 196 | FixedIPs interface{} |
| 197 | DeviceID string |
| 198 | DeviceOwner string |
| 199 | SecurityGroups []string |
| 200 | } |
| 201 | |
| 202 | func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (*Port, error) { |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 203 | type port struct { |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 204 | Name *string `json:"name,omitempty"` |
| 205 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 206 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 207 | DeviceID *string `json:"device_id,omitempty"` |
| 208 | DeviceOwner *string `json:"device_owner,omitempty"` |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 209 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 210 | } |
| 211 | type request struct { |
| 212 | Port port `json:"port"` |
| 213 | } |
| 214 | |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 215 | // Populate request body |
| 216 | reqBody := request{Port: port{ |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 217 | Name: maybeString(opts.Name), |
| 218 | AdminStateUp: opts.AdminStateUp, |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 219 | DeviceID: maybeString(opts.DeviceID), |
| 220 | DeviceOwner: maybeString(opts.DeviceOwner), |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 221 | }} |
| 222 | |
| 223 | if opts.FixedIPs != nil { |
| 224 | reqBody.Port.FixedIPs = opts.FixedIPs |
| 225 | } |
| 226 | |
| 227 | if opts.SecurityGroups != nil { |
| 228 | reqBody.Port.SecurityGroups = opts.SecurityGroups |
| 229 | } |
| 230 | |
| 231 | // Response |
| 232 | type response struct { |
| 233 | Port *Port `json:"port"` |
| 234 | } |
| 235 | var res response |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 236 | _, err := perigee.Request("PUT", updateURL(c, id), perigee.Options{ |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 237 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 238 | ReqBody: &reqBody, |
| 239 | Results: &res, |
| 240 | OkCodes: []int{200, 201}, |
| 241 | }) |
| 242 | if err != nil { |
| 243 | return nil, err |
| 244 | } |
| 245 | |
| 246 | return res.Port, nil |
| 247 | } |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 248 | |
| 249 | func Delete(c *gophercloud.ServiceClient, id string) error { |
Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame] | 250 | _, err := perigee.Request("DELETE", deleteURL(c, id), perigee.Options{ |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 251 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 252 | OkCodes: []int{204}, |
| 253 | }) |
| 254 | return err |
| 255 | } |