| Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 1 | package services | 
 | 2 |  | 
 | 3 | import ( | 
| Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 4 | 	"net/http" | 
| Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 5 | 	"strconv" | 
 | 6 |  | 
| Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 7 | 	"github.com/racker/perigee" | 
 | 8 | 	"github.com/rackspace/gophercloud" | 
| Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 9 | 	"github.com/rackspace/gophercloud/openstack/utils" | 
| Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 10 | ) | 
 | 11 |  | 
| Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 12 | type response struct { | 
| Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 13 | 	Service Service `json:"service"` | 
| Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 14 | } | 
 | 15 |  | 
| Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 16 | // Create adds a new service of the requested type to the catalog. | 
| Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 17 | func Create(client *gophercloud.ServiceClient, serviceType string) (*Service, error) { | 
| Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 18 | 	type request struct { | 
 | 19 | 		Type string `json:"type"` | 
 | 20 | 	} | 
 | 21 |  | 
| Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 22 | 	req := request{Type: serviceType} | 
 | 23 | 	var resp response | 
 | 24 |  | 
 | 25 | 	_, err := perigee.Request("POST", getListURL(client), perigee.Options{ | 
| Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 26 | 		MoreHeaders: client.Provider.AuthenticatedHeaders(), | 
| Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 27 | 		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 Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 37 |  | 
 | 38 | // ListOpts allows you to query the List method. | 
 | 39 | type ListOpts struct { | 
 | 40 | 	ServiceType string | 
 | 41 | 	PerPage     int | 
 | 42 | 	Page        int | 
 | 43 | } | 
 | 44 |  | 
 | 45 | // List enumerates the services available to a specific user. | 
| Ash Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 46 | func List(client *gophercloud.ServiceClient, opts ListOpts) gophercloud.Pager { | 
| Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 47 | 	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 Wilson | bddac13 | 2014-09-12 14:20:16 -0400 | [diff] [blame] | 59 | 	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 Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 68 | 	}) | 
| Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 69 | } | 
| Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 70 |  | 
| Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 71 | // Get returns additional information about a service, given its ID. | 
 | 72 | func Get(client *gophercloud.ServiceClient, serviceID string) (*Service, error) { | 
| Ash Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 73 | 	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 Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 84 |  | 
 | 85 | // Update changes the service type of an existing service.s | 
| Ash Wilson | 64f4415 | 2014-09-05 13:45:03 -0400 | [diff] [blame] | 86 | func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (*Service, error) { | 
| Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame] | 87 | 	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 Wilson | d24786d | 2014-09-03 15:38:00 -0400 | [diff] [blame] | 106 |  | 
 | 107 | // Delete removes an existing service. | 
 | 108 | // It either deletes all associated endpoints, or fails until all endpoints are deleted. | 
 | 109 | func 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 | } |