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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# Copyright [2015] Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import abc
from oslo_config.cfg import NoSuchOptError
from oslo_log import log as logging
from oslo_utils import importutils
import six
from trove.cluster import models as cluster_models
from trove.cluster.models import DBCluster
from trove.common import cfg
from trove.common import exception
from trove.common.i18n import _
from trove.common import policy
from trove.common import wsgi
from trove.datastore import models as datastore_models
from trove.extensions.common import models
from trove.extensions.common import views
from trove.instance import models as instance_models
from trove.instance.models import DBInstance
LOG = logging.getLogger(__name__)
import_class = importutils.import_class
CONF = cfg.CONF
class ExtensionController(wsgi.Controller):
@classmethod
def authorize_target_action(cls, context, target_rule_name,
target_id, is_cluster=False):
target = None
if is_cluster:
target = cluster_models.Cluster.load(context, target_id)
else:
target = instance_models.Instance.load(context, target_id)
if not target:
if is_cluster:
raise exception.ClusterNotFound(cluster=target_id)
raise exception.InstanceNotFound(instance=target_id)
target_type = 'cluster' if is_cluster else 'instance'
policy.authorize_on_target(
context, '%s:extension:%s' % (target_type, target_rule_name),
{'tenant': target.tenant_id})
@six.add_metaclass(abc.ABCMeta)
class BaseDatastoreRootController(ExtensionController):
"""Base class that defines the contract for root controllers."""
@abc.abstractmethod
def root_index(self, req, tenant_id, instance_id, is_cluster):
pass
@abc.abstractmethod
def root_create(self, req, body, tenant_id, instance_id, is_cluster):
pass
@abc.abstractmethod
def root_delete(self, req, tenant_id, instance_id, is_cluster):
pass
@staticmethod
def _get_password_from_body(body=None):
if body:
return body['password'] if 'password' in body else None
return None
class DefaultRootController(BaseDatastoreRootController):
def root_index(self, req, tenant_id, instance_id, is_cluster):
"""Returns True if root is enabled; False otherwise."""
if is_cluster:
raise exception.ClusterOperationNotSupported(
operation='show_root')
LOG.info("Getting root enabled for instance '%s'.", instance_id)
LOG.info("req : '%s'\n\n", req)
context = req.environ[wsgi.CONTEXT_KEY]
is_root_enabled = models.Root.load(context, instance_id)
return wsgi.Result(views.RootEnabledView(is_root_enabled).data(), 200)
def root_create(self, req, body, tenant_id, instance_id, is_cluster):
if is_cluster:
raise exception.ClusterOperationNotSupported(
operation='enable_root')
LOG.info("Enabling root for instance '%s'.", instance_id)
LOG.info("req : '%s'\n\n", req)
context = req.environ[wsgi.CONTEXT_KEY]
password = DefaultRootController._get_password_from_body(body)
root = models.Root.create(context, instance_id, password)
return wsgi.Result(views.RootCreatedView(root).data(), 200)
def root_delete(self, req, tenant_id, instance_id, is_cluster):
if is_cluster:
raise exception.ClusterOperationNotSupported(
operation='disable_root')
LOG.info("Disabling root for instance '%s'.", instance_id)
LOG.info("req : '%s'\n\n", req)
context = req.environ[wsgi.CONTEXT_KEY]
is_root_enabled = models.Root.load(context, instance_id)
if not is_root_enabled:
raise exception.RootHistoryNotFound()
models.Root.delete(context, instance_id)
return wsgi.Result(None, 204)
class ClusterRootController(DefaultRootController):
def root_index(self, req, tenant_id, instance_id, is_cluster):
"""Returns True if root is enabled; False otherwise."""
if is_cluster:
return self.cluster_root_index(req, tenant_id, instance_id)
else:
return self.instance_root_index(req, tenant_id, instance_id)
def instance_root_index(self, req, tenant_id, instance_id):
LOG.info("Getting root enabled for instance '%s'.", instance_id)
LOG.info("req : '%s'\n\n", req)
context = req.environ[wsgi.CONTEXT_KEY]
try:
is_root_enabled = models.ClusterRoot.load(context, instance_id)
except exception.UnprocessableEntity:
raise exception.UnprocessableEntity(
_("Cluster %s is not ready.") % instance_id)
return wsgi.Result(views.RootEnabledView(is_root_enabled).data(), 200)
def cluster_root_index(self, req, tenant_id, cluster_id):
LOG.info("Getting root enabled for cluster '%s'.", cluster_id)
single_instance_id, cluster_instances = self._get_cluster_instance_id(
tenant_id, cluster_id)
return self.instance_root_index(req, tenant_id, single_instance_id)
def _block_cluster_instance_actions(self):
return False
def check_cluster_instance_actions(self, instance_id):
# Check if instance is in a cluster and if actions are allowed
instance = DBInstance.find_by(id=instance_id)
if instance.cluster_id and self._block_cluster_instance_actions():
raise exception.ClusterInstanceOperationNotSupported()
def root_create(self, req, body, tenant_id, instance_id, is_cluster):
if is_cluster:
return self.cluster_root_create(req, body, tenant_id, instance_id)
else:
self.check_cluster_instance_actions(instance_id)
return self.instance_root_create(req, body, instance_id)
def instance_root_create(self, req, body, instance_id,
cluster_instances=None):
LOG.info("Enabling root for instance '%s'.", instance_id)
LOG.info("req : '%s'\n\n", req)
context = req.environ[wsgi.CONTEXT_KEY]
password = ClusterRootController._get_password_from_body(body)
root = models.ClusterRoot.create(context, instance_id,
password, cluster_instances)
return wsgi.Result(views.RootCreatedView(root).data(), 200)
def cluster_root_create(self, req, body, tenant_id, cluster_id):
LOG.info("Enabling root for cluster '%s'.", cluster_id)
single_instance_id, cluster_instances = self._get_cluster_instance_id(
tenant_id, cluster_id)
return self.instance_root_create(req, body, single_instance_id,
cluster_instances)
def _find_cluster_node_ids(self, tenant_id, cluster_id):
args = {'tenant_id': tenant_id,
'cluster_id': cluster_id,
'deleted': False}
cluster_instances = DBInstance.find_all(**args).all()
return [db_instance.id for db_instance in cluster_instances]
def _get_cluster_instance_id(self, tenant_id, cluster_id):
instance_ids = self._find_cluster_node_ids(tenant_id, cluster_id)
single_instance_id = instance_ids[0]
return single_instance_id, instance_ids
class RootController(ExtensionController):
"""Controller for instance functionality."""
def index(self, req, tenant_id, instance_id):
"""Returns True if root is enabled; False otherwise."""
datastore_manager, is_cluster = self._get_datastore(tenant_id,
instance_id)
context = req.environ[wsgi.CONTEXT_KEY]
self.authorize_target_action(context, 'root:index', instance_id,
is_cluster=is_cluster)
root_controller = self.load_root_controller(datastore_manager)
return root_controller.root_index(req, tenant_id, instance_id,
is_cluster)
def create(self, req, tenant_id, instance_id, body=None):
"""Enable the root user for the db instance."""
datastore_manager, is_cluster = self._get_datastore(tenant_id,
instance_id)
context = req.environ[wsgi.CONTEXT_KEY]
self.authorize_target_action(context, 'root:create', instance_id,
is_cluster=is_cluster)
root_controller = self.load_root_controller(datastore_manager)
if root_controller is not None:
return root_controller.root_create(req, body, tenant_id,
instance_id, is_cluster)
else:
opt = 'root_controller'
raise NoSuchOptError(opt, group='datastore_manager')
def delete(self, req, tenant_id, instance_id):
datastore_manager, is_cluster = self._get_datastore(tenant_id,
instance_id)
context = req.environ[wsgi.CONTEXT_KEY]
self.authorize_target_action(context, 'root:delete', instance_id,
is_cluster=is_cluster)
root_controller = self.load_root_controller(datastore_manager)
if root_controller is not None:
return root_controller.root_delete(req, tenant_id,
instance_id, is_cluster)
else:
opt = 'root_controller'
raise NoSuchOptError(opt, group='datastore_manager')
def _get_datastore(self, tenant_id, instance_or_cluster_id):
"""
Returns datastore manager and a boolean
showing if instance_or_cluster_id is a cluster id
"""
args = {'id': instance_or_cluster_id, 'tenant_id': tenant_id}
is_cluster = False
try:
db_info = DBInstance.find_by(**args)
except exception.ModelNotFoundError:
is_cluster = True
db_info = DBCluster.find_by(**args)
ds_version = (datastore_models.DatastoreVersion.
load_by_uuid(db_info.datastore_version_id))
ds_manager = ds_version.manager
return (ds_manager, is_cluster)
def load_root_controller(self, manager):
try:
clazz = CONF.get(manager).get('root_controller')
LOG.debug("Loading Root Controller class %s.", clazz)
if not clazz:
raise exception.DatastoreOperationNotSupported(
datastore=manager, operation='root')
root_controller = import_class(clazz)
return root_controller()
except NoSuchOptError:
LOG.warning(
f"root_controller not configured for datastore {manager}")
raise exception.DatastoreOperationNotSupported(
datastore=manager, operation='root')
|