blob: c6820c700b2c1a7c3d4b77fdc2a9a9345b9ae097 [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,
24 OkCodes: []int{201},
Ash Wilsonb73b7f82014-08-29 15:38:06 -040025 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040026 return result
Ash Wilsonb73b7f82014-08-29 15:38:06 -040027}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040028
29// ListOpts allows you to query the List method.
30type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050031 ServiceType string `q:"type"`
32 PerPage int `q:"perPage"`
33 Page int `q:"page"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040034}
35
36// List enumerates the services available to a specific user.
Ash Wilson3c8cc772014-09-16 11:40:49 -040037func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perritt19803442014-10-28 12:11:10 -050038 u := listURL(client)
39 q, err := gophercloud.BuildQueryString(opts)
40 if err != nil {
41 return pagination.Pager{Err: err}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040042 }
Jon Perritt19803442014-10-28 12:11:10 -050043 u += q.String()
Ash Wilsonb8b16f82014-10-20 10:19:49 -040044 createPage := func(r pagination.PageResult) pagination.Page {
45 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040046 }
47
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040048 return pagination.NewPager(client, u, createPage)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040049}
Ash Wilsonb1129972014-09-03 14:45:21 -040050
Ash Wilson5266e492014-09-09 15:44:30 -040051// Get returns additional information about a service, given its ID.
Ash Wilson74e2bb82014-09-30 17:08:48 -040052func Get(client *gophercloud.ServiceClient, serviceID string) GetResult {
53 var result GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050054 _, result.Err = client.Request("GET", serviceURL(client, serviceID), gophercloud.RequestOpts{
55 JSONResponse: &result.Body,
56 OkCodes: []int{200},
Ash Wilsonb1129972014-09-03 14:45:21 -040057 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040058 return result
Ash Wilsonb1129972014-09-03 14:45:21 -040059}
Ash Wilsond1b72132014-09-03 15:26:26 -040060
Ash Wilson81ab83a2014-10-02 11:06:23 -040061// Update changes the service type of an existing service.
Ash Wilson74e2bb82014-09-30 17:08:48 -040062func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult {
Ash Wilsond1b72132014-09-03 15:26:26 -040063 type request struct {
64 Type string `json:"type"`
65 }
66
67 req := request{Type: serviceType}
68
Ash Wilson74e2bb82014-09-30 17:08:48 -040069 var result UpdateResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050070 _, result.Err = client.Request("PATCH", serviceURL(client, serviceID), gophercloud.RequestOpts{
71 JSONBody: &req,
72 JSONResponse: &result.Body,
73 OkCodes: []int{200},
Ash Wilsond1b72132014-09-03 15:26:26 -040074 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040075 return result
Ash Wilsond1b72132014-09-03 15:26:26 -040076}
Ash Wilsond24786d2014-09-03 15:38:00 -040077
78// Delete removes an existing service.
79// It either deletes all associated endpoints, or fails until all endpoints are deleted.
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010080func Delete(client *gophercloud.ServiceClient, serviceID string) DeleteResult {
81 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050082 _, res.Err = client.Request("DELETE", serviceURL(client, serviceID), gophercloud.RequestOpts{
83 OkCodes: []int{204},
Ash Wilsond24786d2014-09-03 15:38:00 -040084 })
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010085 return res
Ash Wilsond24786d2014-09-03 15:38:00 -040086}