~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to nova/api/openstack/common.py

  • Committer: Tarmac
  • Author(s): Brian Lamar, Dan Prince
  • Date: 2011-08-31 22:55:34 UTC
  • mfrom: (1443.3.61 instance_states)
  • Revision ID: tarmac-20110831225534-upfhtsvcsafyql6x
Fixed and improved the way instance "states" are set. Instead of relying on solely the power_state of a VM, there are now explicitly defined VM states and VM task states which respectively define the current state of the VM and the task which is currently being performed by the VM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from nova import log as logging
28
28
from nova import quota
29
29
from nova.api.openstack import wsgi
30
 
from nova.compute import power_state as compute_power_state
 
30
from nova.compute import vm_states
 
31
from nova.compute import task_states
31
32
 
32
33
 
33
34
LOG = logging.getLogger('nova.api.openstack.common')
38
39
XML_NS_V11 = 'http://docs.openstack.org/compute/api/v1.1'
39
40
 
40
41
 
41
 
_STATUS_MAP = {
42
 
    None: 'BUILD',
43
 
    compute_power_state.NOSTATE: 'BUILD',
44
 
    compute_power_state.RUNNING: 'ACTIVE',
45
 
    compute_power_state.BLOCKED: 'ACTIVE',
46
 
    compute_power_state.SUSPENDED: 'SUSPENDED',
47
 
    compute_power_state.PAUSED: 'PAUSED',
48
 
    compute_power_state.SHUTDOWN: 'SHUTDOWN',
49
 
    compute_power_state.SHUTOFF: 'SHUTOFF',
50
 
    compute_power_state.CRASHED: 'ERROR',
51
 
    compute_power_state.FAILED: 'ERROR',
52
 
    compute_power_state.BUILDING: 'BUILD',
 
42
_STATE_MAP = {
 
43
    vm_states.ACTIVE: {
 
44
        'default': 'ACTIVE',
 
45
        task_states.REBOOTING: 'REBOOT',
 
46
        task_states.UPDATING_PASSWORD: 'PASSWORD',
 
47
        task_states.RESIZE_VERIFY: 'VERIFY_RESIZE',
 
48
    },
 
49
    vm_states.BUILDING: {
 
50
        'default': 'BUILD',
 
51
    },
 
52
    vm_states.REBUILDING: {
 
53
        'default': 'REBUILD',
 
54
    },
 
55
    vm_states.STOPPED: {
 
56
        'default': 'STOPPED',
 
57
    },
 
58
    vm_states.MIGRATING: {
 
59
        'default': 'MIGRATING',
 
60
    },
 
61
    vm_states.RESIZING: {
 
62
        'default': 'RESIZE',
 
63
    },
 
64
    vm_states.PAUSED: {
 
65
        'default': 'PAUSED',
 
66
    },
 
67
    vm_states.SUSPENDED: {
 
68
        'default': 'SUSPENDED',
 
69
    },
 
70
    vm_states.RESCUED: {
 
71
        'default': 'RESCUE',
 
72
    },
 
73
    vm_states.ERROR: {
 
74
        'default': 'ERROR',
 
75
    },
 
76
    vm_states.DELETED: {
 
77
        'default': 'DELETED',
 
78
    },
53
79
}
54
80
 
55
81
 
56
 
def status_from_power_state(power_state):
57
 
    """Map the power state to the server status string"""
58
 
    return _STATUS_MAP[power_state]
59
 
 
60
 
 
61
 
def power_states_from_status(status):
62
 
    """Map the server status string to a list of power states"""
63
 
    power_states = []
64
 
    for power_state, status_map in _STATUS_MAP.iteritems():
65
 
        # Skip the 'None' state
66
 
        if power_state is None:
67
 
            continue
68
 
        if status.lower() == status_map.lower():
69
 
            power_states.append(power_state)
70
 
    return power_states
 
82
def status_from_state(vm_state, task_state='default'):
 
83
    """Given vm_state and task_state, return a status string."""
 
84
    task_map = _STATE_MAP.get(vm_state, dict(default='UNKNOWN_STATE'))
 
85
    status = task_map.get(task_state, task_map['default'])
 
86
    LOG.debug("Generated %(status)s from vm_state=%(vm_state)s "
 
87
              "task_state=%(task_state)s." % locals())
 
88
    return status
 
89
 
 
90
 
 
91
def vm_state_from_status(status):
 
92
    """Map the server status string to a vm state."""
 
93
    for state, task_map in _STATE_MAP.iteritems():
 
94
        status_string = task_map.get("default")
 
95
        if status.lower() == status_string.lower():
 
96
            return state
71
97
 
72
98
 
73
99
def get_pagination_params(request):