1package services
2
3import (
4	"github.com/gophercloud/gophercloud"
5	"github.com/gophercloud/gophercloud/pagination"
6)
7
8// CreateOptsBuilder allows extensions to add additional parameters to the
9// Create request.
10type CreateOptsBuilder interface {
11	ToServiceCreateMap() (map[string]interface{}, error)
12}
13
14// CreateOpts contains all the values needed to create a new VPN service
15type CreateOpts struct {
16	// TenantID specifies a tenant to own the VPN service. The caller must have
17	// an admin role in order to set this. Otherwise, this field is left unset
18	// and the caller will be the owner.
19	TenantID string `json:"tenant_id,omitempty"`
20
21	// SubnetID is the ID of the subnet.
22	SubnetID string `json:"subnet_id,omitempty"`
23
24	// RouterID is the ID of the router.
25	RouterID string `json:"router_id" required:"true"`
26
27	// Description is the human readable description of the service.
28	Description string `json:"description,omitempty"`
29
30	// AdminStateUp is the administrative state of the resource, which is up (true) or down (false).
31	AdminStateUp *bool `json:"admin_state_up"`
32
33	// Name is the human readable name of the service.
34	Name string `json:"name,omitempty"`
35
36	// The ID of the flavor.
37	FlavorID string `json:"flavor_id,omitempty"`
38}
39
40// ToServiceCreateMap casts a CreateOpts struct to a map.
41func (opts CreateOpts) ToServiceCreateMap() (map[string]interface{}, error) {
42	return gophercloud.BuildRequestBody(opts, "vpnservice")
43}
44
45// Create accepts a CreateOpts struct and uses the values to create a new
46// VPN service.
47func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
48	b, err := opts.ToServiceCreateMap()
49	if err != nil {
50		r.Err = err
51		return
52	}
53	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
54	return
55}
56
57// Delete will permanently delete a particular VPN service based on its
58// unique ID.
59func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
60	_, r.Err = c.Delete(resourceURL(c, id), nil)
61	return
62}
63
64// UpdateOptsBuilder allows extensions to add additional parameters to the
65// Update request.
66type UpdateOptsBuilder interface {
67	ToServiceUpdateMap() (map[string]interface{}, error)
68}
69
70// UpdateOpts contains the values used when updating a VPN service
71type UpdateOpts struct {
72	// Name is the human readable name of the service.
73	Name *string `json:"name,omitempty"`
74
75	// Description is the human readable description of the service.
76	Description *string `json:"description,omitempty"`
77
78	// AdminStateUp is the administrative state of the resource, which is up (true) or down (false).
79	AdminStateUp *bool `json:"admin_state_up,omitempty"`
80}
81
82// ToServiceUpdateMap casts aa UodateOpts struct to a map.
83func (opts UpdateOpts) ToServiceUpdateMap() (map[string]interface{}, error) {
84	return gophercloud.BuildRequestBody(opts, "vpnservice")
85}
86
87// Update allows VPN services to be updated.
88func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
89	b, err := opts.ToServiceUpdateMap()
90	if err != nil {
91		r.Err = err
92		return
93	}
94	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
95		OkCodes: []int{200},
96	})
97	return
98}
99
100// ListOptsBuilder allows extensions to add additional parameters to the
101// List request.
102type ListOptsBuilder interface {
103	ToServiceListQuery() (string, error)
104}
105
106// ListOpts allows the filtering and sorting of paginated collections through
107// the API. Filtering is achieved by passing in struct field values that map to
108// the VPN service attributes you want to see returned.
109type ListOpts struct {
110	TenantID     string `q:"tenant_id"`
111	Name         string `q:"name"`
112	Description  string `q:"description"`
113	AdminStateUp *bool  `q:"admin_state_up"`
114	Status       string `q:"status"`
115	SubnetID     string `q:"subnet_id"`
116	RouterID     string `q:"router_id"`
117	ProjectID    string `q:"project_id"`
118	ExternalV6IP string `q:"external_v6_ip"`
119	ExternalV4IP string `q:"external_v4_ip"`
120	FlavorID     string `q:"flavor_id"`
121}
122
123// ToServiceListQuery formats a ListOpts into a query string.
124func (opts ListOpts) ToServiceListQuery() (string, error) {
125	q, err := gophercloud.BuildQueryString(opts)
126	return q.String(), err
127}
128
129// List returns a Pager which allows you to iterate over a collection of
130// VPN services. It accepts a ListOpts struct, which allows you to filter
131// and sort the returned collection for greater efficiency.
132func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
133	url := rootURL(c)
134	if opts != nil {
135		query, err := opts.ToServiceListQuery()
136		if err != nil {
137			return pagination.Pager{Err: err}
138		}
139		url += query
140	}
141	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
142		return ServicePage{pagination.LinkedPageBase{PageResult: r}}
143	})
144}
145
146// Get retrieves a particular VPN service based on its unique ID.
147func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
148	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
149	return
150}
151