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) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 12 | } |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 13 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 14 | type ListOptsBuilder interface { |
| 15 | ToServiceListMap() (string, error) |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 16 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 17 | |
| 18 | // ListOpts allows you to query the List method. |
| 19 | type ListOpts struct { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 20 | ServiceType string `q:"type"` |
| 21 | PerPage int `q:"perPage"` |
| 22 | Page int `q:"page"` |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 23 | } |
| 24 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 25 | func (opts ListOpts) ToServiceListMap() (string, error) { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 26 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 27 | return q.String(), err |
| 28 | } |
Ash Wilson | ab6be61 | 2014-09-15 15:51:22 -0400 | [diff] [blame] | 29 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 30 | // List enumerates the services available to a specific user. |
| 31 | func 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 Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 43 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 44 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 45 | // Get returns additional information about a service, given its ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 46 | func Get(client *gophercloud.ServiceClient, serviceID string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 47 | _, r.Err = client.Get(serviceURL(client, serviceID), &r.Body, nil) |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 48 | } |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 49 | |
Ash Wilson | 81ab83a | 2014-10-02 11:06:23 -0400 | [diff] [blame] | 50 | // Update changes the service type of an existing service. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 51 | func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (r UpdateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 52 | b := map[string]string{"type": serviceType} |
| 53 | _, r.Err = client.Patch(serviceURL(client, serviceID), &b, &r.Body, nil) |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 54 | } |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 55 | |
| 56 | // Delete removes an existing service. |
| 57 | // 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] | 58 | func Delete(client *gophercloud.ServiceClient, serviceID string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 59 | _, r.Err = client.Delete(serviceURL(client, serviceID), nil) |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 60 | } |