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