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