blob: bb7bb04761afba44116ee4fb1691b382ffaaee76 [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)
jrperritt29ae6b32016-04-13 12:59:37 -050012 return
Jon Perrittdb0ae142016-03-13 00:33:41 -060013}
Ash Wilsonb73b7f82014-08-29 15:38:06 -040014
Jon Perrittdb0ae142016-03-13 00:33:41 -060015type ListOptsBuilder interface {
16 ToServiceListMap() (string, error)
Ash Wilsonb73b7f82014-08-29 15:38:06 -040017}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040018
19// ListOpts allows you to query the List method.
20type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050021 ServiceType string `q:"type"`
22 PerPage int `q:"perPage"`
23 Page int `q:"page"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040024}
25
Jon Perrittdb0ae142016-03-13 00:33:41 -060026func (opts ListOpts) ToServiceListMap() (string, error) {
Jon Perritt19803442014-10-28 12:11:10 -050027 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060028 return q.String(), err
29}
Ash Wilsonab6be612014-09-15 15:51:22 -040030
Jon Perrittdb0ae142016-03-13 00:33:41 -060031// List enumerates the services available to a specific user.
32func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
33 u := listURL(client)
34 if opts != nil {
35 q, err := opts.ToServiceListMap()
36 if err != nil {
37 return pagination.Pager{Err: err}
38 }
39 u += q
40 }
41 return pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
42 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
43 })
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040044}
Ash Wilsonb1129972014-09-03 14:45:21 -040045
Ash Wilson5266e492014-09-09 15:44:30 -040046// Get returns additional information about a service, given its ID.
Jon Perritt3860b512016-03-29 12:01:48 -050047func Get(client *gophercloud.ServiceClient, serviceID string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060048 _, r.Err = client.Get(serviceURL(client, serviceID), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050049 return
Ash Wilsonb1129972014-09-03 14:45:21 -040050}
Ash Wilsond1b72132014-09-03 15:26:26 -040051
Ash Wilson81ab83a2014-10-02 11:06:23 -040052// Update changes the service type of an existing service.
Jon Perritt3860b512016-03-29 12:01:48 -050053func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060054 b := map[string]string{"type": serviceType}
55 _, r.Err = client.Patch(serviceURL(client, serviceID), &b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050056 return
Ash Wilsond1b72132014-09-03 15:26:26 -040057}
Ash Wilsond24786d2014-09-03 15:38:00 -040058
59// Delete removes an existing service.
60// It either deletes all associated endpoints, or fails until all endpoints are deleted.
Jon Perritt3860b512016-03-29 12:01:48 -050061func Delete(client *gophercloud.ServiceClient, serviceID string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060062 _, r.Err = client.Delete(serviceURL(client, serviceID), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050063 return
Ash Wilsond24786d2014-09-03 15:38:00 -040064}