def power_state(vm, state, force):
 power_status = vm.get_status()
 check_status = ' '.join(state.split('_')).upper()
 if ((not force) and (power_status in ['SUSPENDED', 'POWERING ON', 'RESETTING', 'BLOCKED ON MSG'])):
  return ('VM is in %s power state. Force is required!' % power_status)
 if (power_status == check_status):
  return False
 else:
  try:
   if (state == 'powered_off'):
    vm.power_off(sync_run=True)
   elif (state == 'powered_on'):
    vm.power_on(sync_run=True)
   elif (state == 'restarted'):
    if (power_status in ('POWERED ON', 'POWERING ON', 'RESETTING')):
     vm.reset(sync_run=False)
    else:
     return ('Cannot restart VM in the current state %s' % power_status)
   return True
  except Exception:
   return get_exception()
 return False

def power_state ( vm , state , force ) : power_status = vm . get_status ( ) check_status = ' ' . join ( state . split ( '_' ) ) . upper ( ) if ( ( not force ) and ( power_status in [ 'SUSPENDED' , 'POWERING ON' , 'RESETTING' , 'BLOCKED ON MSG' ] ) ) : return ( 'VM is in %s power state. Force is required!' % power_status ) if ( power_status == check_status ) : return False else : try : if ( state == 'powered_off' ) : vm . power_off ( sync_run = True ) elif ( state == 'powered_on' ) : vm . power_on ( sync_run = True ) elif ( state == 'restarted' ) : if ( power_status in ( 'POWERED ON' , 'POWERING ON' , 'RESETTING' ) ) : vm . reset ( sync_run = False ) else : return ( 'Cannot restart VM in the current state %s' % power_status ) return True except Exception : return get_exception ( ) return False
correctly set the power status for a vm determined by the current and requested states .