Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 1 | package services |
| 2 | |
| 3 | import ( |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 4 | "strconv" |
| 5 | |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 6 | "github.com/racker/perigee" |
| 7 | "github.com/rackspace/gophercloud" |
Ash Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 9 | ) |
| 10 | |
Ash Wilson | d1b7213 | 2014-09-03 15:26:26 -0400 | [diff] [blame^] | 11 | type response struct { |
| 12 | Service ServiceResult `json:"service"` |
| 13 | } |
| 14 | |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 15 | // Create adds a new service of the requested type to the catalog. |
| 16 | func Create(client *gophercloud.ServiceClient, serviceType string) (*ServiceResult, error) { |
| 17 | type request struct { |
| 18 | Type string `json:"type"` |
| 19 | } |
| 20 | |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 21 | req := request{Type: serviceType} |
| 22 | var resp response |
| 23 | |
| 24 | _, err := perigee.Request("POST", getListURL(client), perigee.Options{ |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 25 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Ash Wilson | b73b7f8 | 2014-08-29 15:38:06 -0400 | [diff] [blame] | 26 | 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 Wilson | 2f5dd1f | 2014-09-03 14:01:37 -0400 | [diff] [blame] | 36 | |
| 37 | // ListOpts allows you to query the List method. |
| 38 | type ListOpts struct { |
| 39 | ServiceType string |
| 40 | PerPage int |
| 41 | Page int |
| 42 | } |
| 43 | |
| 44 | // List enumerates the services available to a specific user. |
| 45 | func 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 Wilson | b112997 | 2014-09-03 14:45:21 -0400 | [diff] [blame] | 70 | |
| 71 | // Info returns additional information about a service, given its ID. |
| 72 | func Info(client *gophercloud.ServiceClient, serviceID string) (*ServiceResult, 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 |
| 86 | func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (*ServiceResult, error) { |
| 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 | } |