Reworked glance salt formulas
image_import and image_absent are added
image_download added to be used with
barbican's sign_image
Change-Id: I9d885aa9b139b440dcbc08263341bc7ff1f38f05
Related-Issue: PROD-19310
diff --git a/_states/glancev2.py b/_states/glancev2.py
new file mode 100644
index 0000000..2c5a84f
--- /dev/null
+++ b/_states/glancev2.py
@@ -0,0 +1,271 @@
+# -*- coding: utf-8 -*-
+'''
+Managing Images in OpenStack Glance
+===================================
+'''
+# Import python libs
+import logging
+import time
+
+
+# Import OpenStack libs
+def __virtual__():
+ return 'glancev2' if 'glancev2.image_list' in __salt__ else False
+
+
+log = logging.getLogger(__name__)
+
+
+def _glancev2_call(fname, *args, **kwargs):
+ return __salt__['glancev2.{}'.format(fname)](*args, **kwargs)
+
+
+def image_present(name, cloud_name, location, image_properties,
+ import_from_format='raw', timeout=30,
+ sleep_time=5, checksum=None):
+ """
+ Creates a task to import an image
+
+ This state checks if an image is present and, if not, creates a task
+ with import_type that would download an image from a remote location and
+ upload it to Glance.
+ After the task is created, its status is monitored. On success the state
+ would check that an image is present and return its ID.
+
+ Also, this state can update(add and replace) image properties,
+ but !!It can't delete properties, that are already in state
+
+ :param name: name of the image
+ :param cloud_name: name of the cloud in cloud.yaml
+ :param location: url link describing where to obtain image
+ :param image_properties: Dict that contains params needed
+ to create or update image.
+ :param container_format: Format of the container
+ :param disk_format: Format of the disk
+ :param protected: If true, image will not be deletable.
+ :param tags: List of strings related to the image
+ :param visibility: Scope of image accessibility.
+ Valid values: public, private, community, shared
+ :param import_from_format: (optional) Format to import the image from
+ :param timeout: (optional) Time for task to download image
+ :param sleep_time: (optional) Timer countdown
+ :param checksum: (optional) checksum of the image to verify it
+ """
+ try:
+ exact_image = _glancev2_call(
+ 'image_get_details', name=name, cloud_name=cloud_name
+ )
+ except Exception as e:
+ if 'ResourceNotFound' in repr(e):
+ image_properties['name'] = name
+ task_params = {"import_from": location,
+ "import_from_format": import_from_format,
+ "image_properties": image_properties
+ }
+ # Try create task
+ try:
+ task = _glancev2_call(
+ 'task_create', task_type='import', task_input=task_params,
+ cloud_name=cloud_name
+ )
+ except Exception as e:
+ log.error(
+ 'Glance image create failed on create task with {}'.format(
+ e)
+ )
+ return _create_failed(name, 'image_task')
+ while timeout > 0:
+ if task['status'] == 'success':
+ break
+ elif task['status'] == 'failure':
+ log.error('Glance task failed to complete')
+ return _create_failed(name, 'image')
+ else:
+ timeout -= sleep_time
+ time.sleep(sleep_time)
+ # Check task status again
+ try:
+ task = _glancev2_call(
+ 'task_show', task_id=task['id'],
+ cloud_name=cloud_name
+ )
+ except Exception as e:
+ log.error(
+ 'Glance failed to check '
+ 'task status with {}'.format(e)
+ )
+ return _create_failed(name, 'image_task')
+ if timeout <= 0 and task['status'] != 'success':
+ log.error(
+ 'Glance task failed to import '
+ 'image for given amount of time'
+ )
+ return _create_failed(name, 'image')
+ # Task successfully finished
+ # and now check that is created the image
+
+ image = _glancev2_call(
+ 'image_list', name=name, cloud_name=cloud_name
+ )['images'][0]
+
+ if not image:
+ return _create_failed(name, 'image')
+
+ resp = _created(name, 'image', image)
+
+ if checksum:
+ if image['status'] == 'active':
+ if 'checksum' not in image:
+ log.error(
+ 'Glance image. No checksum for image.'
+ 'Image status is active'
+ )
+ return _create_failed(name, 'image')
+ if image['checksum'] != checksum:
+ log.error(
+ 'Glance image create failed since '
+ 'image_checksum should be '
+ '{} but it is {}'.format(checksum,
+ image['checksum'])
+ )
+ return _create_failed(name, 'image')
+
+ elif image['status'] in ['saving', 'queued']:
+ resp['comment'] = resp['comment'] \
+ + " checksum couldn't be verified, " \
+ "since status is not active"
+ return resp
+ if 'MultipleResourcesFound' in repr(e):
+ return _find_failed(name, 'image')
+ to_change = []
+ for prop in image_properties:
+ path = prop.replace('~', '~0').replace('/', '~1')
+ if prop in exact_image:
+ if exact_image[prop] != image_properties[prop]:
+ to_change.append({
+ 'op': 'replace',
+ 'path': '/{}'.format(path),
+ 'value': image_properties[prop]
+ })
+ else:
+ to_change.append({
+ 'op': 'add',
+ 'path': '/{}'.format(path),
+ 'value': image_properties[prop]
+ })
+ if to_change:
+ try:
+ resp = _glancev2_call(
+ 'image_update', name=name,
+ properties=to_change, cloud_name=cloud_name,
+ )
+ except Exception as e:
+ log.error('Glance image update failed with {}'.format(e))
+ return _update_failed(name, 'image')
+ return _updated(name, 'image', resp)
+ return _no_changes(name, 'image')
+
+
+def image_absent(name, cloud_name):
+ try:
+ image = _glancev2_call(
+ 'image_get_details', name=name, cloud_name=cloud_name
+ )
+ except Exception as e:
+ if 'ResourceNotFound' in repr(e):
+ return _absent(name, 'image')
+ if 'MultipleResourcesFound' in repr(e):
+ return _find_failed(name, 'image')
+ try:
+ _glancev2_call(
+ 'image_delete', name=name, cloud_name=cloud_name
+ )
+ except Exception as e:
+ log.error('Glance image delete failed with {}'.format(e))
+ return _delete_failed(name, 'image')
+ return _deleted(name, 'image')
+
+
+def _created(name, resource, resource_definition):
+ changes_dict = {
+ 'name': name,
+ 'changes': resource_definition,
+ 'result': True,
+ 'comment': '{}{} created'.format(resource, name)
+ }
+ return changes_dict
+
+
+def _updated(name, resource, resource_definition):
+ changes_dict = {
+ 'name': name,
+ 'changes': resource_definition,
+ 'result': True,
+ 'comment': '{}{} updated'.format(resource, name)
+ }
+ return changes_dict
+
+
+def _no_changes(name, resource):
+ changes_dict = {
+ 'name': name,
+ 'changes': {},
+ 'result': True,
+ 'comment': '{}{} is in desired state'.format(resource, name)
+ }
+ return changes_dict
+
+
+def _deleted(name, resource):
+ changes_dict = {
+ 'name': name,
+ 'changes': {},
+ 'result': True,
+ 'comment': '{}{} removed'.format(resource, name)
+ }
+ return changes_dict
+
+
+def _absent(name, resource):
+ changes_dict = {'name': name,
+ 'changes': {},
+ 'comment': '{0} {1} not present'.format(resource, name),
+ 'result': True}
+ return changes_dict
+
+
+def _delete_failed(name, resource):
+ changes_dict = {'name': name,
+ 'changes': {},
+ 'comment': '{0} {1} failed to delete'.format(resource,
+ name),
+ 'result': False}
+ return changes_dict
+
+
+def _create_failed(name, resource):
+ changes_dict = {'name': name,
+ 'changes': {},
+ 'comment': '{0} {1} failed to create'.format(resource,
+ name),
+ 'result': False}
+ return changes_dict
+
+
+def _update_failed(name, resource):
+ changes_dict = {'name': name,
+ 'changes': {},
+ 'comment': '{0} {1} failed to update'.format(resource,
+ name),
+ 'result': False}
+ return changes_dict
+
+
+def _find_failed(name, resource):
+ changes_dict = {
+ 'name': name,
+ 'changes': {},
+ 'comment': '{0} {1} found multiple {0}'.format(resource, name),
+ 'result': False,
+ }
+ return changes_dict