blob: 1d9aaa873a279284fae24005a265d000c4cdbb34 [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
22 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040023 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsonb73b7f82014-08-29 15:38:06 -040024 ReqBody: &req,
Ash Wilsond3dc2542014-10-20 10:10:48 -040025 Results: &result.Body,
Ash Wilsonb73b7f82014-08-29 15:38:06 -040026 OkCodes: []int{201},
27 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040028 return result
Ash Wilsonb73b7f82014-08-29 15:38:06 -040029}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040030
31// ListOpts allows you to query the List method.
32type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050033 ServiceType string `q:"type"`
34 PerPage int `q:"perPage"`
35 Page int `q:"page"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040036}
37
38// List enumerates the services available to a specific user.
Ash Wilson3c8cc772014-09-16 11:40:49 -040039func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perritt19803442014-10-28 12:11:10 -050040 u := listURL(client)
41 q, err := gophercloud.BuildQueryString(opts)
42 if err != nil {
43 return pagination.Pager{Err: err}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040044 }
Jon Perritt19803442014-10-28 12:11:10 -050045 u += q.String()
Ash Wilsonb8b16f82014-10-20 10:19:49 -040046 createPage := func(r pagination.PageResult) pagination.Page {
47 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040048 }
49
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040050 return pagination.NewPager(client, u, createPage)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040051}
Ash Wilsonb1129972014-09-03 14:45:21 -040052
Ash Wilson5266e492014-09-09 15:44:30 -040053// Get returns additional information about a service, given its ID.
Ash Wilson74e2bb82014-09-30 17:08:48 -040054func Get(client *gophercloud.ServiceClient, serviceID string) GetResult {
55 var result GetResult
56 _, result.Err = perigee.Request("GET", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040057 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond3dc2542014-10-20 10:10:48 -040058 Results: &result.Body,
Ash Wilsonb1129972014-09-03 14:45:21 -040059 OkCodes: []int{200},
60 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040061 return result
Ash Wilsonb1129972014-09-03 14:45:21 -040062}
Ash Wilsond1b72132014-09-03 15:26:26 -040063
Ash Wilson81ab83a2014-10-02 11:06:23 -040064// Update changes the service type of an existing service.
Ash Wilson74e2bb82014-09-30 17:08:48 -040065func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult {
Ash Wilsond1b72132014-09-03 15:26:26 -040066 type request struct {
67 Type string `json:"type"`
68 }
69
70 req := request{Type: serviceType}
71
Ash Wilson74e2bb82014-09-30 17:08:48 -040072 var result UpdateResult
73 _, result.Err = perigee.Request("PATCH", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040074 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond1b72132014-09-03 15:26:26 -040075 ReqBody: &req,
Ash Wilsond3dc2542014-10-20 10:10:48 -040076 Results: &result.Body,
Ash Wilsond1b72132014-09-03 15:26:26 -040077 OkCodes: []int{200},
78 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040079 return result
Ash Wilsond1b72132014-09-03 15:26:26 -040080}
Ash Wilsond24786d2014-09-03 15:38:00 -040081
82// Delete removes an existing service.
83// It either deletes all associated endpoints, or fails until all endpoints are deleted.
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010084func Delete(client *gophercloud.ServiceClient, serviceID string) DeleteResult {
85 var res DeleteResult
86 _, res.Err = perigee.Request("DELETE", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040087 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond24786d2014-09-03 15:38:00 -040088 OkCodes: []int{204},
89 })
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010090 return res
Ash Wilsond24786d2014-09-03 15:38:00 -040091}