Rework nova modules and states
Closes-issue: https://mirantis.jira.com/browse/PROD-20787
Change-Id: If9ea6ff8c53c876e678180c3df3792d198df2ec0
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}