summaryrefslogtreecommitdiff
path: root/nova/api/openstack/compute/shelve.py
blob: abcb42ee8e9855682bc3e79304f50cda12235306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#   Copyright 2013 Rackspace Hosting
#
#   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.

"""The shelved mode extension."""

from oslo_log import log as logging
from webob import exc

from nova.api.openstack import api_version_request
from nova.api.openstack import common
from nova.api.openstack.compute.schemas import shelve as shelve_schemas
from nova.api.openstack import wsgi
from nova.api import validation
from nova.compute import api as compute
from nova import exception
from nova.policies import shelve as shelve_policies

LOG = logging.getLogger(__name__)


class ShelveController(wsgi.Controller):
    def __init__(self):
        super(ShelveController, self).__init__()
        self.compute_api = compute.API()

    @wsgi.response(202)
    @wsgi.expected_errors((404, 403, 409, 400))
    @wsgi.action('shelve')
    def _shelve(self, req, id, body):
        """Move an instance into shelved mode."""
        context = req.environ["nova.context"]

        instance = common.get_instance(self.compute_api, context, id)
        context.can(shelve_policies.POLICY_ROOT % 'shelve',
                    target={'user_id': instance.user_id,
                            'project_id': instance.project_id})
        try:
            self.compute_api.shelve(context, instance)
        except (
            exception.InstanceIsLocked,
            exception.UnexpectedTaskStateError,
        ) as e:
            raise exc.HTTPConflict(explanation=e.format_message())
        except exception.InstanceInvalidState as state_error:
            common.raise_http_conflict_for_instance_invalid_state(state_error,
                                                                  'shelve', id)
        except exception.ForbiddenPortsWithAccelerator as e:
            raise exc.HTTPBadRequest(explanation=e.format_message())

    @wsgi.response(202)
    @wsgi.expected_errors((400, 404, 409))
    @wsgi.action('shelveOffload')
    def _shelve_offload(self, req, id, body):
        """Force removal of a shelved instance from the compute node."""
        context = req.environ["nova.context"]
        instance = common.get_instance(self.compute_api, context, id)
        context.can(shelve_policies.POLICY_ROOT % 'shelve_offload',
                    target={'user_id': instance.user_id,
                            'project_id': instance.project_id})
        try:
            self.compute_api.shelve_offload(context, instance)
        except exception.InstanceIsLocked as e:
            raise exc.HTTPConflict(explanation=e.format_message())
        except exception.InstanceInvalidState as state_error:
            common.raise_http_conflict_for_instance_invalid_state(state_error,
                                                              'shelveOffload',
                                                              id)

        except exception.ForbiddenPortsWithAccelerator as e:
            raise exc.HTTPBadRequest(explanation=e.format_message())

    @wsgi.response(202)
    @wsgi.expected_errors((400, 404, 409))
    @wsgi.action('unshelve')
    # In microversion 2.77 we support specifying 'availability_zone' to
    # unshelve a server. But before 2.77 there is no request body
    # schema validation (because of body=null).
    @validation.schema(
        shelve_schemas.unshelve_v277,
        min_version='2.77',
        max_version='2.90'
    )
    # In microversion 2.91 we support specifying 'host' to
    # unshelve an instance to a specific hostself.
    # 'availability_zone' = None is supported as well to unpin the
    # availability zone of an instance bonded to this availability_zone
    @validation.schema(shelve_schemas.unshelve_v291, min_version='2.91')
    def _unshelve(self, req, id, body):
        """Restore an instance from shelved mode."""
        context = req.environ["nova.context"]
        instance = common.get_instance(self.compute_api, context, id)
        context.can(
            shelve_policies.POLICY_ROOT % 'unshelve',
            target={'project_id': instance.project_id}
        )

        unshelve_args = {}

        unshelve_dict = body.get('unshelve')
        support_az = api_version_request.is_supported(
            req, '2.77')
        support_host = api_version_request.is_supported(
            req, '2.91')
        if unshelve_dict:
            if support_az and 'availability_zone' in unshelve_dict:
                unshelve_args['new_az'] = (
                    unshelve_dict['availability_zone']
                )
            if support_host:
                unshelve_args['host'] = unshelve_dict.get('host')

        try:
            self.compute_api.unshelve(
                context,
                instance,
                **unshelve_args,
            )
        except (
            exception.InstanceIsLocked,
            exception.UnshelveInstanceInvalidState,
            exception.UnshelveHostNotInAZ,
            exception.MismatchVolumeAZException,
        ) as e:
            raise exc.HTTPConflict(explanation=e.format_message())
        except exception.InstanceInvalidState as state_error:
            common.raise_http_conflict_for_instance_invalid_state(
                state_error, 'unshelve', id)
        except (
            exception.InvalidRequest,
            exception.ExtendedResourceRequestOldCompute,
            exception.ComputeHostNotFound,
        ) as e:
            raise exc.HTTPBadRequest(explanation=e.format_message())