summaryrefslogtreecommitdiff
path: root/nova/objects/cell_mapping.py
blob: 595ec43e480e3cedf8813640f2fb2e5e0ccb3540 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#    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.

from urllib import parse as urlparse

from oslo_log import log as logging
from oslo_utils import versionutils
from sqlalchemy import sql
from sqlalchemy.sql import expression

import nova.conf
from nova.db.api import api as api_db_api
from nova.db.api import models as api_db_models
from nova import exception
from nova.objects import base
from nova.objects import fields

CONF = nova.conf.CONF
LOG = logging.getLogger(__name__)


def _parse_netloc(netloc):
    """Parse a user:pass@host:port and return a dict suitable for formatting
    a cell mapping template.
    """
    these = {
        'username': None,
        'password': None,
        'hostname': None,
        'port': None,
    }

    if '@' in netloc:
        userpass, hostport = netloc.split('@', 1)
    else:
        hostport = netloc
        userpass = ''

    if hostport.startswith('['):
        host_end = hostport.find(']')
        if host_end < 0:
            raise ValueError('Invalid IPv6 URL')
        these['hostname'] = hostport[1:host_end]
        these['port'] = hostport[host_end + 1:]
    elif ':' in hostport:
        these['hostname'], these['port'] = hostport.split(':', 1)
    else:
        these['hostname'] = hostport

    if ':' in userpass:
        these['username'], these['password'] = userpass.split(':', 1)
    else:
        these['username'] = userpass

    return these


@base.NovaObjectRegistry.register
class CellMapping(base.NovaTimestampObject, base.NovaObject):
    # Version 1.0: Initial version
    # Version 1.1: Added disabled field
    VERSION = '1.1'

    CELL0_UUID = '00000000-0000-0000-0000-000000000000'

    fields = {
        'id': fields.IntegerField(read_only=True),
        'uuid': fields.UUIDField(),
        'name': fields.StringField(nullable=True),
        'transport_url': fields.StringField(),
        'database_connection': fields.StringField(),
        'disabled': fields.BooleanField(default=False),
    }

    def obj_make_compatible(self, primitive, target_version):
        super(CellMapping, self).obj_make_compatible(primitive, target_version)
        target_version = versionutils.convert_version_to_tuple(target_version)
        if target_version < (1, 1):
            if 'disabled' in primitive:
                del primitive['disabled']

    @property
    def identity(self):
        if 'name' in self and self.name:
            return '%s(%s)' % (self.uuid, self.name)
        else:
            return self.uuid

    @staticmethod
    def _format_url(url, default):
        default_url = urlparse.urlparse(default)

        subs = {
            'username': default_url.username,
            'password': default_url.password,
            'hostname': default_url.hostname,
            'port': default_url.port,
            'scheme': default_url.scheme,
            'query': default_url.query,
            'fragment': default_url.fragment,
            'path': default_url.path.lstrip('/'),
        }

        # NOTE(danms): oslo.messaging has an extended format for the URL
        # which we need to support:
        #   scheme://user:pass@host:port[,user1:pass@host1:port, ...]/path
        # Encode these values, if they exist, as indexed keys like
        # username1, password1, hostname1, port1.
        if ',' in default_url.netloc:
            netlocs = default_url.netloc.split(',')
            index = 0
            for netloc in netlocs:
                index += 1
                these = _parse_netloc(netloc)
                for key in these:
                    subs['%s%i' % (key, index)] = these[key]

        return url.format(**subs)

    @staticmethod
    def format_db_url(url):
        if CONF.database.connection is None:
            if '{' in url:
                LOG.error('Cell mapping database_connection is a template, '
                          'but [database]/connection is not set')
            return url
        try:
            return CellMapping._format_url(url, CONF.database.connection)
        except Exception:
            LOG.exception('Failed to parse [database]/connection to '
                          'format cell mapping')
            return url

    @staticmethod
    def format_mq_url(url):
        if CONF.transport_url is None:
            if '{' in url:
                LOG.error('Cell mapping transport_url is a template, but '
                          '[DEFAULT]/transport_url is not set')
            return url
        try:
            return CellMapping._format_url(url, CONF.transport_url)
        except Exception:
            LOG.exception('Failed to parse [DEFAULT]/transport_url to '
                          'format cell mapping')
            return url

    @staticmethod
    def _from_db_object(context, cell_mapping, db_cell_mapping):
        for key in cell_mapping.fields:
            val = db_cell_mapping[key]
            if key == 'database_connection':
                val = cell_mapping.format_db_url(val)
            elif key == 'transport_url':
                val = cell_mapping.format_mq_url(val)
            setattr(cell_mapping, key, val)
        cell_mapping.obj_reset_changes()
        cell_mapping._context = context
        return cell_mapping

    @staticmethod
    @api_db_api.context_manager.reader
    def _get_by_uuid_from_db(context, uuid):

        db_mapping = context.session\
            .query(api_db_models.CellMapping).filter_by(uuid=uuid).first()
        if not db_mapping:
            raise exception.CellMappingNotFound(uuid=uuid)

        return db_mapping

    @base.remotable_classmethod
    def get_by_uuid(cls, context, uuid):
        db_mapping = cls._get_by_uuid_from_db(context, uuid)

        return cls._from_db_object(context, cls(), db_mapping)

    @staticmethod
    @api_db_api.context_manager.writer
    def _create_in_db(context, updates):

        db_mapping = api_db_models.CellMapping()
        db_mapping.update(updates)
        db_mapping.save(context.session)
        return db_mapping

    @base.remotable
    def create(self):
        db_mapping = self._create_in_db(self._context, self.obj_get_changes())
        self._from_db_object(self._context, self, db_mapping)

    @staticmethod
    @api_db_api.context_manager.writer
    def _save_in_db(context, uuid, updates):

        db_mapping = context.session.query(
                api_db_models.CellMapping).filter_by(uuid=uuid).first()
        if not db_mapping:
            raise exception.CellMappingNotFound(uuid=uuid)

        db_mapping.update(updates)
        context.session.add(db_mapping)
        return db_mapping

    @base.remotable
    def save(self):
        changes = self.obj_get_changes()
        db_mapping = self._save_in_db(self._context, self.uuid, changes)
        self._from_db_object(self._context, self, db_mapping)
        self.obj_reset_changes()

    @staticmethod
    @api_db_api.context_manager.writer
    def _destroy_in_db(context, uuid):

        result = context.session.query(api_db_models.CellMapping).filter_by(
                uuid=uuid).delete()
        if not result:
            raise exception.CellMappingNotFound(uuid=uuid)

    @base.remotable
    def destroy(self):
        self._destroy_in_db(self._context, self.uuid)

    def is_cell0(self):
        return self.obj_attr_is_set('uuid') and self.uuid == self.CELL0_UUID


