blob: a3eefea188603b875777f0893a641e41a45ca342 [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
Ash Wilson2f5dd1f2014-09-03 14:01:37 -04004 "strconv"
5
Ash Wilsonb73b7f82014-08-29 15:38:06 -04006 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
Ash Wilson2f5dd1f2014-09-03 14:01:37 -04008 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilson3c8cc772014-09-16 11:40:49 -04009 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonb73b7f82014-08-29 15:38:06 -040010)
11
Ash Wilsond1b72132014-09-03 15:26:26 -040012type response struct {
Ash Wilson64f44152014-09-05 13:45:03 -040013 Service Service `json:"service"`
Ash Wilsond1b72132014-09-03 15:26:26 -040014}
15
Ash Wilsonb73b7f82014-08-29 15:38:06 -040016// Create adds a new service of the requested type to the catalog.
Ash Wilson74e2bb82014-09-30 17:08:48 -040017func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult {
Ash Wilsonb73b7f82014-08-29 15:38:06 -040018 type request struct {
19 Type string `json:"type"`
20 }
21
Ash Wilsonb73b7f82014-08-29 15:38:06 -040022 req := request{Type: serviceType}
Ash Wilsonb73b7f82014-08-29 15:38:06 -040023
Ash Wilson74e2bb82014-09-30 17:08:48 -040024 var result CreateResult
25 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilsona87ee062014-09-03 11:26:06 -040026 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Ash Wilsonb73b7f82014-08-29 15:38:06 -040027 ReqBody: &req,
Ash Wilsond3dc2542014-10-20 10:10:48 -040028 Results: &result.Body,
Ash Wilsonb73b7f82014-08-29 15:38:06 -040029 OkCodes: []int{201},
30 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040031 return result
Ash Wilsonb73b7f82014-08-29 15:38:06 -040032}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040033
34// ListOpts allows you to query the List method.
35type ListOpts struct {
36 ServiceType string
37 PerPage int
38 Page int
39}
40
41// List enumerates the services available to a specific user.
Ash Wilson3c8cc772014-09-16 11:40:49 -040042func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040043 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 Wilson1e1b7332014-09-30 16:38:11 -040053 u := listURL(client) + utils.BuildQuery(q)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040054
Ash Wilsonb8b16f82014-10-20 10:19:49 -040055 createPage := func(r pagination.PageResult) pagination.Page {
56 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040057 }
58
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040059 return pagination.NewPager(client, u, createPage)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040060}
Ash Wilsonb1129972014-09-03 14:45:21 -040061
Ash Wilson5266e492014-09-09 15:44:30 -040062// Get returns additional information about a service, given its ID.
Ash Wilson74e2bb82014-09-30 17:08:48 -040063func Get(client *gophercloud.ServiceClient, serviceID string) GetResult {
64 var result GetResult
65 _, result.Err = perigee.Request("GET", serviceURL(client, serviceID), perigee.Options{
Ash Wilsonb1129972014-09-03 14:45:21 -040066 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Ash Wilsond3dc2542014-10-20 10:10:48 -040067 Results: &result.Body,
Ash Wilsonb1129972014-09-03 14:45:21 -040068 OkCodes: []int{200},
69 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040070 return result
Ash Wilsonb1129972014-09-03 14:45:21 -040071}
Ash Wilsond1b72132014-09-03 15:26:26 -040072
Ash Wilson81ab83a2014-10-02 11:06:23 -040073// Update changes the service type of an existing service.
Ash Wilson74e2bb82014-09-30 17:08:48 -040074func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult {
Ash Wilsond1b72132014-09-03 15:26:26 -040075 type request struct {
76 Type string `json:"type"`
77 }
78
79 req := request{Type: serviceType}
80
Ash Wilson74e2bb82014-09-30 17:08:48 -040081 var result UpdateResult
82 _, result.Err = perigee.Request("PATCH", serviceURL(client, serviceID), perigee.Options{
Ash Wilsond1b72132014-09-03 15:26:26 -040083 MoreHeaders: client.Provider.AuthenticatedHeaders(),
84 ReqBody: &req,
Ash Wilsond3dc2542014-10-20 10:10:48 -040085 Results: &result.Body,
Ash Wilsond1b72132014-09-03 15:26:26 -040086 OkCodes: []int{200},
87 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040088 return result
Ash Wilsond1b72132014-09-03 15:26:26 -040089}
Ash Wilsond24786d2014-09-03 15:38:00 -040090
91// Delete removes an existing service.
92// It either deletes all associated endpoints, or fails until all endpoints are deleted.
93func Delete(client *gophercloud.ServiceClient, serviceID string) error {
Ash Wilson1e1b7332014-09-30 16:38:11 -040094 _, err := perigee.Request("DELETE", serviceURL(client, serviceID), perigee.Options{
Ash Wilsond24786d2014-09-03 15:38:00 -040095 MoreHeaders: client.Provider.AuthenticatedHeaders(),
96 OkCodes: []int{204},
97 })
98 return err
99}