-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathregion.py
More file actions
159 lines (126 loc) · 3.76 KB
/
region.py
File metadata and controls
159 lines (126 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
"""SingleStoreDB Cluster Management."""
from typing import Dict
from typing import Optional
from .manager import Manager
from .utils import NamedList
from .utils import vars_to_str
class Region(object):
"""
Cluster region information.
This object is not directly instantiated. It is used in results
of ``WorkspaceManager`` API calls.
See Also
--------
:attr:`WorkspaceManager.regions`
"""
def __init__(self, id: str, name: str, provider: str):
"""Use :attr:`WorkspaceManager.regions` instead."""
#: Unique ID of the region
self.id = id
#: Name of the region
self.name = name
#: Name of the cloud provider
self.provider = provider
self._manager: Optional[Manager] = None
def __str__(self) -> str:
"""Return string representation."""
return vars_to_str(self)
def __repr__(self) -> str:
"""Return string representation."""
return str(self)
@classmethod
def from_dict(cls, obj: Dict[str, str], manager: Manager) -> 'Region':
"""
Convert dictionary to a ``Region`` object.
Parameters
----------
obj : dict
Key-value pairs to retrieve region information from
manager : WorkspaceManager, optional
The WorkspaceManager the Region belongs to
Returns
-------
:class:`Region`
"""
out = cls(
id=obj['regionID'],
name=obj['region'],
provider=obj['provider'],
)
out._manager = manager
return out
class RegionManager(Manager):
"""
SingleStoreDB region manager.
This class should be instantiated using :func:`singlestoredb.manage_regions`.
Parameters
----------
access_token : str, optional
The API key or other access token for the workspace management API
version : str, optional
Version of the API to use
base_url : str, optional
Base URL of the workspace management API
See Also
--------
:func:`singlestoredb.manage_regions`
"""
#: Object type
obj_type = 'region'
def list_regions(self) -> NamedList[Region]:
"""
List all available regions.
Returns
-------
NamedList[Region]
List of available regions
Raises
------
ManagementError
If there is an error getting the regions
"""
res = self._get('regions')
return NamedList(
[Region.from_dict(item, self) for item in res.json()],
)
def list_shared_tier_regions(self) -> NamedList[Region]:
"""
List regions that support shared tier workspaces.
Returns
-------
NamedList[Region]
List of regions that support shared tier workspaces
Raises
------
ManagementError
If there is an error getting the regions
"""
res = self._get('regions/sharedtier')
return NamedList(
[Region.from_dict(item, self) for item in res.json()],
)
def manage_regions(
access_token: Optional[str] = None,
version: Optional[str] = None,
base_url: Optional[str] = None,
) -> RegionManager:
"""
Retrieve a SingleStoreDB region manager.
Parameters
----------
access_token : str, optional
The API key or other access token for the workspace management API
version : str, optional
Version of the API to use (default: 'v1')
base_url : str, optional
Base URL of the workspace management API
Returns
-------
:class:`RegionManager`
"""
return RegionManager(
access_token=access_token,
version=version,
base_url=base_url,
)