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 |
| 29 | } |
| 30 | |
| 31 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
| 32 | // Build query parameters |
| 33 | q := make(map[string]string) |
| 34 | if opts.Status != "" { |
| 35 | q["status"] = opts.Status |
| 36 | } |
| 37 | if opts.Name != "" { |
| 38 | q["name"] = opts.Name |
| 39 | } |
| 40 | if opts.AdminStateUp != nil { |
| 41 | q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp) |
| 42 | } |
| 43 | if opts.NetworkID != "" { |
| 44 | q["network_id"] = opts.NetworkID |
| 45 | } |
| 46 | if opts.TenantID != "" { |
| 47 | q["tenant_id"] = opts.TenantID |
| 48 | } |
| 49 | if opts.DeviceOwner != "" { |
| 50 | q["device_owner"] = opts.DeviceOwner |
| 51 | } |
| 52 | if opts.MACAddress != "" { |
| 53 | q["mac_address"] = opts.MACAddress |
| 54 | } |
| 55 | if opts.ID != "" { |
| 56 | q["id"] = opts.ID |
| 57 | } |
| 58 | if opts.SecurityGroups != "" { |
| 59 | q["security_groups"] = opts.SecurityGroups |
| 60 | } |
| 61 | if opts.DeviceID != "" { |
| 62 | q["device_id"] = opts.DeviceID |
| 63 | } |
| 64 | if opts.BindingHostID != "" { |
| 65 | q["binding:host_id"] = opts.BindingHostID |
| 66 | } |
| 67 | if opts.BindingVIFType != "" { |
| 68 | q["binding:vif_type"] = opts.BindingVIFType |
| 69 | } |
| 70 | if opts.BindingVNICType != "" { |
| 71 | q["binding:vnic_type"] = opts.BindingVNICType |
| 72 | } |
| 73 | if opts.NetworkID != "" { |
| 74 | q["network_id"] = opts.NetworkID |
| 75 | } |
| 76 | if opts.Limit != 0 { |
| 77 | q["limit"] = strconv.Itoa(opts.Limit) |
| 78 | } |
| 79 | if opts.Page != "" { |
| 80 | q["page"] = opts.Page |
| 81 | } |
| 82 | if opts.PerPage != "" { |
| 83 | q["per_page"] = opts.PerPage |
| 84 | } |
| 85 | |
| 86 | u := ListURL(c) + utils.BuildQuery(q) |
| 87 | return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page { |
| 88 | return PortPage{pagination.LinkedPageBase(r)} |
| 89 | }) |
| 90 | } |
Jamie Hannaford | a311f18 | 2014-09-19 11:19:10 +0200 | [diff] [blame^] | 91 | |
| 92 | func Get(c *gophercloud.ServiceClient, id string) (*Port, error) { |
| 93 | var p Port |
| 94 | _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{ |
| 95 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
| 96 | Results: &struct { |
| 97 | Port *Port `json:"port"` |
| 98 | }{&p}, |
| 99 | OkCodes: []int{200}, |
| 100 | }) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | return &p, nil |
| 105 | } |