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 | |
| 94 | u := ListURL(c) + utils.BuildQuery(q) |
| 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 |
| 102 | _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{ |
| 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 | |
| 115 | type PortOpts struct { |
| 116 | NetworkID string |
| 117 | Status string |
| 118 | Name string |
| 119 | AdminStateUp *bool |
| 120 | TenantID string |
| 121 | MACAddress string |
| 122 | FixedIPs interface{} |
| 123 | SecurityGroups []string |
| 124 | } |
| 125 | |
| 126 | func maybeString(original string) *string { |
| 127 | if original != "" { |
| 128 | return &original |
| 129 | } |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | func Create(c *gophercloud.ServiceClient, opts PortOpts) (*Port, error) { |
| 134 | type port struct { |
| 135 | NetworkID string `json:"network_id,omitempty"` |
| 136 | Status *string `json:"status,omitempty"` |
| 137 | Name *string `json:"name,omitempty"` |
| 138 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 139 | TenantID *string `json:"tenant_id,omitempty"` |
| 140 | MACAddress *string `json:"mac_address,omitempty"` |
| 141 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
| 142 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 143 | } |
| 144 | type request struct { |
| 145 | Port port `json:"port"` |
| 146 | } |
| 147 | |
| 148 | // Validate |
| 149 | if opts.NetworkID == "" { |
| 150 | return nil, ErrNetworkIDRequired |
| 151 | } |
| 152 | |
| 153 | // Populate request body |
| 154 | reqBody := request{Port: port{ |
| 155 | NetworkID: opts.NetworkID, |
| 156 | Status: maybeString(opts.Status), |
| 157 | Name: maybeString(opts.Name), |
| 158 | AdminStateUp: opts.AdminStateUp, |
| 159 | TenantID: maybeString(opts.TenantID), |
| 160 | MACAddress: maybeString(opts.MACAddress), |
| 161 | }} |
| 162 | |
| 163 | if opts.FixedIPs != nil { |
| 164 | reqBody.Port.FixedIPs = opts.FixedIPs |
| 165 | } |
| 166 | |
| 167 | if opts.SecurityGroups != nil { |
| 168 | reqBody.Port.SecurityGroups = opts.SecurityGroups |
| 169 | } |
| 170 | |
| 171 | // Response |
| 172 | type response struct { |
| 173 | Port *Port `json:"port"` |
| 174 | } |
| 175 | var res response |
| 176 | _, err := perigee.Request("POST", CreateURL(c), perigee.Options{ |
| 177 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 178 | ReqBody: &reqBody, |
| 179 | Results: &res, |
| 180 | OkCodes: []int{201}, |
Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 181 | DumpReqJson: true, |
Jamie Hannaford | a5fb782 | 2014-09-19 15:07:02 +0200 | [diff] [blame] | 182 | }) |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | |
| 187 | return res.Port, nil |
| 188 | } |
| 189 | |
| 190 | func Update(c *gophercloud.ServiceClient, id string, opts PortOpts) (*Port, error) { |
| 191 | type port struct { |
| 192 | NetworkID string `json:"network_id,omitempty"` |
| 193 | Status *string `json:"status,omitempty"` |
| 194 | Name *string `json:"name,omitempty"` |
| 195 | AdminStateUp *bool `json:"admin_state_up,omitempty"` |
| 196 | TenantID *string `json:"tenant_id,omitempty"` |
| 197 | MACAddress *string `json:"mac_address,omitempty"` |
| 198 | FixedIPs interface{} `json:"fixed_ips,omitempty"` |
| 199 | SecurityGroups []string `json:"security_groups,omitempty"` |
| 200 | } |
| 201 | type request struct { |
| 202 | Port port `json:"port"` |
| 203 | } |
| 204 | |
| 205 | // Validate |
| 206 | if opts.NetworkID == "" { |
| 207 | return nil, ErrNetworkIDRequired |
| 208 | } |
| 209 | |
| 210 | // Populate request body |
| 211 | reqBody := request{Port: port{ |
| 212 | NetworkID: opts.NetworkID, |
| 213 | Status: maybeString(opts.Status), |
| 214 | Name: maybeString(opts.Name), |
| 215 | AdminStateUp: opts.AdminStateUp, |
| 216 | TenantID: maybeString(opts.TenantID), |
| 217 | MACAddress: maybeString(opts.MACAddress), |
| 218 | }} |
| 219 | |
| 220 | if opts.FixedIPs != nil { |
| 221 | reqBody.Port.FixedIPs = opts.FixedIPs |
| 222 | } |
| 223 | |
| 224 | if opts.SecurityGroups != nil { |
| 225 | reqBody.Port.SecurityGroups = opts.SecurityGroups |
| 226 | } |
| 227 | |
| 228 | // Response |
| 229 | type response struct { |
| 230 | Port *Port `json:"port"` |
| 231 | } |
| 232 | var res response |
| 233 | _, err := perigee.Request("PUT", UpdateURL(c, id), perigee.Options{ |
| 234 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 235 | ReqBody: &reqBody, |
| 236 | Results: &res, |
| 237 | OkCodes: []int{200, 201}, |
| 238 | }) |
| 239 | if err != nil { |
| 240 | return nil, err |
| 241 | } |
| 242 | |
| 243 | return res.Port, nil |
| 244 | } |
Jamie Hannaford | d444b7a | 2014-09-19 15:08:27 +0200 | [diff] [blame] | 245 | |
| 246 | func Delete(c *gophercloud.ServiceClient, id string) error { |
| 247 | _, err := perigee.Request("DELETE", DeleteURL(c, id), perigee.Options{ |
| 248 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 249 | OkCodes: []int{204}, |
| 250 | }) |
| 251 | return err |
| 252 | } |