blob: fbd6a7c15fcd1bb9a47f88e59e5b17e91cb3e3c0 [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 Wilson64f44152014-09-05 13:45:03 -040017func Create(client *gophercloud.ServiceClient, serviceType string) (*Service, error) {
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}
23 var resp response
24
25 _, err := perigee.Request("POST", getListURL(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,
28 Results: &resp,
29 OkCodes: []int{201},
30 })
31 if err != nil {
32 return nil, err
33 }
34
35 return &resp.Service, nil
36}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040037
38// ListOpts allows you to query the List method.
39type ListOpts struct {
40 ServiceType string
41 PerPage int
42 Page int
43}
44
45// List enumerates the services available to a specific user.
Ash Wilson3c8cc772014-09-16 11:40:49 -040046func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040047 q := make(map[string]string)
48 if opts.ServiceType != "" {
49 q["type"] = opts.ServiceType
50 }
51 if opts.Page != 0 {
52 q["page"] = strconv.Itoa(opts.Page)
53 }
54 if opts.PerPage != 0 {
55 q["perPage"] = strconv.Itoa(opts.PerPage)
56 }
57 u := getListURL(client) + utils.BuildQuery(q)
58
Ash Wilson3c8cc772014-09-16 11:40:49 -040059 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
60 return ServicePage{pagination.LinkedPageBase(r)}
Ash Wilsonab6be612014-09-15 15:51:22 -040061 }
62
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040063 return pagination.NewPager(client, u, createPage)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040064}
Ash Wilsonb1129972014-09-03 14:45:21 -040065
Ash Wilson5266e492014-09-09 15:44:30 -040066// Get returns additional information about a service, given its ID.
67func Get(client *gophercloud.ServiceClient, serviceID string) (*Service, error) {
Ash Wilsonb1129972014-09-03 14:45:21 -040068 var resp response
69 _, err := perigee.Request("GET", getServiceURL(client, serviceID), perigee.Options{
70 MoreHeaders: client.Provider.AuthenticatedHeaders(),
71 Results: &resp,
72 OkCodes: []int{200},
73 })
74 if err != nil {
75 return nil, err
76 }
77 return &resp.Service, nil
78}
Ash Wilsond1b72132014-09-03 15:26:26 -040079
80// Update changes the service type of an existing service.s
Ash Wilson64f44152014-09-05 13:45:03 -040081func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (*Service, error) {
Ash Wilsond1b72132014-09-03 15:26:26 -040082 type request struct {
83 Type string `json:"type"`
84 }
85
86 req := request{Type: serviceType}
87
88 var resp response
89 _, err := perigee.Request("PATCH", getServiceURL(client, serviceID), perigee.Options{
90 MoreHeaders: client.Provider.AuthenticatedHeaders(),
91 ReqBody: &req,
92 Results: &resp,
93 OkCodes: []int{200},
94 })
95 if err != nil {
96 return nil, err
97 }
98
99 return &resp.Service, nil
100}
Ash Wilsond24786d2014-09-03 15:38:00 -0400101
102// Delete removes an existing service.
103// It either deletes all associated endpoints, or fails until all endpoints are deleted.
104func Delete(client *gophercloud.ServiceClient, serviceID string) error {
105 _, err := perigee.Request("DELETE", getServiceURL(client, serviceID), perigee.Options{
106 MoreHeaders: client.Provider.AuthenticatedHeaders(),
107 OkCodes: []int{204},
108 })
109 return err
110}