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