/
load_balancer_director_backend.go
152 lines (135 loc) · 4.03 KB
/
load_balancer_director_backend.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package fastly
import (
"fmt"
"net/url"
"time"
)
// DirectorBackend is the relationship between a director and a backend in the
// Fastly API.
type DirectorBackend struct {
Backend *string `mapstructure:"backend_name"`
CreatedAt *time.Time `mapstructure:"created_at"`
DeletedAt *time.Time `mapstructure:"deleted_at"`
Director *string `mapstructure:"director_name"`
ServiceID *string `mapstructure:"service_id"`
ServiceVersion *int `mapstructure:"version"`
UpdatedAt *time.Time `mapstructure:"updated_at"`
}
// CreateDirectorBackendInput is used as input to the CreateDirectorBackend
// function.
type CreateDirectorBackendInput struct {
// Backend is the name of the backend (required).
Backend string
// Director is the name of the director (required).
Director string
// ServiceID is the ID of the service (required).
ServiceID string
// ServiceVersion is the specific configuration version (required).
ServiceVersion int
}
// CreateDirectorBackend creates a new resource.
func (c *Client) CreateDirectorBackend(i *CreateDirectorBackendInput) (*DirectorBackend, error) {
if i.Backend == "" {
return nil, ErrMissingBackend
}
if i.Director == "" {
return nil, ErrMissingDirector
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}
path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
i.ServiceID, i.ServiceVersion, url.PathEscape(i.Director), url.PathEscape(i.Backend))
resp, err := c.PostForm(path, i, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var b *DirectorBackend
if err := decodeBodyMap(resp.Body, &b); err != nil {
return nil, err
}
return b, nil
}
// GetDirectorBackendInput is used as input to the GetDirectorBackend function.
type GetDirectorBackendInput struct {
// Backend is the name of the backend (required).
Backend string
// Director is the name of the director (required).
Director string
// ServiceID is the ID of the service (required).
ServiceID string
// ServiceVersion is the specific configuration version (required).
ServiceVersion int
}
// GetDirectorBackend retrieves the specified resource.
func (c *Client) GetDirectorBackend(i *GetDirectorBackendInput) (*DirectorBackend, error) {
if i.Backend == "" {
return nil, ErrMissingBackend
}
if i.Director == "" {
return nil, ErrMissingDirector
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
}
path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
i.ServiceID, i.ServiceVersion, url.PathEscape(i.Director), url.PathEscape(i.Backend))
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var b *DirectorBackend
if err := decodeBodyMap(resp.Body, &b); err != nil {
return nil, err
}
return b, nil
}
// DeleteDirectorBackendInput is the input parameter to DeleteDirectorBackend.
type DeleteDirectorBackendInput struct {
// Backend is the name of the backend (required).
Backend string
// Director is the name of the director (required).
Director string
// ServiceID is the ID of the service (required).
ServiceID string
// ServiceVersion is the specific configuration version (required).
ServiceVersion int
}
// DeleteDirectorBackend deletes the specified resource.
func (c *Client) DeleteDirectorBackend(i *DeleteDirectorBackendInput) error {
if i.Backend == "" {
return ErrMissingBackend
}
if i.Director == "" {
return ErrMissingDirector
}
if i.ServiceID == "" {
return ErrMissingServiceID
}
if i.ServiceVersion == 0 {
return ErrMissingServiceVersion
}
path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
i.ServiceID, i.ServiceVersion, url.PathEscape(i.Director), url.PathEscape(i.Backend))
resp, err := c.Delete(path, nil)
if err != nil {
return err
}
defer resp.Body.Close()
var r *statusResp
if err := decodeBodyMap(resp.Body, &r); err != nil {
return err
}
if !r.Ok() {
return ErrNotOK
}
return nil
}