| 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 |
"net/http" |
| 23 |
"strconv" |
| 24 |
) |
| 25 |
|
| 26 |
type ResourceGroup struct { |
| 27 |
Name string `json:"name,omitempty"` |
| 28 |
Description string `json:"description,omitempty"` |
| 29 |
// A string to string property map. |
| 30 |
Props map[string]string `json:"props,omitempty"` |
| 31 |
SelectFilter AutoSelectFilter `json:"select_filter,omitempty"` |
| 32 |
// unique object id |
| 33 |
Uuid string `json:"uuid,omitempty"` |
| 34 |
} |
| 35 |
|
| 36 |
type ResourceGroupModify struct { |
| 37 |
Description string `json:"description,omitempty"` |
| 38 |
// A string to string property map. |
| 39 |
OverrideProps map[string]string `json:"override_props,omitempty"` |
| 40 |
DeleteProps []string `json:"delete_props,omitempty"` |
| 41 |
DeleteNamespaces []string `json:"delete_namespaces,omitempty"` |
| 42 |
SelectFilter AutoSelectFilter `json:"select_filter,omitempty"` |
| 43 |
} |
| 44 |
|
| 45 |
type ResourceGroupSpawn struct { |
| 46 |
// name of the resulting resource-definition |
| 47 |
ResourceDefinitionName string `json:"resource_definition_name"` |
| 48 |
// 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. |
| 49 |
ResourceDefinitionExternalName string `json:"resource_definition_external_name,omitempty"` |
| 50 |
// sizes (in kib) of the resulting volume-definitions |
| 51 |
VolumeSizes []int64 `json:"volume_sizes,omitempty"` |
| 52 |
SelectFilter AutoSelectFilter `json:"select_filter,omitempty"` |
| 53 |
// If false, the length of the vlm_sizes has to match the number of volume-groups or an error is returned. If true and there are more vlm_sizes than volume-groups, the additional volume-definitions will simply have no pre-set properties (i.e. \"empty\" volume-definitions) If true and there are less vlm_sizes than volume-groups, the additional volume-groups won't be used. If the count of vlm_sizes matches the number of volume-groups, this \"partial\" parameter has no effect. |
| 54 |
Partial bool `json:"partial,omitempty"` |
| 55 |
// If true, the spawn command will only create the resource-definition with the volume-definitions but will not perform an auto-place, even if it is configured. |
| 56 |
DefinitionsOnly bool `json:"definitions_only,omitempty"` |
| 57 |
} |
| 58 |
|
| 59 |
type ResourceGroupAdjust struct { |
| 60 |
SelectFilter *AutoSelectFilter `json:"select_filter,omitempty"` |
| 61 |
} |
| 62 |
|
| 63 |
type VolumeGroup struct { |
| 64 |
VolumeNumber int32 `json:"volume_number,omitempty"` |
| 65 |
// A string to string property map. |
| 66 |
Props map[string]string `json:"props,omitempty"` |
| 67 |
// unique object id |
| 68 |
Uuid string `json:"uuid,omitempty"` |
| 69 |
Flags []string `json:"flags,omitempty"` |
| 70 |
} |
| 71 |
|
| 72 |
type VolumeGroupModify struct { |
| 73 |
// A string to string property map. |
| 74 |
OverrideProps map[string]string `json:"override_props,omitempty"` |
| 75 |
// To add a flag just specify the flag name, to remove a flag prepend it with a '-'. Flags: * GROSS_SIZE |
| 76 |
Flags []string `json:"flags,omitempty"` |
| 77 |
DeleteProps []string `json:"delete_props,omitempty"` |
| 78 |
DeleteNamespaces []string `json:"delete_namespaces,omitempty"` |
| 79 |
} |
| 80 |
|
| 81 |
// QuerySizeInfoResponseSpaceInfo contains information returned from the QuerySizeInfo API call |
| 82 |
type QuerySizeInfoResponseSpaceInfo struct { |
| 83 |
MaxVlmSizeInKib int64 `json:"max_vlm_size_in_kib"` |
| 84 |
AvailableSizeInKib *int64 `json:"available_size_in_kib,omitempty"` |
| 85 |
CapacityInKib *int64 `json:"capacity_in_kib,omitempty"` |
| 86 |
DefaultMaxOversubscriptionRatio *float64 `json:"default_max_oversubscription_ratio,omitempty"` |
| 87 |
NextSpawnResult []QuerySizeInfoSpawnResult `json:"next_spawn_result,omitempty"` |
| 88 |
} |
| 89 |
|
| 90 |
// QuerySizeInfoSpawnResult describes the result of a potential spawn operation |
| 91 |
type QuerySizeInfoSpawnResult struct { |
| 92 |
NodeName string `json:"node_name"` |
| 93 |
StorPoolName string `json:"stor_pool_name"` |
| 94 |
} |
| 95 |
|
| 96 |
// QuerySizeInfoRequest is the request object for the QuerySizeInfo API call |
| 97 |
type QuerySizeInfoRequest struct { |
| 98 |
SelectFilter *AutoSelectFilter `json:"select_filter,omitempty"` |
| 99 |
} |
| 100 |
|
| 101 |
type QuerySizeInfoResponse struct { |
| 102 |
SpaceInfo *QuerySizeInfoResponseSpaceInfo `json:"space_info,omitempty"` |
| 103 |
Reports []ApiCallRc `json:"reports,omitempty"` |
| 104 |
} |
| 105 |
|
| 106 |
// custom code |
| 107 |
|
| 108 |
// ResourceGroupProvider acts as an abstraction for a |
| 109 |
// ResourceGroupService. It can be swapped out for another |
| 110 |
// ResourceGroupService implementation, for example for testing. |
| 111 |
type ResourceGroupProvider interface { |
| 112 |
// GetAll lists all resource-groups |
| 113 |
GetAll(ctx context.Context, opts ...*ListOpts) ([]ResourceGroup, error) |
| 114 |
// Get return information about a resource-defintion |
| 115 |
Get(ctx context.Context, resGrpName string, opts ...*ListOpts) (ResourceGroup, error) |
| 116 |
// Create adds a new resource-group |
| 117 |
Create(ctx context.Context, resGrp ResourceGroup) error |
| 118 |
// Modify allows to modify a resource-group |
| 119 |
Modify(ctx context.Context, resGrpName string, props ResourceGroupModify) error |
| 120 |
// Delete deletes a resource-group |
| 121 |
Delete(ctx context.Context, resGrpName string) error |
| 122 |
// Spawn creates a new resource-definition and auto-deploys if configured to do so |
| 123 |
Spawn(ctx context.Context, resGrpName string, resGrpSpwn ResourceGroupSpawn) error |
| 124 |
// GetVolumeGroups lists all volume-groups for a resource-group |
| 125 |
GetVolumeGroups(ctx context.Context, resGrpName string, opts ...*ListOpts) ([]VolumeGroup, error) |
| 126 |
// GetVolumeGroup lists a volume-group for a resource-group |
| 127 |
GetVolumeGroup(ctx context.Context, resGrpName string, volNr int, opts ...*ListOpts) (VolumeGroup, error) |
| 128 |
// Create adds a new volume-group to a resource-group |
| 129 |
CreateVolumeGroup(ctx context.Context, resGrpName string, volGrp VolumeGroup) error |
| 130 |
// Modify allows to modify a volume-group of a resource-group |
| 131 |
ModifyVolumeGroup(ctx context.Context, resGrpName string, volNr int, props VolumeGroupModify) error |
| 132 |
DeleteVolumeGroup(ctx context.Context, resGrpName string, volNr int) error |
| 133 |
// GetPropsInfos gets meta information about the properties that can be |
| 134 |
// set on a resource group. |
| 135 |
GetPropsInfos(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error) |
| 136 |
// GetVolumeGroupPropsInfos gets meta information about the properties |
| 137 |
// that can be set on a resource group. |
| 138 |
GetVolumeGroupPropsInfos(ctx context.Context, resGrpName string, opts ...*ListOpts) ([]PropsInfo, error) |
| 139 |
// Adjust all resource-definitions (calls autoplace for) of the given resource-group |
| 140 |
Adjust(ctx context.Context, resGrpName string, adjust ResourceGroupAdjust) error |
| 141 |
// AdjustAll adjusts all resource-definitions (calls autoplace) according to their associated resource group. |
| 142 |
AdjustAll(ctx context.Context, adjust ResourceGroupAdjust) error |
| 143 |
// QuerySizeInfo returns information about the space available in a resource group |
| 144 |
QuerySizeInfo(ctx context.Context, resGrpName string, req QuerySizeInfoRequest) (QuerySizeInfoResponse, error) |
| 145 |
} |
| 146 |
|
| 147 |
var _ ResourceGroupProvider = &ResourceGroupService{} |
| 148 |
|
| 149 |
// ResourceGroupService is the service that deals with resource group related tasks. |
| 150 |
type ResourceGroupService struct { |
| 151 |
client *Client |
| 152 |
} |
| 153 |
|
| 154 |
// GetAll lists all resource-groups |
| 155 |
func (n *ResourceGroupService) GetAll(ctx context.Context, opts ...*ListOpts) ([]ResourceGroup, error) { |
| 156 |
var resGrps []ResourceGroup |
| 157 |
_, err := n.client.doGET(ctx, "/v1/resource-groups", &resGrps, opts...) |
| 158 |
return resGrps, err |
| 159 |
} |
| 160 |
|
| 161 |
// Get return information about a resource-defintion |
| 162 |
func (n *ResourceGroupService) Get(ctx context.Context, resGrpName string, opts ...*ListOpts) (ResourceGroup, error) { |
| 163 |
var resGrp ResourceGroup |
| 164 |
_, err := n.client.doGET(ctx, "/v1/resource-groups/"+resGrpName, &resGrp, opts...) |
| 165 |
return resGrp, err |
| 166 |
} |
| 167 |
|
| 168 |
// Create adds a new resource-group |
| 169 |
func (n *ResourceGroupService) Create(ctx context.Context, resGrp ResourceGroup) error { |
| 170 |
_, err := n.client.doPOST(ctx, "/v1/resource-groups", resGrp) |
| 171 |
return err |
| 172 |
} |
| 173 |
|
| 174 |
// Modify allows to modify a resource-group |
| 175 |
func (n *ResourceGroupService) Modify(ctx context.Context, resGrpName string, props ResourceGroupModify) error { |
| 176 |
_, err := n.client.doPUT(ctx, "/v1/resource-groups/"+resGrpName, props) |
| 177 |
return err |
| 178 |
} |
| 179 |
|
| 180 |
// Delete deletes a resource-group |
| 181 |
func (n *ResourceGroupService) Delete(ctx context.Context, resGrpName string) error { |
| 182 |
_, err := n.client.doDELETE(ctx, "/v1/resource-groups/"+resGrpName, nil) |
| 183 |
return err |
| 184 |
} |
| 185 |
|
| 186 |
// Spawn creates a new resource-definition and auto-deploys if configured to do so |
| 187 |
func (n *ResourceGroupService) Spawn(ctx context.Context, resGrpName string, resGrpSpwn ResourceGroupSpawn) error { |
| 188 |
_, err := n.client.doPOST(ctx, "/v1/resource-groups/"+resGrpName+"/spawn", resGrpSpwn) |
| 189 |
return err |
| 190 |
} |
| 191 |
|
| 192 |
// GetVolumeGroups lists all volume-groups for a resource-group |
| 193 |
func (n *ResourceGroupService) GetVolumeGroups(ctx context.Context, resGrpName string, opts ...*ListOpts) ([]VolumeGroup, error) { |
| 194 |
var volGrps []VolumeGroup |
| 195 |
_, err := n.client.doGET(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups", &volGrps, opts...) |
| 196 |
return volGrps, err |
| 197 |
} |
| 198 |
|
| 199 |
// GetVolumeGroup lists a volume-group for a resource-group |
| 200 |
func (n *ResourceGroupService) GetVolumeGroup(ctx context.Context, resGrpName string, volNr int, opts ...*ListOpts) (VolumeGroup, error) { |
| 201 |
var volGrp VolumeGroup |
| 202 |
_, err := n.client.doGET(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups/"+strconv.Itoa(volNr), &volGrp, opts...) |
| 203 |
return volGrp, err |
| 204 |
} |
| 205 |
|
| 206 |
// Create adds a new volume-group to a resource-group |
| 207 |
func (n *ResourceGroupService) CreateVolumeGroup(ctx context.Context, resGrpName string, volGrp VolumeGroup) error { |
| 208 |
_, err := n.client.doPOST(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups", volGrp) |
| 209 |
return err |
| 210 |
} |
| 211 |
|
| 212 |
// Modify allows to modify a volume-group of a resource-group |
| 213 |
func (n *ResourceGroupService) ModifyVolumeGroup(ctx context.Context, resGrpName string, volNr int, props VolumeGroupModify) error { |
| 214 |
_, err := n.client.doPUT(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups/"+strconv.Itoa(volNr), props) |
| 215 |
return err |
| 216 |
} |
| 217 |
|
| 218 |
func (n *ResourceGroupService) DeleteVolumeGroup(ctx context.Context, resGrpName string, volNr int) error { |
| 219 |
_, err := n.client.doDELETE(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups/"+strconv.Itoa(volNr), nil) |
| 220 |
return err |
| 221 |
} |
| 222 |
|
| 223 |
// GetPropsInfos gets meta information about the properties that can be set on |
| 224 |
// a resource group. |
| 225 |
func (n *ResourceGroupService) GetPropsInfos(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error) { |
| 226 |
var infos []PropsInfo |
| 227 |
_, err := n.client.doGET(ctx, "/v1/resource-groups/properties/info", &infos, opts...) |
| 228 |
return infos, err |
| 229 |
} |
| 230 |
|
| 231 |
// GetVolumeGroupPropsInfos gets meta information about the properties that can |
| 232 |
// be set on a resource group. |
| 233 |
func (n *ResourceGroupService) GetVolumeGroupPropsInfos(ctx context.Context, resGrpName string, opts ...*ListOpts) ([]PropsInfo, error) { |
| 234 |
var infos []PropsInfo |
| 235 |
_, err := n.client.doGET(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups/properties/info", &infos, opts...) |
| 236 |
return infos, err |
| 237 |
} |
| 238 |
|
| 239 |
// Adjust all resource-definitions (calls autoplace for) of the given resource-group |
| 240 |
func (n *ResourceGroupService) Adjust(ctx context.Context, resGrpName string, adjust ResourceGroupAdjust) error { |
| 241 |
_, err := n.client.doPOST(ctx, "/v1/resource-groups/"+resGrpName+"/adjust", adjust) |
| 242 |
return err |
| 243 |
} |
| 244 |
|
| 245 |
// AdjustAll adjusts all resource-definitions (calls autoplace) according to their associated resource group. |
| 246 |
func (n *ResourceGroupService) AdjustAll(ctx context.Context, adjust ResourceGroupAdjust) error { |
| 247 |
_, err := n.client.doPOST(ctx, "/v1/resource-groups/adjustall", adjust) |
| 248 |
return err |
| 249 |
} |
| 250 |
|
| 251 |
// QuerySizeInfo returns information about the space available in a resource group |
| 252 |
func (n *ResourceGroupService) QuerySizeInfo(ctx context.Context, resGrpName string, req QuerySizeInfoRequest) (QuerySizeInfoResponse, error) { |
| 253 |
var resp QuerySizeInfoResponse |
| 254 |
httpReq, err := n.client.newRequest(http.MethodPost, "/v1/resource-groups/"+resGrpName+"/query-size-info", req) |
| 255 |
if err != nil { |
| 256 |
return resp, err |
| 257 |
} |
| 258 |
_, err = n.client.doJSON(ctx, httpReq, &resp) |
| 259 |
return resp, err |
| 260 |
} |