-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuckets.py
More file actions
142 lines (119 loc) · 5.23 KB
/
buckets.py
File metadata and controls
142 lines (119 loc) · 5.23 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
import logging
from ..common import union
from ..exceptions import CTERAException, ObjectNotFoundException
from .base_command import BaseCommand
from . import query
logger = logging.getLogger('cterasdk.core')
class Buckets(BaseCommand):
"""
Portal Storage Node APIs
"""
default = ['name']
def _get_entire_object(self, name):
ref = f'/locations/{name}'
try:
return self._core.api.get(ref)
except CTERAException as error:
raise CTERAException(f'Bucket not found: {ref}') from error
def _get_tenant_base_object_ref(self, name):
return self._core.portals.get(name, include=['baseObjectRef']).baseObjectRef
def get(self, name, include=None):
"""
Get a Bucket
:param str name: Name of the bucket
:param list[str] include: List of fields to retrieve, defaults to ``['name']``
"""
include = union(include or [], Buckets.default)
include = ['/' + attr for attr in include]
bucket = self._core.api.get_multi('/locations/' + name, include)
if bucket.name is None:
raise ObjectNotFoundException(f'/locations/{name}')
return bucket
def add(self, name, bucket, read_only=False, dedicated_to=None, direct=None):
"""
Add a Bucket
:param str name: Name of the bucket
:param cterasdk.core.types.Bucket bucket: Storage bucket to add
:param bool,optional read_only: Set bucket to read-delete only, defaults to False
:param str,optional dedicated_to: Name of a tenant, defaults to ``None``
:param bool,optional direct: Enable CTERA Direct IO
"""
param = bucket.to_server_object()
param.name = name
param.readOnly = read_only
param.dedicated = bool(dedicated_to)
param.dedicatedPortal = self._get_tenant_base_object_ref(dedicated_to) if dedicated_to else None
if direct is not None:
param.directUpload = direct
logger.info('Adding %s bucket: %s', bucket.__class__.__name__, name)
response = self._core.api.add('/locations', param)
logger.info('Bucket added: %s', name)
return response
def modify(self, current_name, new_name=None, read_only=None, dedicated_to=None, verify_ssl=None, direct=None):
"""
Modify a Bucket
:param str current_name: The current bucket name
:param str,optional new_name: New name
:param bool,optional read_only: Set bucket to read-delete only
:param bool,optional dedicated: Dedicate bucket to a tenant
:param bool,optional verify_ssl: ``False`` to trust all certificate, ``True`` to verify.
:param str,optional dedicated_to: Tenant name
:param bool,optional direct: Set CTERA Direct IO
"""
param = self._get_entire_object(current_name)
if new_name:
param.name = new_name
if read_only is not None:
param.readOnly = read_only
if dedicated_to is not None:
if isinstance(dedicated_to, bool):
if not dedicated_to:
param.dedicated = False
param.dedicatedPortal = None
else:
raise ValueError("'dedicated_to' must be either False or a 'str'")
elif isinstance(dedicated_to, str):
param.dedicated = True
param.dedicatedPortal = self._get_tenant_base_object_ref(dedicated_to) if dedicated_to else None
if verify_ssl is not None:
param.trustAllCertificates = not verify_ssl
if direct is not None:
param.directUpload = direct
logger.info("Modifying bucket: %s", current_name)
response = self._core.api.put(f'/locations/{current_name}', param)
logger.info("Bucket modified: %s", current_name)
return response
def list_buckets(self, include=None):
"""
List Buckets.
Restricted to the Global Administration Portal. Browse it using :py:func:`cterasdk.core.portals.browse_global_admin`.
:param list[str],optional include: List of fields to retrieve, defaults to ``['name']``
"""
include = union(include or [], Buckets.default)
param = query.QueryParamBuilder().include(include).build()
return query.iterator(self._core, '/locations', param)
def delete(self, name):
"""
Delete a Bucket
:param str name: Name of the bucket
"""
logger.info('Deleting bucket. %s', {'name': name})
response = self._core.api.delete(f'/locations/{name}')
logger.info('Bucket deleted. %s', {'name': name})
return response
def read_write(self, name):
"""
Set bucket to Read Write
:param str name: Name of the bucket
"""
logger.info('Setting bucket to read-write. %s', {'name': name})
return self._read_only(name, False)
def read_only(self, name):
"""
Set bucket to Read Only
:param str name: Name of the bucket
"""
logger.info('Setting bucket to read-delete only. %s', {'name': name})
return self._read_only(name, True)
def _read_only(self, name, enabled):
return self._core.api.put(f'/locations/{name}/readOnly', enabled)