forked from fastly/go-fastly
/
director_backend.go
162 lines (132 loc) · 3.84 KB
/
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
153
154
155
156
157
158
159
160
161
162
package fastly
import (
"fmt"
"net/url"
"time"
)
// DirectorBackend is the relationship between a director and a backend in the
// Fastly API.
type DirectorBackend struct {
ServiceID string `mapstructure:"service_id"`
Version int `mapstructure:"version"`
Director string `mapstructure:"director_name"`
Backend string `mapstructure:"backend_name"`
CreatedAt *time.Time `mapstructure:"created_at"`
UpdatedAt *time.Time `mapstructure:"updated_at"`
DeletedAt *time.Time `mapstructure:"deleted_at"`
}
// CreateDirectorBackendInput is used as input to the CreateDirectorBackend
// function.
type CreateDirectorBackendInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version int
// Director is the name of the director (required).
Director string
// Backend is the name of the backend (required).
Backend string
}
// CreateDirectorBackend creates a new Fastly backend.
func (c *Client) CreateDirectorBackend(i *CreateDirectorBackendInput) (*DirectorBackend, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == 0 {
return nil, ErrMissingVersion
}
if i.Director == "" {
return nil, ErrMissingDirector
}
if i.Backend == "" {
return nil, ErrMissingBackend
}
path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
i.Service, i.Version, url.PathEscape(i.Director), url.PathEscape(i.Backend))
resp, err := c.PostForm(path, i, nil)
if err != nil {
return nil, err
}
var b *DirectorBackend
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
// GetDirectorBackendInput is used as input to the GetDirectorBackend function.
type GetDirectorBackendInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version int
// Director is the name of the director (required).
Director string
// Backend is the name of the backend (required).
Backend string
}
// GetDirectorBackend gets the backend configuration with the given parameters.
func (c *Client) GetDirectorBackend(i *GetDirectorBackendInput) (*DirectorBackend, error) {
if i.Service == "" {
return nil, ErrMissingService
}
if i.Version == 0 {
return nil, ErrMissingVersion
}
if i.Director == "" {
return nil, ErrMissingDirector
}
if i.Backend == "" {
return nil, ErrMissingBackend
}
path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
i.Service, i.Version, url.PathEscape(i.Director), url.PathEscape(i.Backend))
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}
var b *DirectorBackend
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
// DeleteDirectorBackendInput is the input parameter to DeleteDirectorBackend.
type DeleteDirectorBackendInput struct {
// Service is the ID of the service. Version is the specific configuration
// version. Both fields are required.
Service string
Version int
// Director is the name of the director (required).
Director string
// Backend is the name of the backend (required).
Backend string
}
// DeleteDirectorBackend deletes the given backend version.
func (c *Client) DeleteDirectorBackend(i *DeleteDirectorBackendInput) error {
if i.Service == "" {
return ErrMissingService
}
if i.Version == 0 {
return ErrMissingVersion
}
if i.Director == "" {
return ErrMissingDirector
}
if i.Backend == "" {
return ErrMissingBackend
}
path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
i.Service, i.Version, url.PathEscape(i.Director), url.PathEscape(i.Backend))
resp, err := c.Delete(path, nil)
if err != nil {
return err
}
var r *statusResp
if err := decodeJSON(&r, resp.Body); err != nil {
return err
}
if !r.Ok() {
return fmt.Errorf("Not Ok")
}
return nil
}