Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 1 | package services |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // Create adds a new service of the requested type to the catalog. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 9 | func Create(client *gophercloud.ServiceClient, serviceType string) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 10 | b := map[string]string{"type": serviceType} |
| 11 | _, r.Err = client.Post(listURL(client), b, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 12 | return |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 13 | } |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 14 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 15 | type ListOptsBuilder interface { |
| 16 | ToServiceListMap() (string, error) |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 17 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 18 | |
| 19 | // ListOpts allows you to query the List method. |
| 20 | type ListOpts struct { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 21 | ServiceType string `q:"type"` |
| 22 | PerPage int `q:"perPage"` |
| 23 | Page int `q:"page"` |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 24 | } |
| 25 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 26 | func (opts ListOpts) ToServiceListMap() (string, error) { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 27 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 28 | return q.String(), err |
| 29 | } |
Ash Wilson | ab6be61 | 2014-09-15 15:51:22 -0400 | [diff] [blame] | 30 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 31 | // List enumerates the services available to a specific user. |
| 32 | func 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 Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 44 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 45 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 46 | // Get returns additional information about a service, given its ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 47 | func Get(client *gophercloud.ServiceClient, serviceID string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 48 | _, r.Err = client.Get(serviceURL(client, serviceID), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 49 | return |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 50 | } |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 51 | |
Ash Wilson | 81ab83a | 2014-10-02 11:06:23 -0400 | [diff] [blame] | 52 | // Update changes the service type of an existing service. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 53 | func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (r UpdateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 54 | b := map[string]string{"type": serviceType} |
| 55 | _, r.Err = client.Patch(serviceURL(client, serviceID), &b, &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 56 | return |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 57 | } |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 58 | |
| 59 | // Delete removes an existing service. |
| 60 | // It either deletes all associated endpoints, or fails until all endpoints are deleted. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 61 | func Delete(client *gophercloud.ServiceClient, serviceID string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 62 | _, r.Err = client.Delete(serviceURL(client, serviceID), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 63 | return |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 64 | } |