"Fossies" - the Fresh Open Source Software Archive

Member "solum-12.0.0/solum/common/clients.py" (30 Mar 2022, 13031 Bytes) of package /linux/misc/openstack/solum-12.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 "clients.py" see the Fossies "Dox" file reference documentation.

    1 # Copyright 2014 - Rackspace Hosting.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 from glanceclient import client as glanceclient
   16 from heatclient import client as heatclient
   17 from mistralclient.api import client as mistralclient
   18 from neutronclient.neutron import client as neutronclient
   19 from oslo_config import cfg
   20 from swiftclient import client as swiftclient
   21 from zaqarclient.queues.v1 import client as zaqarclient
   22 
   23 from solum.common import exception
   24 from solum.common import solum_barbicanclient
   25 from solum.common import solum_keystoneclient
   26 from solum.i18n import _
   27 
   28 
   29 GLOBAL_CLIENT_OPTS = [
   30     cfg.StrOpt('region_name',
   31                default='RegionOne',
   32                help=_(
   33                    'Region of endpoint in Identity service catalog to use'
   34                    ' for all clients.')),
   35 ]
   36 
   37 barbican_client_opts = [
   38     cfg.BoolOpt('insecure',
   39                 default=False,
   40                 help=_("If set, then the server's certificate for barbican "
   41                        "will not be verified.")), ]
   42 
   43 # Note: this config is duplicated in many projects that use OpenStack
   44 # clients. This should really be in the client.
   45 # There is a place holder bug here:
   46 # https://bugs.launchpad.net/solum/+bug/1292334
   47 # that we use to track this.
   48 glance_client_opts = [
   49     cfg.StrOpt('endpoint_type',
   50                default='publicURL',
   51                help=_(
   52                    'Type of endpoint in Identity service catalog to use '
   53                    'for communication with the Glance service.')),
   54     cfg.StrOpt('region_name',
   55                default='',
   56                help=_(
   57                    'Region of endpoint in Identity service catalog to use.'))]
   58 
   59 heat_client_opts = [
   60     cfg.StrOpt('endpoint_type',
   61                default='publicURL',
   62                help=_(
   63                    'Type of endpoint in Identity service catalog to use '
   64                    'for communication with the OpenStack service.')),
   65     cfg.StrOpt('region_name',
   66                default='',
   67                help=_(
   68                    'Region of endpoint in Identity service catalog to use.')),
   69     cfg.StrOpt('ca_file',
   70                help=_('Optional CA cert file to use in SSL connections.')),
   71     cfg.StrOpt('cert_file',
   72                help=_('Optional PEM-formatted certificate chain file.')),
   73     cfg.StrOpt('key_file',
   74                help=_('Optional PEM-formatted file that contains the '
   75                       'private key.')),
   76     cfg.BoolOpt('insecure',
   77                 default=False,
   78                 help=_("If set, then the server's certificate will not "
   79                        "be verified."))]
   80 
   81 zaqar_client_opts = [
   82     cfg.StrOpt('endpoint_type',
   83                default='publicURL',
   84                help=_(
   85                    'Type of endpoint in Queue service catalog to use '
   86                    'for communication with the Zaqar service.')),
   87     cfg.StrOpt('region_name',
   88                default='',
   89                help=_(
   90                    'Region of endpoint in Identity service catalog to use.')),
   91     cfg.BoolOpt('insecure',
   92                 default=False,
   93                 help=_("If set, then the server's certificate for zaqar "
   94                        "will not be verified."))]
   95 
   96 neutron_client_opts = [
   97     cfg.StrOpt('endpoint_type',
   98                default='publicURL',
   99                help=_(
  100                    'Type of endpoint in Identity service catalog to use '
  101                    'for communication with the Neutron service.')),
  102     cfg.StrOpt('region_name',
  103                default='',
  104                help=_(
  105                    'Region of endpoint in Identity service catalog to use.')),
  106     cfg.StrOpt('ca_cert',
  107                help=_('Optional CA bundle file to use in SSL connections.')),
  108     cfg.BoolOpt('insecure',
  109                 default=False,
  110                 help=_("If set, then the server's certificate for neutron "
  111                        "will not be verified."))]
  112 
  113 swift_client_opts = [
  114     cfg.StrOpt('endpoint_type',
  115                default='publicURL',
  116                help=_(
  117                    'Type of endpoint in Identity service catalog to use '
  118                    'for communication with the Swift service.')),
  119     cfg.StrOpt('region_name',
  120                default='',
  121                help=_(
  122                    'Region of endpoint in Identity service catalog to use.')),
  123     cfg.StrOpt('cacert',
  124                help=_('Optional CA cert file to use in SSL connections.')),
  125     cfg.BoolOpt('insecure',
  126                 default=False,
  127                 help=_("If set the server certificate will not be verified."))]
  128 
  129 mistral_client_opts = [
  130     cfg.StrOpt('endpoint_type',
  131                default='publicURL',
  132                help=_(
  133                    'Type of endpoint in Identity service catalog to use '
  134                    'for communication with the mistral service.')),
  135     cfg.StrOpt('region_name',
  136                default='',
  137                help=_(
  138                    'Region of endpoint in Identity service catalog to use.')),
  139     cfg.StrOpt('cacert',
  140                help=_('Optional CA cert file to use in SSL connections '
  141                       'with Mistral.')),
  142     cfg.BoolOpt('insecure',
  143                 default=False,
  144                 help=_("If set the server certificate will not be verified "
  145                        "while using Mistral."))]
  146 
  147 
  148 def list_opts():
  149     yield None, GLOBAL_CLIENT_OPTS
  150     yield 'barbican_client', barbican_client_opts
  151     yield 'glance_client', glance_client_opts
  152     yield 'heat_client', heat_client_opts
  153     yield 'zaqar_client', zaqar_client_opts
  154     yield 'neutron_client', neutron_client_opts
  155     yield 'swift_client', swift_client_opts
  156     yield 'mistral_client', mistral_client_opts
  157 
  158 
  159 cfg.CONF.register_opts(GLOBAL_CLIENT_OPTS)
  160 cfg.CONF.register_opts(barbican_client_opts, group='barbican_client')
  161 cfg.CONF.register_opts(glance_client_opts, group='glance_client')
  162 cfg.CONF.register_opts(heat_client_opts, group='heat_client')
  163 cfg.CONF.register_opts(zaqar_client_opts, group='zaqar_client')
  164 cfg.CONF.register_opts(neutron_client_opts, group='neutron_client')
  165 cfg.CONF.register_opts(swift_client_opts, group='swift_client')
  166 cfg.CONF.register_opts(mistral_client_opts, group='mistral_client')
  167 
  168 
  169 def get_client_option(client, option):
  170     value = getattr(getattr(cfg.CONF, '%s_client' % client), option)
  171     if option == 'region_name':
  172         global_region = cfg.CONF.get(option)
  173         return value or global_region
  174     else:
  175         return value
  176 
  177 
  178 class OpenStackClients(object):
  179     """Convenience class to create and cache client instances."""
  180 
  181     def __init__(self, context):
  182         self.context = context
  183         self._barbican = None
  184         self._keystone = None
  185         self._glance = None
  186         self._heat = None
  187         self._neutron = None
  188         self._zaqar = None
  189         self._mistral = None
  190 
  191     def url_for(self, **kwargs):
  192         return self.keystone().client.service_catalog.url_for(**kwargs)
  193 
  194     @property
  195     def auth_url(self):
  196         return self.keystone().endpoint
  197 
  198     @property
  199     def auth_token(self):
  200         return self.context.auth_token or self.keystone().auth_token
  201 
  202     @exception.wrap_keystone_exception
  203     def barbican(self):
  204         if self._barbican:
  205             return self._barbican
  206 
  207         insecure = get_client_option('barbican', 'insecure')
  208         self._barbican = solum_barbicanclient.BarbicanClient(
  209             verify=not insecure)
  210         return self._barbican
  211 
  212     def keystone(self):
  213         if self._keystone:
  214             return self._keystone
  215 
  216         self._keystone = solum_keystoneclient.KeystoneClient(self.context)
  217         return self._keystone
  218 
  219     @exception.wrap_keystone_exception
  220     def zaqar(self):
  221         if self._zaqar:
  222             return self._zaqar
  223 
  224         endpoint_type = get_client_option('zaqar', 'endpoint_type')
  225         region_name = get_client_option('zaqar', 'region_name')
  226         endpoint_url = self.url_for(service_type='queuing',
  227                                     interface=endpoint_type,
  228                                     region_name=region_name)
  229         conf = {'auth_opts':
  230                 {'backend': 'keystone',
  231                  'options': {'os_auth_token': self.auth_token,
  232                              'os_auth_url': self.auth_url,
  233                              'insecure': get_client_option('zaqar',
  234                                                            'insecure')}
  235                  }
  236                 }
  237         self._zaqar = zaqarclient.Client(endpoint_url, conf=conf)
  238         return self._zaqar
  239 
  240     @exception.wrap_keystone_exception
  241     def neutron(self):
  242         if self._neutron:
  243             return self._neutron
  244 
  245         endpoint_type = get_client_option('neutron', 'endpoint_type')
  246         region_name = get_client_option('neutron', 'region_name')
  247         endpoint_url = self.url_for(service_type='network',
  248                                     interface=endpoint_type,
  249                                     region_name=region_name)
  250         args = {
  251             'auth_url': self.auth_url,
  252             'endpoint_url': endpoint_url,
  253             'token': self.auth_token,
  254             'username': None,
  255             'password': None,
  256             'insecure': get_client_option('neutron', 'insecure'),
  257             'ca_cert': get_client_option('neutron', 'ca_cert')
  258         }
  259         self._neutron = neutronclient.Client('2.0', **args)
  260         return self._neutron
  261 
  262     @exception.wrap_keystone_exception
  263     def glance(self):
  264         if self._glance:
  265             return self._glance
  266 
  267         args = {
  268             'token': self.auth_token,
  269         }
  270         endpoint_type = get_client_option('glance', 'endpoint_type')
  271         region_name = get_client_option('glance', 'region_name')
  272         endpoint = self.url_for(service_type='image',
  273                                 interface=endpoint_type,
  274                                 region_name=region_name)
  275         self._glance = glanceclient.Client('2', endpoint, **args)
  276 
  277         return self._glance
  278 
  279     @exception.wrap_keystone_exception
  280     def mistral(self):
  281         if self._mistral:
  282             return self._mistral
  283 
  284         args = {
  285             'auth_token': self.auth_token,
  286         }
  287         endpoint_type = get_client_option('mistral', 'endpoint_type')
  288         region_name = get_client_option('mistral', 'region_name')
  289         endpoint = self.url_for(service_type='workflow',
  290                                 interface=endpoint_type,
  291                                 region_name=region_name)
  292         self._mistral = mistralclient.client(mistral_url=endpoint, **args)
  293 
  294         return self._mistral
  295 
  296     @exception.wrap_keystone_exception
  297     def heat(self, username=None, password=None, token=None):
  298         if self._heat:
  299             return self._heat
  300 
  301         if token:
  302             token_to_use = token
  303         else:
  304             token_to_use = self.auth_token
  305 
  306         endpoint_type = get_client_option('heat', 'endpoint_type')
  307         args = {
  308             'auth_url': self.auth_url,
  309             'token': token_to_use,
  310             'username': username,
  311             'password': password,
  312             'ca_file': get_client_option('heat', 'ca_file'),
  313             'cert_file': get_client_option('heat', 'cert_file'),
  314             'key_file': get_client_option('heat', 'key_file'),
  315             'insecure': get_client_option('heat', 'insecure')
  316         }
  317 
  318         region_name = get_client_option('heat', 'region_name')
  319         endpoint = self.url_for(service_type='orchestration',
  320                                 interface=endpoint_type,
  321                                 region_name=region_name)
  322         self._heat = heatclient.Client('1', endpoint, **args)
  323 
  324         return self._heat
  325 
  326     @exception.wrap_keystone_exception
  327     def swift(self):
  328         # Not caching swift connections because of range requests
  329         # Check how glance_store uses swift client for a reference
  330         endpoint_type = get_client_option('swift', 'endpoint_type')
  331         region_name = get_client_option('swift', 'region_name')
  332         args = {
  333             'auth_version': '2.0',
  334             'preauthtoken': self.auth_token,
  335             'preauthurl': self.url_for(service_type='object-store',
  336                                        interface=endpoint_type,
  337                                        region_name=region_name),
  338             'os_options': {'endpoint_type': endpoint_type,
  339                            'region_name': region_name},
  340             'cacert': get_client_option('swift', 'cacert'),
  341             'insecure': get_client_option('swift', 'insecure')
  342         }
  343         return swiftclient.Connection(**args)