blob: e2611921ad95ff96b47f4f4b57dc6b3419420ecd [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 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 Wilson64f44152014-09-05 13:45:03 -040016func Create(client *gophercloud.ServiceClient, serviceType string) (*Service, error) {
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}
22 var resp response
23
24 _, err := perigee.Request("POST", getListURL(client), perigee.Options{
Ash Wilsona87ee062014-09-03 11:26:06 -040025 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Ash Wilsonb73b7f82014-08-29 15:38:06 -040026 ReqBody: &req,
27 Results: &resp,
28 OkCodes: []int{201},
29 })
30 if err != nil {
31 return nil, err
32 }
33
34 return &resp.Service, nil
35}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040036
37// ListOpts allows you to query the List method.
38type ListOpts struct {
39 ServiceType string
40 PerPage int
41 Page int
42}
43
44// List enumerates the services available to a specific user.
Ash Wilson64f44152014-09-05 13:45:03 -040045func List(client *gophercloud.ServiceClient, opts ListOpts) (*ServiceList, error) {
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040046 q := make(map[string]string)
47 if opts.ServiceType != "" {
48 q["type"] = opts.ServiceType
49 }
50 if opts.Page != 0 {
51 q["page"] = strconv.Itoa(opts.Page)
52 }
53 if opts.PerPage != 0 {
54 q["perPage"] = strconv.Itoa(opts.PerPage)
55 }
56 u := getListURL(client) + utils.BuildQuery(q)
57
Ash Wilson64f44152014-09-05 13:45:03 -040058 var resp ServiceList
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040059 _, err := perigee.Request("GET", u, perigee.Options{
60 MoreHeaders: client.Provider.AuthenticatedHeaders(),
61 Results: &resp,
62 OkCodes: []int{200},
63 })
64 if err != nil {
65 return nil, err
66 }
67
68 return &resp, nil
69}
Ash Wilsonb1129972014-09-03 14:45:21 -040070
Ash Wilson5266e492014-09-09 15:44:30 -040071// Get returns additional information about a service, given its ID.
72func Get(client *gophercloud.ServiceClient, serviceID string) (*Service, error) {
Ash Wilsonb1129972014-09-03 14:45:21 -040073 var resp response
74 _, err := perigee.Request("GET", getServiceURL(client, serviceID), perigee.Options{
75 MoreHeaders: client.Provider.AuthenticatedHeaders(),
76 Results: &resp,
77 OkCodes: []int{200},
78 })
79 if err != nil {
80 return nil, err
81 }
82 return &resp.Service, nil
83}
Ash Wilsond1b72132014-09-03 15:26:26 -040084
85// Update changes the service type of an existing service.s
Ash Wilson64f44152014-09-05 13:45:03 -040086func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (*Service, error) {
Ash Wilsond1b72132014-09-03 15:26:26 -040087 type request struct {
88 Type string `json:"type"`
89 }
90
91 req := request{Type: serviceType}
92
93 var resp response
94 _, err := perigee.Request("PATCH", getServiceURL(client, serviceID), perigee.Options{
95 MoreHeaders: client.Provider.AuthenticatedHeaders(),
96 ReqBody: &req,
97 Results: &resp,
98 OkCodes: []int{200},
99 })
100 if err != nil {
101 return nil, err
102 }
103
104 return &resp.Service, nil
105}
Ash Wilsond24786d2014-09-03 15:38:00 -0400106
107// Delete removes an existing service.
108// It either deletes all associated endpoints, or fails until all endpoints are deleted.
109func Delete(client *gophercloud.ServiceClient, serviceID string) error {
110 _, err := perigee.Request("DELETE", getServiceURL(client, serviceID), perigee.Options{
111 MoreHeaders: client.Provider.AuthenticatedHeaders(),
112 OkCodes: []int{204},
113 })
114 return err
115}