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