blob: a93cfe7f4dee95ca3e24042b5980263e62b5db4b [file] [log] [blame]
Ash Wilsonb73b7f82014-08-29 15:38:06 -04001package services
2
3import (
Ash Wilsonb73b7f82014-08-29 15:38:06 -04004 "github.com/rackspace/gophercloud"
Ash Wilson3c8cc772014-09-16 11:40:49 -04005 "github.com/rackspace/gophercloud/pagination"
Ash Wilsonb73b7f82014-08-29 15:38:06 -04006)
7
Ash Wilsond1b72132014-09-03 15:26:26 -04008type response struct {
Ash Wilson64f44152014-09-05 13:45:03 -04009 Service Service `json:"service"`
Ash Wilsond1b72132014-09-03 15:26:26 -040010}
11
Ash Wilsonb73b7f82014-08-29 15:38:06 -040012// Create adds a new service of the requested type to the catalog.
Ash Wilson74e2bb82014-09-30 17:08:48 -040013func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult {
Ash Wilsonb73b7f82014-08-29 15:38:06 -040014 type request struct {
15 Type string `json:"type"`
16 }
17
Ash Wilsonb73b7f82014-08-29 15:38:06 -040018 req := request{Type: serviceType}
Ash Wilsonb73b7f82014-08-29 15:38:06 -040019
Ash Wilson74e2bb82014-09-30 17:08:48 -040020 var result CreateResult
Ash Wilson4bf41a32015-02-12 15:52:44 -050021 _, result.Err = client.Request("POST", listURL(client), gophercloud.RequestOpts{
22 JSONBody: &req,
23 JSONResponse: &result.Body,
Ash Wilsonb73b7f82014-08-29 15:38:06 -040024 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040025 return result
Ash Wilsonb73b7f82014-08-29 15:38:06 -040026}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040027
28// ListOpts allows you to query the List method.
29type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050030 ServiceType string `q:"type"`
31 PerPage int `q:"perPage"`
32 Page int `q:"page"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040033}
34
35// List enumerates the services available to a specific user.
Ash Wilson3c8cc772014-09-16 11:40:49 -040036func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perritt19803442014-10-28 12:11:10 -050037 u := listURL(client)
38 q, err := gophercloud.BuildQueryString(opts)
39 if err != nil {
40 return pagination.Pager{Err: err}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040041 }
Jon Perritt19803442014-10-28 12:11:10 -050042 u += q.String()
Ash Wilsonb8b16f82014-10-20 10:19:49 -040043 createPage := func(r pagination.PageResult) pagination.Page {
44 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040045 }
46
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040047 return pagination.NewPager(client, u, createPage)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040048}
Ash Wilsonb1129972014-09-03 14:45:21 -040049
Ash Wilson5266e492014-09-09 15:44:30 -040050// Get returns additional information about a service, given its ID.
Ash Wilson74e2bb82014-09-30 17:08:48 -040051func Get(client *gophercloud.ServiceClient, serviceID string) GetResult {
52 var result GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050053 _, result.Err = client.Request("GET", serviceURL(client, serviceID), gophercloud.RequestOpts{
54 JSONResponse: &result.Body,
Ash Wilsonb1129972014-09-03 14:45:21 -040055 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040056 return result
Ash Wilsonb1129972014-09-03 14:45:21 -040057}
Ash Wilsond1b72132014-09-03 15:26:26 -040058
Ash Wilson81ab83a2014-10-02 11:06:23 -040059// Update changes the service type of an existing service.
Ash Wilson74e2bb82014-09-30 17:08:48 -040060func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult {
Ash Wilsond1b72132014-09-03 15:26:26 -040061 type request struct {
62 Type string `json:"type"`
63 }
64
65 req := request{Type: serviceType}
66
Ash Wilson74e2bb82014-09-30 17:08:48 -040067 var result UpdateResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050068 _, result.Err = client.Request("PATCH", serviceURL(client, serviceID), gophercloud.RequestOpts{
69 JSONBody: &req,
70 JSONResponse: &result.Body,
71 OkCodes: []int{200},
Ash Wilsond1b72132014-09-03 15:26:26 -040072 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040073 return result
Ash Wilsond1b72132014-09-03 15:26:26 -040074}
Ash Wilsond24786d2014-09-03 15:38:00 -040075
76// Delete removes an existing service.
77// It either deletes all associated endpoints, or fails until all endpoints are deleted.
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010078func Delete(client *gophercloud.ServiceClient, serviceID string) DeleteResult {
79 var res DeleteResult
Jamie Hannafordc530ba12015-03-23 17:50:46 +010080 _, res.Err = client.Request("DELETE", serviceURL(client, serviceID), gophercloud.RequestOpts{})
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010081 return res
Ash Wilsond24786d2014-09-03 15:38:00 -040082}