Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 1 | package services |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | |
| 6 | "github.com/racker/perigee" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/openstack/utils" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
| 12 | type response struct { |
| 13 | Service Service `json:"service"` |
| 14 | } |
| 15 | |
| 16 | // Create adds a new service of the requested type to the catalog. |
| 17 | func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult { |
| 18 | type request struct { |
| 19 | Type string `json:"type"` |
| 20 | } |
| 21 | |
| 22 | req := request{Type: serviceType} |
| 23 | |
| 24 | var result CreateResult |
| 25 | _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{ |
| 26 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 27 | ReqBody: &req, |
| 28 | Results: &result.Resp, |
| 29 | OkCodes: []int{201}, |
| 30 | }) |
| 31 | return result |
| 32 | } |
| 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. |
| 42 | func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
| 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 | } |
| 53 | u := listURL(client) + utils.BuildQuery(q) |
| 54 | |
| 55 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
| 56 | return ServicePage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
| 57 | } |
| 58 | |
| 59 | return pagination.NewPager(client, u, createPage) |
| 60 | } |
| 61 | |
| 62 | // Get returns additional information about a service, given its ID. |
| 63 | func Get(client *gophercloud.ServiceClient, serviceID string) GetResult { |
| 64 | var result GetResult |
| 65 | _, result.Err = perigee.Request("GET", serviceURL(client, serviceID), perigee.Options{ |
| 66 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 67 | Results: &result.Resp, |
| 68 | OkCodes: []int{200}, |
| 69 | }) |
| 70 | return result |
| 71 | } |
| 72 | |
| 73 | // Update changes the service type of an existing service. |
| 74 | func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult { |
| 75 | type request struct { |
| 76 | Type string `json:"type"` |
| 77 | } |
| 78 | |
| 79 | req := request{Type: serviceType} |
| 80 | |
| 81 | var result UpdateResult |
| 82 | _, result.Err = perigee.Request("PATCH", serviceURL(client, serviceID), perigee.Options{ |
| 83 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 84 | ReqBody: &req, |
| 85 | Results: &result.Resp, |
| 86 | OkCodes: []int{200}, |
| 87 | }) |
| 88 | return result |
| 89 | } |
| 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 { |
| 94 | _, err := perigee.Request("DELETE", serviceURL(client, serviceID), perigee.Options{ |
| 95 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 96 | OkCodes: []int{204}, |
| 97 | }) |
| 98 | return err |
| 99 | } |