aboutsummaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2016-08-23 13:44:36 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2016-08-26 09:21:06 +0800
commit5f5af6b1d8d0af19db143e67f04f67a643af863c (patch)
treea533329bde635c1fe42f125c587fdc6b1cebed49 /utils
parent3c9c072e82feca7c374aa83fe7cb4466c71823ce (diff)
refactor create or get image process to eliminate reduplicate
Some places when create image is referred, they check if image exist, if exist use it directly, else create a new one. Abstract a method to integrate the process JIRA: FUNCTEST-432 Change-Id: I83d1112aa5cb0d3cdfab92fd49cd5f2c1cceff82 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils')
-rwxr-xr-xutils/openstack_utils.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/utils/openstack_utils.py b/utils/openstack_utils.py
index d30ca629c..a7bc899ab 100755
--- a/utils/openstack_utils.py
+++ b/utils/openstack_utils.py
@@ -889,6 +889,26 @@ def create_glance_image(glance_client, image_name, file_path, disk="qcow2",
return None
+def get_or_create_image(name, path, format):
+ image_exists = False
+ glance_client = get_glance_client()
+
+ image_id = get_image_id(glance_client, name)
+ if image_id != '':
+ logger.info("Using existing image '%s'..." % name)
+ image_exists = True
+ else:
+ logger.info("Creating image '%s' from '%s'..." % (name, path))
+ image_id = create_glance_image(glance_client, name, path, format)
+ if not image_id:
+ logger.error("Failed to create a Glance image...")
+ else:
+ logger.debug("Image '%s' with ID=%s created successfully."
+ % (name, image_id))
+
+ return image_exists, image_id
+
+
def delete_glance_image(nova_client, image_id):
try:
nova_client.images.delete(image_id)