@base.NovaObjectRegistry.register
class CellMappingList(base.ObjectListBase, base.NovaObject):
    # Version 1.0: Initial version
    # Version 1.1: Add get_by_disabled()
    VERSION = '1.1'

    fields = {
        'objects': fields.ListOfObjectsField('CellMapping'),
    }

    @staticmethod
    @api_db_api.context_manager.reader
    def _get_all_from_db(context):
        return context.session.query(api_db_models.CellMapping).order_by(
            expression.asc(api_db_models.CellMapping.id)).all()

    @base.remotable_classmethod
    def get_all(cls, context):
        db_mappings = cls._get_all_from_db(context)
        return base.obj_make_list(context, cls(), CellMapping, db_mappings)

    @staticmethod
    @api_db_api.context_manager.reader
    def _get_by_disabled_from_db(context, disabled):
        if disabled:
            return context.session.query(api_db_models.CellMapping)\
                .filter_by(disabled=sql.true())\
                .order_by(expression.asc(api_db_models.CellMapping.id)).all()
        else:
            return context.session.query(api_db_models.CellMapping)\
                .filter_by(disabled=sql.false())\
                .order_by(expression.asc(api_db_models.CellMapping.id)).all()

    @base.remotable_classmethod
    def get_by_disabled(cls, context, disabled):
        db_mappings = cls._get_by_disabled_from_db(context, disabled)
        return base.obj_make_list(context, cls(), CellMapping, db_mappings)

    @staticmethod
    @api_db_api.context_manager.reader
    def _get_by_project_id_from_db(context, project_id):
        # SELECT DISTINCT cell_id FROM instance_mappings \
        #   WHERE project_id = $project_id;
        cell_ids = context.session.query(
            api_db_models.InstanceMapping.cell_id).filter_by(
            project_id=project_id).distinct().subquery()
        # SELECT cell_mappings WHERE cell_id IN ($cell_ids);
        return context.session.query(api_db_models.CellMapping).filter(
            api_db_models.CellMapping.id.in_(cell_ids)).all()

    @classmethod
    def get_by_project_id(cls, context, project_id):
        """Return a list of CellMapping objects which correspond to cells in
        which project_id has InstanceMappings.
        """
        db_mappings = cls._get_by_project_id_from_db(context, project_id)
        return base.obj_make_list(context, cls(), CellMapping, db_mappings)