Debian Codegraph @

client/resourcedefinition.go

raw
1 // A REST client to interact with LINSTOR's REST API
2 // Copyright (C) LINBIT HA-Solutions GmbH
3 // All Rights Reserved.
4 // Author: Roland Kammerer <[email protected]>
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License"); you may
7 // not use this file except in compliance with the License. You may obtain
8 // a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 // License for the specific language governing permissions and limitations
16 // under the License.
17
18 package client
19
20 import (
21 "context"
22 "encoding/json"
23 "fmt"
24 "net/url"
25 "strconv"
26
27 "github.com/google/go-querystring/query"
28
29 "github.com/LINBIT/golinstor/clonestatus"
30 "github.com/LINBIT/golinstor/devicelayerkind"
31 )
32
33 // ResourceDefinitionService is a struct for the client pointer
34 type ResourceDefinitionService struct {
35 client *Client
36 }
37
38 // ResourceDefinition is a struct to store the information about a resource-definition
39 type ResourceDefinition struct {
40 Name string `json:"name,omitempty"`
41 // External name can be used to have native resource names. If you need to store a non Linstor compatible resource name use this field and Linstor will generate a compatible name.
42 ExternalName string `json:"external_name,omitempty"`
43 // A string to string property map.
44 Props map[string]string `json:"props,omitempty"`
45 Flags []string `json:"flags,omitempty"`
46 LayerData []ResourceDefinitionLayer `json:"layer_data,omitempty"`
47 // unique object id
48 Uuid string `json:"uuid,omitempty"`
49 // name of the linked resource group, if there is a link
50 ResourceGroupName string `json:"resource_group_name,omitempty"`
51 }
52
53 // ResourceDefinitionCreate is a struct for holding the data needed to create a resource-defintion
54 type ResourceDefinitionCreate struct {
55 // drbd port for resources
56 DrbdPort int32 `json:"drbd_port,omitempty"`
57 // drbd resource secret
58 DrbdSecret string `json:"drbd_secret,omitempty"`
59 DrbdTransportType string `json:"drbd_transport_type,omitempty"`
60 ResourceDefinition ResourceDefinition `json:"resource_definition"`
61 }
62
63 // ResourceDefinitionLayer is a struct for the storing the layertype of a resource-defintion
64 type ResourceDefinitionLayer struct {
65 Type devicelayerkind.DeviceLayerKind `json:"type,omitempty"`
66 Data *DrbdResourceDefinitionLayer `json:"data,omitempty"`
67 }
68
69 // DrbdResourceDefinitionLayer is a struct which contains the information about the layertype of a resource-definition on drbd level
70 type DrbdResourceDefinitionLayer struct {
71 ResourceNameSuffix string `json:"resource_name_suffix,omitempty"`
72 PeerSlots int32 `json:"peer_slots,omitempty"`
73 AlStripes int64 `json:"al_stripes,omitempty"`
74 // used drbd port for this resource
75 Port int32 `json:"port,omitempty"`
76 TransportType string `json:"transport_type,omitempty"`
77 // drbd resource secret
78 Secret string `json:"secret,omitempty"`
79 Down bool `json:"down,omitempty"`
80 }
81
82 // VolumeDefinitionCreate is a struct used for creating volume-definitions
83 type VolumeDefinitionCreate struct {
84 VolumeDefinition VolumeDefinition `json:"volume_definition"`
85 DrbdMinorNumber int32 `json:"drbd_minor_number,omitempty"`
86 }
87
88 // VolumeDefinition is a struct which is used to store volume-definition properties
89 type VolumeDefinition struct {
90 VolumeNumber *int32 `json:"volume_number,omitempty"`
91 // Size of the volume in Kibi.
92 SizeKib uint64 `json:"size_kib"`
93 // A string to string property map.
94 Props map[string]string `json:"props,omitempty"`
95 Flags []string `json:"flags,omitempty"`
96 LayerData []VolumeDefinitionLayer `json:"layer_data,omitempty"`
97 // unique object id
98 Uuid string `json:"uuid,omitempty"`
99 }
100
101 type VolumeDefinitionModify struct {
102 SizeKib uint64 `json:"size_kib,omitempty"`
103 GenericPropsModify
104 // To add a flag just specify the flag name, to remove a flag prepend it with a '-'. Flags: * GROSS_SIZE
105 Flags []string `json:"flags,omitempty"`
106 }
107
108 // VolumeDefinitionLayer is a struct for the layer-type of a volume-definition
109 type VolumeDefinitionLayer struct {
110 Type devicelayerkind.DeviceLayerKind `json:"type"`
111 Data OneOfDrbdVolumeDefinition `json:"data,omitempty"`
112 }
113
114 // DrbdVolumeDefinition is a struct containing volume-definition on drbd level
115 type DrbdVolumeDefinition struct {
116 ResourceNameSuffix string `json:"resource_name_suffix,omitempty"`
117 VolumeNumber int32 `json:"volume_number,omitempty"`
118 MinorNumber int32 `json:"minor_number,omitempty"`
119 }
120
121 type ResourceDefinitionCloneRequest struct {
122 Name string `json:"name,omitempty"`
123 ExternalName string `json:"external_name,omitempty"`
124 UseZfsClone bool `json:"use_zfs_clone,omitempty"`
125 LayerList []devicelayerkind.DeviceLayerKind `json:"layer_list,omitempty"`
126 VolumePassphrases []string `json:"volume_passphrases,omitempty"`
127 ResourceGroup string `json:"resource_group,omitempty"`
128 GenericPropsModify `json:",inline"`
129 }
130
131 type ResourceDefinitionCloneStarted struct {
132 // Path for clone status
133 Location string `json:"location"`
134 // name of the source resource
135 SourceName string `json:"source_name"`
136 // name of the clone resource
137 CloneName string `json:"clone_name"`
138 Messages *[]ApiCallRc `json:"messages,omitempty"`
139 }
140
141 type ResourceDefinitionCloneStatus struct {
142 Status clonestatus.CloneStatus `json:"status"`
143 }
144
145 type ResourceDefinitionSyncStatus struct {
146 SyncedOnAll bool `json:"synced_on_all"`
147 }
148
149 type ResourceDefinitionTruncateOpts struct {
150 // DeleteEmptyResourceDefinition also deletes the resource-definition when it has
151 // neither resources nor snapshots left after the truncate.
152 DeleteEmptyResourceDefinition bool `url:"delete_empty_resource_definition"`
153 }
154
155 // custom code
156
157 type ResourceDefinitionWithVolumeDefinition struct {
158 ResourceDefinition
159 VolumeDefinitions []VolumeDefinition `json:"volume_definitions,omitempty"`
160 }
161
162 type RDGetAllRequest struct {
163 // ResourceDefinitions filters the returned resource definitions by the given names
164 ResourceDefinitions []string `url:"resource_definitions,omitempty"`
165 // Props filters the returned resource definitions on their property values (uses key=value syntax)
166 Props []string `url:"props,omitempty"`
167 Offset int `url:"offset,omitempty"`
168 Limit int `url:"offset,omitempty"`
169 // WithVolumeDefinitions, if set to true, LINSTOR will also include volume definitions in the response.
170 WithVolumeDefinitions bool `url:"with_volume_definitions,omitempty"`
171 }
172
173 // ResourceDefinitionProvider acts as an abstraction for a
174 // ResourceDefinitionService. It can be swapped out for another
175 // ResourceDefinitionService implementation, for example for testing.
176 type ResourceDefinitionProvider interface {
177 // GetAll lists all resource-definitions
178 GetAll(ctx context.Context, request RDGetAllRequest) ([]ResourceDefinitionWithVolumeDefinition, error)
179 // Get return information about a resource-defintion
180 Get(ctx context.Context, resDefName string, opts ...*ListOpts) (ResourceDefinition, error)
181 // Create adds a new resource-definition
182 Create(ctx context.Context, resDef ResourceDefinitionCreate) error
183 // Modify allows to modify a resource-definition
184 Modify(ctx context.Context, resDefName string, props GenericPropsModify) error
185 // Delete completely deletes a resource-definition
186 Delete(ctx context.Context, resDefName string) error
187 // Truncate deletes all resources of the resource-definition without deleting
188 // the resource-definition itself or any of its snapshots.
189 Truncate(ctx context.Context, resDefName string, opts ...*ResourceDefinitionTruncateOpts) error
190 // GetVolumeDefinitions returns all volume-definitions of a resource-definition
191 GetVolumeDefinitions(ctx context.Context, resDefName string, opts ...*ListOpts) ([]VolumeDefinition, error)
192 // GetVolumeDefinition shows the properties of a specific volume-definition
193 GetVolumeDefinition(ctx context.Context, resDefName string, volNr int, opts ...*ListOpts) (VolumeDefinition, error)
194 // CreateVolumeDefinition adds a volume-definition to a resource-definition. Only the size is required.
195 CreateVolumeDefinition(ctx context.Context, resDefName string, volDef VolumeDefinitionCreate) error
196 // ModifyVolumeDefinition give the abilty to modify a specific volume-definition
197 ModifyVolumeDefinition(ctx context.Context, resDefName string, volNr int, props VolumeDefinitionModify) error
198 // DeleteVolumeDefinition deletes a specific volume-definition
199 DeleteVolumeDefinition(ctx context.Context, resDefName string, volNr int) error
200 // GetPropsInfos gets meta information about the properties that can be
201 // set on a resource definition.
202 GetPropsInfos(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error)
203 // GetDRBDProxyPropsInfos gets meta information about the properties
204 // that can be set on a resource definition for drbd proxy.
205 GetDRBDProxyPropsInfos(ctx context.Context, resDefName string, opts ...*ListOpts) ([]PropsInfo, error)
206 // AttachExternalFile adds an external file to the resource definition. This
207 // means that the file will be deployed to every node the resource is deployed on.
208 AttachExternalFile(ctx context.Context, resDefName string, filePath string) error
209 // DetachExternalFile removes a binding between an external file and a resource definition.
210 // This means that the file will no longer be deployed on every node the resource
211 // is deployed on.
212 DetachExternalFile(ctx context.Context, resDefName string, filePath string) error
213 // Clone starts cloning a resource definition and all resources using a method optimized for the storage driver.
214 Clone(ctx context.Context, srcResDef string, request ResourceDefinitionCloneRequest) (ResourceDefinitionCloneStarted, error)
215 // CloneStatus fetches the current status of a clone operation started by Clone.
216 CloneStatus(ctx context.Context, srcResDef, targetResDef string) (ResourceDefinitionCloneStatus, error)
217 // SyncStatus checks if a resource is currently in sync on all nodes
218 SyncStatus(ctx context.Context, resDef string) (ResourceDefinitionSyncStatus, error)
219 }
220
221 var _ ResourceDefinitionProvider = &ResourceDefinitionService{}
222
223 // resourceDefinitionLayerIn is a struct for resource-definitions
224 type resourceDefinitionLayerIn struct {
225 Type devicelayerkind.DeviceLayerKind `json:"type,omitempty"`
226 Data json.RawMessage `json:"data,omitempty"`
227 }
228
229 // UnmarshalJSON is needed for the unmarshal interface for ResourceDefinitionLayer types
230 func (rd *ResourceDefinitionLayer) UnmarshalJSON(b []byte) error {
231 var rdIn resourceDefinitionLayerIn
232 if err := json.Unmarshal(b, &rdIn); err != nil {
233 return err
234 }
235
236 rd.Type = rdIn.Type
237 switch rd.Type {
238 case devicelayerkind.Drbd:
239 dst := new(DrbdResourceDefinitionLayer)
240 if rdIn.Data != nil {
241 if err := json.Unmarshal(rdIn.Data, &dst); err != nil {
242 return err
243 }
244 }
245 rd.Data = dst
246 case devicelayerkind.Luks, devicelayerkind.Storage, devicelayerkind.Nvme, devicelayerkind.Writecache, devicelayerkind.Cache: // valid types, but do not set data
247 default:
248 return fmt.Errorf("'%+v' is not a valid type to Unmarshal", rd.Type)
249 }
250
251 return nil
252 }
253
254 // volumeDefinitionLayerIn is a struct for volume-defintion-layers
255 type volumeDefinitionLayerIn struct {
256 Type devicelayerkind.DeviceLayerKind `json:"type,omitempty"`
257 Data json.RawMessage `json:"data,omitempty"`
258 }
259
260 // UnmarshalJSON is needed for the unmarshal interface for VolumeDefinitionLayer types
261 func (vd *VolumeDefinitionLayer) UnmarshalJSON(b []byte) error {
262 var vdIn volumeDefinitionLayerIn
263 if err := json.Unmarshal(b, &vdIn); err != nil {
264 return err
265 }
266
267 vd.Type = vdIn.Type
268 switch vd.Type {
269 case devicelayerkind.Drbd:
270 dst := new(DrbdVolumeDefinition)
271 if vdIn.Data != nil {
272 if err := json.Unmarshal(vdIn.Data, &dst); err != nil {
273 return err
274 }
275 }
276 vd.Data = dst
277 case devicelayerkind.Luks, devicelayerkind.Storage, devicelayerkind.Nvme, devicelayerkind.Writecache, devicelayerkind.Cache: // valid types, but do not set data
278 default:
279 return fmt.Errorf("'%+v' is not a valid type to Unmarshal", vd.Type)
280 }
281
282 return nil
283 }
284
285 // OneOfDrbdVolumeDefinition is used to prevent other layertypes than drbd-volume-defintion
286 type OneOfDrbdVolumeDefinition interface {
287 isOneOfDrbdVolumeDefinition()
288 }
289
290 // Function used if volume-defintion-layertype is correct
291 func (d *DrbdVolumeDefinition) isOneOfDrbdVolumeDefinition() {}
292
293 // GetAll lists all resource-definitions
294 func (n *ResourceDefinitionService) GetAll(ctx context.Context, request RDGetAllRequest) ([]ResourceDefinitionWithVolumeDefinition, error) {
295 val, err := query.Values(request)
296 if err != nil {
297 return nil, err
298 }
299
300 var resDefs []ResourceDefinitionWithVolumeDefinition
301 _, err = n.client.doGET(ctx, "/v1/resource-definitions?"+val.Encode(), &resDefs)
302 return resDefs, err
303 }
304
305 // Get return information about a resource-defintion
306 func (n *ResourceDefinitionService) Get(ctx context.Context, resDefName string, opts ...*ListOpts) (ResourceDefinition, error) {
307 var resDef ResourceDefinition
308 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resDefName, &resDef, opts...)
309 return resDef, err
310 }
311
312 // Create adds a new resource-definition
313 func (n *ResourceDefinitionService) Create(ctx context.Context, resDef ResourceDefinitionCreate) error {
314 _, err := n.client.doPOST(ctx, "/v1/resource-definitions", resDef)
315 return err
316 }
317
318 // Modify allows to modify a resource-definition
319 func (n *ResourceDefinitionService) Modify(ctx context.Context, resDefName string, props GenericPropsModify) error {
320 _, err := n.client.doPUT(ctx, "/v1/resource-definitions/"+resDefName, props)
321 return err
322 }
323
324 // Delete completely deletes a resource-definition
325 func (n *ResourceDefinitionService) Delete(ctx context.Context, resDefName string) error {
326 _, err := n.client.doDELETE(ctx, "/v1/resource-definitions/"+resDefName, nil)
327 return err
328 }
329
330 // Truncate deletes all resources of the resource-definition without deleting
331 // the resource-definition itself or any of its snapshots.
332 func (n *ResourceDefinitionService) Truncate(ctx context.Context, resDefName string, opts ...*ResourceDefinitionTruncateOpts) error {
333 opt, err := Optional(opts...)
334 if err != nil {
335 return err
336 }
337
338 vals, err := query.Values(opt)
339 if err != nil {
340 return fmt.Errorf("failed to encode resource-definition truncate options: %w", err)
341 }
342
343 _, err = n.client.doDELETE(ctx, "/v1/resource-definitions/"+resDefName+"/resources?"+vals.Encode(), nil)
344 return err
345 }
346
347 // GetVolumeDefinitions returns all volume-definitions of a resource-definition
348 func (n *ResourceDefinitionService) GetVolumeDefinitions(ctx context.Context, resDefName string, opts ...*ListOpts) ([]VolumeDefinition, error) {
349 var volDefs []VolumeDefinition
350 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resDefName+"/volume-definitions", &volDefs, opts...)
351 return volDefs, err
352 }
353
354 // GetVolumeDefinition shows the properties of a specific volume-definition
355 func (n *ResourceDefinitionService) GetVolumeDefinition(ctx context.Context, resDefName string, volNr int, opts ...*ListOpts) (VolumeDefinition, error) {
356 var volDef VolumeDefinition
357 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resDefName+"/volume-definitions/"+strconv.Itoa(volNr), &volDef, opts...)
358 return volDef, err
359 }
360
361 // CreateVolumeDefinition adds a volume-definition to a resource-definition. Only the size is required.
362 func (n *ResourceDefinitionService) CreateVolumeDefinition(ctx context.Context, resDefName string, volDef VolumeDefinitionCreate) error {
363 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+resDefName+"/volume-definitions", volDef)
364 return err
365 }
366
367 // ModifyVolumeDefinition give the abilty to modify a specific volume-definition
368 func (n *ResourceDefinitionService) ModifyVolumeDefinition(ctx context.Context, resDefName string, volNr int, props VolumeDefinitionModify) error {
369 _, err := n.client.doPUT(ctx, "/v1/resource-definitions/"+resDefName+"/volume-definitions/"+strconv.Itoa(volNr), props)
370 return err
371 }
372
373 // DeleteVolumeDefinition deletes a specific volume-definition
374 func (n *ResourceDefinitionService) DeleteVolumeDefinition(ctx context.Context, resDefName string, volNr int) error {
375 _, err := n.client.doDELETE(ctx, "/v1/resource-definitions/"+resDefName+"/volume-definitions/"+strconv.Itoa(volNr), nil)
376 return err
377 }
378
379 // GetPropsInfos gets meta information about the properties that can be set on
380 // a resource definition.
381 func (n *ResourceDefinitionService) GetPropsInfos(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error) {
382 var infos []PropsInfo
383 _, err := n.client.doGET(ctx, "/v1/resource-definitions/properties/info", &infos, opts...)
384 return infos, err
385 }
386
387 // GetDRBDProxyPropsInfos gets meta information about the properties that can
388 // be set on a resource definition for drbd proxy.
389 func (n *ResourceDefinitionService) GetDRBDProxyPropsInfos(ctx context.Context, resDefName string, opts ...*ListOpts) ([]PropsInfo, error) {
390 var infos []PropsInfo
391 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resDefName+"/drbd-proxy/properties/info", &infos, opts...)
392 return infos, err
393 }
394
395 // AttachExternalFile adds an external file to the resource definition. This
396 // means that the file will be deployed to every node the resource is deployed on.
397 func (n *ResourceDefinitionService) AttachExternalFile(ctx context.Context, resDefName string, filePath string) error {
398 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+resDefName+"/files/"+url.QueryEscape(filePath), nil)
399 return err
400 }
401
402 // DetachExternalFile removes a binding between an external file and a resource definition.
403 // This means that the file will no longer be deployed on every node the resource
404 // is deployed on.
405 func (n *ResourceDefinitionService) DetachExternalFile(ctx context.Context, resDefName string, filePath string) error {
406 _, err := n.client.doDELETE(ctx, "/v1/resource-definitions/"+resDefName+"/files/"+url.QueryEscape(filePath), nil)
407 return err
408 }
409
410 // Clone starts cloning a resource definition and all resources using a method optimized for the storage driver.
411 func (n *ResourceDefinitionService) Clone(ctx context.Context, srcResDef string, request ResourceDefinitionCloneRequest) (ResourceDefinitionCloneStarted, error) {
412 var resp ResourceDefinitionCloneStarted
413
414 req, err := n.client.newRequest("POST", "/v1/resource-definitions/"+srcResDef+"/clone", request)
415 if err != nil {
416 return ResourceDefinitionCloneStarted{}, err
417 }
418
419 _, err = n.client.doJSON(ctx, req, &resp)
420 if err != nil {
421 return ResourceDefinitionCloneStarted{}, err
422 }
423
424 return resp, nil
425 }
426
427 // CloneStatus fetches the current status of a clone operation started by Clone.
428 func (n *ResourceDefinitionService) CloneStatus(ctx context.Context, srcResDef, targetResDef string) (ResourceDefinitionCloneStatus, error) {
429 var status ResourceDefinitionCloneStatus
430 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+srcResDef+"/clone/"+targetResDef, &status)
431 return status, err
432 }
433
434 // SyncStatus checks if a resource is currently in sync on all nodes
435 func (n *ResourceDefinitionService) SyncStatus(ctx context.Context, resDef string) (ResourceDefinitionSyncStatus, error) {
436 var status ResourceDefinitionSyncStatus
437 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resDef+"/sync-status", &status)
438 return status, err
439 }