Debian Codegraph @

fastly/bigquery.go

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