...

Source file src/github.com/LINBIT/golinstor/client/resourcegroup.go

Documentation: github.com/LINBIT/golinstor/client

     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 <roland.kammerer@linbit.com>
     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  	"strconv"
    23  )
    24  
    25  type ResourceGroup struct {
    26  	Name        string `json:"name,omitempty"`
    27  	Description string `json:"description,omitempty"`
    28  	// A string to string property map.
    29  	Props        map[string]string `json:"props,omitempty"`
    30  	SelectFilter AutoSelectFilter  `json:"select_filter,omitempty"`
    31  	// unique object id
    32  	Uuid string `json:"uuid,omitempty"`
    33  }
    34  
    35  type ResourceGroupModify struct {
    36  	Description string `json:"description,omitempty"`
    37  	// A string to string property map.
    38  	OverrideProps    map[string]string `json:"override_props,omitempty"`
    39  	DeleteProps      []string          `json:"delete_props,omitempty"`
    40  	DeleteNamespaces []string          `json:"delete_namespaces,omitempty"`
    41  	SelectFilter     AutoSelectFilter  `json:"select_filter,omitempty"`
    42  }
    43  
    44  type ResourceGroupSpawn struct {
    45  	// name of the resulting resource-definition
    46  	ResourceDefinitionName string `json:"resource_definition_name"`
    47  	// 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.
    48  	ResourceDefinitionExternalName string `json:"resource_definition_external_name,omitempty"`
    49  	// sizes (in kib) of the resulting volume-definitions
    50  	VolumeSizes  []int64          `json:"volume_sizes,omitempty"`
    51  	SelectFilter AutoSelectFilter `json:"select_filter,omitempty"`
    52  	// 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.
    53  	Partial bool `json:"partial,omitempty"`
    54  	// 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.
    55  	DefinitionsOnly bool `json:"definitions_only,omitempty"`
    56  }
    57  
    58  type ResourceGroupAdjust struct {
    59  	SelectFilter *AutoSelectFilter `json:"select_filter,omitempty"`
    60  }
    61  
    62  type VolumeGroup struct {
    63  	VolumeNumber int32 `json:"volume_number,omitempty"`
    64  	// A string to string property map.
    65  	Props map[string]string `json:"props,omitempty"`
    66  	// unique object id
    67  	Uuid  string   `json:"uuid,omitempty"`
    68  	Flags []string `json:"flags,omitempty"`
    69  }
    70  
    71  type VolumeGroupModify struct {
    72  	// A string to string property map.
    73  	OverrideProps map[string]string `json:"override_props,omitempty"`
    74  	// To add a flag just specify the flag name, to remove a flag prepend it with a '-'.  Flags:   * GROSS_SIZE
    75  	Flags            []string `json:"flags,omitempty"`
    76  	DeleteProps      []string `json:"delete_props,omitempty"`
    77  	DeleteNamespaces []string `json:"delete_namespaces,omitempty"`
    78  }
    79  
    80  // custom code
    81  
    82  // ResourceGroupProvider acts as an abstraction for a
    83  // ResourceGroupService. It can be swapped out for another
    84  // ResourceGroupService implementation, for example for testing.
    85  type ResourceGroupProvider interface {
    86  	// GetAll lists all resource-groups
    87  	GetAll(ctx context.Context, opts ...*ListOpts) ([]ResourceGroup, error)
    88  	// Get return information about a resource-defintion
    89  	Get(ctx context.Context, resGrpName string, opts ...*ListOpts) (ResourceGroup, error)
    90  	// Create adds a new resource-group
    91  	Create(ctx context.Context, resGrp ResourceGroup) error
    92  	// Modify allows to modify a resource-group
    93  	Modify(ctx context.Context, resGrpName string, props ResourceGroupModify) error
    94  	// Delete deletes a resource-group
    95  	Delete(ctx context.Context, resGrpName string) error
    96  	// Spawn creates a new resource-definition and auto-deploys if configured to do so
    97  	Spawn(ctx context.Context, resGrpName string, resGrpSpwn ResourceGroupSpawn) error
    98  	// GetVolumeGroups lists all volume-groups for a resource-group
    99  	GetVolumeGroups(ctx context.Context, resGrpName string, opts ...*ListOpts) ([]VolumeGroup, error)
   100  	// GetVolumeGroup lists a volume-group for a resource-group
   101  	GetVolumeGroup(ctx context.Context, resGrpName string, volNr int, opts ...*ListOpts) (VolumeGroup, error)
   102  	// Create adds a new volume-group to a resource-group
   103  	CreateVolumeGroup(ctx context.Context, resGrpName string, volGrp VolumeGroup) error
   104  	// Modify allows to modify a volume-group of a resource-group
   105  	ModifyVolumeGroup(ctx context.Context, resGrpName string, volNr int, props VolumeGroupModify) error
   106  	DeleteVolumeGroup(ctx context.Context, resGrpName string, volNr int) error
   107  	// GetPropsInfos gets meta information about the properties that can be
   108  	// set on a resource group.
   109  	GetPropsInfos(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error)
   110  	// GetVolumeGroupPropsInfos gets meta information about the properties
   111  	// that can be set on a resource group.
   112  	GetVolumeGroupPropsInfos(ctx context.Context, resGrpName string, opts ...*ListOpts) ([]PropsInfo, error)
   113  	// Adjust all resource-definitions (calls autoplace for) of the given resource-group
   114  	Adjust(ctx context.Context, resGrpName string, adjust ResourceGroupAdjust) error
   115  	// AdjustAll adjusts all resource-definitions (calls autoplace) according to their associated resource group.
   116  	AdjustAll(ctx context.Context, adjust ResourceGroupAdjust) error
   117  }
   118  
   119  var _ ResourceGroupProvider = &ResourceGroupService{}
   120  
   121  // ResourceGroupService is the service that deals with resource group related tasks.
   122  type ResourceGroupService struct {
   123  	client *Client
   124  }
   125  
   126  // GetAll lists all resource-groups
   127  func (n *ResourceGroupService) GetAll(ctx context.Context, opts ...*ListOpts) ([]ResourceGroup, error) {
   128  	var resGrps []ResourceGroup
   129  	_, err := n.client.doGET(ctx, "/v1/resource-groups", &resGrps, opts...)
   130  	return resGrps, err
   131  }
   132  
   133  // Get return information about a resource-defintion
   134  func (n *ResourceGroupService) Get(ctx context.Context, resGrpName string, opts ...*ListOpts) (ResourceGroup, error) {
   135  	var resGrp ResourceGroup
   136  	_, err := n.client.doGET(ctx, "/v1/resource-groups/"+resGrpName, &resGrp, opts...)
   137  	return resGrp, err
   138  }
   139  
   140  // Create adds a new resource-group
   141  func (n *ResourceGroupService) Create(ctx context.Context, resGrp ResourceGroup) error {
   142  	_, err := n.client.doPOST(ctx, "/v1/resource-groups", resGrp)
   143  	return err
   144  }
   145  
   146  // Modify allows to modify a resource-group
   147  func (n *ResourceGroupService) Modify(ctx context.Context, resGrpName string, props ResourceGroupModify) error {
   148  	_, err := n.client.doPUT(ctx, "/v1/resource-groups/"+resGrpName, props)
   149  	return err
   150  }
   151  
   152  // Delete deletes a resource-group
   153  func (n *ResourceGroupService) Delete(ctx context.Context, resGrpName string) error {
   154  	_, err := n.client.doDELETE(ctx, "/v1/resource-groups/"+resGrpName, nil)
   155  	return err
   156  }
   157  
   158  // Spawn creates a new resource-definition and auto-deploys if configured to do so
   159  func (n *ResourceGroupService) Spawn(ctx context.Context, resGrpName string, resGrpSpwn ResourceGroupSpawn) error {
   160  	_, err := n.client.doPOST(ctx, "/v1/resource-groups/"+resGrpName+"/spawn", resGrpSpwn)
   161  	return err
   162  }
   163  
   164  // GetVolumeGroups lists all volume-groups for a resource-group
   165  func (n *ResourceGroupService) GetVolumeGroups(ctx context.Context, resGrpName string, opts ...*ListOpts) ([]VolumeGroup, error) {
   166  	var volGrps []VolumeGroup
   167  	_, err := n.client.doGET(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups", &volGrps, opts...)
   168  	return volGrps, err
   169  }
   170  
   171  // GetVolumeGroup lists a volume-group for a resource-group
   172  func (n *ResourceGroupService) GetVolumeGroup(ctx context.Context, resGrpName string, volNr int, opts ...*ListOpts) (VolumeGroup, error) {
   173  	var volGrp VolumeGroup
   174  	_, err := n.client.doGET(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups/"+strconv.Itoa(volNr), &volGrp, opts...)
   175  	return volGrp, err
   176  }
   177  
   178  // Create adds a new volume-group to a resource-group
   179  func (n *ResourceGroupService) CreateVolumeGroup(ctx context.Context, resGrpName string, volGrp VolumeGroup) error {
   180  	_, err := n.client.doPOST(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups", volGrp)
   181  	return err
   182  }
   183  
   184  // Modify allows to modify a volume-group of a resource-group
   185  func (n *ResourceGroupService) ModifyVolumeGroup(ctx context.Context, resGrpName string, volNr int, props VolumeGroupModify) error {
   186  	_, err := n.client.doPUT(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups/"+strconv.Itoa(volNr), props)
   187  	return err
   188  }
   189  
   190  func (n *ResourceGroupService) DeleteVolumeGroup(ctx context.Context, resGrpName string, volNr int) error {
   191  	_, err := n.client.doDELETE(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups/"+strconv.Itoa(volNr), nil)
   192  	return err
   193  }
   194  
   195  // GetPropsInfos gets meta information about the properties that can be set on
   196  // a resource group.
   197  func (n *ResourceGroupService) GetPropsInfos(ctx context.Context, opts ...*ListOpts) ([]PropsInfo, error) {
   198  	var infos []PropsInfo
   199  	_, err := n.client.doGET(ctx, "/v1/resource-groups/properties/info", &infos, opts...)
   200  	return infos, err
   201  }
   202  
   203  // GetVolumeGroupPropsInfos gets meta information about the properties that can
   204  // be set on a resource group.
   205  func (n *ResourceGroupService) GetVolumeGroupPropsInfos(ctx context.Context, resGrpName string, opts ...*ListOpts) ([]PropsInfo, error) {
   206  	var infos []PropsInfo
   207  	_, err := n.client.doGET(ctx, "/v1/resource-groups/"+resGrpName+"/volume-groups/properties/info", &infos, opts...)
   208  	return infos, err
   209  }
   210  
   211  // Adjust all resource-definitions (calls autoplace for) of the given resource-group
   212  func (n *ResourceGroupService) Adjust(ctx context.Context, resGrpName string, adjust ResourceGroupAdjust) error {
   213  	_, err := n.client.doPOST(ctx, "/v1/resource-groups/"+resGrpName+"/adjust", adjust)
   214  	return err
   215  }
   216  
   217  // AdjustAll adjusts all resource-definitions (calls autoplace) according to their associated resource group.
   218  func (n *ResourceGroupService) AdjustAll(ctx context.Context, adjust ResourceGroupAdjust) error {
   219  	_, err := n.client.doPOST(ctx, "/v1/resource-groups/adjustall", adjust)
   220  	return err
   221  }
   222  

View as plain text