Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 1 | package services |
| 2 | |
| 3 | import ( |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 8 | type response struct { |
Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 9 | Service Service `json:"service"` |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 10 | } |
| 11 | |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 12 | // Create adds a new service of the requested type to the catalog. |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 13 | func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult { |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 14 | type request struct { |
| 15 | Type string `json:"type"` |
| 16 | } |
| 17 | |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 18 | req := request{Type: serviceType} |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 19 | |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 20 | var result CreateResult |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 21 | _, result.Err = client.Request("POST", listURL(client), gophercloud.RequestOpts{ |
| 22 | JSONBody: &req, |
| 23 | JSONResponse: &result.Body, |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 24 | }) |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 25 | return result |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 26 | } |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 27 | |
| 28 | // ListOpts allows you to query the List method. |
| 29 | type ListOpts struct { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 30 | ServiceType string `q:"type"` |
| 31 | PerPage int `q:"perPage"` |
| 32 | Page int `q:"page"` |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // List enumerates the services available to a specific user. |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 36 | func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 37 | u := listURL(client) |
| 38 | q, err := gophercloud.BuildQueryString(opts) |
| 39 | if err != nil { |
| 40 | return pagination.Pager{Err: err} |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 41 | } |
Jon Perritt | 1980344 | 2014-10-28 12:11:10 -0500 | [diff] [blame] | 42 | u += q.String() |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 43 | createPage := func(r pagination.PageResult) pagination.Page { |
| 44 | return ServicePage{pagination.LinkedPageBase{PageResult: r}} |
Ash Wilson | ab6be61 | 2014-09-15 15:51:22 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Ash Wilson | cd95a0c | 2014-09-16 13:07:31 -0400 | [diff] [blame] | 47 | return pagination.NewPager(client, u, createPage) |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 48 | } |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 49 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 50 | // Get returns additional information about a service, given its ID. |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 51 | func Get(client *gophercloud.ServiceClient, serviceID string) GetResult { |
| 52 | var result GetResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 53 | _, result.Err = client.Request("GET", serviceURL(client, serviceID), gophercloud.RequestOpts{ |
| 54 | JSONResponse: &result.Body, |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 55 | }) |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 56 | return result |
Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 57 | } |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 58 | |
Ash Wilson | 81ab83a | 2014-10-02 11:06:23 -0400 | [diff] [blame] | 59 | // Update changes the service type of an existing service. |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 60 | func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult { |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 61 | type request struct { |
| 62 | Type string `json:"type"` |
| 63 | } |
| 64 | |
| 65 | req := request{Type: serviceType} |
| 66 | |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 67 | var result UpdateResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 68 | _, result.Err = client.Request("PATCH", serviceURL(client, serviceID), gophercloud.RequestOpts{ |
| 69 | JSONBody: &req, |
| 70 | JSONResponse: &result.Body, |
| 71 | OkCodes: []int{200}, |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 72 | }) |
Ash Wilson | 74e2bb8 | 2014-09-30 17:08:48 -0400 | [diff] [blame] | 73 | return result |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 74 | } |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 75 | |
| 76 | // Delete removes an existing service. |
| 77 | // 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] | 78 | func Delete(client *gophercloud.ServiceClient, serviceID string) DeleteResult { |
| 79 | var res DeleteResult |
Jamie Hannaford | c530ba1 | 2015-03-23 17:50:46 +0100 | [diff] [blame] | 80 | _, res.Err = client.Request("DELETE", serviceURL(client, serviceID), gophercloud.RequestOpts{}) |
Jamie Hannaford | 8ab3c14 | 2014-10-27 11:33:39 +0100 | [diff] [blame] | 81 | return res |
Ash Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 82 | } |