"Fossies" - the Fresh Open Source Software Archive

Member "zun-4.0.2/zun/common/clients.py" (1 Feb 2021, 5705 Bytes) of package /linux/misc/openstack/zun-4.0.2.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 and the latest Fossies "Diffs" side-by-side code changes report: 4.0.1_vs_4.0.2.

    1 # Copyright 2016 Intel.
    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 cinderclient import client as cinderclient
   16 from glanceclient import client as glanceclient
   17 from keystoneauth1.loading import adapter as ka_adapter
   18 from neutronclient.v2_0 import client as neutronclient
   19 
   20 from zun.common import exception
   21 from zun.common import keystone
   22 import zun.conf
   23 
   24 
   25 class OpenStackClients(object):
   26     """Convenience class to create and cache client instances."""
   27 
   28     def __init__(self, context):
   29         self.context = context
   30         self._keystone = None
   31         self._glance = None
   32         self._neutron = None
   33         self._cinder = None
   34         self._placement = None
   35         self._placement_ks_filter = None
   36 
   37     def url_for(self, **kwargs):
   38         return self.keystone().session.get_endpoint(**kwargs)
   39 
   40     def zun_url(self):
   41         endpoint_type = self._get_client_option('zun', 'endpoint_type')
   42         region_name = self._get_client_option('zun', 'region_name')
   43         return self.url_for(service_type='container',
   44                             interface=endpoint_type,
   45                             region_name=region_name)
   46 
   47     @property
   48     def auth_token(self):
   49         return self.context.auth_token or self.keystone().auth_token
   50 
   51     def keystone(self):
   52         if self._keystone:
   53             return self._keystone
   54 
   55         self._keystone = keystone.KeystoneClientV3(self.context)
   56         return self._keystone
   57 
   58     def _get_client_option(self, client, option):
   59         return getattr(getattr(zun.conf.CONF, '%s_client' % client), option)
   60 
   61     @exception.wrap_keystone_exception
   62     def glance(self):
   63         if self._glance:
   64             return self._glance
   65 
   66         glanceclient_version = self._get_client_option('glance', 'api_version')
   67         session = self.keystone().session
   68 
   69         endpoint_type = self._get_client_option('glance', 'endpoint_type')
   70         region_name = self._get_client_option('glance', 'region_name')
   71         glance_url = self.url_for(service_type='image',
   72                                   interface=endpoint_type,
   73                                   region_name=region_name)
   74 
   75         self._glance = glanceclient.Client(glanceclient_version,
   76                                            endpoint=glance_url,
   77                                            session=session)
   78 
   79         return self._glance
   80 
   81     @exception.wrap_keystone_exception
   82     def neutron(self):
   83         if self._neutron:
   84             return self._neutron
   85 
   86         session = self.keystone().session
   87         if self._get_client_option('neutron', 'ca_file'):
   88             session.verify = self._get_client_option('neutron', 'ca_file')
   89         if self._get_client_option('neutron', 'insecure'):
   90             session.verify = False
   91         endpoint_type = self._get_client_option('neutron', 'endpoint_type')
   92         region_name = self._get_client_option('neutron', 'region_name')
   93         self._neutron = neutronclient.Client(session=session,
   94                                              endpoint_type=endpoint_type,
   95                                              region_name=region_name)
   96 
   97         return self._neutron
   98 
   99     @exception.wrap_keystone_exception
  100     def cinder(self):
  101         if self._cinder:
  102             return self._cinder
  103 
  104         session = self.keystone().session
  105         if self._get_client_option('cinder', 'ca_file'):
  106             session.verify = self._get_client_option('cinder', 'ca_file')
  107         if self._get_client_option('cinder', 'insecure'):
  108             session.verify = False
  109         cinder_api_version = self._get_client_option('cinder', 'api_version')
  110         region_name = self._get_client_option('cinder', 'region_name')
  111         endpoint_type = self._get_client_option('cinder', 'endpoint_type')
  112         kwargs = {
  113             'session': self.keystone().session,
  114             'endpoint_type': endpoint_type,
  115             'region_name': region_name,
  116             'cacert': self._get_client_option('cinder', 'ca_file'),
  117             'insecure': self._get_client_option('cinder', 'insecure')
  118         }
  119         self._cinder = cinderclient.Client(version=cinder_api_version,
  120                                            **kwargs)
  121 
  122         return self._cinder
  123 
  124     @exception.wrap_keystone_exception
  125     def placement(self):
  126         if self._placement:
  127             return self._placement, self._placement_ks_filter
  128 
  129         session = self.keystone().session
  130         if self._get_client_option('placement', 'ca_file'):
  131             session.verify = self._get_client_option('placement', 'ca_file')
  132         if self._get_client_option('placement', 'insecure'):
  133             session.verify = False
  134         region_name = self._get_client_option('placement', 'region_name')
  135         endpoint_type = self._get_client_option('placement', 'endpoint_type')
  136         kwargs = {
  137             'session': self.keystone().session,
  138             'auth': self.keystone().auth,
  139         }
  140         self._placement_ks_filter = {'service_type': 'placement',
  141                                      'region_name': region_name,
  142                                      'interface': endpoint_type}
  143         self._placement = ka_adapter.Adapter().load_from_options(**kwargs)
  144 
  145         return self._placement, self._placement_ks_filter