Merge "Rework nova modules and states"
diff --git a/_modules/novav21/__init__.py b/_modules/novav21/__init__.py
new file mode 100644
index 0000000..8ea591c
--- /dev/null
+++ b/_modules/novav21/__init__.py
@@ -0,0 +1,75 @@
+# 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.
+
+try:
+ import os_client_config
+ REQUIREMENTS_MET = True
+except ImportError:
+ REQUIREMENTS_MET = False
+
+__virtualname__ = 'novav21'
+
+import aggregates
+import flavors
+import keypairs
+import quotas
+import servers
+
+aggregate_add_host = aggregates.add_host
+aggregate_create = aggregates.create
+aggregate_delete = aggregates.delete
+aggregate_get = aggregates.get
+aggregate_list = aggregates.list_
+aggregate_remove_host = aggregates.remove_host
+aggregate_set_metadata = aggregates.set_metadata
+flavor_add_extra_specs = flavors.add_extra_specs
+flavor_create = flavors.create
+flavor_delete = flavors.delete
+flavor_delete_extra_spec = flavors.delete_extra_spec
+flavor_get = flavors.get
+flavor_get_extra_specs = flavors.get_extra_specs
+flavor_list = flavors.list_
+keypair_create = keypairs.create
+keypair_delete = keypairs.delete
+keypair_get = keypairs.get
+keypair_list = keypairs.list_
+quota_delete = quotas.delete
+quota_list = quotas.list_
+quota_update = quotas.update
+server_create = servers.create
+server_delete = servers.delete
+server_get = servers.get
+server_list = servers.list_
+server_lock = servers.lock
+server_resume = servers.resume
+server_suspend = servers.suspend
+server_unlock = servers.unlock
+
+
+__all__ = (
+ 'aggregate_add_host', 'aggregate_create', 'aggregate_delete',
+ 'aggregate_get', 'aggregate_list', 'aggregate_remove_host',
+ 'aggregate_set_metadata', 'flavor_add_extra_specs', 'flavor_create',
+ 'flavor_delete', 'flavor_delete_extra_spec', 'flavor_get',
+ 'flavor_get_extra_specs', 'flavor_list', 'keypair_create',
+ 'keypair_delete', 'keypair_get', 'keypair_list', 'quota_delete',
+ 'quota_list', 'quota_update', 'server_create', 'server_delete',
+ 'server_get', 'server_list', 'server_lock', 'server_resume',
+ 'server_suspend', 'server_unlock')
+
+
+def __virtual__():
+ if REQUIREMENTS_MET:
+ return __virtualname__
+ else:
+ return False, ("The novav21 execution module cannot be loaded: "
+ "os_client_config package not found.")
diff --git a/_modules/novav21/aggregates.py b/_modules/novav21/aggregates.py
new file mode 100644
index 0000000..9f61f2d
--- /dev/null
+++ b/_modules/novav21/aggregates.py
@@ -0,0 +1,80 @@
+# 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 common
+
+# Function alias to not shadow built-ins
+__func_alias__ = {
+ 'list_': 'list'
+}
+
+
+@common.function_descriptor('find', 'Host aggregate', 'aggregates')
+@common.send('get')
+def list_(**kwargs):
+ """List host aggregates"""
+ url = '/os-aggregates'
+ return url, {}
+
+
+@common.function_descriptor('update', 'Host aggregate', 'aggregate')
+@common.get_by_name_or_uuid(list_, 'aggregates')
+@common.send('post')
+def add_host(aggregate_id, host, **kwargs):
+ """Add host to a host aggregate"""
+ url = '/os-aggregates/%s/action' % aggregate_id
+ return url, {'json': {'add_host': {'host': host}}}
+
+
+@common.function_descriptor('create', 'Host aggregate', 'aggregate')
+@common.send('post')
+def create(name, availability_zone, **kwargs):
+ """Create a host aggregate"""
+ url = '/os-aggregates'
+ req = {'name': name, 'availability_zone': availability_zone}
+ return url, {'json': {'aggregate': req}}
+
+
+@common.function_descriptor('delete', 'Host aggregate')
+@common.get_by_name_or_uuid(list_, 'aggregates')
+@common.send('delete')
+def delete(aggregate_id, **kwargs):
+ """Delete a host aggregate"""
+ url = '/os-aggregates/%s' % aggregate_id
+ return url, {}
+
+
+@common.function_descriptor('find', 'Host aggregate', 'aggregate')
+@common.get_by_name_or_uuid(list_, 'aggregates')
+@common.send('get')
+def get(aggregate_id, **kwargs):
+ """Get a host aggregate"""
+ url = '/os-aggregates/%s' % aggregate_id
+ return url, {}
+
+
+@common.function_descriptor('update', 'Host aggregate', 'aggregate')
+@common.get_by_name_or_uuid(list_, 'aggregates')
+@common.send('post')
+def remove_host(aggregate_id, host, **kwargs):
+ """Remove host from a host aggregate"""
+ url = '/os-aggregates/%s/action' % aggregate_id
+ return url, {'json': {'remove_host': {'host': host}}}
+
+
+@common.function_descriptor('update', 'Host aggregate', 'aggregate')
+@common.get_by_name_or_uuid(list_, 'aggregates')
+@common.send('post')
+def set_metadata(aggregate_id, **kwargs):
+ """Set host aggregate metadata"""
+ url = '/os-aggregates/%s/action' % aggregate_id
+ return url, {'json': {'set_metadata': {'metadata': kwargs}}}
diff --git a/_modules/novav21/common.py b/_modules/novav21/common.py
new file mode 100644
index 0000000..391eab4
--- /dev/null
+++ b/_modules/novav21/common.py
@@ -0,0 +1,129 @@
+# 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 six
+import logging
+import uuid
+
+import os_client_config
+from salt import exceptions
+
+
+log = logging.getLogger(__name__)
+
+SERVICE_KEY = 'compute'
+
+
+def get_raw_client(cloud_name):
+ config = os_client_config.OpenStackConfig()
+ cloud = config.get_one_cloud(cloud_name)
+ adapter = cloud.get_session_client(SERVICE_KEY)
+ adapter.version = '2.1'
+ endpoints = []
+ try:
+ access_info = adapter.session.auth.get_access(adapter.session)
+ endpoints = access_info.service_catalog.get_endpoints()
+ except (AttributeError, ValueError) as exc:
+ six.raise_from(exc, exceptions.SaltInvocationError(
+ "Cannot load keystoneauth plugin. Please check your environment "
+ "configuration."))
+ if SERVICE_KEY not in endpoints:
+ raise exceptions.SaltInvocationError("Cannot find compute endpoint in "
+ "environment endpoint list.")
+ return adapter
+
+
+def send(method):
+ def wrap(func):
+ @six.wraps(func)
+ def wrapped_f(*args, **kwargs):
+ cloud_name = kwargs.pop('cloud_name', None)
+ if not cloud_name:
+ raise exceptions.SaltInvocationError(
+ "No cloud_name specified. Please provide cloud_name "
+ "parameter")
+ adapter = get_raw_client(cloud_name)
+ kwarg_keys = list(kwargs.keys())
+ for k in kwarg_keys:
+ if k.startswith('__'):
+ kwargs.pop(k)
+ url, request_kwargs = func(*args, **kwargs)
+ try:
+ response = getattr(adapter, method.lower())(url,
+ **request_kwargs)
+ except Exception as e:
+ log.exception("Error occurred when executing request")
+ return {"result": False,
+ "comment": six.text_type(e),
+ "status_code": getattr(e, "http_status", 500)}
+ return {"result": True,
+ "body": response.json() if response.content else {},
+ "status_code": response.status_code}
+ return wrapped_f
+ return wrap
+
+
+def _check_uuid(val):
+ try:
+ return str(uuid.UUID(val)) == val
+ except (TypeError, ValueError, AttributeError):
+ return False
+
+
+def get_by_name_or_uuid(resource_list, resp_key):
+ def wrap(func):
+ @six.wraps(func)
+ def wrapped_f(*args, **kwargs):
+ if 'name' in kwargs:
+ ref = kwargs.pop('name', None)
+ start_arg = 0
+ else:
+ start_arg = 1
+ ref = args[0]
+ item_id = None
+ if _check_uuid(ref):
+ item_id = ref
+ else:
+ cloud_name = kwargs['cloud_name']
+ resp = resource_list(cloud_name=cloud_name)["body"][resp_key]
+ for item in resp:
+ if item["name"] == ref:
+ if item_id is not None:
+ return {
+ "name": ref,
+ "changes": {},
+ "result": False,
+ "comment": "Multiple resources ({resource}) "
+ "with requested name found ".format(
+ resource=resp_key)}
+ item_id = item["id"]
+ if not item_id:
+ return {
+ "name": ref,
+ "changes": {},
+ "result": False,
+ "comment": "Resource ({resource}) "
+ "with requested name not found ".format(
+ resource=resp_key)}
+ return func(item_id, *args[start_arg:], **kwargs)
+ return wrapped_f
+ return wrap
+
+
+def function_descriptor(action_type, resource_human_readable_name,
+ body_response_key=None):
+ def decorator(fun):
+ fun._action_type = action_type
+ fun._body_response_key = body_response_key or ''
+ fun._resource_human_readable_name = resource_human_readable_name
+ return fun
+ return decorator
diff --git a/_modules/novav21/flavors.py b/_modules/novav21/flavors.py
new file mode 100644
index 0000000..53cd7a4
--- /dev/null
+++ b/_modules/novav21/flavors.py
@@ -0,0 +1,81 @@
+# 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 common
+
+# Function alias to not shadow built-ins
+__func_alias__ = {
+ 'list_': 'list'
+}
+
+
+@common.function_descriptor('update', 'Flavor extra specs', 'extra_specs')
+@common.send('post')
+def add_extra_specs(flavor_id, **kwargs):
+ # NOTE: flavor_id can be any string, don't convert flavor name to uuid
+ url = '/flavors/{flavor_id}/os-extra_specs'.format(flavor_id=flavor_id)
+ return url, {'json': {"extra_specs": kwargs}}
+
+
+@common.function_descriptor('create', 'Flavor', 'flavor')
+@common.send('post')
+def create(name, vcpus, ram, disk, **kwargs):
+ """Create flavor(s)."""
+ url = '/flavors'
+ req = {'flavor': {'name': name, 'vcpus': vcpus, 'ram': ram, 'disk': disk}}
+ req['flavor'].update(kwargs)
+ return url, {'json': req}
+
+
+@common.function_descriptor('delete', 'Flavor')
+@common.send('delete')
+def delete(flavor_id, **kwargs):
+ """Delete flavor."""
+ # NOTE: flavor_id can be any string, don't convert flavor name to uuid
+ url = '/flavors/{flavor_id}'.format(flavor_id=flavor_id)
+ return url, {}
+
+
+@common.function_descriptor('update', 'Flavor extra specs')
+@common.send('delete')
+def delete_extra_spec(flavor_id, key_name, **kwargs):
+ # NOTE: flavor_id can be any string, don't convert flavor name to uuid
+ url = '/flavors/{flavor_id}/os-extra_specs/{key_name}'.format(
+ flavor_id=flavor_id, key_name=key_name)
+ return url, {}
+
+
+@common.function_descriptor('find', 'Flavor', 'flavor')
+@common.send('get')
+def get(flavor_id, **kwargs):
+ """Return one flavor."""
+ # NOTE: flavor_id can be any string, don't convert flavor name to uuid
+ url = '/flavors/{flavor_id}'.format(flavor_id=flavor_id)
+ return url, {}
+
+
+@common.function_descriptor('find', 'Flavor extra specs', 'extra_specs')
+@common.send('get')
+def get_extra_specs(flavor_id, **kwargs):
+ # NOTE: flavor_id can be any string, don't convert flavor name to uuid
+ url = '/flavors/{flavor_id}/os-extra_specs'.format(flavor_id=flavor_id)
+ return url, {}
+
+
+@common.function_descriptor('find', 'Flavor', 'flavors')
+@common.send('get')
+def list_(detail=False, **kwargs):
+ """Return list of flavors."""
+ url = '/flavors'
+ if detail:
+ url = '%s/detail' % url
+ return url, {}
diff --git a/_modules/novav21/keypairs.py b/_modules/novav21/keypairs.py
new file mode 100644
index 0000000..1a1317b
--- /dev/null
+++ b/_modules/novav21/keypairs.py
@@ -0,0 +1,54 @@
+# 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 six.moves.urllib.parse as urllib_parse
+
+import common
+
+# Function alias to not shadow built-ins
+__func_alias__ = {
+ 'list_': 'list'
+}
+
+
+@common.function_descriptor('create', 'Keypair', 'keypair')
+@common.send('post')
+def create(name, public_key, **kwargs):
+ """Create a keypair"""
+ url = '/os-keypairs'
+ body = {'name': name, 'public_key': public_key}
+ body.update(kwargs)
+ return url, {'json': {'keypair': body}}
+
+
+@common.function_descriptor('delete', 'Keypair')
+@common.send('delete')
+def delete(name, **kwargs):
+ """Delete keypair"""
+ url = '/os-keypairs/{}?{}'.format(name, urllib_parse.urlencode(kwargs))
+ return url, {}
+
+
+@common.function_descriptor('find', 'Keypair', 'keypair')
+@common.send('get')
+def get(name, **kwargs):
+ """Get a keypair of a project (and user)"""
+ url = '/os-keypairs/{}?{}'.format(name, urllib_parse.urlencode(kwargs))
+ return url, {}
+
+
+@common.function_descriptor('find', 'Keypair', 'keypairs')
+@common.send('get')
+def list_(**kwargs):
+ """List keypairs of a project (and user)"""
+ url = '/os-keypairs?{}'.format(urllib_parse.urlencode(kwargs))
+ return url, {}
diff --git a/_modules/novav21/quotas.py b/_modules/novav21/quotas.py
new file mode 100644
index 0000000..3b303f2
--- /dev/null
+++ b/_modules/novav21/quotas.py
@@ -0,0 +1,48 @@
+# 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 common
+
+# Function alias to not shadow built-ins
+__func_alias__ = {
+ 'list_': 'list'
+}
+
+
+@common.function_descriptor('delete', 'Project quota')
+@common.send('get')
+def delete(project_id, user_id=None, **kwargs):
+ """List quotas of a project (and user)"""
+ url = '/os-quota-sets/%s' % project_id
+ if user_id:
+ url = '%s?user_id=%s' % (url, user_id)
+ return url, {}
+
+
+@common.function_descriptor('find', 'Project quota', 'quota_set')
+@common.send('get')
+def list_(project_id, user_id=None, **kwargs):
+ """List quotas of a project (and user)"""
+ url = '/os-quota-sets/%s' % project_id
+ if user_id:
+ url = '%s?user_id=%s' % (url, user_id)
+ return url, {}
+
+
+@common.function_descriptor('update', 'Project quota', 'quota_set')
+@common.send('put')
+def update(project_id, user_id=None, **kwargs):
+ """Update quota of the specified project (and user)"""
+ url = '/os-quota-sets/%s' % project_id
+ if user_id:
+ url = '%s?user_id=%s' % (url, user_id)
+ return url, {'json': {'quota_set': kwargs}}
diff --git a/_modules/novav21/servers.py b/_modules/novav21/servers.py
new file mode 100644
index 0000000..6606c3e
--- /dev/null
+++ b/_modules/novav21/servers.py
@@ -0,0 +1,89 @@
+# 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 six.moves.urllib.parse as urllib_parse
+
+import common
+
+# Function alias to not shadow built-ins
+__func_alias__ = {
+ 'list_': 'list'
+}
+
+
+@common.send('get')
+def list_(**kwargs):
+ """Return list of servers."""
+ url = '/servers?{}'.format(urllib_parse.urlencode(kwargs))
+ return url, {}
+
+
+@common.send('post')
+def create(name, flavor, **kwargs):
+ """Create server(s)."""
+ # TODO: add something useful :)
+ url = '/servers'
+ req = {'server': {'name': name, 'flavor': flavor}}
+ req['server'].update(kwargs)
+ return url, {'json': req}
+
+
+@common.get_by_name_or_uuid(list_, 'servers')
+@common.send('delete')
+def delete(server_id, **kwargs):
+ """Delete server."""
+ url = '/servers/{server_id}'.format(server_id=server_id)
+ return url, {}
+
+
+@common.get_by_name_or_uuid(list_, 'servers')
+@common.send('get')
+def get(server_id, **kwargs):
+ """Return one server."""
+ url = '/servers/{server_id}'.format(server_id=server_id)
+ return url, {}
+
+
+@common.get_by_name_or_uuid(list_, 'servers')
+@common.send('post')
+def lock(server_id, **kwargs):
+ """Lock server."""
+ url = '/servers/{server_id}/action'.format(server_id=server_id)
+ req = {"lock": None}
+ return url, {"json": req}
+
+
+@common.get_by_name_or_uuid(list_, 'servers')
+@common.send('post')
+def resume(server_id, **kwargs):
+ """Resume server after suspend."""
+ url = '/servers/{server_id}/action'.format(server_id=server_id)
+ req = {"resume": None}
+ return url, {"json": req}
+
+
+@common.get_by_name_or_uuid(list_, 'servers')
+@common.send('post')
+def suspend(server_id, **kwargs):
+ """Suspend server."""
+ url = '/servers/{server_id}/action'.format(server_id=server_id)
+ req = {"suspend": None}
+ return url, {"json": req}
+
+
+@common.get_by_name_or_uuid(list_, 'servers')
+@common.send('post')
+def unlock(server_id, **kwargs):
+ """Unlock server."""
+ url = '/servers/{server_id}/action'.format(server_id=server_id)
+ req = {"unlock": None}
+ return url, {"json": req}
diff --git a/_states/novav21.py b/_states/novav21.py
new file mode 100644
index 0000000..1feefee
--- /dev/null
+++ b/_states/novav21.py
@@ -0,0 +1,489 @@
+# 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 logging
+import six
+from six.moves import zip_longest
+
+import salt
+
+LOG = logging.getLogger(__name__)
+
+KEYSTONE_LOADED = False
+
+
+def __virtual__():
+ """Only load if the nova module is in __salt__"""
+ if 'keystonev3.project_get_details' in __salt__:
+ global KEYSTONE_LOADED
+ KEYSTONE_LOADED = True
+ return 'novav21'
+
+
+class SaltModuleCallException(Exception):
+
+ def __init__(self, result_dict, *args, **kwargs):
+ super(SaltModuleCallException, self).__init__(*args, **kwargs)
+ self.result_dict = result_dict
+
+
+def _get_failure_function_mapping():
+ return {
+ 'create': _create_failed,
+ 'update': _update_failed,
+ 'find': _find_failed,
+ 'delete': _delete_failed,
+ }
+
+
+def _call_nova_salt_module(call_string, name, module_name='novav21'):
+ def inner(*args, **kwargs):
+ func = __salt__['%s.%s' % (module_name, call_string)]
+ result = func(*args, **kwargs)
+ if not result['result']:
+ ret = _get_failure_function_mapping()[func._action_type](
+ name, func._resource_human_readable_name)
+ ret['comment'] += '\nStatus code: %s\n%s' % (result['status_code'],
+ result['comment'])
+ raise SaltModuleCallException(ret)
+ return result['body'].get(func._body_response_key)
+ return inner
+
+
+def _error_handler(fun):
+ @six.wraps(fun)
+ def inner(*args, **kwargs):
+ try:
+ return fun(*args, **kwargs)
+ except SaltModuleCallException as e:
+ return e.result_dict
+ return inner
+
+
+@_error_handler
+def flavor_present(name, cloud_name, vcpus=1, ram=256, disk=0, flavor_id=None,
+ extra_specs=None):
+ """Ensures that the flavor exists"""
+ extra_specs = extra_specs or {}
+ # There is no way to query flavors by name
+ flavors = _call_nova_salt_module('flavor_list', name)(
+ detail=True, cloud_name=cloud_name)
+ flavor = [flavor for flavor in flavors if flavor['name'] == name]
+ # Flavor names are unique, there is either 1 or 0 with requested name
+ if flavor:
+ flavor = flavor[0]
+ current_extra_specs = _call_nova_salt_module(
+ 'flavor_get_extra_specs', name)(
+ flavor['id'], cloud_name=cloud_name)
+ to_delete = set(current_extra_specs) - set(extra_specs)
+ to_add = set(extra_specs) - set(current_extra_specs)
+ for spec in to_delete:
+ _call_nova_salt_module('flavor_delete_extra_spec', name)(
+ flavor['id'], spec, cloud_name=cloud_name)
+ _call_nova_salt_module('flavor_add_extra_specs', name)(
+ flavor['id'], cloud_name=cloud_name, **extra_specs)
+ if to_delete or to_add:
+ ret = _updated(name, 'Flavor', extra_specs)
+ else:
+ ret = _no_change(name, 'Flavor')
+ else:
+ flavor = _call_nova_salt_module('flavor_create', name)(
+ name, vcpus, ram, disk, id=flavor_id, cloud_name=cloud_name)
+ _call_nova_salt_module('flavor_add_extra_specs', name)(
+ flavor['id'], cloud_name=cloud_name, **extra_specs)
+ flavor['extra_specs'] = extra_specs
+ ret = _created(name, 'Flavor', flavor)
+ return ret
+
+
+@_error_handler
+def flavor_absent(name, cloud_name):
+ """Ensure flavor is absent"""
+ # There is no way to query flavors by name
+ flavors = _call_nova_salt_module('flavor_list', name)(
+ detail=True, cloud_name=cloud_name)
+ flavor = [flavor for flavor in flavors if flavor['name'] == name]
+ # Flavor names are unique, there is either 1 or 0 with requested name
+ if flavor:
+ _call_nova_salt_module('flavor_delete', name)(
+ flavor[0]['id'], cloud_name=cloud_name)
+ return _deleted(name, 'Flavor')
+ return _non_existent(name, 'Flavor')
+
+
+def _get_keystone_project_id_by_name(project_name, cloud_name):
+ if not KEYSTONE_LOADED:
+ LOG.error("Keystone module not found, can not look up project ID "
+ "by name")
+ return None
+ project = __salt__['keystonev3.project_get_details'](
+ project_name, cloud_name=cloud_name)
+ if not project:
+ return None
+ return project['project']['id']
+
+
+@_error_handler
+def quota_present(name, cloud_name, **kwargs):
+ """Ensures that the nova quota exists
+
+ :param name: project name to ensure quota for.
+ """
+ project_name = name
+ project_id = _get_keystone_project_id_by_name(project_name, cloud_name)
+ changes = {}
+ if not project_id:
+ ret = _update_failed(project_name, 'Project quota')
+ ret['comment'] += ('\nCould not retrieve keystone project %s' %
+ project_name)
+ return ret
+ quota = _call_nova_salt_module('quota_list', project_name)(
+ project_id, cloud_name=cloud_name)
+ for key, value in kwargs.items():
+ if quota.get(key) != value:
+ changes[key] = value
+ if changes:
+ _call_nova_salt_module('quota_update', project_name)(
+ project_id, cloud_name=cloud_name, **changes)
+ return _updated(project_name, 'Project quota', changes)
+ else:
+ return _no_change(project_name, 'Project quota')
+
+
+@_error_handler
+def quota_absent(name, cloud_name):
+ """Ensures that the nova quota set to default
+
+ :param name: project name to reset quota for.
+ """
+ project_name = name
+ project_id = _get_keystone_project_id_by_name(project_name, cloud_name)
+ if not project_id:
+ ret = _delete_failed(project_name, 'Project quota')
+ ret['comment'] += ('\nCould not retrieve keystone project %s' %
+ project_name)
+ return ret
+ _call_nova_salt_module('quota_delete', name)(
+ project_id, cloud_name=cloud_name)
+ return _deleted(name, 'Project quota')
+
+
+@_error_handler
+def aggregate_present(name, cloud_name, availability_zone_name=None,
+ hosts=None, metadata=None):
+ """Ensures that the nova aggregate exists"""
+ aggregates = _call_nova_salt_module('aggregate_list', name)(
+ cloud_name=cloud_name)
+ aggregate_exists = [agg for agg in aggregates
+ if agg['name'] == name]
+ metadata = metadata or {}
+ hosts = hosts or []
+ if availability_zone_name:
+ metadata.update(availability_zone=availability_zone_name)
+ if not aggregate_exists:
+ aggregate = _call_nova_salt_module('aggregate_create', name)(
+ name, availability_zone_name, cloud_name=cloud_name)
+ if metadata:
+ _call_nova_salt_module('aggregate_set_metadata', name)(
+ cloud_name=cloud_name, **metadata)
+ aggregate['metadata'] = metadata
+ for host in hosts or []:
+ _call_nova_salt_module('aggregate_add_host', name)(
+ name, host, cloud_name=cloud_name)
+ aggregate['hosts'] = hosts
+ return _created(name, 'Host aggregate', aggregate)
+ else:
+ aggregate = aggregate_exists[0]
+ changes = {}
+ existing_meta = set(aggregate['metadata'].items())
+ requested_meta = set(metadata.items())
+ if existing_meta - requested_meta or requested_meta - existing_meta:
+ _call_nova_salt_module('aggregate_set_metadata', name)(
+ name, cloud_name=cloud_name, **metadata)
+ changes['metadata'] = metadata
+ hosts_to_add = set(hosts) - set(aggregate['hosts'])
+ hosts_to_remove = set(aggregate['hosts']) - set(hosts)
+ if hosts_to_remove or hosts_to_add:
+ for host in hosts_to_add:
+ _call_nova_salt_module('aggregate_add_host', name)(
+ name, host, cloud_name=cloud_name)
+ for host in hosts_to_remove:
+ _call_nova_salt_module('aggregate_remove_host', name)(
+ name, host, cloud_name=cloud_name)
+ changes['hosts'] = hosts
+ if changes:
+ return _updated(name, 'Host aggregate', changes)
+ else:
+ return _no_change(name, 'Host aggregate')
+
+
+@_error_handler
+def aggregate_absent(name, cloud_name):
+ """Ensure aggregate is absent"""
+ existing_aggregates = _call_nova_salt_module('aggregate_list', name)(
+ cloud_name=cloud_name)
+ matching_aggs = [agg for agg in existing_aggregates
+ if agg['name'] == name]
+ if matching_aggs:
+ _call_nova_salt_module('aggregate_delete', name)(
+ name, cloud_name=cloud_name)
+ return _deleted(name, 'Host Aggregate')
+ return _non_existent(name, 'Host Aggregate')
+
+
+@_error_handler
+def keypair_present(name, cloud_name, public_key_file=None, public_key=None):
+ """Ensures that the Nova key-pair exists"""
+ existing_keypairs = _call_nova_salt_module('keypair_list', name)(
+ cloud_name=cloud_name)
+ matching_kps = [kp for kp in existing_keypairs
+ if kp['keypair']['name'] == name]
+ if public_key_file and not public_key:
+ with salt.utils.fopen(public_key_file, 'r') as f:
+ public_key = f.read()
+ if not public_key:
+ ret = _create_failed(name, 'Keypair')
+ ret['comment'] += '\nPlease specify public key for keypair creation.'
+ return ret
+ if matching_kps:
+ # Keypair names are unique, there is either 1 or 0 with requested name
+ kp = matching_kps[0]['keypair']
+ if kp['public_key'] != public_key:
+ _call_nova_salt_module('keypair_delete', name)(
+ name, cloud_name=cloud_name)
+ else:
+ return _no_change(name, 'Keypair')
+ res = _call_nova_salt_module('keypair_create', name)(
+ name, cloud_name=cloud_name, public_key=public_key)
+ return _created(name, 'Keypair', res)
+
+
+@_error_handler
+def keypair_absent(name, cloud_name):
+ """Ensure keypair is absent"""
+ existing_keypairs = _call_nova_salt_module('keypair_list', name)(
+ cloud_name=cloud_name)
+ matching_kps = [kp for kp in existing_keypairs
+ if kp['keypair']['name'] == name]
+ if matching_kps:
+ _call_nova_salt_module('keypair_delete', name)(
+ name, cloud_name=cloud_name)
+ return _deleted(name, 'Keypair')
+ return _non_existent(name, 'Keypair')
+
+
+def cell_present(name='cell1', transport_url='none:///', db_engine='mysql',
+ db_name='nova_upgrade', db_user='nova', db_password=None,
+ db_address='0.0.0.0'):
+ """Ensure nova cell is present
+
+ For newly created cells this state also runs discover_hosts and
+ map_instances."""
+ cell_info = __salt__['cmd.shell'](
+ "nova-manage cell_v2 list_cells --verbose | "
+ "awk '/%s/ {print $4,$6,$8}'" % name).split()
+ db_connection = (
+ '%(db_engine)s+pymysql://%(db_user)s:%(db_password)s@'
+ '%(db_address)s/%(db_name)s?charset=utf8' % {
+ 'db_engine': db_engine, 'db_user': db_user,
+ 'db_password': db_password, 'db_address': db_address,
+ 'db_name': db_name})
+ args = {'transport_url': transport_url, 'db_connection': db_connection}
+ # There should be at least 1 component printed to cell_info
+ if len(cell_info) >= 1:
+ cell_info = dict(zip_longest(
+ ('cell_uuid', 'existing_transport_url', 'existing_db_connection'),
+ cell_info))
+ cell_uuid, existing_transport_url, existing_db_connection = cell_info
+ command_string = ''
+ if existing_transport_url != transport_url:
+ command_string = (
+ '%s --transport-url %%(transport_url)s' % command_string)
+ if existing_db_connection != db_connection:
+ command_string = (
+ '%s --database_connection %%(db_connection)s' % command_string)
+ if not command_string:
+ return _no_change(name, 'Nova cell')
+ try:
+ __salt__['cmd.shell'](
+ ('nova-manage cell_v2 update_cell --cell_uuid %s %s' % (
+ cell_uuid, command_string)) % args)
+ LOG.warning("Updating the transport_url or database_connection "
+ "fields on a running system will NOT result in all "
+ "nodes immediately using the new values. Use caution "
+ "when changing these values.")
+ ret = _updated(name, 'Nova cell', args)
+ except Exception as e:
+ ret = _update_failed(name, 'Nova cell')
+ ret['comment'] += '\nException: %s' % e
+ return ret
+ args.update(name=name)
+ try:
+ cell_uuid = __salt__['cmd.shell'](
+ 'nova-manage cell_v2 create_cell --name %(name)s '
+ '--transport-url %(transport_url)s '
+ '--database_connection %(db_connection)s --verbose' % args)
+ __salt__['cmd.shell']('nova-manage cell_v2 discover_hosts '
+ '--cell_uuid %s --verbose' % cell_uuid)
+ __salt__['cmd.shell']('nova-manage cell_v2 map_instances '
+ '--cell_uuid %s' % cell_uuid)
+ ret = _created(name, 'Nova cell', args)
+ except Exception as e:
+ ret = _create_failed(name, 'Nova cell')
+ ret['comment'] += '\nException: %s' % e
+ return ret
+
+
+def cell_absent(name, force=False):
+ """Ensure cell is absent"""
+ cell_uuid = __salt__['cmd.shell'](
+ "nova-manage cell_v2 list_cells | awk '/%s/ {print $4}'" % name)
+ if not cell_uuid:
+ return _non_existent(name, 'Nova cell')
+ try:
+ __salt__['cmd.shell'](
+ 'nova-manage cell_v2 delete_cell --cell_uuid %s %s' % (
+ cell_uuid, '--force' if force else ''))
+ ret = _deleted(name, 'Nova cell')
+ except Exception as e:
+ ret = _delete_failed(name, 'Nova cell')
+ ret['comment'] += '\nException: %s' % e
+ return ret
+
+
+def _db_version_update(db, version, human_readable_resource_name):
+ existing_version = __salt__['cmd.shell'](
+ 'nova-manage %s version 2>/dev/null' % db)
+ try:
+ existing_version = int(existing_version)
+ version = int(version)
+ except Exception as e:
+ ret = _update_failed(existing_version,
+ human_readable_resource_name)
+ ret['comment'] += ('\nCan not convert existing or requested version '
+ 'to integer, exception: %s' % e)
+ LOG.error(ret['comment'])
+ return ret
+ if existing_version < version:
+ try:
+ __salt__['cmd.shell'](
+ 'nova-manage %s sync --version %s' % (db, version))
+ ret = _updated(existing_version, human_readable_resource_name,
+ {db: '%s sync --version %s' % (db, version)})
+ except Exception as e:
+ ret = _update_failed(existing_version,
+ human_readable_resource_name)
+ ret['comment'] += '\nException: %s' % e
+ return ret
+ return _no_change(existing_version, human_readable_resource_name)
+
+
+def api_db_version_present(name=None, version="20"):
+ """Ensures that specific api_db version is present"""
+ return _db_version_update('api_db', version, 'Nova API database version')
+
+
+def db_version_present(name=None, version="334"):
+ """Ensures that specific db version is present"""
+ return _db_version_update('db', version, 'Nova database version')
+
+
+def online_data_migrations_present(name=None, api_db_version="20",
+ db_version="334"):
+ """Runs online_data_migrations if databases are of specific versions"""
+ ret = {'name': 'online_data_migrations', 'changes': {}, 'result': False,
+ 'comment': 'Current nova api_db version != {0} or nova db version '
+ '!= {1}.'.format(api_db_version, db_version)}
+ cur_api_db_version = __salt__['cmd.shell'](
+ 'nova-manage api_db version 2>/dev/null')
+ cur_db_version = __salt__['cmd.shell'](
+ 'nova-manage db version 2>/dev/null')
+ try:
+ cur_api_db_version = int(cur_api_db_version)
+ cur_db_version = int(cur_db_version)
+ api_db_version = int(api_db_version)
+ db_version = int(db_version)
+ except Exception as e:
+ LOG.error(ret['comment'])
+ ret['comment'] = ('\nCan not convert existing or requested database '
+ 'versions to integer, exception: %s' % e)
+ return ret
+ if cur_api_db_version == api_db_version and cur_db_version == db_version:
+ try:
+ __salt__['cmd.shell']('nova-manage db online_data_migrations')
+ ret['result'] = True
+ ret['comment'] = ('nova-manage db online_data_migrations was '
+ 'executed successfuly')
+ ret['changes']['online_data_migrations'] = (
+ 'online_data_migrations run on nova api_db version {0} and '
+ 'nova db version {1}'.format(api_db_version, db_version))
+ except Exception as e:
+ ret['comment'] = (
+ 'Failed to execute online_data_migrations on nova api_db '
+ 'version %s and nova db version %s, exception: %s' % (
+ api_db_version, db_version, e))
+ return ret
+
+
+def _find_failed(name, resource):
+ return {
+ 'name': name, 'changes': {}, 'result': False,
+ 'comment': 'Failed to find {0}s with name {1}'.format(resource, name)}
+
+
+def _created(name, resource, changes):
+ return {
+ 'name': name, 'changes': changes, 'result': True,
+ 'comment': '{0} {1} created'.format(resource, name)}
+
+
+def _create_failed(name, resource):
+ return {
+ 'name': name, 'changes': {}, 'result': False,
+ 'comment': '{0} {1} creation failed'.format(resource, name)}
+
+
+def _no_change(name, resource):
+ return {
+ 'name': name, 'changes': {}, 'result': True,
+ 'comment': '{0} {1} already is in the desired state'.format(
+ resource, name)}
+
+
+def _updated(name, resource, changes):
+ return {
+ 'name': name, 'changes': changes, 'result': True,
+ 'comment': '{0} {1} was updated'.format(resource, name)}
+
+
+def _update_failed(name, resource):
+ return {
+ 'name': name, 'changes': {}, 'result': False,
+ 'comment': '{0} {1} update failed'.format(resource, name)}
+
+
+def _deleted(name, resource):
+ return {
+ 'name': name, 'changes': {}, 'result': True,
+ 'comment': '{0} {1} deleted'.format(resource, name)}
+
+
+def _delete_failed(name, resource):
+ return {
+ 'name': name, 'changes': {}, 'result': False,
+ 'comment': '{0} {1} deletion failed'.format(resource, name)}
+
+
+def _non_existent(name, resource):
+ return {
+ 'name': name, 'changes': {}, 'result': True,
+ 'comment': '{0} {1} does not exist'.format(resource, name)}