def image_present(name, visibility='public', protected=None, checksum=None, location=None, disk_format='raw', wait_for=None, timeout=30):
 ret = {'name': name, 'changes': {}, 'result': True, 'comment': ''}
 acceptable = ['queued', 'saving', 'active']
 if ((wait_for is None) and (checksum is None)):
  wait_for = 'saving'
 elif ((wait_for is None) and (checksum is not None)):
  wait_for = 'active'
 while (len(acceptable) > 1):
  if (acceptable[0] == wait_for):
   break
  else:
   acceptable.pop(0)
 (image, msg) = _find_image(name)
 if (image is False):
  if __opts__['test']:
   ret['result'] = None
  else:
   ret['result'] = False
  ret['comment'] = msg
  return ret
 log.debug(msg)
 if ((image is None) and (location is not None)):
  if __opts__['test']:
   ret['result'] = None
   ret['comment'] = 'glance.image_present would create an image from {0}'.format(location)
   return ret
  image = __salt__['glance.image_create'](name=name, protected=protected, visibility=visibility, location=location, disk_format=disk_format)
  log.debug('Created new image:\n{0}'.format(image))
  ret['changes'] = {name: {'new': {'id': image['id']}, 'old': None}}
  timer = timeout
  while (timer > 0):
   if (('status' in image) and (image['status'] in acceptable)):
    log.debug('Image {0} has reached status {1}'.format(image['name'], image['status']))
    break
   else:
    timer -= 5
    time.sleep(5)
    (image, msg) = _find_image(name)
    if (not image):
     ret['result'] = False
     ret['comment'] += (('Created image {0} '.format(name) + ' vanished:\n') + msg)
     return ret
  if ((timer <= 0) and (image['status'] not in acceptable)):
   ret['result'] = False
   ret['comment'] += (("Image didn't reach an acceptable " + 'state ({0}) before timeout:\n'.format(acceptable)) + ' DCTB Last status was "{0}".\n'.format(image['status']))
 elif (location is None):
  if __opts__['test']:
   ret['result'] = None
   ret['comment'] = ('No location to copy image from specified,\n' + 'glance.image_present would not create one')
  else:
   ret['result'] = False
   ret['comment'] = ('No location to copy image from specified,\n' + 'not creating a new image.')
  return ret
 if (name in ret['changes']):
  ret['changes'][name]['new']['status'] = image['status']
 if visibility:
  if (image['visibility'] != visibility):
   old_value = image['visibility']
   if (not __opts__['test']):
    image = __salt__['glance.image_update'](id=image['id'], visibility=visibility)
   if (image['visibility'] != visibility):
    if (not __opts__['test']):
     ret['result'] = False
    elif __opts__['test']:
     ret['result'] = None
    ret['comment'] += '"visibility" is {0}, should be {1}.\n'.format(image['visibility'], visibility)
   else:
    if ('new' in ret['changes']):
     ret['changes']['new']['visibility'] = visibility
    else:
     ret['changes']['new'] = {'visibility': visibility}
    if ('old' in ret['changes']):
     ret['changes']['old']['visibility'] = old_value
    else:
     ret['changes']['old'] = {'visibility': old_value}
  else:
   ret['comment'] += '"visibility" is correct ({0}).\n'.format(visibility)
 if (protected is not None):
  if ((not isinstance(protected, bool)) or (image['protected'] ^ protected)):
   if (not __opts__['test']):
    ret['result'] = False
   else:
    ret['result'] = None
   ret['comment'] += '"protected" is {0}, should be {1}.\n'.format(image['protected'], protected)
  else:
   ret['comment'] += '"protected" is correct ({0}).\n'.format(protected)
 if (('status' in image) and checksum):
  if (image['status'] == 'active'):
   if ('checksum' not in image):
    image = __salt__['glance.image_show'](image['id'])
   if ('checksum' not in image):
    if (not __opts__['test']):
     ret['result'] = False
    else:
     ret['result'] = None
    ret['comment'] += ('No checksum available for this image:\n' + ' DCTB Image has status "{0}".'.format(image['status']))
   elif (image['checksum'] != checksum):
    if (not __opts__['test']):
     ret['result'] = False
    else:
     ret['result'] = None
    ret['comment'] += '"checksum" is {0}, should be {1}.\n'.format(image['checksum'], checksum)
   else:
    ret['comment'] += '"checksum" is correct ({0}).\n'.format(checksum)
  elif (image['status'] in ['saving', 'queued']):
   ret['comment'] += ("Checksum won't be verified as image " + 'hasn\'t reached\n DCTB  "status=active" yet.\n')
 log.debug('glance.image_present will return: {0}'.format(ret))
 return ret

