blob: 3ee924f3ee69ddd5533019994da67116b4e14c2f [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
Jamie Hannaford562a7d52015-03-24 16:20:16 +010021 _, result.Err = client.Post(listURL(client), req, &result.Body, nil)
Ash Wilson74e2bb82014-09-30 17:08:48 -040022 return result
Ash Wilsonb73b7f82014-08-29 15:38:06 -040023}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040024
25// ListOpts allows you to query the List method.
26type ListOpts struct {
Jon Perritt19803442014-10-28 12:11:10 -050027 ServiceType string `q:"type"`
28 PerPage int `q:"perPage"`
29 Page int `q:"page"`
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040030}
31
32// List enumerates the services available to a specific user.
Ash Wilson3c8cc772014-09-16 11:40:49 -040033func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perritt19803442014-10-28 12:11:10 -050034 u := listURL(client)
35 q, err := gophercloud.BuildQueryString(opts)
36 if err != nil {
37 return pagination.Pager{Err: err}
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040038 }
Jon Perritt19803442014-10-28 12:11:10 -050039 u += q.String()
Ash Wilsonb8b16f82014-10-20 10:19:49 -040040 createPage := func(r pagination.PageResult) pagination.Page {
41 return ServicePage{pagination.LinkedPageBase{PageResult: r}}
Ash Wilsonab6be612014-09-15 15:51:22 -040042 }
43
Ash Wilsoncd95a0c2014-09-16 13:07:31 -040044 return pagination.NewPager(client, u, createPage)
Ash Wilson2f5dd1f2014-09-03 14:01:37 -040045}
Ash Wilsonb1129972014-09-03 14:45:21 -040046
Ash Wilson5266e492014-09-09 15:44:30 -040047// Get returns additional information about a service, given its ID.
Ash Wilson74e2bb82014-09-30 17:08:48 -040048func Get(client *gophercloud.ServiceClient, serviceID string) GetResult {
49 var result GetResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +010050 _, result.Err = client.Get(serviceURL(client, serviceID), &result.Body, nil)
Ash Wilson74e2bb82014-09-30 17:08:48 -040051 return result
Ash Wilsonb1129972014-09-03 14:45:21 -040052}
Ash Wilsond1b72132014-09-03 15:26:26 -040053
Ash Wilson81ab83a2014-10-02 11:06:23 -040054// Update changes the service type of an existing service.
Ash Wilson74e2bb82014-09-30 17:08:48 -040055func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) UpdateResult {
Ash Wilsond1b72132014-09-03 15:26:26 -040056 type request struct {
57 Type string `json:"type"`
58 }
59
60 req := request{Type: serviceType}
61
Ash Wilson74e2bb82014-09-30 17:08:48 -040062 var result UpdateResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050063 _, result.Err = client.Request("PATCH", serviceURL(client, serviceID), gophercloud.RequestOpts{
64 JSONBody: &req,
65 JSONResponse: &result.Body,
66 OkCodes: []int{200},
Ash Wilsond1b72132014-09-03 15:26:26 -040067 })
Ash Wilson74e2bb82014-09-30 17:08:48 -040068 return result
Ash Wilsond1b72132014-09-03 15:26:26 -040069}
Ash Wilsond24786d2014-09-03 15:38:00 -040070
71// Delete removes an existing service.
72// It either deletes all associated endpoints, or fails until all endpoints are deleted.
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010073func Delete(client *gophercloud.ServiceClient, serviceID string) DeleteResult {
74 var res DeleteResult
Jamie Hannaford562a7d52015-03-24 16:20:16 +010075 _, res.Err = client.Delete(serviceURL(client, serviceID), nil)
Jamie Hannaford8ab3c142014-10-27 11:33:39 +010076 return res
Ash Wilsond24786d2014-09-03 15:38:00 -040077}