Debian Codegraph @

fastly/sumologic.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "net/url"
6 "sort"
7 "time"
8 )
9
10 // Sumologic represents a sumologic response from the Fastly API.
11 type Sumologic struct {
12 ServiceID string `mapstructure:"service_id"`
13 Version int `mapstructure:"version"`
14
15 Name string `mapstructure:"name"`
16 Address string `mapstructure:"address"`
17 URL string `mapstructure:"url"`
18 Format string `mapstructure:"format"`
19 ResponseCondition string `mapstructure:"response_condition"`
20 MessageType string `mapstructure:"message_type"`
21 FormatVersion int `mapstructure:"format_version"`
22 CreatedAt *time.Time `mapstructure:"created_at"`
23 UpdatedAt *time.Time `mapstructure:"updated_at"`
24 DeletedAt *time.Time `mapstructure:"deleted_at"`
25 Placement string `mapstructure:"placement"`
26 }
27
28 // sumologicsByName is a sortable list of sumologics.
29 type sumologicsByName []*Sumologic
30
31 // Len, Swap, and Less implement the sortable interface.
32 func (s sumologicsByName) Len() int { return len(s) }
33 func (s sumologicsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
34 func (s sumologicsByName) Less(i, j int) bool {
35 return s[i].Name < s[j].Name
36 }
37
38 // ListSumologicsInput is used as input to the ListSumologics function.
39 type ListSumologicsInput struct {
40 // Service is the ID of the service (required).
41 Service string
42
43 // Version is the specific configuration version (required).
44 Version int
45 }
46
47 // ListSumologics returns the list of sumologics for the configuration version.
48 func (c *Client) ListSumologics(i *ListSumologicsInput) ([]*Sumologic, error) {
49 if i.Service == "" {
50 return nil, ErrMissingService
51 }
52
53 if i.Version == 0 {
54 return nil, ErrMissingVersion
55 }
56
57 path := fmt.Sprintf("/service/%s/version/%d/logging/sumologic", i.Service, i.Version)
58 resp, err := c.Get(path, nil)
59 if err != nil {
60 return nil, err
61 }
62
63 var ss []*Sumologic
64 if err := decodeJSON(&ss, resp.Body); err != nil {
65 return nil, err
66 }
67 sort.Stable(sumologicsByName(ss))
68 return ss, nil
69 }
70
71 // CreateSumologicInput is used as input to the CreateSumologic function.
72 type CreateSumologicInput struct {
73 // Service is the ID of the service. Version is the specific configuration
74 // version. Both fields are required.
75 Service string
76 Version int
77
78 Name string `form:"name,omitempty"`
79 Address string `form:"address,omitempty"`
80 URL string `form:"url,omitempty"`
81 Format string `form:"format,omitempty"`
82 ResponseCondition string `form:"response_condition,omitempty"`
83 MessageType string `form:"message_type,omitempty"`
84 FormatVersion int `form:"format_version,omitempty"`
85 Placement string `form:"placement,omitempty"`
86 }
87
88 // CreateSumologic creates a new Fastly sumologic.
89 func (c *Client) CreateSumologic(i *CreateSumologicInput) (*Sumologic, error) {
90 if i.Service == "" {
91 return nil, ErrMissingService
92 }
93
94 if i.Version == 0 {
95 return nil, ErrMissingVersion
96 }
97
98 path := fmt.Sprintf("/service/%s/version/%d/logging/sumologic", i.Service, i.Version)
99 resp, err := c.PostForm(path, i, nil)
100 if err != nil {
101 return nil, err
102 }
103
104 var s *Sumologic
105 if err := decodeJSON(&s, resp.Body); err != nil {
106 return nil, err
107 }
108 return s, nil
109 }
110
111 // GetSumologicInput is used as input to the GetSumologic function.
112 type GetSumologicInput struct {
113 // Service is the ID of the service. Version is the specific configuration
114 // version. Both fields are required.
115 Service string
116 Version int
117
118 // Name is the name of the sumologic to fetch.
119 Name string
120 }
121
122 // GetSumologic gets the sumologic configuration with the given parameters.
123 func (c *Client) GetSumologic(i *GetSumologicInput) (*Sumologic, error) {
124 if i.Service == "" {
125 return nil, ErrMissingService
126 }
127
128 if i.Version == 0 {
129 return nil, ErrMissingVersion
130 }
131
132 if i.Name == "" {
133 return nil, ErrMissingName
134 }
135
136 path := fmt.Sprintf("/service/%s/version/%d/logging/sumologic/%s", i.Service, i.Version, url.PathEscape(i.Name))
137 resp, err := c.Get(path, nil)
138 if err != nil {
139 return nil, err
140 }
141
142 var s *Sumologic
143 if err := decodeJSON(&s, resp.Body); err != nil {
144 return nil, err
145 }
146 return s, nil
147 }
148
149 // UpdateSumologicInput is used as input to the UpdateSumologic function.
150 type UpdateSumologicInput struct {
151 // Service is the ID of the service. Version is the specific configuration
152 // version. Both fields are required.
153 Service string
154 Version int
155
156 // Name is the name of the sumologic to update.
157 Name string
158
159 NewName string `form:"name,omitempty"`
160 Address string `form:"address,omitempty"`
161 URL string `form:"url,omitempty"`
162 Format string `form:"format,omitempty"`
163 ResponseCondition string `form:"response_condition,omitempty"`
164 MessageType string `form:"message_type,omitempty"`
165 FormatVersion int `form:"format_version,omitempty"`
166 Placement string `form:"placement,omitempty"`
167 }
168
169 // UpdateSumologic updates a specific sumologic.
170 func (c *Client) UpdateSumologic(i *UpdateSumologicInput) (*Sumologic, error) {
171 if i.Service == "" {
172 return nil, ErrMissingService
173 }
174
175 if i.Version == 0 {
176 return nil, ErrMissingVersion
177 }
178
179 if i.Name == "" {
180 return nil, ErrMissingName
181 }
182
183 path := fmt.Sprintf("/service/%s/version/%d/logging/sumologic/%s", i.Service, i.Version, url.PathEscape(i.Name))
184 resp, err := c.PutForm(path, i, nil)
185 if err != nil {
186 return nil, err
187 }
188
189 var s *Sumologic
190 if err := decodeJSON(&s, resp.Body); err != nil {
191 return nil, err
192 }
193 return s, nil
194 }
195
196 // DeleteSumologicInput is the input parameter to DeleteSumologic.
197 type DeleteSumologicInput struct {
198 // Service is the ID of the service. Version is the specific configuration
199 // version. Both fields are required.
200 Service string
201 Version int
202
203 // Name is the name of the sumologic to delete (required).
204 Name string
205 }
206
207 // DeleteSumologic deletes the given sumologic version.
208 func (c *Client) DeleteSumologic(i *DeleteSumologicInput) error {
209 if i.Service == "" {
210 return ErrMissingService
211 }
212
213 if i.Version == 0 {
214 return ErrMissingVersion
215 }
216
217 if i.Name == "" {
218 return ErrMissingName
219 }
220
221 path := fmt.Sprintf("/service/%s/version/%d/logging/sumologic/%s", i.Service, i.Version, url.PathEscape(i.Name))
222 resp, err := c.Delete(path, nil)
223 if err != nil {
224 return err
225 }
226
227 var r *statusResp
228 if err := decodeJSON(&r, resp.Body); err != nil {
229 return err
230 }
231 if !r.Ok() {
232 return fmt.Errorf("Not Ok")
233 }
234 return nil
235 }