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