def image_present ( name , visibility = 'public' , protected = None , checksum = None , location = None , disk_format = 'raw' , wait_for = None , timeout = 30 ) : ret = { 'name' : name , 'changes' : { } , 'result' : True , 'comment' : '' } acceptable = [ 'queued' , 'saving' , 'active' ] if ( ( wait_for is None ) and ( checksum is None ) ) : wait_for = 'saving' elif ( ( wait_for is None ) and ( checksum is not None ) ) : wait_for = 'active' while ( len ( acceptable ) > 1 ) : if ( acceptable [ 0 ] == wait_for ) : break else : acceptable . pop ( 0 ) ( image , msg ) = _find_image ( name ) if ( image is False ) : if __opts__ [ 'test' ] : ret [ 'result' ] = None else : ret [ 'result' ] = False ret [ 'comment' ] = msg return ret log . debug ( msg ) if ( ( image is None ) and ( location is not None ) ) : if __opts__ [ 'test' ] : ret [ 'result' ] = None ret [ 'comment' ] = 'glance.image_present would create an image from {0}' . format ( location ) return ret image = __salt__ [ 'glance.image_create' ] ( name = name , protected = protected , visibility = visibility , location = location , disk_format = disk_format ) log . debug ( 'Created new image:\n{0}' . format ( image ) ) ret [ 'changes' ] = { name : { 'new' : { 'id' : image [ 'id' ] } , 'old' : None } } timer = timeout while ( timer > 0 ) : if ( ( 'status' in image ) and ( image [ 'status' ] in acceptable ) ) : log . debug ( 'Image {0} has reached status {1}' . format ( image [ 'name' ] , image [ 'status' ] ) ) break else : timer -= 5 time . sleep ( 5 ) ( image , msg ) = _find_image ( name ) if ( not image ) : ret [ 'result' ] = False ret [ 'comment' ] += ( ( 'Created image {0} ' . format ( name ) + ' vanished:\n' ) + msg ) return ret if ( ( timer <= 0 ) and ( image [ 'status' ] not in acceptable ) ) : ret [ 'result' ] = False ret [ 'comment' ] += ( ( "Image didn't reach an acceptable " + 'state ({0}) before timeout:\n' . format ( acceptable ) ) + ' DCTB Last status was "{0}".\n' . format ( image [ 'status' ] ) ) elif ( location is None ) : if __opts__ [ 'test' ] : ret [ 'result' ] = None ret [ 'comment' ] = ( 'No location to copy image from specified,\n' + 'glance.image_present would not create one' ) else : ret [ 'result' ] = False ret [ 'comment' ] = ( 'No location to copy image from specified,\n' + 'not creating a new image.' ) return ret if ( name in ret [ 'changes' ] ) : ret [ 'changes' ] [ name ] [ 'new' ] [ 'status' ] = image [ 'status' ] if visibility : if ( image [ 'visibility' ] != visibility ) : old_value = image [ 'visibility' ] if ( not __opts__ [ 'test' ] ) : image = __salt__ [ 'glance.image_update' ] ( id = image [ 'id' ] , visibility = visibility ) if ( image [ 'visibility' ] != visibility ) : if ( not __opts__ [ 'test' ] ) : ret [ 'result' ] = False elif __opts__ [ 'test' ] : ret [ 'result' ] = None ret [ 'comment' ] += '"visibility" is {0}, should be {1}.\n' . format ( image [ 'visibility' ] , visibility ) else : if ( 'new' in ret [ 'changes' ] ) : ret [ 'changes' ] [ 'new' ] [ 'visibility' ] = visibility else : ret [ 'changes' ] [ 'new' ] = { 'visibility' : visibility } if ( 'old' in ret [ 'changes' ] ) : ret [ 'changes' ] [ 'old' ] [ 'visibility' ] = old_value else : ret [ 'changes' ] [ 'old' ] = { 'visibility' : old_value } else : ret [ 'comment' ] += '"visibility" is correct ({0}).\n' . format ( visibility ) if ( protected is not None ) : if ( ( not isinstance ( protected , bool ) ) or ( image [ 'protected' ] ^ protected ) ) : if ( not __opts__ [ 'test' ] ) : ret [ 'result' ] = False else : ret [ 'result' ] = None ret [ 'comment' ] += '"protected" is {0}, should be {1}.\n' . format ( image [ 'protected' ] , protected ) else : ret [ 'comment' ] += '"protected" is correct ({0}).\n' . format ( protected ) if ( ( 'status' in image ) and checksum ) : if ( image [ 'status' ] == 'active' ) : if ( 'checksum' not in image ) : image = __salt__ [ 'glance.image_show' ] ( image [ 'id' ] ) if ( 'checksum' not in image ) : if ( not __opts__ [ 'test' ] ) : ret [ 'result' ] = False else : ret [ 'result' ] = None ret [ 'comment' ] += ( 'No checksum available for this image:\n' + ' DCTB Image has status "{0}".' . format ( image [ 'status' ] ) ) elif ( image [ 'checksum' ] != checksum ) : if ( not __opts__ [ 'test' ] ) : ret [ 'result' ] = False else : ret [ 'result' ] = None ret [ 'comment' ] += '"checksum" is {0}, should be {1}.\n' . format ( image [ 'checksum' ] , checksum ) else : ret [ 'comment' ] += '"checksum" is correct ({0}).\n' . format ( checksum ) elif ( image [ 'status' ] in [ 'saving' , 'queued' ] ) : ret [ 'comment' ] += ( "Checksum won't be verified as image " + 'hasn\'t reached\n DCTB  "status=active" yet.\n' ) log . debug ( 'glance.image_present will return: {0}' . format ( ret ) ) return ret
ensure image is present on the computenode name : string uuid of image .