Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 57e2801

Browse files
committed
Adding Support for LBaaS v2 - Listeners
1 parent f17786f commit 57e2801

File tree

4 files changed

+702
-0
lines changed

4 files changed

+702
-0
lines changed
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
package listeners
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/rackspace/gophercloud"
7+
"github.com/rackspace/gophercloud/pagination"
8+
)
9+
10+
// AdminState gives users a solid type to work with for create and update
11+
// operations. It is recommended that users use the `Up` and `Down` enums.
12+
type AdminState *bool
13+
14+
// Convenience vars for AdminStateUp values.
15+
var (
16+
iTrue = true
17+
iFalse = false
18+
19+
Up AdminState = &iTrue
20+
Down AdminState = &iFalse
21+
)
22+
23+
// ListOpts allows the filtering and sorting of paginated collections through
24+
// the API. Filtering is achieved by passing in struct field values that map to
25+
// the floating IP attributes you want to see returned. SortKey allows you to
26+
// sort by a particular network attribute. SortDir sets the direction, and is
27+
// either `asc' or `desc'. Marker and Limit are used for pagination.
28+
type ListOpts struct {
29+
ID string `q:"id"`
30+
Name string `q:"name"`
31+
AdminStateUp *bool `q:"admin_state_up"`
32+
TenantID string `q:"tenant_id"`
33+
DefaultPoolID string `q:"default_pool_id"`
34+
Protocol string `q:"protocol"`
35+
ProtocolPort int `q:"protocol_port"`
36+
ConnectionLimit int `q:"connection_limit"`
37+
Limit int `q:"limit"`
38+
Marker string `q:"marker"`
39+
SortKey string `q:"sort_key"`
40+
SortDir string `q:"sort_dir"`
41+
}
42+
43+
// List returns a Pager which allows you to iterate over a collection of
44+
// routers. It accepts a ListOpts struct, which allows you to filter and sort
45+
// the returned collection for greater efficiency.
46+
//
47+
// Default policy settings return only those routers that are owned by the
48+
// tenant who submits the request, unless an admin user submits the request.
49+
func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
50+
q, err := gophercloud.BuildQueryString(&opts)
51+
if err != nil {
52+
return pagination.Pager{Err: err}
53+
}
54+
u := rootURL(c) + q.String()
55+
return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
56+
return ListenerPage{pagination.LinkedPageBase{PageResult: r}}
57+
})
58+
}
59+
60+
var (
61+
errLoadbalancerIdRequired = fmt.Errorf("Loadbalancer ID is required")
62+
errProtocolRequired = fmt.Errorf("Protocol is required")
63+
errProtocolPortRequired = fmt.Errorf("Protocol port is required")
64+
)
65+
66+
// CreateOpts contains all the values needed to create a new Listener.
67+
type CreateOpts struct {
68+
// Required. The protocol - can either be TCP, HTTP or HTTPS.
69+
Protocol string
70+
71+
// Required. The port on which to listen for client traffic.
72+
ProtocolPort int
73+
74+
// Required for admins. Indicates the owner of the Listener.
75+
TenantID string
76+
77+
// Required. The load balancer on which to provision this listener.
78+
LoadbalancerID string
79+
80+
// Human-readable name for the Listener. Does not have to be unique.
81+
Name string
82+
83+
// Optional. The ID of the default pool with which the Listener is associated.
84+
DefaultPoolID string
85+
86+
// Optional. Human-readable description for the Listener.
87+
Description string
88+
89+
// Optional. The maximum number of connections allowed for the Listener.
90+
ConnLimit *int
91+
92+
// Optional. A reference to a container of TLS secrets.
93+
DefaultTlsContainerRef string
94+
95+
// Optional. A list of references to TLS secrets.
96+
SniContainerRefs []string
97+
98+
// Optional. The administrative state of the Listener. A valid value is true (UP)
99+
// or false (DOWN).
100+
AdminStateUp *bool
101+
}
102+
103+
// Create is an operation which provisions a new Listeners based on the
104+
// configuration defined in the CreateOpts struct. Once the request is
105+
// validated and progress has started on the provisioning process, a
106+
// CreateResult will be returned.
107+
//
108+
// Users with an admin role can create Listeners on behalf of other tenants by
109+
// specifying a TenantID attribute different than their own.
110+
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
111+
var res CreateResult
112+
113+
// Validate required opts
114+
if opts.LoadbalancerID == "" {
115+
res.Err = errLoadbalancerIdRequired
116+
return res
117+
}
118+
if opts.Protocol == "" {
119+
res.Err = errProtocolRequired
120+
return res
121+
}
122+
if opts.ProtocolPort == 0 {
123+
res.Err = errProtocolPortRequired
124+
return res
125+
}
126+
127+
type listener struct {
128+
Name *string `json:"name,omitempty"`
129+
LoadbalancerID string `json:"loadbalancer_id,omitempty"`
130+
Protocol string `json:"protocol"`
131+
ProtocolPort int `json:"protocol_port"`
132+
DefaultPoolID *string `json:"default_pool_id,omitempty"`
133+
Description *string `json:"description,omitempty"`
134+
TenantID *string `json:"tenant_id,omitempty"`
135+
ConnLimit *int `json:"connection_limit,omitempty"`
136+
AdminStateUp *bool `json:"admin_state_up,omitempty"`
137+
DefaultTlsContainerRef *string `json:"default_tls_container_ref,omitempty"`
138+
SniContainerRefs []string `json:"sni_container_refs,omitempty"`
139+
}
140+
141+
type request struct {
142+
Listener listener `json:"listener"`
143+
}
144+
145+
reqBody := request{Listener: listener{
146+
Name: gophercloud.MaybeString(opts.Name),
147+
LoadbalancerID: opts.LoadbalancerID,
148+
Protocol: opts.Protocol,
149+
ProtocolPort: opts.ProtocolPort,
150+
DefaultPoolID: gophercloud.MaybeString(opts.DefaultPoolID),
151+
Description: gophercloud.MaybeString(opts.Description),
152+
TenantID: gophercloud.MaybeString(opts.TenantID),
153+
ConnLimit: opts.ConnLimit,
154+
AdminStateUp: opts.AdminStateUp,
155+
DefaultTlsContainerRef: gophercloud.MaybeString(opts.DefaultTlsContainerRef),
156+
SniContainerRefs: opts.SniContainerRefs,
157+
}}
158+
159+
_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
160+
return res
161+
}
162+
163+
// Get retrieves a particular Listeners based on its unique ID.
164+
func Get(c *gophercloud.ServiceClient, id string) GetResult {
165+
var res GetResult
166+
_, res.Err = c.Get(resourceURL(c, id), &res.Body, nil)
167+
return res
168+
}
169+
170+
// UpdateOpts contains all the values that can be updated on an existing Listener.
171+
// Attributes not listed here but appear in CreateOpts are immutable and cannot
172+
// be updated.
173+
type UpdateOpts struct {
174+
// Human-readable name for the Listener. Does not have to be unique.
175+
Name string
176+
177+
// Optional. Human-readable description for the Listener.
178+
Description string
179+
180+
// Optional. The maximum number of connections allowed for the Listener.
181+
ConnLimit *int
182+
183+
// Optional. A reference to a container of TLS secrets.
184+
DefaultTlsContainerRef string
185+
186+
// Optional. A list of references to TLS secrets.
187+
SniContainerRefs []string
188+
189+
// Optional. The administrative state of the Listener. A valid value is true (UP)
190+
// or false (DOWN).
191+
AdminStateUp *bool
192+
}
193+
194+
// Update is an operation which modifies the attributes of the specified Listener.
195+
func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
196+
type listener struct {
197+
Name string `json:"name,omitempty"`
198+
Description *string `json:"description,omitempty"`
199+
ConnLimit *int `json:"connection_limit,omitempty"`
200+
AdminStateUp *bool `json:"admin_state_up,omitempty"`
201+
DefaultTlsContainerRef *string `json:"default_tls_container_ref,omitempty"`
202+
SniContainerRefs []string `json:"sni_container_refs,omitempty"`
203+
}
204+
205+
type request struct {
206+
Listener listener `json:"listener"`
207+
}
208+
209+
reqBody := request{Listener: listener{
210+
Name: opts.Name,
211+
Description: gophercloud.MaybeString(opts.Description),
212+
ConnLimit: opts.ConnLimit,
213+
AdminStateUp: opts.AdminStateUp,
214+
DefaultTlsContainerRef: gophercloud.MaybeString(opts.DefaultTlsContainerRef),
215+
SniContainerRefs: opts.SniContainerRefs,
216+
}}
217+
218+
var res UpdateResult
219+
_, res.Err = c.Put(resourceURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{
220+
OkCodes: []int{200, 202},
221+
})
222+
223+
return res
224+
}
225+
226+
// Delete will permanently delete a particular Listeners based on its unique ID.
227+
func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
228+
var res DeleteResult
229+
_, res.Err = c.Delete(resourceURL(c, id), nil)
230+
return res
231+
}

0 commit comments

Comments
 (0)