File: api.py

package info (click to toggle)
openstack-trove 1%3A24.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,976 kB
  • sloc: python: 50,665; sh: 2,866; makefile: 71
file content (124 lines) | stat: -rw-r--r-- 4,700 bytes parent folder | download | duplicates (3)
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
#    Copyright 2013 OpenStack Foundation
#
#    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.

from oslo_log import log as logging
import oslo_messaging as messaging

from trove.common import cfg
from trove.common.rpc import conductor_guest_serializer as sz
from trove.common.serializable_notification import SerializableNotification
from trove import rpc

CONF = cfg.CONF
LOG = logging.getLogger(__name__)


class API(object):
    """API for interacting with trove conductor.

    API version history:
        * 1.0 - Initial version.

    When updating this API, also update API_LATEST_VERSION
    """

    # API_LATEST_VERSION should bump the minor number each time
    # a method signature is added or changed
    API_LATEST_VERSION = '1.0'

    # API_BASE_VERSION should only change on major version upgrade
    API_BASE_VERSION = '1.0'

    VERSION_ALIASES = {
        'icehouse': '1.0',
        'juno': '1.0',
        'kilo': '1.0',
        'liberty': '1.0',
        'mitaka': '1.0',
        'newton': '1.0',

        'latest': API_LATEST_VERSION
    }

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

        version_cap = self.VERSION_ALIASES.get(
            CONF.upgrade_levels.conductor, CONF.upgrade_levels.conductor)
        target = messaging.Target(topic=CONF.conductor_queue,
                                  version=version_cap)

        self.client = self.get_client(target, version_cap)

    def get_client(self, target, version_cap, serializer=None):
        return rpc.get_client(target, key=CONF.instance_rpc_encr_key,
                              version_cap=version_cap,
                              serializer=serializer,
                              secure_serializer=sz.ConductorGuestSerializer)

    def heartbeat(self, instance_id, payload, sent=None):
        LOG.debug("Making async call to cast heartbeat for instance: %s",
                  instance_id)
        version = self.API_BASE_VERSION

        cctxt = self.client.prepare(version=version)
        cctxt.cast(self.context, "heartbeat",
                   instance_id=instance_id,
                   sent=sent,
                   payload=payload)

    def update_backup(self, instance_id, backup_id, sent=None,
                      **backup_fields):
        LOG.debug("Making async call to cast update_backup for instance: %s",
                  instance_id)
        version = self.API_BASE_VERSION

        cctxt = self.client.prepare(version=version)
        cctxt.cast(self.context, "update_backup",
                   instance_id=instance_id,
                   backup_id=backup_id,
                   sent=sent,
                   **backup_fields)

    def report_root(self, instance_id):
        LOG.debug("Making async call to cast report_root for instance: %s",
                  instance_id)
        version = self.API_BASE_VERSION
        cctxt = self.client.prepare(version=version)
        cctxt.cast(self.context, "report_root",
                   instance_id=instance_id)

    def notify_end(self, **notification_args):
        LOG.debug("Making async call to cast end notification")
        version = self.API_BASE_VERSION
        cctxt = self.client.prepare(version=version)
        context = self.context
        serialized = SerializableNotification.serialize(context,
                                                        context.notification)
        cctxt.cast(self.context, "notify_end",
                   serialized_notification=serialized,
                   notification_args=notification_args)

    def notify_exc_info(self, message, exception):
        LOG.debug("Making async call to cast error notification")
        version = self.API_BASE_VERSION
        cctxt = self.client.prepare(version=version)
        context = self.context
        serialized = SerializableNotification.serialize(context,
                                                        context.notification)
        serialized.update({'instance_id': CONF.guest_id})
        cctxt.cast(self.context, "notify_exc_info",
                   serialized_notification=serialized,
                   message=message, exception=exception)