simplify get_xx process using query.find() 61/30661/9
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Thu, 16 Mar 2017 08:59:48 +0000 (16:59 +0800)
committerSerena Feng <feng.xiaowei@zte.com.cn>
Thu, 23 Mar 2017 06:26:26 +0000 (06:26 +0000)
Change-Id: I2a911fc15c1456b409db840b9ae76c04a23d449d
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
deploy/post/glance.py
deploy/post/neutron.py
deploy/post/nova.py

index 7ea4b51..4171c8a 100644 (file)
@@ -10,6 +10,7 @@ import os
 
 import glanceclient
 
+from deploy.common import query
 import keystoneauth
 
 
@@ -35,11 +36,7 @@ class Glance(keystoneauth.Keystoneauth):
         return id
 
     def get_by_name(self, name):
-        for image in self.list():
-            if image.name == name:
-                return image.id
-
-        return None
+        return query.find(lambda image: image.name == name, self.list())
 
     def list(self):
         return self.controller.list()
index e9cea8b..dc2e30b 100644 (file)
@@ -8,6 +8,7 @@
 ##############################################################################
 from neutronclient.neutron import client as neutronclient
 
+from deploy.common import query
 import keystoneauth
 
 
@@ -17,14 +18,14 @@ class Neutron(object):
         self.client = neutronclient.Client(api_v, session=session)
 
     def create_network(self, name, body):
-        if not self.is_network_exist(name):
+        if not self.get_network_by_name(name):
             return self._create_network(name, body)
         else:
             print('admin_ext [{}] already exist'.format(name))
             return None
 
     def create_subnet(self, body=None):
-        if not self.is_subnet_exist(body):
+        if not self.get_subnet_by_name(body):
             return self._create_subnet(body)
         else:
             print ('subnet [{}] already exist'.format(body))
@@ -36,20 +37,18 @@ class Neutron(object):
     def list_subnets(self):
         return self.client.list_subnets()['subnets']
 
-    def is_network_exist(self, name):
-        return [] != filter(lambda n: n['name'] == name, self.list_networks())
+    def get_network_by_name(self, name):
+        return query.find(lambda nw: nw['name'] == name, self.list_networks())
 
-    def is_subnet_exist(self, body):
-        print 'body: {}'.format(body)
-
-        def same_subnet(n):
-            print 'n: {}'.format(n)
+    def get_subnet_by_name(self, body):
+        def _same_subnet(this, that):
             for item in ['name', 'network_id']:
-                if n[item] != body['subnets'][0][item]:
+                if this[item] != that[item]:
                     return False
             return True
 
-        return [] != filter(lambda n: same_subnet(n), self.list_subnets())
+        return query.find(lambda n: _same_subnet(n, body['subnets'][0]),
+                          self.list_subnets())
 
     def _create_network(self, name, body):
         try:
index 0ab42e2..6f5eae9 100644 (file)
@@ -8,6 +8,7 @@
 ##############################################################################
 import novaclient.client
 
+from deploy.common import query
 import keystoneauth
 
 
@@ -23,11 +24,8 @@ class Nova(keystoneauth.Keystoneauth):
         return flavor.id
 
     def get_flavor_by_name(self, name):
-        for flavor in self.list_flavors():
-            if flavor.name == name:
-                return flavor.id
-
-        return None
+        return query.find(lambda flavor: flavor.name == name,
+                          self.list_flavors())
 
     def list_flavors(self):
         return self.flavors.list(detailed=True)