"Fossies" - the Fresh Open Source Software Archive

Member "senlin-8.0.0/senlin/engine/notifications/message.py" (16 Oct 2019, 3967 Bytes) of package /linux/misc/openstack/senlin-8.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "message.py" see the Fossies "Dox" file reference documentation.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 from oslo_config import cfg
   14 from oslo_context import context as oslo_context
   15 from oslo_log import log as logging
   16 import six
   17 import tenacity
   18 
   19 from senlin.common import context as senlin_context
   20 from senlin.common import exception
   21 from senlin.drivers import base as driver_base
   22 from senlin.objects import credential as co
   23 
   24 LOG = logging.getLogger(__name__)
   25 
   26 CONF = cfg.CONF
   27 
   28 RETRY_ATTEMPTS = 3
   29 RETRY_INITIAL_DELAY = 1
   30 RETRY_BACKOFF = 1
   31 RETRY_MAX = 3
   32 
   33 
   34 class Message(object):
   35     """Zaqar message type of notification."""
   36     def __init__(self, queue_name, **kwargs):
   37         self.user = kwargs.get('user', '')
   38         self.project = kwargs.get('project', '')
   39         self.domain = kwargs.get('domain', '')
   40 
   41         self.queue_name = queue_name
   42 
   43         self._zaqarclient = None
   44         self._keystoneclient = None
   45 
   46     def zaqar(self):
   47         if self._zaqarclient is not None:
   48             return self._zaqarclient
   49         params = self._build_conn_params(self.user, self.project)
   50         self._zaqarclient = driver_base.SenlinDriver().message(params)
   51         return self._zaqarclient
   52 
   53     def _build_conn_params(self, user, project):
   54         """Build connection params for specific user and project.
   55 
   56         :param user: The ID of the user for which a trust will be used.
   57         :param project: The ID of the project for which a trust will be used.
   58         :returns: A dict containing the required parameters for connection
   59                   creation.
   60         """
   61         service_creds = senlin_context.get_service_credentials()
   62         params = {
   63             'username': service_creds.get('username'),
   64             'password': service_creds.get('password'),
   65             'auth_url': service_creds.get('auth_url'),
   66             'user_domain_name': service_creds.get('user_domain_name')
   67         }
   68 
   69         cred = co.Credential.get(oslo_context.get_current(), user, project)
   70         if cred is None:
   71             raise exception.TrustNotFound(trustor=user)
   72         params['trust_id'] = cred.cred['openstack']['trust']
   73 
   74         return params
   75 
   76     @tenacity.retry(
   77         retry=tenacity.retry_if_exception_type(exception.EResourceCreation),
   78         wait=tenacity.wait_incrementing(
   79             RETRY_INITIAL_DELAY, RETRY_BACKOFF, RETRY_MAX),
   80         stop=tenacity.stop_after_attempt(RETRY_ATTEMPTS))
   81     def post_lifecycle_hook_message(self, lifecycle_action_token, node_id,
   82                                     resource_id, lifecycle_transition_type):
   83         message_list = [{
   84             "ttl": CONF.notification.ttl,
   85             "body": {
   86                 "lifecycle_action_token": lifecycle_action_token,
   87                 "node_id": node_id,
   88                 "resource_id": resource_id,
   89                 "lifecycle_transition_type": lifecycle_transition_type
   90             }
   91         }]
   92         try:
   93             if not self.zaqar().queue_exists(self.queue_name):
   94                 kwargs = {
   95                     "_max_messages_post_size":
   96                         CONF.notification.max_message_size,
   97                     "description": "Senlin lifecycle hook notification",
   98                     "name": self.queue_name
   99                 }
  100                 self.zaqar().queue_create(**kwargs)
  101 
  102             return self.zaqar().message_post(self.queue_name, message_list)
  103         except exception.InternalError as ex:
  104             raise exception.EResourceCreation(
  105                 type='queue',
  106                 message=six.text_type(ex))