Implement endpoints.Create.
diff --git a/openstack/identity/v3/endpoints/requests.go b/openstack/identity/v3/endpoints/requests.go
index e3d0eb2..574ded3 100644
--- a/openstack/identity/v3/endpoints/requests.go
+++ b/openstack/identity/v3/endpoints/requests.go
@@ -2,8 +2,11 @@
import (
"errors"
+ "strconv"
+ "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/openstack/utils"
)
// Interface describes the availability of a specific service endpoint.
@@ -30,8 +33,65 @@
}
// Create inserts a new Endpoint into the service catalog.
+// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
func Create(client *gophercloud.ServiceClient, opts EndpointOpts) (*Endpoint, error) {
- return nil, errors.New("Not implemented")
+ // Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output.
+ type endpoint struct {
+ Interface string `json:"interface"`
+ Name string `json:"name"`
+ Region *string `json:"region,omitempty"`
+ URL string `json:"url"`
+ ServiceID string `json:"service_id"`
+ }
+
+ type request struct {
+ Endpoint endpoint `json:"endpoint"`
+ }
+
+ type response struct {
+ Endpoint Endpoint `json:"endpoint"`
+ }
+
+ // Ensure that EndpointOpts is fully populated.
+ if opts.Interface == "" {
+ return nil, ErrInterfaceRequired
+ }
+ if opts.Name == "" {
+ return nil, ErrNameRequired
+ }
+ if opts.URL == "" {
+ return nil, ErrURLRequired
+ }
+ if opts.ServiceID == "" {
+ return nil, ErrServiceIDRequired
+ }
+
+ // Populate the request body.
+ reqBody := request{
+ Endpoint: endpoint{
+ Interface: string(opts.Interface),
+ Name: opts.Name,
+ URL: opts.URL,
+ ServiceID: opts.ServiceID,
+ },
+ }
+
+ if opts.Region != "" {
+ reqBody.Endpoint.Region = &opts.Region
+ }
+
+ var respBody response
+ _, err := perigee.Request("POST", getListURL(client), perigee.Options{
+ MoreHeaders: client.Provider.AuthenticatedHeaders(),
+ ReqBody: &reqBody,
+ Results: &respBody,
+ OkCodes: []int{201},
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return &respBody.Endpoint, nil
}
// ListOpts allows finer control over the the endpoints returned by a List call.
diff --git a/openstack/identity/v3/endpoints/requests_test.go b/openstack/identity/v3/endpoints/requests_test.go
index 77a2292..7bd6fcf 100644
--- a/openstack/identity/v3/endpoints/requests_test.go
+++ b/openstack/identity/v3/endpoints/requests_test.go
@@ -38,6 +38,7 @@
}
`)
+ w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"endpoint": {
@@ -68,7 +69,7 @@
t.Fatalf("Unable to create an endpoint: %v", err)
}
- expected := Endpoint{
+ expected := &Endpoint{
ID: "12",
Interface: InterfacePublic,
Name: "the-endiest-of-points",