File: api.py

package info (click to toggle)
openstack-trove 2014.1.3-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,752 kB
  • ctags: 6,663
  • sloc: python: 37,317; xml: 1,485; sh: 281; makefile: 49
file content (143 lines) | stat: -rw-r--r-- 6,187 bytes parent folder | download
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
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
#    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.


"""
Routes all the requests to the task manager.
"""


from trove.common import cfg
from trove.openstack.common.rpc import proxy
from trove.openstack.common import log as logging
from trove.openstack.common.gettextutils import _


CONF = cfg.CONF
LOG = logging.getLogger(__name__)
RPC_API_VERSION = "1.0"


class API(proxy.RpcProxy):
    """API for interacting with the task manager."""

    def __init__(self, context):
        self.context = context
        super(API, self).__init__(self._get_routing_key(),
                                  RPC_API_VERSION)

    def _transform_obj(self, obj_ref):
        # Turn the object into a dictionary and remove the mgr
        if "__dict__" in dir(obj_ref):
            obj_dict = obj_ref.__dict__
            # We assume manager contains a object due to the *clients
            if obj_dict.get('manager'):
                del obj_dict['manager']
            return obj_dict
        raise ValueError("Could not transform %s" % obj_ref)

    def _get_routing_key(self):
        """Create the routing key for the taskmanager"""
        return CONF.taskmanager_queue

    def resize_volume(self, new_size, instance_id):
        LOG.debug("Making async call to resize volume for instance: %s"
                  % instance_id)
        self.cast(self.context, self.make_msg("resize_volume",
                                              new_size=new_size,
                                              instance_id=instance_id))

    def resize_flavor(self, instance_id, old_flavor, new_flavor):
        LOG.debug("Making async call to resize flavor for instance: %s" %
                  instance_id)
        self.cast(self.context,
                  self.make_msg("resize_flavor",
                                instance_id=instance_id,
                                old_flavor=self._transform_obj(old_flavor),
                                new_flavor=self._transform_obj(new_flavor)))

    def reboot(self, instance_id):
        LOG.debug("Making async call to reboot instance: %s" % instance_id)
        self.cast(self.context,
                  self.make_msg("reboot", instance_id=instance_id))

    def restart(self, instance_id):
        LOG.debug("Making async call to restart instance: %s" % instance_id)
        self.cast(self.context,
                  self.make_msg("restart", instance_id=instance_id))

    def migrate(self, instance_id, host):
        LOG.debug("Making async call to migrate instance: %s" % instance_id)
        self.cast(self.context,
                  self.make_msg("migrate", instance_id=instance_id, host=host))

    def delete_instance(self, instance_id):
        LOG.debug("Making async call to delete instance: %s" % instance_id)
        self.cast(self.context,
                  self.make_msg("delete_instance", instance_id=instance_id))

    def create_backup(self, backup_info, instance_id):
        LOG.debug("Making async call to create a backup for instance: %s" %
                  instance_id)
        self.cast(self.context, self.make_msg("create_backup",
                                              backup_info=backup_info,
                                              instance_id=instance_id))

    def delete_backup(self, backup_id):
        LOG.debug("Making async call to delete backup: %s" % backup_id)
        self.cast(self.context, self.make_msg("delete_backup",
                                              backup_id=backup_id))

    def create_instance(self, instance_id, name, flavor,
                        image_id, databases, users, datastore_manager,
                        packages, volume_size, backup_id=None,
                        availability_zone=None, root_password=None,
                        nics=None, overrides=None):
        LOG.debug("Making async call to create instance %s " % instance_id)
        self.cast(self.context,
                  self.make_msg("create_instance",
                                instance_id=instance_id, name=name,
                                flavor=self._transform_obj(flavor),
                                image_id=image_id,
                                databases=databases,
                                users=users,
                                datastore_manager=
                                datastore_manager,
                                packages=packages,
                                volume_size=volume_size,
                                backup_id=backup_id,
                                availability_zone=availability_zone,
                                root_password=root_password,
                                nics=nics,
                                overrides=overrides))

    def update_overrides(self, instance_id, overrides=None):
        LOG.debug(_("Making async call to update configuration overrides for "
                    "instance %s") % instance_id)

        self.cast(self.context,
                  self.make_msg("update_overrides",
                                instance_id=instance_id,
                                overrides=overrides))

    def unassign_configuration(self, instance_id, flavor, configuration_id):
        LOG.debug(_("Making async call to unassign configuration for "
                    "instance %s") % instance_id)

        self.cast(self.context,
                  self.make_msg("unassign_configuration",
                                instance_id=instance_id,
                                flavor=self._transform_obj(flavor),
                                configuration_id=configuration_id))