Ash Wilson | 0a997f8 | 2014-09-03 15:50:52 -0400 | [diff] [blame] | 1 | package endpoints |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 4 | "strconv" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 5 | |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 6 | "github.com/racker/perigee" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 10 | ) |
| 11 | |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 12 | // EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint. |
| 13 | type EndpointOpts struct { |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 14 | Availability gophercloud.Availability |
| 15 | Name string |
| 16 | Region string |
| 17 | URL string |
| 18 | ServiceID string |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | // Create inserts a new Endpoint into the service catalog. |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 22 | // Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required. |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 23 | func Create(client *gophercloud.ServiceClient, opts EndpointOpts) (*Endpoint, error) { |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 24 | // Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output. |
| 25 | type endpoint struct { |
| 26 | Interface string `json:"interface"` |
| 27 | Name string `json:"name"` |
| 28 | Region *string `json:"region,omitempty"` |
| 29 | URL string `json:"url"` |
| 30 | ServiceID string `json:"service_id"` |
| 31 | } |
| 32 | |
| 33 | type request struct { |
| 34 | Endpoint endpoint `json:"endpoint"` |
| 35 | } |
| 36 | |
| 37 | type response struct { |
Ash Wilson | b18fc10 | 2014-09-30 15:26:01 -0400 | [diff] [blame] | 38 | Endpoint `json:"endpoint"` |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Ensure that EndpointOpts is fully populated. |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 42 | if opts.Availability == "" { |
| 43 | return nil, ErrAvailabilityRequired |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 44 | } |
| 45 | if opts.Name == "" { |
| 46 | return nil, ErrNameRequired |
| 47 | } |
| 48 | if opts.URL == "" { |
| 49 | return nil, ErrURLRequired |
| 50 | } |
| 51 | if opts.ServiceID == "" { |
| 52 | return nil, ErrServiceIDRequired |
| 53 | } |
| 54 | |
| 55 | // Populate the request body. |
| 56 | reqBody := request{ |
| 57 | Endpoint: endpoint{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 58 | Interface: string(opts.Availability), |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 59 | Name: opts.Name, |
| 60 | URL: opts.URL, |
| 61 | ServiceID: opts.ServiceID, |
| 62 | }, |
| 63 | } |
Ash Wilson | b18fc10 | 2014-09-30 15:26:01 -0400 | [diff] [blame] | 64 | reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region) |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 65 | |
| 66 | var respBody response |
Ash Wilson | 1e1b733 | 2014-09-30 16:38:11 -0400 | [diff] [blame^] | 67 | _, err := perigee.Request("POST", listURL(client), perigee.Options{ |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 68 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 69 | ReqBody: &reqBody, |
| 70 | Results: &respBody, |
| 71 | OkCodes: []int{201}, |
| 72 | }) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | |
| 77 | return &respBody.Endpoint, nil |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // ListOpts allows finer control over the the endpoints returned by a List call. |
| 81 | // All fields are optional. |
| 82 | type ListOpts struct { |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 83 | Availability gophercloud.Availability |
| 84 | ServiceID string |
| 85 | Page int |
| 86 | PerPage int |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria. |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 90 | func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 91 | q := make(map[string]string) |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 92 | if opts.Availability != "" { |
| 93 | q["interface"] = string(opts.Availability) |
Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame] | 94 | } |
| 95 | if opts.ServiceID != "" { |
| 96 | q["service_id"] = opts.ServiceID |
| 97 | } |
| 98 | if opts.Page != 0 { |
| 99 | q["page"] = strconv.Itoa(opts.Page) |
| 100 | } |
| 101 | if opts.PerPage != 0 { |
| 102 | q["per_page"] = strconv.Itoa(opts.Page) |
| 103 | } |
| 104 | |
Ash Wilson | 3c8cc77 | 2014-09-16 11:40:49 -0400 | [diff] [blame] | 105 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | fc55c82 | 2014-09-25 13:18:16 -0400 | [diff] [blame] | 106 | return EndpointPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Ash Wilson | ab6be61 | 2014-09-15 15:51:22 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Ash Wilson | 1e1b733 | 2014-09-30 16:38:11 -0400 | [diff] [blame^] | 109 | u := listURL(client) + utils.BuildQuery(q) |
Ash Wilson | cd95a0c | 2014-09-16 13:07:31 -0400 | [diff] [blame] | 110 | return pagination.NewPager(client, u, createPage) |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Update changes an existing endpoint with new data. |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 114 | // All fields are optional in the provided EndpointOpts. |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 115 | func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) (*Endpoint, error) { |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 116 | type endpoint struct { |
| 117 | Interface *string `json:"interface,omitempty"` |
| 118 | Name *string `json:"name,omitempty"` |
| 119 | Region *string `json:"region,omitempty"` |
| 120 | URL *string `json:"url,omitempty"` |
| 121 | ServiceID *string `json:"service_id,omitempty"` |
| 122 | } |
| 123 | |
| 124 | type request struct { |
| 125 | Endpoint endpoint `json:"endpoint"` |
| 126 | } |
| 127 | |
| 128 | type response struct { |
| 129 | Endpoint Endpoint `json:"endpoint"` |
| 130 | } |
| 131 | |
| 132 | reqBody := request{Endpoint: endpoint{}} |
Ash Wilson | b18fc10 | 2014-09-30 15:26:01 -0400 | [diff] [blame] | 133 | reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability)) |
| 134 | reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name) |
| 135 | reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region) |
| 136 | reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL) |
| 137 | reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID) |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 138 | |
| 139 | var respBody response |
Ash Wilson | 1e1b733 | 2014-09-30 16:38:11 -0400 | [diff] [blame^] | 140 | _, err := perigee.Request("PATCH", endpointURL(client, endpointID), perigee.Options{ |
Ash Wilson | f04a74c | 2014-09-04 11:16:20 -0400 | [diff] [blame] | 141 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 142 | ReqBody: &reqBody, |
| 143 | Results: &respBody, |
| 144 | OkCodes: []int{200}, |
| 145 | }) |
| 146 | if err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | |
| 150 | return &respBody.Endpoint, nil |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Delete removes an endpoint from the service catalog. |
| 154 | func Delete(client *gophercloud.ServiceClient, endpointID string) error { |
Ash Wilson | 1e1b733 | 2014-09-30 16:38:11 -0400 | [diff] [blame^] | 155 | _, err := perigee.Request("DELETE", endpointURL(client, endpointID), perigee.Options{ |
Ash Wilson | 70db2ab | 2014-09-04 11:18:32 -0400 | [diff] [blame] | 156 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 157 | OkCodes: []int{204}, |
| 158 | }) |
| 159 | return err |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 160 | } |