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 275
|
# Copyright 2011 OpenStack Foundation
# 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 webob.exc
from trove.common import cfg
from trove.common import exception
from trove.common import pagination
from trove.common import utils
from trove.common import wsgi
from trove.extensions.mysql.common import populate_validated_databases
from trove.extensions.mysql.common import populate_users
from trove.instance import models, views
from trove.datastore import models as datastore_models
from trove.backup.models import Backup as backup_model
from trove.backup import views as backup_views
from trove.openstack.common import log as logging
from trove.openstack.common import strutils
from trove.openstack.common.gettextutils import _
import trove.common.apischema as apischema
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
class InstanceController(wsgi.Controller):
"""Controller for instance functionality"""
schemas = apischema.instance.copy()
if not CONF.trove_volume_support:
# see instance.models.create for further validation around this
LOG.info("Removing volume attributes from schema")
schemas['create']['properties']['instance']['required'].pop()
@classmethod
def get_action_schema(cls, body, action_schema):
action_type = body.keys()[0]
action_schema = action_schema.get(action_type, {})
if action_type == 'resize':
# volume or flavorRef
resize_action = body[action_type].keys()[0]
action_schema = action_schema.get(resize_action, {})
return action_schema
@classmethod
def get_schema(cls, action, body):
action_schema = super(InstanceController, cls).get_schema(action, body)
if action == 'action':
# resize or restart
action_schema = cls.get_action_schema(body, action_schema)
return action_schema
def action(self, req, body, tenant_id, id):
"""
Handles requests that modify existing instances in some manner. Actions
could include 'resize', 'restart', 'reset_password'
:param req: http request object
:param body: deserialized body of the request as a dict
:param tenant_id: the tenant id for whom owns the instance
:param id: ???
"""
LOG.info("req : '%s'\n\n" % req)
LOG.info("Comitting an ACTION again instance %s for tenant '%s'"
% (id, tenant_id))
if not body:
raise exception.BadRequest(_("Invalid request body."))
context = req.environ[wsgi.CONTEXT_KEY]
instance = models.Instance.load(context, id)
_actions = {
'restart': self._action_restart,
'resize': self._action_resize,
'reset_password': self._action_reset_password
}
selected_action = None
for key in body:
if key in _actions:
selected_action = _actions[key]
return selected_action(instance, body)
def _action_restart(self, instance, body):
instance.restart()
return wsgi.Result(None, 202)
def _action_resize(self, instance, body):
"""
Handles 2 cases
1. resize volume
body only contains {volume: {size: x}}
2. resize instance
body only contains {flavorRef: http.../2}
If the body has both we will throw back an error.
"""
options = {
'volume': self._action_resize_volume,
'flavorRef': self._action_resize_flavor
}
selected_option = None
args = None
for key in options:
if key in body['resize']:
selected_option = options[key]
args = body['resize'][key]
break
return selected_option(instance, args)
def _action_resize_volume(self, instance, volume):
instance.resize_volume(volume['size'])
return wsgi.Result(None, 202)
def _action_resize_flavor(self, instance, flavorRef):
new_flavor_id = utils.get_id_from_href(flavorRef)
instance.resize_flavor(new_flavor_id)
return wsgi.Result(None, 202)
def _action_reset_password(self, instance, body):
raise webob.exc.HTTPNotImplemented()
def index(self, req, tenant_id):
"""Return all instances."""
LOG.info(_("req : '%s'\n\n") % req)
LOG.info(_("Indexing a database instance for tenant '%s'") % tenant_id)
context = req.environ[wsgi.CONTEXT_KEY]
servers, marker = models.Instances.load(context)
view = views.InstancesView(servers, req=req)
paged = pagination.SimplePaginatedDataView(req.url, 'instances', view,
marker)
return wsgi.Result(paged.data(), 200)
def backups(self, req, tenant_id, id):
"""Return all backups for the specified instance."""
LOG.info(_("req : '%s'\n\n") % req)
LOG.info(_("Indexing backups for instance '%s'") %
id)
context = req.environ[wsgi.CONTEXT_KEY]
backups, marker = backup_model.list_for_instance(context, id)
view = backup_views.BackupViews(backups)
paged = pagination.SimplePaginatedDataView(req.url, 'backups', view,
marker)
return wsgi.Result(paged.data(), 200)
def show(self, req, tenant_id, id):
"""Return a single instance."""
LOG.info(_("req : '%s'\n\n") % req)
LOG.info(_("Showing a database instance for tenant '%s'") % tenant_id)
LOG.info(_("id : '%s'\n\n") % id)
context = req.environ[wsgi.CONTEXT_KEY]
server = models.load_instance_with_guest(models.DetailInstance,
context, id)
return wsgi.Result(views.InstanceDetailView(server,
req=req).data(), 200)
def delete(self, req, tenant_id, id):
"""Delete a single instance."""
LOG.info(_("req : '%s'\n\n") % req)
LOG.info(_("Deleting a database instance for tenant '%s'") % tenant_id)
LOG.info(_("id : '%s'\n\n") % id)
# TODO(hub-cap): turn this into middleware
context = req.environ[wsgi.CONTEXT_KEY]
instance = models.load_any_instance(context, id)
instance.delete()
# TODO(cp16net): need to set the return code correctly
return wsgi.Result(None, 202)
def create(self, req, body, tenant_id):
# TODO(hub-cap): turn this into middleware
LOG.info(_("Creating a database instance for tenant '%s'") % tenant_id)
LOG.info(strutils.mask_password(_("req : '%s'\n\n") % req))
LOG.info(strutils.mask_password(_("body : '%s'\n\n") % body))
context = req.environ[wsgi.CONTEXT_KEY]
datastore_args = body['instance'].get('datastore', {})
datastore, datastore_version = (
datastore_models.get_datastore_version(**datastore_args))
image_id = datastore_version.image_id
name = body['instance']['name']
flavor_ref = body['instance']['flavorRef']
flavor_id = utils.get_id_from_href(flavor_ref)
configuration = self._configuration_parse(context, body)
databases = populate_validated_databases(
body['instance'].get('databases', []))
database_names = [database.get('_name', '') for database in databases]
users = None
try:
users = populate_users(body['instance'].get('users', []),
database_names)
except ValueError as ve:
raise exception.BadRequest(msg=ve)
if 'volume' in body['instance']:
volume_size = int(body['instance']['volume']['size'])
else:
volume_size = None
if 'restorePoint' in body['instance']:
backupRef = body['instance']['restorePoint']['backupRef']
backup_id = utils.get_id_from_href(backupRef)
else:
backup_id = None
if 'availability_zone' in body['instance']:
availability_zone = body['instance']['availability_zone']
else:
availability_zone = None
if 'nics' in body['instance']:
nics = body['instance']['nics']
else:
nics = None
instance = models.Instance.create(context, name, flavor_id,
image_id, databases, users,
datastore, datastore_version,
volume_size, backup_id,
availability_zone, nics,
configuration)
view = views.InstanceDetailView(instance, req=req)
return wsgi.Result(view.data(), 200)
def _configuration_parse(self, context, body):
if 'configuration' in body['instance']:
configuration_ref = body['instance']['configuration']
if configuration_ref:
configuration_id = utils.get_id_from_href(configuration_ref)
return configuration_id
def update(self, req, id, body, tenant_id):
"""Updates the instance to attach/detach configuration."""
LOG.info(_("Updating instance for tenant id %s") % tenant_id)
LOG.info(_("req: %s") % req)
LOG.info(_("body: %s") % body)
context = req.environ[wsgi.CONTEXT_KEY]
instance = models.Instance.load(context, id)
# if configuration is set, then we will update the instance to use
# the new configuration. If configuration is empty, we want to
# disassociate the instance from the configuration group and remove the
# active overrides file.
configuration_id = self._configuration_parse(context, body)
if configuration_id:
instance.assign_configuration(configuration_id)
else:
instance.unassign_configuration()
return wsgi.Result(None, 202)
def configuration(self, req, tenant_id, id):
"""
Returns the default configuration template applied to the instance.
"""
LOG.debug("getting default configuration for the instance(%s)" % id)
context = req.environ[wsgi.CONTEXT_KEY]
instance = models.Instance.load(context, id)
LOG.debug("server: %s" % instance)
config = instance.get_default_configration_template()
LOG.debug("default config for instance is: %s" % config)
return wsgi.Result(views.DefaultConfigurationView(
config).data(), 200)
|