Debian Codegraph @

client/resource.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 "strconv"
25
26 "github.com/google/go-querystring/query"
27
28 "github.com/LINBIT/golinstor/devicelayerkind"
29 "github.com/LINBIT/golinstor/snapshotshipstatus"
30 )
31
32 // ResourceService is a struct which contains the pointer of the client
33 type ResourceService struct {
34 client *Client
35 }
36
37 // copy & paste from generated code
38
39 // Resource is a struct which holds the information of a resource
40 type Resource struct {
41 Name string `json:"name,omitempty"`
42 NodeName string `json:"node_name,omitempty"`
43 // A string to string property map.
44 Props map[string]string `json:"props,omitempty"`
45 Flags []string `json:"flags,omitempty"`
46 LayerObject *ResourceLayer `json:"layer_object,omitempty"`
47 State *ResourceState `json:"state,omitempty"`
48 // unique object id
49 Uuid string `json:"uuid,omitempty"`
50 // milliseconds since unix epoch in UTC
51 CreateTimestamp *TimeStampMs `json:"create_timestamp,omitempty"`
52 }
53
54 type ResourceWithVolumes struct {
55 Resource
56 // milliseconds since unix epoch in UTC
57 CreateTimestamp *TimeStampMs `json:"create_timestamp,omitempty"`
58 Volumes []Volume `json:"volumes,omitempty"`
59 // shared space name of the data storage pool of the first volume of
60 // the resource or empty if data storage pool is not shared
61 SharedName string `json:"shared_name,omitempty"`
62 }
63
64 type ResourceDefinitionModify struct {
65 // drbd port for resources
66 DrbdPort int32 `json:"drbd_port,omitempty"`
67 // drbd peer slot number
68 DrbdPeerSlots int32 `json:"drbd_peer_slots,omitempty"`
69 LayerStack []devicelayerkind.DeviceLayerKind `json:"layer_stack,omitempty"`
70 // change resource group to the given group name
71 ResourceGroup string `json:"resource_group,omitempty"`
72 GenericPropsModify
73 }
74
75 // ResourceCreate is a struct where the properties of a resource are stored to create it
76 type ResourceCreate struct {
77 Resource Resource `json:"resource,omitempty"`
78 LayerList []devicelayerkind.DeviceLayerKind `json:"layer_list,omitempty"`
79 DrbdNodeId int32 `json:"drbd_node_id,omitempty"`
80 }
81
82 // ResourceLayer is a struct to store layer-information abour a resource
83 type ResourceLayer struct {
84 Children []ResourceLayer `json:"children,omitempty"`
85 ResourceNameSuffix string `json:"resource_name_suffix,omitempty"`
86 Type devicelayerkind.DeviceLayerKind `json:"type,omitempty"`
87 Drbd *DrbdResource `json:"drbd,omitempty"`
88 Luks *LuksResource `json:"luks,omitempty"`
89 Storage *StorageResource `json:"storage,omitempty"`
90 Nvme *NvmeResource `json:"nvme,omitempty"`
91 Writecache *WritecacheResource `json:"writecache,omitempty"`
92 Cache *CacheResource `json:"cache,omitempty"`
93 BCache *BCacheResource `json:"bcache,omitempty"`
94 }
95
96 type WritecacheResource struct {
97 WritecacheVolumes []WritecacheVolume `json:"writecache_volumes,omitempty"`
98 }
99
100 type WritecacheVolume struct {
101 VolumeNumber int32 `json:"volume_number,omitempty"`
102 // block device path
103 DevicePath string `json:"device_path,omitempty"`
104 // block device path used as cache device
105 DevicePathCache string `json:"device_path_cache,omitempty"`
106 AllocatedSizeKib int64 `json:"allocated_size_kib,omitempty"`
107 UsableSizeKib int64 `json:"usable_size_kib,omitempty"`
108 // String describing current volume state
109 DiskState string `json:"disk_state,omitempty"`
110 }
111
112 type BCacheResource struct {
113 BCacheVolumes []BCacheVolume `json:"bcache_volumes,omitempty"`
114 }
115
116 type BCacheVolume struct {
117 VolumeNumber int32 `json:"volume_number,omitempty"`
118 // block device path
119 DevicePath string `json:"device_path,omitempty"`
120 // block device path used as cache device
121 DevicePathCache string `json:"device_path_cache,omitempty"`
122 AllocatedSizeKib int64 `json:"allocated_size_kib,omitempty"`
123 UsableSizeKib int64 `json:"usable_size_kib,omitempty"`
124 // String describing current volume state
125 DiskState string `json:"disk_state,omitempty"`
126 }
127
128 // DrbdResource is a struct used to give linstor drbd properties for a resource
129 type DrbdResource struct {
130 DrbdResourceDefinition DrbdResourceDefinitionLayer `json:"drbd_resource_definition,omitempty"`
131 NodeId int32 `json:"node_id,omitempty"`
132 PeerSlots int32 `json:"peer_slots,omitempty"`
133 AlStripes int32 `json:"al_stripes,omitempty"`
134 AlSize int64 `json:"al_size,omitempty"`
135 Flags []string `json:"flags,omitempty"`
136 DrbdVolumes []DrbdVolume `json:"drbd_volumes,omitempty"`
137 Connections map[string]DrbdConnection `json:"connections,omitempty"`
138 PromotionScore int32 `json:"promotion_score,omitempty"`
139 MayPromote bool `json:"may_promote,omitempty"`
140 }
141
142 // DrbdConnection is a struct representing the DRBD connection status
143 type DrbdConnection struct {
144 Connected bool `json:"connected,omitempty"`
145 // DRBD connection status
146 Message string `json:"message,omitempty"`
147 }
148
149 // DrbdVolume is a struct for linstor to get inormation about a drbd-volume
150 type DrbdVolume struct {
151 DrbdVolumeDefinition DrbdVolumeDefinition `json:"drbd_volume_definition,omitempty"`
152 // drbd device path e.g. '/dev/drbd1000'
153 DevicePath string `json:"device_path,omitempty"`
154 // block device used by drbd
155 BackingDevice string `json:"backing_device,omitempty"`
156 MetaDisk string `json:"meta_disk,omitempty"`
157 AllocatedSizeKib int64 `json:"allocated_size_kib,omitempty"`
158 UsableSizeKib int64 `json:"usable_size_kib,omitempty"`
159 // String describing current volume state
160 DiskState string `json:"disk_state,omitempty"`
161 // Storage pool name used for external meta data; null for internal
162 ExtMetaStorPool string `json:"ext_meta_stor_pool,omitempty"`
163 }
164
165 // LuksResource is a struct to store storage-volumes for a luks-resource
166 type LuksResource struct {
167 StorageVolumes []LuksVolume `json:"storage_volumes,omitempty"`
168 }
169
170 // LuksVolume is a struct used for information about a luks-volume
171 type LuksVolume struct {
172 VolumeNumber int32 `json:"volume_number,omitempty"`
173 // block device path
174 DevicePath string `json:"device_path,omitempty"`
175 // block device used by luks
176 BackingDevice string `json:"backing_device,omitempty"`
177 AllocatedSizeKib int64 `json:"allocated_size_kib,omitempty"`
178 UsableSizeKib int64 `json:"usable_size_kib,omitempty"`
179 // String describing current volume state
180 DiskState string `json:"disk_state,omitempty"`
181 Opened bool `json:"opened,omitempty"`
182 }
183
184 // StorageResource is a struct which contains the storage-volumes for a storage-resource
185 type StorageResource struct {
186 StorageVolumes []StorageVolume `json:"storage_volumes,omitempty"`
187 }
188
189 // StorageVolume is a struct to store standard poperties of a Volume
190 type StorageVolume struct {
191 VolumeNumber int32 `json:"volume_number,omitempty"`
192 // block device path
193 DevicePath string `json:"device_path,omitempty"`
194 AllocatedSizeKib int64 `json:"allocated_size_kib,omitempty"`
195 UsableSizeKib int64 `json:"usable_size_kib,omitempty"`
196 // String describing current volume state
197 DiskState string `json:"disk_state,omitempty"`
198 }
199
200 type NvmeResource struct {
201 NvmeVolumes []NvmeVolume `json:"nvme_volumes,omitempty"`
202 }
203
204 type NvmeVolume struct {
205 VolumeNumber int32 `json:"volume_number,omitempty"`
206 // block device path
207 DevicePath string `json:"device_path,omitempty"`
208 // block device used by nvme
209 BackingDevice string `json:"backing_device,omitempty"`
210 AllocatedSizeKib int64 `json:"allocated_size_kib,omitempty"`
211 UsableSizeKib int64 `json:"usable_size_kib,omitempty"`
212 // String describing current volume state
213 DiskState string `json:"disk_state,omitempty"`
214 }
215
216 // ResourceState is a struct for getting the status of a resource
217 type ResourceState struct {
218 InUse *bool `json:"in_use,omitempty"`
219 }
220
221 // Volume is a struct which holds the information about a linstor-volume
222 type Volume struct {
223 VolumeNumber int32 `json:"volume_number,omitempty"`
224 StoragePoolName string `json:"storage_pool_name,omitempty"`
225 ProviderKind ProviderKind `json:"provider_kind,omitempty"`
226 DevicePath string `json:"device_path,omitempty"`
227 AllocatedSizeKib int64 `json:"allocated_size_kib,omitempty"`
228 UsableSizeKib int64 `json:"usable_size_kib,omitempty"`
229 // A string to string property map.
230 Props map[string]string `json:"props,omitempty"`
231 Flags []string `json:"flags,omitempty"`
232 State VolumeState `json:"state,omitempty"`
233 LayerDataList []VolumeLayer `json:"layer_data_list,omitempty"`
234 // unique object id
235 Uuid string `json:"uuid,omitempty"`
236 Reports []ApiCallRc `json:"reports,omitempty"`
237 }
238
239 // VolumeLayer is a struct for storing the layer-properties of a linstor-volume
240 type VolumeLayer struct {
241 Type devicelayerkind.DeviceLayerKind `json:"type,omitempty"`
242 Data OneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume `json:"data,omitempty"`
243 }
244
245 // VolumeState is a struct which contains the disk-state for volume
246 type VolumeState struct {
247 DiskState string `json:"disk_state,omitempty"`
248 }
249
250 // AutoPlaceRequest is a struct to store the paramters for the linstor auto-place command
251 type AutoPlaceRequest struct {
252 DisklessOnRemaining bool `json:"diskless_on_remaining,omitempty"`
253 SelectFilter AutoSelectFilter `json:"select_filter,omitempty"`
254 LayerList []devicelayerkind.DeviceLayerKind `json:"layer_list,omitempty"`
255 }
256
257 // AutoSelectFilter is a struct used to have information about the auto-select function
258 type AutoSelectFilter struct {
259 PlaceCount int32 `json:"place_count,omitempty"`
260 AdditionalPlaceCount int32 `json:"additional_place_count,omitempty"`
261 NodeNameList []string `json:"node_name_list,omitempty"`
262 StoragePool string `json:"storage_pool,omitempty"`
263 StoragePoolList []string `json:"storage_pool_list,omitempty"`
264 StoragePoolDisklessList []string `json:"storage_pool_diskless_list,omitempty"`
265 NotPlaceWithRsc []string `json:"not_place_with_rsc,omitempty"`
266 NotPlaceWithRscRegex string `json:"not_place_with_rsc_regex,omitempty"`
267 ReplicasOnSame []string `json:"replicas_on_same,omitempty"`
268 ReplicasOnDifferent []string `json:"replicas_on_different,omitempty"`
269 XReplicasOnDifferent map[string]int `json:"x_replicas_on_different_map,omitempty"`
270 LayerStack []string `json:"layer_stack,omitempty"`
271 ProviderList []string `json:"provider_list,omitempty"`
272 DisklessOnRemaining bool `json:"diskless_on_remaining,omitempty"`
273 DisklessType string `json:"diskless_type,omitempty"`
274 Overprovision *float64 `json:"overprovision,omitempty"`
275 }
276
277 // ResourceConnection is a struct which holds information about a connection between to nodes
278 type ResourceConnection struct {
279 // source node of the connection
280 NodeA string `json:"node_a,omitempty"`
281 // target node of the connection
282 NodeB string `json:"node_b,omitempty"`
283 // A string to string property map.
284 Props map[string]string `json:"props,omitempty"`
285 Flags []string `json:"flags,omitempty"`
286 Port int32 `json:"port,omitempty"`
287 }
288
289 // Snapshot is a struct for information about a snapshot
290 type Snapshot struct {
291 Name string `json:"name,omitempty"`
292 ResourceName string `json:"resource_name,omitempty"`
293 Nodes []string `json:"nodes,omitempty"`
294 // Snapshot-related properties
295 SnapshotDefinitionProps map[string]string `json:"snapshot_definition_props,omitempty"`
296 // Properties on the RD at the time the snapshot was cut.
297 ResourceDefinitionProps map[string]string `json:"resource_definition_props,omitempty"`
298 // Deprecated, use SnapshotDefinitionProps or ResourceDefinitionProps instead.
299 Props map[string]string `json:"props,omitempty"`
300 Flags []string `json:"flags,omitempty"`
301 VolumeDefinitions []SnapshotVolumeDefinition `json:"volume_definitions,omitempty"`
302 // unique object id
303 Uuid string `json:"uuid,omitempty"`
304 Snapshots []SnapshotNode `json:"snapshots,omitempty"`
305 }
306
307 // SnapshotNode Actual snapshot data from a node
308 type SnapshotNode struct {
309 // Snapshot name this snapshots belongs to
310 SnapshotName string `json:"snapshot_name,omitempty"`
311 // Node name where this snapshot was taken
312 NodeName string `json:"node_name,omitempty"`
313 // milliseconds since unix epoch in UTC
314 CreateTimestamp *TimeStampMs `json:"create_timestamp,omitempty"`
315 Flags []string `json:"flags,omitempty"`
316 // unique object id
317 Uuid string `json:"uuid,omitempty"`
318 // Properties on the snapshot's resource on this node.
319 ResourceProps map[string]string `json:"resource_props,omitempty"`
320 // SnapshotVolumes holds per-volume information about snapshots on this node.
321 SnapshotVolumes []SnapshotVolumeNode `json:"snapshot_volumes,omitempty"`
322 }
323
324 type SnapshotVolumeNode struct {
325 // unique object id
326 Uuid string `json:"uuid,omitempty"`
327 // Volume number of the snapshot
328 VlmNr int32 `json:"vlm_nr,omitempty"`
329 // A string to string property map.
330 Props map[string]string `json:"props,omitempty"`
331 // Optional state for the given snapshot
332 State string `json:"state,omitempty"`
333 }
334
335 // SnapshotShipping struct for SnapshotShipping
336 type SnapshotShipping struct {
337 // Node where to ship the snapshot from
338 FromNode string `json:"from_node"`
339 // NetInterface of the source node
340 FromNic string `json:"from_nic,omitempty"`
341 // Node where to ship the snapshot
342 ToNode string `json:"to_node"`
343 // NetInterface of the destination node
344 ToNic string `json:"to_nic,omitempty"`
345 }
346
347 // SnapshotShippingStatus struct for SnapshotShippingStatus
348 type SnapshotShippingStatus struct {
349 Snapshot Snapshot `json:"snapshot,omitempty"`
350 FromNodeName string `json:"from_node_name,omitempty"`
351 ToNodeName string `json:"to_node_name,omitempty"`
352 Status snapshotshipstatus.SnapshotShipStatus `json:"status,omitempty"`
353 }
354
355 // SnapshotVolumeDefinition is a struct to store the properties of a volume from a snapshot
356 type SnapshotVolumeDefinition struct {
357 VolumeNumber int32 `json:"volume_number,omitempty"`
358 // Volume size in KiB
359 SizeKib uint64 `json:"size_kib,omitempty"`
360 // Properties on the volume definition at the time the snapshot was cut.
361 VolumeDefinitionProps map[string]string `json:"volume_definition_props,omitempty"`
362 }
363
364 // SnapshotRestore is a struct used to hold the information about where a Snapshot has to be restored
365 type SnapshotRestore struct {
366 // Resource where to restore the snapshot
367 ToResource string `json:"to_resource"`
368 // List of nodes where to place the restored snapshot
369 Nodes []string `json:"nodes,omitempty"`
370 }
371
372 type DrbdProxyModify struct {
373 // Compression type used by the proxy.
374 CompressionType string `json:"compression_type,omitempty"`
375 // A string to string property map.
376 CompressionProps map[string]string `json:"compression_props,omitempty"`
377 GenericPropsModify
378 }
379
380 // Candidate struct for Candidate
381 type Candidate struct {
382 StoragePool string `json:"storage_pool,omitempty"`
383 // maximum size in KiB
384 MaxVolumeSizeKib int64 `json:"max_volume_size_kib,omitempty"`
385 NodeNames []string `json:"node_names,omitempty"`
386 AllThin bool `json:"all_thin,omitempty"`
387 }
388
389 // MaxVolumeSizes struct for MaxVolumeSizes
390 type MaxVolumeSizes struct {
391 Candidates []Candidate `json:"candidates,omitempty"`
392 DefaultMaxOversubscriptionRatio float64 `json:"default_max_oversubscription_ratio,omitempty"`
393 }
394
395 type ResourceMakeAvailable struct {
396 LayerList []devicelayerkind.DeviceLayerKind `json:"layer_list,omitempty"`
397 // if true resource will be created as diskful even if diskless would be possible
398 Diskful bool `json:"diskful,omitempty"`
399 // if true the resource is prepared for a live migration to this node: for DRBD
400 // resources allow-two-primaries (and protocol C if needed) is set between the
401 // migration source and this node; for resources in a shared storage pool the
402 // resource is activated on both nodes.
403 AutoManageDualPrimary bool `json:"auto_manage_dual_primary,omitempty"`
404 }
405
406 type ResourceDeleteOpts struct {
407 KeepTiebreaker bool `url:"keep_tiebreaker"`
408 }
409
410 type SnapshotDeleteOpts struct {
411 // Nodes limits the delete to snapshots on the given nodes.
412 Nodes []string `url:"nodes"`
413 // DeleteEmptyResourceDefinition also deletes the resource-definition when it has
414 // neither resources nor snapshots left after this snapshot has been deleted.
415 DeleteEmptyResourceDefinition bool `url:"delete_empty_resource_definition"`
416 }
417
418 type ToggleDiskDiskfulProps struct {
419 LayerList []devicelayerkind.DeviceLayerKind `json:"layer_list,omitempty"`
420 }
421
422 // custom code
423
424 // ResourceProvider acts as an abstraction for an ResourceService. It can be
425 // swapped out for another ResourceService implementation, for example for
426 // testing.
427 type ResourceProvider interface {
428 // GetResourceView returns all resources in the cluster. Filters can be set via ListOpts.
429 GetResourceView(ctx context.Context, opts ...*ListOpts) ([]ResourceWithVolumes, error)
430 // GetAll returns all resources for a resource-definition
431 GetAll(ctx context.Context, resName string, opts ...*ListOpts) ([]Resource, error)
432 // Get returns information about a resource on a specific node
433 Get(ctx context.Context, resName, nodeName string, opts ...*ListOpts) (Resource, error)
434 // Create is used to create a resource on a node
435 Create(ctx context.Context, res ResourceCreate) error
436 // Modify gives the ability to modify a resource on a node
437 Modify(ctx context.Context, resName, nodeName string, props GenericPropsModify) error
438 // Delete deletes a resource on a specific node
439 Delete(ctx context.Context, resName, nodeName string, opts ...*ResourceDeleteOpts) error
440 // GetVolumes lists als volumes of a resource
441 GetVolumes(ctx context.Context, resName, nodeName string, opts ...*ListOpts) ([]Volume, error)
442 // GetVolume returns information about a specific volume defined by it resource,node and volume-number
443 GetVolume(ctx context.Context, resName, nodeName string, volNr int, opts ...*ListOpts) (Volume, error)
444 // ModifyVolume modifies an existing volume with the given props
445 ModifyVolume(ctx context.Context, resName, nodeName string, volNr int, props GenericPropsModify) error
446 // Diskless toggles a resource on a node to diskless - the parameter disklesspool can be set if its needed
447 Diskless(ctx context.Context, resName, nodeName, disklessPoolName string) error
448 // Diskful toggles a resource to diskful - the parameter storagepool can be set if its needed
449 Diskful(ctx context.Context, resName, nodeName, storagePoolName string, props *ToggleDiskDiskfulProps) error
450 // Migrate mirgates a resource from one node to another node
451 Migrate(ctx context.Context, resName, fromNodeName, toNodeName, storagePoolName string) error
452 // Autoplace places a resource on your nodes autmatically
453 Autoplace(ctx context.Context, resName string, apr AutoPlaceRequest) error
454 // GetConnections lists all resource connections if no node-names are given- if two node-names are given it shows the connection between them
455 GetConnections(ctx context.Context, resName, nodeAName, nodeBName string, opts ...*ListOpts) ([]ResourceConnection, error)
456 // ModifyConnection allows to modify the connection between two nodes
457 ModifyConnection(ctx context.Context, resName, nodeAName, nodeBName string, props GenericPropsModify) error
458 // GetSnapshots lists all snapshots of a resource
459 GetSnapshots(ctx context.Context, resName string, opts ...*ListOpts) ([]Snapshot, error)
460 // GetSnapshotView gets information about all snapshots
461 GetSnapshotView(ctx context.Context, opts ...*ListOpts) ([]Snapshot, error)
462 // GetSnapshot returns information about a specific Snapshot by its name
463 GetSnapshot(ctx context.Context, resName, snapName string, opts ...*ListOpts) (Snapshot, error)
464 // CreateSnapshot creates a snapshot of a resource
465 CreateSnapshot(ctx context.Context, snapshot Snapshot) error
466 // CreateSnapshots creates multiple snapshots at once, suspending IO on all volumes at the same time.
467 CreateSnapshots(ctx context.Context, snapshot ...Snapshot) error
468 // DeleteSnapshot deletes a snapshot by its name.
469 DeleteSnapshot(ctx context.Context, resName, snapName string, opts ...*SnapshotDeleteOpts) error
470 // RestoreSnapshot restores a snapshot on a resource
471 RestoreSnapshot(ctx context.Context, origResName, snapName string, snapRestoreConf SnapshotRestore) error
472 // RestoreVolumeDefinitionSnapshot restores a volume-definition-snapshot on a resource
473 RestoreVolumeDefinitionSnapshot(ctx context.Context, origResName, snapName string, snapRestoreConf SnapshotRestore) error
474 // RollbackSnapshot rolls back a snapshot from a specific resource
475 RollbackSnapshot(ctx context.Context, resName, snapName string) error
476 // ModifyDRBDProxy is used to modify drbd-proxy properties
477 ModifyDRBDProxy(ctx context.Context, resName string, props DrbdProxyModify) error
478 // EnableDRBDProxy is used to enable drbd-proxy with the rest-api call from the function enableDisableDRBDProxy
479 EnableDRBDProxy(ctx context.Context, resName, nodeAName, nodeBName string) error
480 // DisableDRBDProxy is used to disable drbd-proxy with the rest-api call from the function enableDisableDRBDProxy
481 DisableDRBDProxy(ctx context.Context, resName, nodeAName, nodeBName string) error
482 // QueryMaxVolumeSize finds the maximum size of a volume for a given filter
483 QueryMaxVolumeSize(ctx context.Context, filter AutoSelectFilter) (MaxVolumeSizes, error)
484 // GetPropsInfos gets meta information about the properties that can be
485 // set on a resource.
486 GetPropsInfos(ctx context.Context, resName string, opts ...*ListOpts) ([]PropsInfo, error)
487 // GetVolumeDefinitionPropsInfos gets meta information about the
488 // properties that can be set on a volume definition.
489 GetVolumeDefinitionPropsInfos(ctx context.Context, resName string, opts ...*ListOpts) ([]PropsInfo, error)
490 // GetVolumePropsInfos gets meta information about the properties that
491 // can be set on a volume.
492 GetVolumePropsInfos(ctx context.Context, resName, nodeName string, opts ...*ListOpts) ([]PropsInfo, error)
493 // GetConnectionPropsInfos gets meta information about the properties
494 // that can be set on a connection.
495 GetConnectionPropsInfos(ctx context.Context, resName string, opts ...*ListOpts) ([]PropsInfo, error)
496 // Activate starts an inactive resource on a given node.
497 Activate(ctx context.Context, resName string, nodeName string) error
498 // Deactivate stops an active resource on given node.
499 Deactivate(ctx context.Context, resName string, nodeName string) error
500 // MakeAvailable adds a resource on a node if not already deployed.
501 // To use a specific storage pool add the StorPoolName property and use
502 // the storage pool name as value. If the StorPoolName property is not
503 // set, a storage pool will be chosen automatically using the
504 // auto-placer.
505 MakeAvailable(ctx context.Context, resName, nodeName string, makeAvailable ResourceMakeAvailable) error
506 // UnmakeAvailable reverts a MakeAvailable.
507 UnmakeAvailable(ctx context.Context, resName, nodeName string) error
508 }
509
510 var _ ResourceProvider = &ResourceService{}
511
512 // volumeLayerIn is a struct for volume-layers
513 type volumeLayerIn struct {
514 Type devicelayerkind.DeviceLayerKind `json:"type,omitempty"`
515 Data json.RawMessage `json:"data,omitempty"`
516 }
517
518 // UnmarshalJSON fulfills the unmarshal interface for the VolumeLayer type
519 func (v *VolumeLayer) UnmarshalJSON(b []byte) error {
520 var vIn volumeLayerIn
521 if err := json.Unmarshal(b, &vIn); err != nil {
522 return err
523 }
524
525 v.Type = vIn.Type
526 switch v.Type {
527 case devicelayerkind.Drbd:
528 dst := new(DrbdVolume)
529 if vIn.Data != nil {
530 if err := json.Unmarshal(vIn.Data, &dst); err != nil {
531 return err
532 }
533 }
534 v.Data = dst
535 case devicelayerkind.Luks:
536 dst := new(LuksVolume)
537 if vIn.Data != nil {
538 if err := json.Unmarshal(vIn.Data, &dst); err != nil {
539 return err
540 }
541 }
542 v.Data = dst
543 case devicelayerkind.Storage:
544 dst := new(StorageVolume)
545 if vIn.Data != nil {
546 if err := json.Unmarshal(vIn.Data, &dst); err != nil {
547 return err
548 }
549 }
550 v.Data = dst
551 case devicelayerkind.Nvme:
552 dst := new(NvmeVolume)
553 if vIn.Data != nil {
554 if err := json.Unmarshal(vIn.Data, &dst); err != nil {
555 return err
556 }
557 }
558 v.Data = dst
559 case devicelayerkind.Writecache:
560 dst := new(WritecacheVolume)
561 if vIn.Data != nil {
562 if err := json.Unmarshal(vIn.Data, &dst); err != nil {
563 return err
564 }
565 }
566 v.Data = dst
567 case devicelayerkind.Cache:
568 default:
569 return fmt.Errorf("'%+v' is not a valid type to Unmarshal", v.Type)
570 }
571
572 return nil
573 }
574
575 // OneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolume is used to prevent that other types than drbd- luks- and storage-volume are used for a VolumeLayer
576 type OneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume interface {
577 isOneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume()
578 }
579
580 // Functions which are used if type is a correct VolumeLayer
581 func (d *DrbdVolume) isOneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume() {
582 }
583 func (d *LuksVolume) isOneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume() {
584 }
585 func (d *StorageVolume) isOneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume() {
586 }
587 func (d *NvmeVolume) isOneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume() {
588 }
589 func (d *WritecacheVolume) isOneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume() {
590 }
591 func (d *CacheVolume) isOneOfDrbdVolumeLuksVolumeStorageVolumeNvmeVolumeWritecacheVolumeCacheVolumeBCacheVolume() {
592 }
593
594 // GetResourceView returns all resources in the cluster. Filters can be set via ListOpts.
595 func (n *ResourceService) GetResourceView(ctx context.Context, opts ...*ListOpts) ([]ResourceWithVolumes, error) {
596 var reses []ResourceWithVolumes
597 _, err := n.client.doGET(ctx, "/v1/view/resources", &reses, opts...)
598 return reses, err
599 }
600
601 // GetAll returns all resources for a resource-definition
602 func (n *ResourceService) GetAll(ctx context.Context, resName string, opts ...*ListOpts) ([]Resource, error) {
603 var reses []Resource
604 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/resources", &reses, opts...)
605 return reses, err
606 }
607
608 // Get returns information about a resource on a specific node
609 func (n *ResourceService) Get(ctx context.Context, resName, nodeName string, opts ...*ListOpts) (Resource, error) {
610 var res Resource
611 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/resources/"+nodeName, &res, opts...)
612 return res, err
613 }
614
615 // Create is used to create a resource on a node
616 func (n *ResourceService) Create(ctx context.Context, res ResourceCreate) error {
617 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+res.Resource.Name+"/resources/"+res.Resource.NodeName, res)
618 return err
619 }
620
621 // Modify gives the ability to modify a resource on a node
622 func (n *ResourceService) Modify(ctx context.Context, resName, nodeName string, props GenericPropsModify) error {
623 _, err := n.client.doPUT(ctx, "/v1/resource-definitions/"+resName+"/resources/"+nodeName, props)
624 return err
625 }
626
627 // Delete deletes a resource on a specific node
628 func (n *ResourceService) Delete(ctx context.Context, resName, nodeName string, opts ...*ResourceDeleteOpts) error {
629 opt, err := Optional(opts...)
630 if err != nil {
631 return err
632 }
633
634 val, err := query.Values(opt)
635 if err != nil {
636 return fmt.Errorf("failed to encode resource delete options: %w", err)
637 }
638
639 _, err = n.client.doDELETE(ctx, "/v1/resource-definitions/"+resName+"/resources/"+nodeName+"?"+val.Encode(), nil)
640 return err
641 }
642
643 func (n *ResourceService) Activate(ctx context.Context, resName, nodeName string) error {
644 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+resName+"/resources/"+nodeName+"/activate", nil)
645 return err
646 }
647
648 func (n *ResourceService) Deactivate(ctx context.Context, resName, nodeName string) error {
649 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+resName+"/resources/"+nodeName+"/deactivate", nil)
650 return err
651 }
652
653 // GetVolumes lists als volumes of a resource
654 func (n *ResourceService) GetVolumes(ctx context.Context, resName, nodeName string, opts ...*ListOpts) ([]Volume, error) {
655 var vols []Volume
656
657 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/resources/"+nodeName+"/volumes", &vols, opts...)
658 return vols, err
659 }
660
661 // GetVolume returns information about a specific volume defined by it resource,node and volume-number
662 func (n *ResourceService) GetVolume(ctx context.Context, resName, nodeName string, volNr int, opts ...*ListOpts) (Volume, error) {
663 var vol Volume
664
665 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/resources/"+nodeName+"/volumes/"+strconv.Itoa(volNr), &vol, opts...)
666 return vol, err
667 }
668
669 // ModifyVolume modifies an existing volume with the given props
670 func (n *ResourceService) ModifyVolume(ctx context.Context, resName, nodeName string, volNr int, props GenericPropsModify) error {
671 u := fmt.Sprintf("/v1/resource-definitions/%s/resources/%s/volumes/%d", resName, nodeName, volNr)
672 _, err := n.client.doPUT(ctx, u, props)
673 return err
674 }
675
676 // Diskless toggles a resource on a node to diskless - the parameter disklesspool can be set if its needed
677 func (n *ResourceService) Diskless(ctx context.Context, resName, nodeName, disklessPoolName string) error {
678 u := "/v1/resource-definitions/" + resName + "/resources/" + nodeName + "/toggle-disk/diskless"
679 if disklessPoolName != "" {
680 u += "/" + disklessPoolName
681 }
682
683 _, err := n.client.doPUT(ctx, u, nil)
684 return err
685 }
686
687 // Diskful toggles a resource to diskful - the parameter storagepool can be set if its needed
688 func (n *ResourceService) Diskful(ctx context.Context, resName, nodeName, storagePoolName string, props *ToggleDiskDiskfulProps) error {
689 u := "/v1/resource-definitions/" + resName + "/resources/" + nodeName + "/toggle-disk/diskful"
690 if storagePoolName != "" {
691 u += "/" + storagePoolName
692 }
693 _, err := n.client.doPUT(ctx, u, props)
694 return err
695 }
696
697 // Migrate mirgates a resource from one node to another node
698 func (n *ResourceService) Migrate(ctx context.Context, resName, fromNodeName, toNodeName, storagePoolName string) error {
699 u := "/v1/resource-definitions/" + resName + "/resources/" + toNodeName + "/migrate-disk/" + fromNodeName
700 if storagePoolName != "" {
701 u += "/" + storagePoolName
702 }
703 _, err := n.client.doPUT(ctx, u, nil)
704 return err
705 }
706
707 // Autoplace places a resource on your nodes autmatically
708 func (n *ResourceService) Autoplace(ctx context.Context, resName string, apr AutoPlaceRequest) error {
709 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+resName+"/autoplace", apr)
710 return err
711 }
712
713 // GetConnections lists all resource connections if no node-names are given- if two node-names are given it shows the connection between them
714 func (n *ResourceService) GetConnections(ctx context.Context, resName, nodeAName, nodeBName string, opts ...*ListOpts) ([]ResourceConnection, error) {
715 var resConns []ResourceConnection
716
717 u := "/v1/resource-definitions/" + resName + "/resources-connections"
718 if nodeAName != "" && nodeBName != "" {
719 u += fmt.Sprintf("/%s/%s", nodeAName, nodeBName)
720 }
721
722 _, err := n.client.doGET(ctx, u, &resConns, opts...)
723 return resConns, err
724 }
725
726 // ModifyConnection allows to modify the connection between two nodes
727 func (n *ResourceService) ModifyConnection(ctx context.Context, resName, nodeAName, nodeBName string, props GenericPropsModify) error {
728 u := fmt.Sprintf("/v1/resource-definitions/%s/resource-connections/%s/%s", resName, nodeAName, nodeBName)
729 _, err := n.client.doPUT(ctx, u, props)
730 return err
731 }
732
733 // GetSnapshots lists all snapshots of a resource
734 func (n *ResourceService) GetSnapshots(ctx context.Context, resName string, opts ...*ListOpts) ([]Snapshot, error) {
735 var snaps []Snapshot
736
737 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/snapshots", &snaps, opts...)
738 return snaps, err
739 }
740
741 // GetSnapshotView gets information about all snapshots
742 func (r *ResourceService) GetSnapshotView(ctx context.Context, opts ...*ListOpts) ([]Snapshot, error) {
743 var snaps []Snapshot
744 _, err := r.client.doGET(ctx, "/v1/view/snapshots", &snaps, opts...)
745 return snaps, err
746 }
747
748 // GetSnapshot returns information about a specific Snapshot by its name
749 func (n *ResourceService) GetSnapshot(ctx context.Context, resName, snapName string, opts ...*ListOpts) (Snapshot, error) {
750 var snap Snapshot
751
752 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/snapshots/"+snapName, &snap, opts...)
753 return snap, err
754 }
755
756 // CreateSnapshot creates a snapshot of a resource
757 func (n *ResourceService) CreateSnapshot(ctx context.Context, snapshot Snapshot) error {
758 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+snapshot.ResourceName+"/snapshots", snapshot)
759 return err
760 }
761
762 func (n *ResourceService) CreateSnapshots(ctx context.Context, snapshots ...Snapshot) error {
763 multiSnapshotRequest := struct {
764 Snapshots []Snapshot `json:"snapshots"`
765 }{
766 Snapshots: snapshots,
767 }
768
769 _, err := n.client.doPOST(ctx, "/v1/actions/snapshot/multi", multiSnapshotRequest)
770 return err
771 }
772
773 // DeleteSnapshot deletes a snapshot by its name.
774 func (n *ResourceService) DeleteSnapshot(ctx context.Context, resName, snapName string, opts ...*SnapshotDeleteOpts) error {
775 opt, err := Optional(opts...)
776 if err != nil {
777 return err
778 }
779
780 vals, err := query.Values(opt)
781 if err != nil {
782 return fmt.Errorf("failed to encode snapshot delete options: %w", err)
783 }
784
785 _, err = n.client.doDELETE(ctx, "/v1/resource-definitions/"+resName+"/snapshots/"+snapName+"?"+vals.Encode(), nil)
786 return err
787 }
788
789 // RestoreSnapshot restores a snapshot on a resource
790 func (n *ResourceService) RestoreSnapshot(ctx context.Context, origResName, snapName string, snapRestoreConf SnapshotRestore) error {
791 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+origResName+"/snapshot-restore-resource/"+snapName, snapRestoreConf)
792 return err
793 }
794
795 // RestoreVolumeDefinitionSnapshot restores a volume-definition-snapshot on a resource
796 func (n *ResourceService) RestoreVolumeDefinitionSnapshot(ctx context.Context, origResName, snapName string, snapRestoreConf SnapshotRestore) error {
797 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+origResName+"/snapshot-restore-volume-definition/"+snapName, snapRestoreConf)
798 return err
799 }
800
801 // RollbackSnapshot rolls back a snapshot from a specific resource
802 func (n *ResourceService) RollbackSnapshot(ctx context.Context, resName, snapName string) error {
803 _, err := n.client.doPOST(ctx, "/v1/resource-definitions/"+resName+"/snapshot-rollback/"+snapName, nil)
804 return err
805 }
806
807 // ModifyDRBDProxy is used to modify drbd-proxy properties
808 func (n *ResourceService) ModifyDRBDProxy(ctx context.Context, resName string, props DrbdProxyModify) error {
809 _, err := n.client.doPUT(ctx, "/v1/resource-definitions/"+resName+"/drbd-proxy", props)
810 return err
811 }
812
813 // enableDisableDRBDProxy enables or disables drbd-proxy between two nodes
814 func (n *ResourceService) enableDisableDRBDProxy(ctx context.Context, what, resName, nodeAName, nodeBName string) error {
815 u := fmt.Sprintf("/v1/resource-definitions/%s/drbd-proxy/%s/%s/%s", resName, what, nodeAName, nodeBName)
816 _, err := n.client.doPOST(ctx, u, nil)
817 return err
818 }
819
820 // EnableDRBDProxy is used to enable drbd-proxy with the rest-api call from the function enableDisableDRBDProxy
821 func (n *ResourceService) EnableDRBDProxy(ctx context.Context, resName, nodeAName, nodeBName string) error {
822 return n.enableDisableDRBDProxy(ctx, "enable", resName, nodeAName, nodeBName)
823 }
824
825 // DisableDRBDProxy is used to disable drbd-proxy with the rest-api call from the function enableDisableDRBDProxy
826 func (n *ResourceService) DisableDRBDProxy(ctx context.Context, resName, nodeAName, nodeBName string) error {
827 return n.enableDisableDRBDProxy(ctx, "disable", resName, nodeAName, nodeBName)
828 }
829
830 // QueryMaxVolumeSize finds the maximum size of a volume for a given filter
831 func (n *ResourceService) QueryMaxVolumeSize(ctx context.Context, filter AutoSelectFilter) (MaxVolumeSizes, error) {
832 var sizes MaxVolumeSizes
833 _, err := n.client.doOPTIONS(ctx, "/v1/query-max-volume-size", &sizes, filter)
834 return sizes, err
835 }
836
837 // GetPropsInfos gets meta information about the properties that can be set on
838 // a resource.
839 func (n *ResourceService) GetPropsInfos(ctx context.Context, resName string, opts ...*ListOpts) ([]PropsInfo, error) {
840 var infos []PropsInfo
841 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/resources/properties/info", &infos, opts...)
842 return infos, err
843 }
844
845 // GetVolumeDefinitionPropsInfos gets meta information about the properties
846 // that can be set on a volume definition.
847 func (n *ResourceService) GetVolumeDefinitionPropsInfos(ctx context.Context, resName string, opts ...*ListOpts) ([]PropsInfo, error) {
848 var infos []PropsInfo
849 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/volume-definitions/properties/info", &infos, opts...)
850 return infos, err
851 }
852
853 // GetVolumePropsInfos gets meta information about the properties that can be
854 // set on a volume.
855 func (n *ResourceService) GetVolumePropsInfos(ctx context.Context, resName, nodeName string, opts ...*ListOpts) ([]PropsInfo, error) {
856 var infos []PropsInfo
857 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/resources/"+nodeName+"/volumes/properties/info", &infos, opts...)
858 return infos, err
859 }
860
861 // GetConnectionPropsInfos gets meta information about the properties that can
862 // be set on a connection.
863 func (n *ResourceService) GetConnectionPropsInfos(ctx context.Context, resName string, opts ...*ListOpts) ([]PropsInfo, error) {
864 var infos []PropsInfo
865 _, err := n.client.doGET(ctx, "/v1/resource-definitions/"+resName+"/resource-connections/properties/info", &infos, opts...)
866 return infos, err
867 }
868
869 // MakeAvailable adds a resource on a node if not already deployed.
870 // To use a specific storage pool add the StorPoolName property and use the
871 // storage pool name as value. If the StorPoolName property is not set, a
872 // storage pool will be chosen automatically using the auto-placer.
873 func (n *ResourceService) MakeAvailable(ctx context.Context, resName, nodeName string, makeAvailable ResourceMakeAvailable) error {
874 u := fmt.Sprintf("/v1/resource-definitions/%s/resources/%s/make-available",
875 resName, nodeName)
876 _, err := n.client.doPOST(ctx, u, makeAvailable)
877 return err
878 }
879
880 // UnmakeAvailable reverts a MakeAvailable.
881 func (n *ResourceService) UnmakeAvailable(ctx context.Context, resName, nodeName string) error {
882 u := fmt.Sprintf("/v1/resource-definitions/%s/resources/%s/unmake-available",
883 resName, nodeName)
884 _, err := n.client.doPOST(ctx, u, nil)
885 return err
886 }