Debian Codegraph @

fastly/director_backend.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "time"
7 )
8
9 // DirectorBackend is the relationship between a director and a backend in the
10 // Fastly API.
11 type DirectorBackend struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Director string `mapstructure:"director_name"`
16 Backend string `mapstructure:"backend_name"`
17 CreatedAt *time.Time `mapstructure:"created_at"`
18 UpdatedAt *time.Time `mapstructure:"updated_at"`
19 DeletedAt *time.Time `mapstructure:"deleted_at"`
20 }
21
22 // CreateDirectorBackendInput is used as input to the CreateDirectorBackend
23 // function.
24 type CreateDirectorBackendInput struct {
25 // Service is the ID of the service. Version is the specific configuration
26 // version. Both fields are required.
27 Service string
28 Version int
29
30 // Director is the name of the director (required).
31 Director string
32
33 // Backend is the name of the backend (required).
34 Backend string
35 }
36
37 // CreateDirectorBackend creates a new Fastly backend.
38 func (c *Client) CreateDirectorBackend(i *CreateDirectorBackendInput) (*DirectorBackend, error) {
39 if i.Service == "" {
40 return nil, ErrMissingService
41 }
42
43 if i.Version == 0 {
44 return nil, ErrMissingVersion
45 }
46
47 if i.Director == "" {
48 return nil, ErrMissingDirector
49 }
50
51 if i.Backend == "" {
52 return nil, ErrMissingBackend
53 }
54
55 path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
56 i.Service, i.Version, url.PathEscape(i.Director), url.PathEscape(i.Backend))
57 resp, err := c.PostForm(path, i, nil)
58 if err != nil {
59 return nil, err
60 }
61
62 var b *DirectorBackend
63 if err := decodeJSON(&b, resp.Body); err != nil {
64 return nil, err
65 }
66 return b, nil
67 }
68
69 // GetDirectorBackendInput is used as input to the GetDirectorBackend function.
70 type GetDirectorBackendInput struct {
71 // Service is the ID of the service. Version is the specific configuration
72 // version. Both fields are required.
73 Service string
74 Version int
75
76 // Director is the name of the director (required).
77 Director string
78
79 // Backend is the name of the backend (required).
80 Backend string
81 }
82
83 // GetDirectorBackend gets the backend configuration with the given parameters.
84 func (c *Client) GetDirectorBackend(i *GetDirectorBackendInput) (*DirectorBackend, error) {
85 if i.Service == "" {
86 return nil, ErrMissingService
87 }
88
89 if i.Version == 0 {
90 return nil, ErrMissingVersion
91 }
92
93 if i.Director == "" {
94 return nil, ErrMissingDirector
95 }
96
97 if i.Backend == "" {
98 return nil, ErrMissingBackend
99 }
100
101 path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
102 i.Service, i.Version, url.PathEscape(i.Director), url.PathEscape(i.Backend))
103 resp, err := c.Get(path, nil)
104 if err != nil {
105 return nil, err
106 }
107
108 var b *DirectorBackend
109 if err := decodeJSON(&b, resp.Body); err != nil {
110 return nil, err
111 }
112 return b, nil
113 }
114
115 // DeleteDirectorBackendInput is the input parameter to DeleteDirectorBackend.
116 type DeleteDirectorBackendInput struct {
117 // Service is the ID of the service. Version is the specific configuration
118 // version. Both fields are required.
119 Service string
120 Version int
121
122 // Director is the name of the director (required).
123 Director string
124
125 // Backend is the name of the backend (required).
126 Backend string
127 }
128
129 // DeleteDirectorBackend deletes the given backend version.
130 func (c *Client) DeleteDirectorBackend(i *DeleteDirectorBackendInput) error {
131 if i.Service == "" {
132 return ErrMissingService
133 }
134
135 if i.Version == 0 {
136 return ErrMissingVersion
137 }
138
139 if i.Director == "" {
140 return ErrMissingDirector
141 }
142
143 if i.Backend == "" {
144 return ErrMissingBackend
145 }
146
147 path := fmt.Sprintf("/service/%s/version/%d/director/%s/backend/%s",
148 i.Service, i.Version, url.PathEscape(i.Director), url.PathEscape(i.Backend))
149 resp, err := c.Delete(path, nil)
150 if err != nil {
151 return err
152 }
153
154 var r *statusResp
155 if err := decodeJSON(&r, resp.Body); err != nil {
156 return err
157 }
158 if !r.Ok() {
159 return fmt.Errorf("Not Ok")
160 }
161 return nil
162 }