Skip to content

Commit 2407a11

Browse files
committed
high-level API: add ResourceGroup object
This new class represents a LINSTOR resource group and makes it accessible in the high level API. It allows what one would expect, including creating, modifying, deleting resource groups and creating new Resource objects from a ResourceGroup. Important: This modifies the semantic of Resource.from_resource_group(). This allows it to follow the semantic used in the Resource object (as well as ResourceGroup). We don't have that many users already using resource groups. Overall this makes it a lot more consistent with the rest of the HL API.
1 parent 3399ccd commit 2407a11

File tree

5 files changed

+368
-16
lines changed

5 files changed

+368
-16
lines changed

doc/introduction.rst

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,40 @@ There are 2 error classes that will or can be thrown from a :py:class:`~.Linstor
3737

3838
Linstor error indicating a network/connection error.
3939

40+
Code Samples Using the High-Level ResourceGroup API
41+
---------------------------------------------------
4042

41-
Code Samples Using the High-Level Resource API
42-
----------------------------------------------
43-
44-
In this section we describe methods that are typically used by plugin developers.
43+
In this section we describe methods that are typically used by plugin developers. Using resource groups is the
44+
prefered way.
4545

4646
Create a resource N-times redundant
4747
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4848

49-
A code sample on how to create a resource "foo", with a size of 20MiB 3-times redundant.
50-
Usually that code is executed in a "create" call in a plugin.
49+
A code sample on how to create a resource "foo", with a size of 20MiB. This first creates (or reuses) a
50+
resource group named threeSSD, that uses a fast SSD pool and places resources 3-times redundant. Usually that
51+
code is executed in a "create" call in a plugin.
5152

5253
.. code-block:: python
5354
5455
import linstor
55-
foo = linstor.Resource('foo', uri='linstor://192.168.0.42') # by default uri is localhost
56-
foo.volumes[0] = linstor.Volume('20 MiB')
57-
foo.placement.redundancy = 3
58-
foo.autoplace()
56+
ssd_grp = linstor.ResourceGroup('threeSSD', uri='linstor://192.168.0.42') # by default uri is localhost
57+
ssd_grp.redundancy = 3 # only if used for the first time
58+
ssd_grp.storage_pool = 'myssdpool' # only if used for the first time
59+
foo = ssd_grp.create_resource('foo', ['20 MiB'])
60+
61+
Code Samples Using the High-Level Resource API
62+
----------------------------------------------
63+
64+
In this section we describe methods that are typically used by plugin developers after a resource is created
65+
from a resource group, or if a resource already exists.
5966

6067
Resizing an existing resource/volume
6168
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6269

6370
.. code-block:: python
6471
6572
import linstor
66-
foo = linstor.Resource('foo')
73+
foo = linstor.Resource('foo') # or from a .create_resource() of a resource group
6774
foo.volumes[0].size = linstor.Volume('30 MiB')
6875
# resize again
6976
foo.volumes[0].size += 10 * 1024 * 1024

doc/linstor.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ linstor.resource module
1919
:undoc-members:
2020
:show-inheritance:
2121

22+
linstor.resourcegroup module
23+
----------------------------
24+
25+
.. automodule:: linstor.resourcegroup
26+
:members:
27+
:undoc-members:
28+
:show-inheritance:
29+
2230
linstor.kv module
2331
-------------------------
2432

linstor/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .linstorapi import Linstor, MultiLinstor
55
from .resource import Resource, Volume
66
from .kv import KV
7+
from .resourcegroup import ResourceGroup
78
from .config import Config
89
from .responses import StoragePoolDriver
910
from .linstorapi import ResourceData

linstor/resource.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,18 @@ def to_unicode(cls, t):
3636
class _Client(object):
3737
def __init__(self, uris, timeout=300, keep_alive=False):
3838
# external properties
39-
self.uri_list = linstor.MultiLinstor.controller_uri_list(uris) # type: list[str]
39+
self._uri_list = linstor.MultiLinstor.controller_uri_list(uris) # type: list[str]
4040
self.timeout = timeout
4141
self.keep_alive = keep_alive
4242

43+
@property
44+
def uri_list(self):
45+
return self._uri_list
46+
47+
@uri_list.setter
48+
def uri_list(self, uri_list):
49+
raise linstor.LinstorReadOnlyAfterSetError()
50+
4351

4452
class _Placement(object):
4553
def __init__(self, redundancy=2):
@@ -232,18 +240,21 @@ def __repr__(self):
232240
return "Resource({n}, {h})".format(n=self, h=self.client.uri_list)
233241

234242
@classmethod
235-
def from_resource_group(cls, linstor_controllers, resource_group_name, resource_name, vlm_sizes):
243+
def from_resource_group(cls, uri, resource_group_name, resource_name, vlm_sizes,
244+
timeout=300, keep_alive=False):
236245
"""
237246
Spawns a new resource definition from the given resource group.
238247
239-
:param str linstor_controllers: string of possible linstor controllers, feeded to .controller_uri_list
248+
:param str uri: A list of controller addresses.
249+
e.g: ``linstor://localhost,10.0.0.2``, ``linstor+ssl://localhost,linstor://192.168.0.1``
240250
:param str resource_group_name: Name of the resource group
241251
:param str resource_name: Name of the new resource definition
242252
:param list[str] vlm_sizes: String list of volume sizes e.g. ['128Mib', '1G']
243253
:return: Resource object of the newly created resource definition
244254
:rtype: Resource
245255
"""
246-
with linstor.MultiLinstor(linstor.MultiLinstor.controller_uri_list(linstor_controllers)) as lin:
256+
c = _Client(uri)
257+
with linstor.MultiLinstor(c.uri_list, timeout, keep_alive) as lin:
247258
result = lin.resource_group_spawn(
248259
resource_group_name,
249260
resource_name,
@@ -258,7 +269,7 @@ def from_resource_group(cls, linstor_controllers, resource_group_name, resource_
258269
)
259270
)
260271

261-
return Resource(resource_name, uri=linstor_controllers)
272+
return Resource(resource_name, uri=uri)
262273
return None
263274

264275
def _set_properties(self):

0 commit comments

Comments
 (0)