blob: 34bf97929d1811fcc00f2c3573922137b642c53b [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Ash Wilsonb73b7f82014-08-29 15:38:06 -04006)
7
8// Create adds a new service of the requested type to the catalog.
Ash Wilson74e2bb82014-09-30 17:08:48 -04009func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060010 var r CreateResult
11 b := map[string]string{"type": serviceType}
12 _, r.Err = client.Post(listURL(client), b, &r.Body, nil)
13 return r
14}
Ash Wilsonb73b7f82014-08-29 15:38:06 -040015
Jon Perrittdb0ae142016-03-13 00:33:41 -060016type ListOptsBuilder interface {
17 ToServiceListMap() (string, error)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040018}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040019
20// ListOpts allows you to query the List method.
21type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050022 ServiceType string `q:"type"`
23 PerPage int `q:"perPage"`
24 Page int `q:"page"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040025}
26
Jon Perrittdb0ae142016-03-13 00:33:41 -060027func (opts ListOpts) ToServiceListMap() (string, error) {
Jon Perritt19803442014-10-28 12:11:10 -050028 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060029 return q.String(), err
30}
Ash Wilsonab6be612014-09-15 15:51:22 -040031
Jon Perrittdb0ae142016-03-13 00:33:41 -060032// List enumerates the services available to a specific user.
33func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
34 u := listURL(client)
35 if opts != nil {
36 q, err := opts.ToServiceListMap()
37 if err != nil {
38 return pagination.Pager{Err: err}
39 }
40 u += q
41 }
42 return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
43 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
44 })
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040045}
Ash Wilsonb1129972014-09-03 14:45:21 -040046
Ash Wilson5266e492014-09-09 15:44:30 -040047// Get returns additional information about a service, given its ID.
Ash Wilson74e2bb82014-09-30 17:08:48 -040048func Get(client *gophercloud.ServiceClient, serviceID string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060049 var r GetResult
50 _, r.Err = client.Get(serviceURL(client, serviceID), &r.Body, nil)
51 return r
Ash Wilsonb1129972014-09-03 14:45:21 -040052}
Ash Wilsond1b72132014-09-03 15:26:26 -040053
Ash Wilson81ab83a2014-10-02 11:06:23 -040054// Update changes the service type of an existing service.
Ash Wilson74e2bb82014-09-30 17:08:48 -040055func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060056 var r UpdateResult
57 b := map[string]string{"type": serviceType}
58 _, r.Err = client.Patch(serviceURL(client, serviceID), &b, &r.Body, nil)
59 return r
Ash Wilsond1b72132014-09-03 15:26:26 -040060}
Ash Wilsond24786d2014-09-03 15:38:00 -040061
62// Delete removes an existing service.
63// It either deletes all associated endpoints, or fails until all endpoints are deleted.
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010064func Delete(client *gophercloud.ServiceClient, serviceID string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060065 var r DeleteResult
66 _, r.Err = client.Delete(serviceURL(client, serviceID), nil)
67 return r
Ash Wilsond24786d2014-09-03 15:38:00 -040068}