]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Standardise client imports
authorZane Bitter <zbitter@redhat.com>
Fri, 14 Dec 2012 21:23:44 +0000 (22:23 +0100)
committerZane Bitter <zbitter@redhat.com>
Fri, 14 Dec 2012 21:25:51 +0000 (22:25 +0100)
Do all client imports once, in the clients module, using the normal
OpenStack idiom for optional imports where appropriate.

Change-Id: I1186eba21159fe20e80c1947779fd164dada5880
Signed-off-by: Zane Bitter <zbitter@redhat.com>
heat/engine/clients.py
heat/engine/resources/eip.py
heat/engine/resources/instance.py
heat/engine/resources/loadbalancer.py
heat/engine/resources/s3.py
heat/engine/resources/security_group.py
heat/engine/resources/volume.py

index 63b4c54a392955ddaa496813a7da6b359e3fb93c..c28f796e9c02bc2c0151fd82ddb1002ed9ab2fde 100644 (file)
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from heat.openstack.common import log as logging
+
+logger = logging.getLogger(__name__)
 
-from novaclient.v1_1 import client as nc
 
-# swiftclient not available in all distributions - make s3 an optional
-# feature
+from heat.common import heat_keystoneclient as hkc
+from novaclient.v1_1 import client as novaclient
 try:
     from swiftclient import client as swiftclient
-    swiftclient_present = True
 except ImportError:
-    swiftclient_present = False
-# quantumclient not available in all distributions - make quantum an optional
-# feature
+    swiftclient = None
+    logger.info('swiftclient not available')
 try:
     from quantumclient.v2_0 import client as quantumclient
-    quantumclient_present = True
 except ImportError:
-    quantumclient_present = False
-
-from heat.common import heat_keystoneclient as kc
-from heat.openstack.common import log as logging
-
-logger = logging.getLogger(__name__)
+    quantumclient = None
+    logger.info('quantumclient not available')
 
 
 class Clients(object):
@@ -53,7 +48,7 @@ class Clients(object):
         if self._keystone:
             return self._keystone
 
-        self._keystone = kc.KeystoneClient(self.context)
+        self._keystone = hkc.KeystoneClient(self.context)
         return self._keystone
 
     def nova(self, service_type='compute'):
@@ -85,20 +80,20 @@ class Clients(object):
             # Workaround for issues with python-keyring, need no_cache=True
             # ref https://bugs.launchpad.net/python-novaclient/+bug/1020238
             # TODO(shardy): May be able to remove when the bug above is fixed
-            client = nc.Client(no_cache=True, **args)
+            client = novaclient.Client(no_cache=True, **args)
             client.authenticate()
             self._nova[service_type] = client
         except TypeError:
             # for compatibility with essex, which doesn't have no_cache=True
             # TODO(shardy): remove when we no longer support essex
-            client = nc.Client(**args)
+            client = novaclient.Client(**args)
             client.authenticate()
             self._nova[service_type] = client
 
         return client
 
     def swift(self):
-        if swiftclient_present == False:
+        if swiftclient is None:
             return None
         if self._swift:
             return self._swift
@@ -137,7 +132,7 @@ class Clients(object):
         return self._swift
 
     def quantum(self):
-        if quantumclient_present == False:
+        if quantumclient is None:
             return None
         if self._quantum:
             logger.debug('using existing _quantum')
index dab1bbc170cd0d8b26959fc470f63af1db0f9a3f..0bcb2e6240fe90623bad0d976df655393f49ed18 100644 (file)
@@ -13,9 +13,9 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from heat.engine import clients
 from heat.common import exception
 from heat.engine import resource
-from novaclient.exceptions import NotFound
 
 from heat.openstack.common import log as logging
 
@@ -36,7 +36,7 @@ class ElasticIp(resource.Resource):
             if self.resource_id is not None:
                 try:
                     ips = self.nova().floating_ips.get(self.resource_id)
-                except NotFound as ex:
+                except clients.novaclient.exceptions.NotFound as ex:
                     logger.warn("Floating IPs not found: %s" % str(ex))
                 else:
                     self.ipaddress = ips.ip
@@ -97,7 +97,7 @@ class ElasticIpAssociation(resource.Resource):
             server = self.nova().servers.get(self.properties['InstanceId'])
             if server:
                 server.remove_floating_ip(self.properties['EIP'])
-        except NotFound as ex:
+        except clients.novaclient.exceptions.NotFound as ex:
             pass
 
 
index 7ff0b0f247f21812dfe5a2337e98915ec1c0ef86..077c087df572c2ba4e25a6374e6bbe2498d6310c 100644 (file)
@@ -18,10 +18,10 @@ import os
 import json
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
-from novaclient.exceptions import NotFound
 import pkgutil
 from urlparse import urlparse
 
+from heat.engine import clients
 from heat.engine import resource
 from heat.common import exception
 
