blob: 7b1a2501e439fdd5d9d6f8f78ccedea3a032bb05 [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
11// Create adds a new service of the requested type to the catalog.
12func Create(client *gophercloud.ServiceClient, serviceType string) (*ServiceResult, error) {
13 type request struct {
14 Type string `json:"type"`
15 }
16
17 type response struct {
18 Service ServiceResult `json:"service"`
19 }
20
21 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.
45func List(client *gophercloud.ServiceClient, opts ListOpts) (*ServiceListResult, error) {
46 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
58 var resp ServiceListResult
59 _, 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
71// Info returns additional information about a service, given its ID.
72func Info(client *gophercloud.ServiceClient, serviceID string) (*ServiceResult, error) {
73 type response struct {
74 Service ServiceResult `json:"service"`
75 }
76
77 var resp response
78 _, err := perigee.Request("GET", getServiceURL(client, serviceID), perigee.Options{
79 MoreHeaders: client.Provider.AuthenticatedHeaders(),
80 Results: &resp,
81 OkCodes: []int{200},
82 })
83 if err != nil {
84 return nil, err
85 }
86 return &resp.Service, nil
87}