This repository has been archived by the owner on May 26, 2021. It is now read-only.
forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
123 lines (108 loc) · 3.85 KB
/
requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package endpoints
import (
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
// EndpointOpts contains the subset of Endpoint attributes that should be used to create or update an Endpoint.
type EndpointOpts struct {
Availability gophercloud.Availability
Name string
Region string
URL string
ServiceID string
}
// 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) CreateResult {
// 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"`
}
// Ensure that EndpointOpts is fully populated.
if opts.Availability == "" {
return createErr(ErrAvailabilityRequired)
}
if opts.Name == "" {
return createErr(ErrNameRequired)
}
if opts.URL == "" {
return createErr(ErrURLRequired)
}
if opts.ServiceID == "" {
return createErr(ErrServiceIDRequired)
}
// Populate the request body.
reqBody := request{
Endpoint: endpoint{
Interface: string(opts.Availability),
Name: opts.Name,
URL: opts.URL,
ServiceID: opts.ServiceID,
},
}
reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
var result CreateResult
_, result.Err = client.Post(listURL(client), reqBody, &result.Body, nil)
return result
}
// ListOpts allows finer control over the endpoints returned by a List call.
// All fields are optional.
type ListOpts struct {
Availability gophercloud.Availability `q:"interface"`
ServiceID string `q:"service_id"`
Page int `q:"page"`
PerPage int `q:"per_page"`
}
// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
u := listURL(client)
q, err := gophercloud.BuildQueryString(opts)
if err != nil {
return pagination.Pager{Err: err}
}
u += q.String()
createPage := func(r pagination.PageResult) pagination.Page {
return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
}
return pagination.NewPager(client, u, createPage)
}
// Update changes an existing endpoint with new data.
// All fields are optional in the provided EndpointOpts.
func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult {
type endpoint struct {
Interface *string `json:"interface,omitempty"`
Name *string `json:"name,omitempty"`
Region *string `json:"region,omitempty"`
URL *string `json:"url,omitempty"`
ServiceID *string `json:"service_id,omitempty"`
}
type request struct {
Endpoint endpoint `json:"endpoint"`
}
reqBody := request{Endpoint: endpoint{}}
reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability))
reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name)
reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL)
reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID)
var result UpdateResult
_, result.Err = client.Request("PATCH", endpointURL(client, endpointID), gophercloud.RequestOpts{
JSONBody: &reqBody,
JSONResponse: &result.Body,
OkCodes: []int{200},
})
return result
}
// Delete removes an endpoint from the service catalog.
func Delete(client *gophercloud.ServiceClient, endpointID string) DeleteResult {
var res DeleteResult
_, res.Err = client.Delete(endpointURL(client, endpointID), nil)
return res
}