blob: a3534e54d77200f11c341bd9a10fe51662e9b426 [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -04006 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonb73b7f82014-08-29 15:38:06 -04007)
8
Ash Wilsond1b72132014-09-03 15:26:26 -04009type response struct {
Ash Wilson64f44152014-09-05 13:45:03 -040010 Service Service `json:"service"`
Ash Wilsond1b72132014-09-03 15:26:26 -040011}
12
Ash Wilsonb73b7f82014-08-29 15:38:06 -040013// Create adds a new service of the requested type to the catalog.
Ash Wilson74e2bb82014-09-30 17:08:48 -040014func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult {
Ash Wilsonb73b7f82014-08-29 15:38:06 -040015 type request struct {
16 Type string `json:"type"`
17 }
18
Ash Wilsonb73b7f82014-08-29 15:38:06 -040019 req := request{Type: serviceType}
Ash Wilsonb73b7f82014-08-29 15:38:06 -040020
Ash Wilson74e2bb82014-09-30 17:08:48 -040021 var result CreateResult
Ash Wilson4bf41a32015-02-12 15:52:44 -050022 _, result.Err = client.Request("POST", listURL(client), gophercloud.RequestOpts{
23 JSONBody: &req,
24 JSONResponse: &result.Body,
25 OkCodes: []int{201},
Ash Wilsonb73b7f82014-08-29 15:38:06 -040026 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040027 return result
Ash Wilsonb73b7f82014-08-29 15:38:06 -040028}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040029
30// ListOpts allows you to query the List method.
31type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050032 ServiceType string `q:"type"`
33 PerPage int `q:"perPage"`
34 Page int `q:"page"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040035}
36
37// List enumerates the services available to a specific user.
Ash Wilson3c8cc772014-09-16 11:40:49 -040038func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perritt19803442014-10-28 12:11:10 -050039 u := listURL(client)
40 q, err := gophercloud.BuildQueryString(opts)
41 if err != nil {
42 return pagination.Pager{Err: err}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040043 }
Jon Perritt19803442014-10-28 12:11:10 -050044 u += q.String()
Ash Wilsonb8b16f82014-10-20 10:19:49 -040045 createPage := func(r pagination.PageResult) pagination.Page {
46 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040047 }
48
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040049 return pagination.NewPager(client, u, createPage)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040050}
Ash Wilsonb1129972014-09-03 14:45:21 -040051
Ash Wilson5266e492014-09-09 15:44:30 -040052// Get returns additional information about a service, given its ID.
Ash Wilson74e2bb82014-09-30 17:08:48 -040053func Get(client *gophercloud.ServiceClient, serviceID string) GetResult {
54 var result GetResult
55 _, result.Err = perigee.Request("GET", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040056 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond3dc2542014-10-20 10:10:48 -040057 Results: &result.Body,
Ash Wilsonb1129972014-09-03 14:45:21 -040058 OkCodes: []int{200},
59 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040060 return result
Ash Wilsonb1129972014-09-03 14:45:21 -040061}
Ash Wilsond1b72132014-09-03 15:26:26 -040062
Ash Wilson81ab83a2014-10-02 11:06:23 -040063// Update changes the service type of an existing service.
Ash Wilson74e2bb82014-09-30 17:08:48 -040064func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult {
Ash Wilsond1b72132014-09-03 15:26:26 -040065 type request struct {
66 Type string `json:"type"`
67 }
68
69 req := request{Type: serviceType}
70
Ash Wilson74e2bb82014-09-30 17:08:48 -040071 var result UpdateResult
72 _, result.Err = perigee.Request("PATCH", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040073 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond1b72132014-09-03 15:26:26 -040074 ReqBody: &req,
Ash Wilsond3dc2542014-10-20 10:10:48 -040075 Results: &result.Body,
Ash Wilsond1b72132014-09-03 15:26:26 -040076 OkCodes: []int{200},
77 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040078 return result
Ash Wilsond1b72132014-09-03 15:26:26 -040079}
Ash Wilsond24786d2014-09-03 15:38:00 -040080
81// Delete removes an existing service.
82// It either deletes all associated endpoints, or fails until all endpoints are deleted.
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010083func Delete(client *gophercloud.ServiceClient, serviceID string) DeleteResult {
84 var res DeleteResult
85 _, res.Err = perigee.Request("DELETE", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040086 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond24786d2014-09-03 15:38:00 -040087 OkCodes: []int{204},
88 })
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010089 return res
Ash Wilsond24786d2014-09-03 15:38:00 -040090}