Debian Codegraph @

client/controller.go

raw
1 // Copyright (C) LINBIT HA-Solutions GmbH
2 // All Rights Reserved.
3 // Author: Roland Kammerer <[email protected]>
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License"); you may
6 // not use this file except in compliance with the License. You may obtain
7 // a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 // License for the specific language governing permissions and limitations
15 // under the License.
16
17 package client
18
19 import (
20 "context"
21 "encoding/base64"
22 "encoding/json"
23 "fmt"
24 "io"
25 "net/url"
26 "strconv"
27 "time"
28 )
29
30 // copy & paste from generated code
31
32 // ControllerVersion represents version information of the LINSTOR controller
33 type ControllerVersion struct {
34 Version string `json:"version,omitempty"`
35 GitHash string `json:"git_hash,omitempty"`
36 BuildTime string `json:"build_time,omitempty"`
37 RestApiVersion string `json:"rest_api_version,omitempty"`
38 }
39
40 // ErrorReport struct for ErrorReport
41 type ErrorReport struct {
42 NodeName string `json:"node_name,omitempty"`
43 ErrorTime TimeStampMs `json:"error_time"`
44 // Filename of the error report on the server. Format is: ```ErrorReport-{instanceid}-{nodeid}-{sequencenumber}.log```
45 Filename string `json:"filename,omitempty"`
46 // Contains the full text of the error report file.
47 Text string `json:"text,omitempty"`
48 // Which module this error occurred.
49 Module string `json:"module,omitempty"`
50 // Linstor version this error report was created.
51 Version string `json:"version,omitempty"`
52 // Peer client that was involved.
53 Peer string `json:"peer,omitempty"`
54 // Exception that occurred
55 Exception string `json:"exception,omitempty"`
56 // Exception message
57 ExceptionMessage string `json:"exception_message,omitempty"`
58 // Origin file of the exception
59 OriginFile string `json:"origin_file,omitempty"`
60 // Origin method where the exception occurred
61 OriginMethod string `json:"origin_method,omitempty"`
62 // Origin line number
63 OriginLine int32 `json:"origin_line,omitempty"`
64 }
65
66 type ErrorReportDelete struct {
67 Since *TimeStampMs `json:"since,omitempty"`
68 To *TimeStampMs `json:"to,omitempty"`
69 // on which nodes to delete error-reports, if empty/null all nodes
70 Nodes []string `json:"nodes,omitempty"`
71 // delete all error reports with the given exception
72 Exception string `json:"exception,omitempty"`
73 // delete all error reports from the given version
74 Version string `json:"version,omitempty"`
75 // error report ids to delete
76 Ids []string `json:"ids,omitempty"`
77 }
78
79 type PropsInfo struct {
80 Info string `json:"info,omitempty"`
81 PropType string `json:"prop_type,omitempty"`
82 Value string `json:"value,omitempty"`
83 Dflt string `json:"dflt,omitempty"`
84 Unit string `json:"unit,omitempty"`
85 }
86
87 // ExternalFile is an external file which can be configured to be deployed by Linstor
88 type ExternalFile struct {
89 Path string
90 Content []byte
91 }
92
93 // externalFileBase64 is a golinstor-internal type which represents an external
94 // file as it is handled by the LINSTOR API. The API expects files to come in
95 // base64 encoding, and also returns files in base64 encoding. To make golinstor
96 // easier to use, we only present the ExternalFile type to our users an
97 // transparently handle the base64 encoding/decoding.
98 type externalFileBase64 struct {
99 Path string `json:"path,omitempty"`
100 ContentBase64 string `json:"content,omitempty"`
101 }
102
103 func (e *ExternalFile) UnmarshalJSON(text []byte) error {
104 v := externalFileBase64{}
105 err := json.Unmarshal(text, &v)
106 if err != nil {
107 return err
108 }
109 e.Path = v.Path
110 e.Content, err = base64.StdEncoding.DecodeString(v.ContentBase64)
111 if err != nil {
112 return err
113 }
114 return nil
115 }
116
117 func (e *ExternalFile) MarshalJSON() ([]byte, error) {
118 v := externalFileBase64{
119 Path: e.Path,
120 ContentBase64: base64.StdEncoding.EncodeToString(e.Content),
121 }
122 return json.Marshal(v)
123 }
124
125 // ExtFileCheckResult represents the result of checking if an external file can be written on a node.
126 type ExtFileCheckResult struct {
127 Allowed bool `json:"allowed"`
128 }
129
130 // custom code
131
132 // ControllerProvider acts as an abstraction for a ControllerService. It can be
133 // swapped out for another ControllerService implementation, for example for
134 // testing.
135 type ControllerProvider interface {
136 // GetVersion queries version information for the controller.
137 GetVersion(ctx context.Context, opts ...*ListOpts) (ControllerVersion, error)
138 // GetConfig queries the configuration of a controller
139 GetConfig(ctx context.Context, opts ...*ListOpts) (ControllerConfig, error)
140 // Modify modifies the controller node and sets/deletes the given properties.
141 Modify(ctx context.Context, props GenericPropsModify) error
142 // GetProps gets all properties of a controller
143 GetProps(ctx context.Context, opts ...*ListOpts) (ControllerProps, error)
144 // DeleteProp deletes the given property/key from the controller object.
145 DeleteProp(ctx context.Context, prop string) error
146 // GetErrorReports returns all error reports. The Text field is not populated,
147 // use GetErrorReport to get the text of an error report.
148 GetErrorReports(ctx context.Context, opts ...*ListOpts) ([]ErrorReport, error)
149 // DeleteErrorReports deletes error reports as specified by the ErrorReportDelete struct.
150 DeleteErrorReports(ctx context.Context, del ErrorReportDelete) error
151 // GetErrorReportsSince returns all error reports created after a certain point in time.
152 GetErrorReportsSince(ctx context.Context, since time.Time, opts ...*ListOpts) ([]ErrorReport, error)
153 // GetErrorReport returns a specific error report, including its text.
154 GetErrorReport(ctx context.Context, id string, opts ...*ListOpts) (ErrorReport, error)
155 // CreateSOSReport creates an SOS report in the log directory of the controller
156 CreateSOSReport(ctx context.Context, opts ...*ListOpts) error
157 // DownloadSOSReport creates and downloads an SOS report. The report is written to w as a tar.gz file.
158 DownloadSOSReport(ctx context.Context, w io.WriteCloser, opts ...*ListOpts) error
159 GetSatelliteConfig(ctx context.Context, node string) (SatelliteConfig, error)
160 ModifySatelliteConfig(ctx context.Context, node string, cfg SatelliteConfig) error
161 // GetPropsInfos gets meta information about the properties that can be
162 // set on a controller.
163 GetPropsInfos(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error)
164 // GetPropsInfosAll gets meta information about all properties that can
165 // be set on a controller and all entities it contains (nodes, resource
166 // definitions, ...).
167 GetPropsInfosAll(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error)
168 // GetExternalFiles gets a list of previously registered external files.
169 GetExternalFiles(ctx context.Context, opts ...*ListOpts) ([]ExternalFile, error)
170 // GetExternalFile gets the requested external file including its content
171 GetExternalFile(ctx context.Context, name string) (ExternalFile, error)
172 // ModifyExternalFile registers or modifies a previously registered
173 // external file
174 ModifyExternalFile(ctx context.Context, name string, file ExternalFile) error
175 // DeleteExternalFile deletes the given external file. This effectively
176 // also deletes the file on all satellites
177 DeleteExternalFile(ctx context.Context, name string) error
178 // CheckExternalFile checks whether an external file can be written on the given node.
179 CheckExternalFile(ctx context.Context, name string, node string) (ExtFileCheckResult, error)
180 }
181
182 // ControllerService is the service that deals with the LINSTOR controller.
183 type ControllerService struct {
184 client *Client
185 }
186
187 // GetVersion queries version information for the controller.
188 func (s *ControllerService) GetVersion(ctx context.Context, opts ...*ListOpts) (ControllerVersion, error) {
189 var vers ControllerVersion
190 _, err := s.client.doGET(ctx, "/v1/controller/version", &vers, opts...)
191 return vers, err
192 }
193
194 // GetConfig queries the configuration of a controller
195 func (s *ControllerService) GetConfig(ctx context.Context, opts ...*ListOpts) (ControllerConfig, error) {
196 var cfg ControllerConfig
197 _, err := s.client.doGET(ctx, "/v1/controller/config", &cfg, opts...)
198 return cfg, err
199 }
200
201 // Modify modifies the controller node and sets/deletes the given properties.
202 func (s *ControllerService) Modify(ctx context.Context, props GenericPropsModify) error {
203 _, err := s.client.doPOST(ctx, "/v1/controller/properties", props)
204 return err
205 }
206
207 type ControllerProps map[string]string
208
209 // GetProps gets all properties of a controller
210 func (s *ControllerService) GetProps(ctx context.Context, opts ...*ListOpts) (ControllerProps, error) {
211 var props ControllerProps
212 _, err := s.client.doGET(ctx, "/v1/controller/properties", &props, opts...)
213 return props, err
214 }
215
216 // DeleteProp deletes the given property/key from the controller object.
217 func (s *ControllerService) DeleteProp(ctx context.Context, prop string) error {
218 _, err := s.client.doDELETE(ctx, "/v1/controller/properties/"+prop, nil)
219 return err
220 }
221
222 // GetPropsInfos gets meta information about the properties that can be set on
223 // a controller.
224 func (s *ControllerService) GetPropsInfos(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error) {
225 var infos []PropsInfo
226 _, err := s.client.doGET(ctx, "/v1/controller/properties/info", &infos, opts...)
227 return infos, err
228 }
229
230 // GetPropsInfosAll gets meta information about all properties that can be set
231 // on a controller and all entities it contains (nodes, resource definitions, ...).
232 func (s *ControllerService) GetPropsInfosAll(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error) {
233 var infos []PropsInfo
234 _, err := s.client.doGET(ctx, "/v1/controller/properties/info/all", &infos, opts...)
235 return infos, err
236 }
237
238 // GetErrorReports returns all error reports. The Text field is not populated,
239 // use GetErrorReport to get the text of an error report.
240 func (s *ControllerService) GetErrorReports(ctx context.Context, opts ...*ListOpts) ([]ErrorReport, error) {
241 var reports []ErrorReport
242 _, err := s.client.doGET(ctx, "/v1/error-reports", &reports, opts...)
243 return reports, err
244 }
245
246 // DeleteErrorReports deletes error reports as specified by the ErrorReportDelete struct.
247 func (s *ControllerService) DeleteErrorReports(ctx context.Context, del ErrorReportDelete) error {
248 // Yes, this is using PATCH. don't ask me why, its just implemented that way...
249 _, err := s.client.doPATCH(ctx, "/v1/error-reports", del)
250 return err
251 }
252
253 // unixMilli returns t formatted as milliseconds since Unix epoch
254 func unixMilli(t time.Time) int64 {
255 return t.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
256 }
257
258 // GetErrorReportsSince returns all error reports created after a certain point in time.
259 func (s *ControllerService) GetErrorReportsSince(ctx context.Context, since time.Time, opts ...*ListOpts) ([]ErrorReport, error) {
260 var reports []ErrorReport
261 v := url.Values{}
262 v.Set("since", strconv.FormatInt(unixMilli(since), 10))
263 _, err := s.client.doGET(ctx, "/v1/error-reports/?"+v.Encode(), &reports, opts...)
264 return reports, err
265 }
266
267 // GetErrorReport returns a specific error report, including its text.
268 func (s *ControllerService) GetErrorReport(ctx context.Context, id string, opts ...*ListOpts) (ErrorReport, error) {
269 var report []ErrorReport
270 _, err := s.client.doGET(ctx, "/v1/error-reports/"+id, &report, opts...)
271 return report[0], err
272 }
273
274 // CreateSOSReport creates an SOS report in the log directory of the controller
275 func (s *ControllerService) CreateSOSReport(ctx context.Context, opts ...*ListOpts) error {
276 _, err := s.client.doGET(ctx, "/v1/sos-report", nil, opts...)
277 return err
278 }
279
280 // DownloadSOSReport creates and downloads an SOS report. The report is written to w as a tar.gz file.
281 func (s *ControllerService) DownloadSOSReport(ctx context.Context, w io.WriteCloser, opts ...*ListOpts) error {
282 opt, err := Optional(opts...)
283 if err != nil {
284 return err
285 }
286
287 reqUrl := "/v1/sos-report/download"
288 u, err := addOptions(reqUrl, opt)
289 if err != nil {
290 return err
291 }
292
293 req, err := s.client.newRequest("GET", u, nil)
294 if err != nil {
295 return err
296 }
297 req.Header.Set("Accept", "application/octet-stream")
298 resp, err := s.client.do(ctx, req)
299 if err != nil {
300 return err
301 }
302 defer resp.Body.Close() //nolint:errcheck
303 _, err = io.Copy(w, resp.Body)
304 return err
305 }
306
307 func (s *ControllerService) GetSatelliteConfig(ctx context.Context, node string) (SatelliteConfig, error) {
308 var cfg SatelliteConfig
309 _, err := s.client.doGET(ctx, "/v1/nodes/"+node+"/config", &cfg)
310 return cfg, err
311 }
312
313 func (s *ControllerService) ModifySatelliteConfig(ctx context.Context, node string, cfg SatelliteConfig) error {
314 _, err := s.client.doPUT(ctx, "/v1/nodes/"+node+"/config", &cfg)
315 return err
316 }
317
318 // GetExternalFiles get a list of previously registered external files.
319 // File contents are not included, unless ListOpts.Content is true.
320 func (s *ControllerService) GetExternalFiles(ctx context.Context, opts ...*ListOpts) ([]ExternalFile, error) {
321 var files []ExternalFile
322 _, err := s.client.doGET(ctx, "/v1/files", &files, opts...)
323 return files, err
324 }
325
326 // GetExternalFile gets the requested external file including its content
327 func (s *ControllerService) GetExternalFile(ctx context.Context, name string) (ExternalFile, error) {
328 file := ExternalFile{}
329 _, err := s.client.doGET(ctx, "/v1/files/"+url.QueryEscape(name), &file)
330 if err != nil {
331 return ExternalFile{}, fmt.Errorf("request failed: %w", err)
332 }
333 return file, nil
334 }
335
336 // ModifyExternalFile registers or modifies a previously registered external
337 // file
338 func (s *ControllerService) ModifyExternalFile(ctx context.Context, name string, file ExternalFile) error {
339 b64file := externalFileBase64{
340 Path: file.Path,
341 ContentBase64: base64.StdEncoding.EncodeToString(file.Content),
342 }
343 _, err := s.client.doPUT(ctx, "/v1/files/"+url.QueryEscape(name), b64file)
344 return err
345 }
346
347 // DeleteExternalFile deletes the given external file. This effectively also
348 // deletes the file on all satellites
349 func (s *ControllerService) DeleteExternalFile(ctx context.Context, name string) error {
350 _, err := s.client.doDELETE(ctx, "/v1/files/"+url.QueryEscape(name), nil)
351 return err
352 }
353
354 // CheckExternalFile checks whether an external file can be written on the given node.
355 func (s *ControllerService) CheckExternalFile(ctx context.Context, name string, node string) (ExtFileCheckResult, error) {
356 var result ExtFileCheckResult
357 _, err := s.client.doGET(ctx, "/v1/files/"+url.QueryEscape(name)+"/check/"+node, &result)
358 return result, err
359 }