@@ -129,7 +129,7 @@ class Instance(resource.Resource):
         if self.ipaddress is None:
             try:
                 server = self.nova().servers.get(self.resource_id)
-            except NotFound as ex:
+            except clients.novaclient.exceptions.NotFound as ex:
                 logger.warn('Instance IP address not found (%s)' % str(ex))
             else:
                 self._set_ipaddress(server.networks)
@@ -305,14 +305,14 @@ class Instance(resource.Resource):
             return
         try:
             server = self.nova().servers.get(self.resource_id)
-        except NotFound:
+        except clients.novaclient.exceptions.NotFound:
             pass
         else:
             server.delete()
             while server.status == 'ACTIVE':
                 try:
                     server.get()
-                except NotFound:
+                except clients.novaclient.exceptions.NotFound:
                     break
                 eventlet.sleep(0.2)
         self.resource_id = None
index 4ebe535d5956ebee850df43dad430d33c0b893ce..19ea37c68c24926f125831df189de939fd2a5638 100644 (file)
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from heat.engine import clients
 from heat.common import exception
 from heat.common import template_format
 from heat.engine.resources import stack
-from novaclient.exceptions import NotFound
 
 from heat.openstack.common import log as logging
 
@@ -212,7 +212,7 @@ class LoadBalancer(stack.Stack):
         '''
         try:
             server = self.nova().servers.get(inst)
-        except NotFound as ex:
+        except clients.novaclient.exceptions.NotFound as ex:
             logger.warn('Instance (%s) not found: %s' % (inst, str(ex)))
         else:
             for n in server.networks:
index b0c897613fcc7366322fa0f167a12a5137647139..97bd847e493d1d3dc6138888b890ee69b5d0d1ac 100644 (file)
@@ -20,11 +20,7 @@ from urlparse import urlparse
 from heat.common import exception
 from heat.engine import resource
 from heat.openstack.common import log as logging
-try:
-    from swiftclient.client import ClientException
-    swiftclient_present = True
-except ImportError:
-    swiftclient_present = False
+from heat.engine import clients
 
 logger = logging.getLogger(__name__)
 
@@ -55,9 +51,9 @@ class S3Bucket(resource.Resource):
         Validate any of the provided params
         '''
         #check if swiftclient is installed
-        if swiftclient_present == False:
+        if clients.swiftclient is None:
             return {'Error':
-                    'S3 services unavaialble because of missing swiftclient.'}
+                    'S3 services unavailable because of missing swiftclient.'}
 
     @staticmethod
     def _create_container_name(resource_name):
@@ -107,7 +103,7 @@ class S3Bucket(resource.Resource):
         if self.resource_id is not None:
             try:
                 self.swift().delete_container(self.resource_id)
-            except ClientException as ex:
+            except clients.swiftclient.ClientException as ex:
                 logger.warn("Delete container failed: %s" % str(ex))
 
     def FnGetRefId(self):
index d648fa7c1cf407dcb70d1e22aacf1be3af83c393..287c0244d59559ba58beb995461fe84aae9adce0 100644 (file)
@@ -13,8 +13,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-from novaclient.exceptions import BadRequest
-from novaclient.exceptions import NotFound
+from heat.engine import clients
 from heat.engine import resource
 
 from heat.openstack.common import log as logging
@@ -58,7 +57,7 @@ class SecurityGroup(resource.Resource):
                                                i['FromPort'],
                                                i['ToPort'],
                                                i['CidrIp'])
-                except BadRequest as ex:
+                except clients.novaclient.exceptions.BadRequest as ex:
                     if ex.message.find('already exists') >= 0:
                         # no worries, the rule is already there
                         pass
@@ -73,13 +72,13 @@ class SecurityGroup(resource.Resource):
         if self.resource_id is not None:
             try:
                 sec = self.nova().security_groups.get(self.resource_id)
-            except NotFound:
+            except clients.novaclient.exceptions.NotFound:
                 pass
             else:
                 for rule in sec.rules:
                     try:
                         self.nova().security_group_rules.delete(rule['id'])
-                    except NotFound:
+                    except clients.novaclient.exceptions.NotFound:
                         pass
 
                 self.nova().security_groups.delete(sec)
index aace548600757e8fd199485697dca797da3dd729..f4b88f236a30055b8ae2f5b11de097ad8874c22e 100644 (file)
@@ -16,9 +16,9 @@
 import eventlet
 from heat.openstack.common import log as logging
 
+from heat.engine import clients
 from heat.common import exception
 from heat.engine import resource
-from novaclient.exceptions import NotFound
 
 logger = logging.getLogger(__name__)
 
@@ -117,10 +117,9 @@ class VolumeAttachment(resource.Resource):
                 except Exception:
                     pass
                 vol.get()
-        except NotFound as e:
+        except clients.novaclient.exceptions.NotFound as e:
             logger.warning('Deleting VolumeAttachment %s %s - not found' %
                     (server_id, volume_id))
-            return
 
 
 def resource_mapping():