From 84b0be2e12d83a48ac15b3e4f457b7e7fb465735 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Thu, 16 Mar 2017 16:59:48 +0800 Subject: simplify get_xx process using query.find() Change-Id: I2a911fc15c1456b409db840b9ae76c04a23d449d Signed-off-by: SerenaFeng --- deploy/post/glance.py | 7 ++----- deploy/post/neutron.py | 21 ++++++++++----------- deploy/post/nova.py | 8 +++----- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/deploy/post/glance.py b/deploy/post/glance.py index 7ea4b51d..4171c8a8 100644 --- a/deploy/post/glance.py +++ b/deploy/post/glance.py @@ -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() diff --git a/deploy/post/neutron.py b/deploy/post/neutron.py index e9cea8b0..dc2e30b2 100644 --- a/deploy/post/neutron.py +++ b/deploy/post/neutron.py @@ -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: diff --git a/deploy/post/nova.py b/deploy/post/nova.py index 0ab42e27..6f5eae91 100644 --- a/deploy/post/nova.py +++ b/deploy/post/nova.py @@ -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) -- cgit 1.2.3-korg