blob: 672b9049a56aadb14152cc79ebc4c0b546b817fc [file] [log] [blame]
Jamie Hannaford548d3402014-09-18 15:50:08 +02001package ports
2
3import (
4 "strconv"
5
Jamie Hannaforda311f182014-09-19 11:19:10 +02006 "github.com/racker/perigee"
Jamie Hannaford548d3402014-09-18 15:50:08 +02007 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/openstack/utils"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
12type 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
31func 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 Hannaforda311f182014-09-19 11:19:10 +020091
92func 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}