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