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 ( |
| 4 | "errors" |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 5 | "strconv" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 6 | |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 7 | "github.com/racker/perigee" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/openstack/utils" |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | // Interface describes the availability of a specific service endpoint. |
| 13 | type Interface string |
| 14 | |
| 15 | const ( |
| 16 | // InterfaceAdmin makes an endpoint only available to administrators. |
| 17 | InterfaceAdmin Interface = "admin" |
| 18 | |
| 19 | // InterfacePublic makes an endpoint available to everyone. |
| 20 | InterfacePublic Interface = "public" |
| 21 | |
| 22 | // InterfaceInternal makes an endpoint only available within the cluster. |
| 23 | InterfaceInternal Interface = "internal" |
| 24 | ) |
| 25 | |
| 26 | // EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint. |
| 27 | type EndpointOpts struct { |
| 28 | Interface Interface |
| 29 | Name string |
| 30 | Region string |
| 31 | URL string |
| 32 | ServiceID string |
| 33 | } |
| 34 | |
| 35 | // Create inserts a new Endpoint into the service catalog. |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 36 | // 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] | 37 | func Create(client *gophercloud.ServiceClient, opts EndpointOpts) (*Endpoint, error) { |
Ash Wilson | 989ce54 | 2014-09-04 10:52:49 -0400 | [diff] [blame] | 38 | // Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output. |
| 39 | type endpoint struct { |
| 40 | Interface string `json:"interface"` |
| 41 | Name string `json:"name"` |
| 42 | Region *string `json:"region,omitempty"` |
| 43 | URL string `json:"url"` |
| 44 | ServiceID string `json:"service_id"` |
| 45 | } |
| 46 | |
| 47 | type request struct { |
| 48 | Endpoint endpoint `json:"endpoint"` |
| 49 | } |
| 50 | |
| 51 | type response struct { |
| 52 | Endpoint Endpoint `json:"endpoint"` |
| 53 | } |
| 54 | |
| 55 | // Ensure that EndpointOpts is fully populated. |
| 56 | if opts.Interface == "" { |
| 57 | return nil, ErrInterfaceRequired |
| 58 | } |
| 59 | if opts.Name == "" { |
| 60 | return nil, ErrNameRequired |
| 61 | } |
| 62 | if opts.URL == "" { |
| 63 | return nil, ErrURLRequired |
| 64 | } |
| 65 | if opts.ServiceID == "" { |
| 66 | return nil, ErrServiceIDRequired |
| 67 | } |
| 68 | |
| 69 | // Populate the request body. |
| 70 | reqBody := request{ |
| 71 | Endpoint: endpoint{ |
| 72 | Interface: string(opts.Interface), |
| 73 | Name: opts.Name, |
| 74 | URL: opts.URL, |
| 75 | ServiceID: opts.ServiceID, |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | if opts.Region != "" { |
| 80 | reqBody.Endpoint.Region = &opts.Region |
| 81 | } |
| 82 | |
| 83 | var respBody response |
| 84 | _, err := perigee.Request("POST", getListURL(client), perigee.Options{ |
| 85 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 86 | ReqBody: &reqBody, |
| 87 | Results: &respBody, |
| 88 | OkCodes: []int{201}, |
| 89 | }) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | |
| 94 | return &respBody.Endpoint, nil |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // ListOpts allows finer control over the the endpoints returned by a List call. |
| 98 | // All fields are optional. |
| 99 | type ListOpts struct { |
| 100 | Interface Interface |
| 101 | ServiceID string |
| 102 | Page int |
| 103 | PerPage int |
| 104 | } |
| 105 | |
| 106 | // List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria. |
| 107 | func List(client *gophercloud.ServiceClient, opts ListOpts) (*EndpointList, error) { |
Ash Wilson | 32c0e8d | 2014-09-04 10:53:08 -0400 | [diff] [blame^] | 108 | q := make(map[string]string) |
| 109 | if opts.Interface != "" { |
| 110 | q["interface"] = string(opts.Interface) |
| 111 | } |
| 112 | if opts.ServiceID != "" { |
| 113 | q["service_id"] = opts.ServiceID |
| 114 | } |
| 115 | if opts.Page != 0 { |
| 116 | q["page"] = strconv.Itoa(opts.Page) |
| 117 | } |
| 118 | if opts.PerPage != 0 { |
| 119 | q["per_page"] = strconv.Itoa(opts.Page) |
| 120 | } |
| 121 | |
| 122 | u := getListURL(client) + utils.BuildQuery(q) |
| 123 | |
| 124 | var respBody []Endpoint |
| 125 | _, err := perigee.Request("GET", u, perigee.Options{ |
| 126 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 127 | Results: &respBody, |
| 128 | OkCodes: []int{200}, |
| 129 | }) |
| 130 | if err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | |
| 134 | return &EndpointList{Endpoints: respBody}, nil |
Ash Wilson | bdfc330 | 2014-09-04 10:16:28 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Update changes an existing endpoint with new data. |
| 138 | func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) (*Endpoint, error) { |
| 139 | return nil, errors.New("Not implemented") |
| 140 | } |
| 141 | |
| 142 | // Delete removes an endpoint from the service catalog. |
| 143 | func Delete(client *gophercloud.ServiceClient, endpointID string) error { |
| 144 | return errors.New("Not implemented") |
| 145 | } |