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