blob: f6cb22dfd707918ecbb009afd606b91d33836fdd [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
Ash Wilsonbddac132014-09-12 14:20:16 -04004 "net/http"
Ash Wilson2f5dd1f2014-09-03 14:01:37 -04005 "strconv"
6
Ash Wilsonb73b7f82014-08-29 15:38:06 -04007 "github.com/racker/perigee"
8 "github.com/rackspace/gophercloud"
Ash Wilson2f5dd1f2014-09-03 14:01:37 -04009 "github.com/rackspace/gophercloud/openstack/utils"
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 Wilsonbddac132014-09-12 14:20:16 -040046func List(client *gophercloud.ServiceClient, opts ListOpts) gophercloud.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 Wilsonbddac132014-09-12 14:20:16 -040059 return gophercloud.NewLinkedPager(u, func(next string) (http.Response, error) {
60 resp, err := perigee.Request("GET", u, perigee.Options{
61 MoreHeaders: client.Provider.AuthenticatedHeaders(),
62 OkCodes: []int{200},
63 })
64 if err != nil {
65 return http.Response{}, err
66 }
67 return resp.HttpResponse, nil
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040068 })
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040069}
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}