1
# Copyright 2012 VMware, Inc.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
16
from oslo.config import cfg
18
from neutron.i18n import _LI
19
from neutron.openstack.common import log as logging
20
from neutron.plugins.vmware.common import exceptions
22
LOG = logging.getLogger(__name__)
24
# Raise if one of those attributes is not specified
25
REQUIRED_ATTRIBUTES = ['default_tz_uuid', 'nsx_user',
26
'nsx_password', 'nsx_controllers']
27
# Emit a INFO log if one of those attributes is not specified
28
IMPORTANT_ATTRIBUTES = ['default_l3_gw_service_uuid']
29
# Deprecated attributes
30
DEPRECATED_ATTRIBUTES = ['metadata_dhcp_host_route',
31
'nvp_user', 'nvp_password', 'nvp_controllers']
34
class NSXCluster(object):
37
Encapsulates controller connections and the API client for a NSX cluster.
39
Controller-specific parameters, such as timeouts are stored in the
40
elements of the controllers attribute, which are dicts.
43
def __init__(self, **kwargs):
44
self._required_attributes = REQUIRED_ATTRIBUTES[:]
45
self._important_attributes = IMPORTANT_ATTRIBUTES[:]
46
self._deprecated_attributes = {}
47
self._sanity_check(kwargs)
49
for opt, val in self._deprecated_attributes.iteritems():
50
LOG.deprecated(_("Attribute '%s' has been deprecated or moved "
51
"to a new section. See new configuration file "
53
depr_func = getattr(self, '_process_%s' % opt, None)
57
# If everything went according to plan these two lists should be empty
58
if self._required_attributes:
59
raise exceptions.InvalidClusterConfiguration(
60
invalid_attrs=self._required_attributes)
61
if self._important_attributes:
62
LOG.info(_LI("The following cluster attributes were "
63
"not specified: %s'"), self._important_attributes)
64
# The API client will be explicitly created by users of this class
65
self.api_client = None
67
def _sanity_check(self, options):
68
# Iterating this way ensures the conf parameters also
69
# define the structure of this class
71
if arg not in DEPRECATED_ATTRIBUTES:
72
setattr(self, arg, options.get(arg, cfg.CONF.get(arg)))
73
self._process_attribute(arg)
74
elif options.get(arg) is not None:
75
# Process deprecated attributes only if specified
76
self._deprecated_attributes[arg] = options.get(arg)
78
def _process_attribute(self, attribute):
79
# Process the attribute only if it's not empty!
80
if getattr(self, attribute, None):
81
if attribute in self._required_attributes:
82
self._required_attributes.remove(attribute)
83
if attribute in self._important_attributes:
84
self._important_attributes.remove(attribute)
85
handler_func = getattr(self, '_process_%s' % attribute, None)
89
def _process_nsx_controllers(self):
90
# If this raises something is not right, so let it bubble up
91
# TODO(salvatore-orlando): Also validate attribute here
92
for i, ctrl in enumerate(self.nsx_controllers or []):
93
if len(ctrl.split(':')) == 1:
94
self.nsx_controllers[i] = '%s:%s' % (ctrl, DEFAULT_PORT)
96
def _process_nvp_controllers(self):
97
self.nsx_controllers = self.nvp_controllers
98
self._process_nsx_controllers()