blob: 425a67c49d6c80a5cd334e265247d796e42acedc [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 Wilson3c8cc772014-09-16 11:40:49 -04008 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonb73b7f82014-08-29 15:38:06 -04009)
10
Ash Wilsond1b72132014-09-03 15:26:26 -040011type response struct {
Ash Wilson64f44152014-09-05 13:45:03 -040012 Service Service `json:"service"`
Ash Wilsond1b72132014-09-03 15:26:26 -040013}
14
Ash Wilsonb73b7f82014-08-29 15:38:06 -040015// Create adds a new service of the requested type to the catalog.
Ash Wilson74e2bb82014-09-30 17:08:48 -040016func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult {
Ash Wilsonb73b7f82014-08-29 15:38:06 -040017 type request struct {
18 Type string `json:"type"`
19 }
20
Ash Wilsonb73b7f82014-08-29 15:38:06 -040021 req := request{Type: serviceType}
Ash Wilsonb73b7f82014-08-29 15:38:06 -040022
Ash Wilson74e2bb82014-09-30 17:08:48 -040023 var result CreateResult
24 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040025 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsonb73b7f82014-08-29 15:38:06 -040026 ReqBody: &req,
Ash Wilsond3dc2542014-10-20 10:10:48 -040027 Results: &result.Body,
Ash Wilsonb73b7f82014-08-29 15:38:06 -040028 OkCodes: []int{201},
29 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040030 return result
Ash Wilsonb73b7f82014-08-29 15:38:06 -040031}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040032
33// ListOpts allows you to query the List method.
34type ListOpts struct {
35 ServiceType string
36 PerPage int
37 Page int
38}
39
40// List enumerates the services available to a specific user.
Ash Wilson3c8cc772014-09-16 11:40:49 -040041func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040042 q := make(map[string]string)
43 if opts.ServiceType != "" {
44 q["type"] = opts.ServiceType
45 }
46 if opts.Page != 0 {
47 q["page"] = strconv.Itoa(opts.Page)
48 }
49 if opts.PerPage != 0 {
50 q["perPage"] = strconv.Itoa(opts.PerPage)
51 }
Jamie Hannaford53da3162014-10-22 17:03:12 +020052 u := listURL(client) + gophercloud.BuildQuery(q)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040053
Ash Wilsonb8b16f82014-10-20 10:19:49 -040054 createPage := func(r pagination.PageResult) pagination.Page {
55 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040056 }
57
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040058 return pagination.NewPager(client, u, createPage)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040059}
Ash Wilsonb1129972014-09-03 14:45:21 -040060
Ash Wilson5266e492014-09-09 15:44:30 -040061// Get returns additional information about a service, given its ID.
Ash Wilson74e2bb82014-09-30 17:08:48 -040062func Get(client *gophercloud.ServiceClient, serviceID string) GetResult {
63 var result GetResult
64 _, result.Err = perigee.Request("GET", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040065 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond3dc2542014-10-20 10:10:48 -040066 Results: &result.Body,
Ash Wilsonb1129972014-09-03 14:45:21 -040067 OkCodes: []int{200},
68 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040069 return result
Ash Wilsonb1129972014-09-03 14:45:21 -040070}
Ash Wilsond1b72132014-09-03 15:26:26 -040071
Ash Wilson81ab83a2014-10-02 11:06:23 -040072// Update changes the service type of an existing service.
Ash Wilson74e2bb82014-09-30 17:08:48 -040073func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult {
Ash Wilsond1b72132014-09-03 15:26:26 -040074 type request struct {
75 Type string `json:"type"`
76 }
77
78 req := request{Type: serviceType}
79
Ash Wilson74e2bb82014-09-30 17:08:48 -040080 var result UpdateResult
81 _, result.Err = perigee.Request("PATCH", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040082 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond1b72132014-09-03 15:26:26 -040083 ReqBody: &req,
Ash Wilsond3dc2542014-10-20 10:10:48 -040084 Results: &result.Body,
Ash Wilsond1b72132014-09-03 15:26:26 -040085 OkCodes: []int{200},
86 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040087 return result
Ash Wilsond1b72132014-09-03 15:26:26 -040088}
Ash Wilsond24786d2014-09-03 15:38:00 -040089
90// Delete removes an existing service.
91// It either deletes all associated endpoints, or fails until all endpoints are deleted.
92func Delete(client *gophercloud.ServiceClient, serviceID string) error {
Ash Wilson1e1b7332014-09-30 16:38:11 -040093 _, err := perigee.Request("DELETE", serviceURL(client, serviceID), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040094 MoreHeaders: client.AuthenticatedHeaders(),
Ash Wilsond24786d2014-09-03 15:38:00 -040095 OkCodes: []int{204},
96 })
97 return err
98}