Add image_id method and update openstack_command

This change adds a new method to libraries/cli.rb which we will use
in new cookbook-openstack-integration-test to obtain a glance image id
from a glance image name.

Additionally, we've had to update openstack-command method to handle
situation where we have commands like "glance image-show <id|name>".
Currently, it only seems to work with commands like
"keystone user-list".

Change-Id: Ie4b063340e813ac50cbd4b21e41cc3fa65eebeca
This commit is contained in:
Matt Thompson
2014-05-21 16:42:35 +01:00
parent 2dd1681754
commit 0742d2c9ee
4 changed files with 53 additions and 2 deletions

View File

@@ -53,7 +53,11 @@ module ::Openstack # rubocop:disable Documentation
# update the provider to use this.
#
def openstack_command(cmd, options = '', env = {}, args = {})
openstackcmd = [cmd] << options
# NOTE: Here we split options (which creates an array) and then merge that
# array into [cmd]. This is done to accomdate cmd + options like:
# keystone user-list
# glance image-show <id|name>
openstackcmd = [cmd].concat(options.split)
args.each do |key, val|
openstackcmd << "--#{key}" << val.to_s
end
@@ -87,4 +91,21 @@ module ::Openstack # rubocop:disable Documentation
end
nil
end
# return id for a glance image.
#
# @param [String] name of image
# @param [Hash] environment to use.
# @return [String] id or nil
def image_id(name, env, args = {})
begin
output = openstack_command('glance', "image-show #{name}", env, args)
prettytable_to_array(output).each do |obj|
return obj['id'] if obj.key?('id')
end
rescue RuntimeError => e
raise "Could not lookup ID for image #{name}. Error was #{e.message}"
end
nil
end
end