blob: 81c89605e25bb0b97df3088ea765041f7c70cd1c [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.
Jon Perritt3860b512016-03-29 12:01:48 -05009func Create(client *gophercloud.ServiceClient, serviceType string) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060010 b := map[string]string{"type": serviceType}
11 _, r.Err = client.Post(listURL(client), b, &r.Body, nil)
Jon Perrittdb0ae142016-03-13 00:33:41 -060012}
Ash Wilsonb73b7f82014-08-29 15:38:06 -040013
Jon Perrittdb0ae142016-03-13 00:33:41 -060014type ListOptsBuilder interface {
15 ToServiceListMap() (string, error)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040016}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040017
18// ListOpts allows you to query the List method.
19type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050020 ServiceType string `q:"type"`
21 PerPage int `q:"perPage"`
22 Page int `q:"page"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040023}
24
Jon Perrittdb0ae142016-03-13 00:33:41 -060025func (opts ListOpts) ToServiceListMap() (string, error) {
Jon Perritt19803442014-10-28 12:11:10 -050026 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060027 return q.String(), err
28}
Ash Wilsonab6be612014-09-15 15:51:22 -040029
Jon Perrittdb0ae142016-03-13 00:33:41 -060030// List enumerates the services available to a specific user.
31func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
32 u := listURL(client)
33 if opts != nil {
34 q, err := opts.ToServiceListMap()
35 if err != nil {
36 return pagination.Pager{Err: err}
37 }
38 u += q
39 }
40 return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
41 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
42 })
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040043}
Ash Wilsonb1129972014-09-03 14:45:21 -040044
Ash Wilson5266e492014-09-09 15:44:30 -040045// Get returns additional information about a service, given its ID.
Jon Perritt3860b512016-03-29 12:01:48 -050046func Get(client *gophercloud.ServiceClient, serviceID string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060047 _, r.Err = client.Get(serviceURL(client, serviceID), &r.Body, nil)
Ash Wilsonb1129972014-09-03 14:45:21 -040048}
Ash Wilsond1b72132014-09-03 15:26:26 -040049
Ash Wilson81ab83a2014-10-02 11:06:23 -040050// Update changes the service type of an existing service.
Jon Perritt3860b512016-03-29 12:01:48 -050051func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060052 b := map[string]string{"type": serviceType}
53 _, r.Err = client.Patch(serviceURL(client, serviceID), &b, &r.Body, nil)
Ash Wilsond1b72132014-09-03 15:26:26 -040054}
Ash Wilsond24786d2014-09-03 15:38:00 -040055
56// Delete removes an existing service.
57// It either deletes all associated endpoints, or fails until all endpoints are deleted.
Jon Perritt3860b512016-03-29 12:01:48 -050058func Delete(client *gophercloud.ServiceClient, serviceID string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060059 _, r.Err = client.Delete(serviceURL(client, serviceID), nil)
Ash Wilsond24786d2014-09-03 15:38:00 -040060}