From 0742d2c9eeb2943fe351429ae161ae0af0562c62 Mon Sep 17 00:00:00 2001 From: Matt Thompson Date: Wed, 21 May 2014 16:42:35 +0100 Subject: [PATCH] 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 ". Currently, it only seems to work with commands like "keystone user-list". Change-Id: Ie4b063340e813ac50cbd4b21e41cc3fa65eebeca --- CHANGELOG.md | 3 +++ libraries/cli.rb | 23 ++++++++++++++++++++++- metadata.rb | 2 +- spec/cli_spec.rb | 27 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b11d6707..02b164d5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # CHANGELOG for cookbook-openstack-common This file is used to list changes made in each version of cookbook-openstack-common. +## 9.5.0 +* Add new image_id cli library method for obtaining glance ID from image name + ## 9.4.1 * Fix to allow database connection options for telemetry nosql diff --git a/libraries/cli.rb b/libraries/cli.rb index 9a752c25..4d1bfba6 100755 --- a/libraries/cli.rb +++ b/libraries/cli.rb @@ -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 + 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 diff --git a/metadata.rb b/metadata.rb index 5c08a50a..f5e794f2 100755 --- a/metadata.rb +++ b/metadata.rb @@ -4,7 +4,7 @@ maintainer_email 'cookbooks@lists.tfoundry.com' license 'Apache 2.0' description 'Common OpenStack attributes, libraries and recipes.' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '9.4.1' +version '9.5.0' recipe 'openstack-common', 'Installs/Configures common recipes' recipe 'openstack-common::set_endpoints_by_interface', 'Set endpoints by interface' diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index 932fb3a4..defa5d5a 100755 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -100,5 +100,32 @@ describe 'openstack-common::default' do expect(result).to eq('1234567890ABCDEFGH') end end + + describe 'image_id' do + let(:env) do + { + 'OS_USERNAME' => 'name', + 'OS_PASSWORD' => 'pass', + 'OS_TENANT_NAME' => 'tenant', + 'OS_AUTH_URL' => 'http://127.0.0.1:35357/v2.0' + } + end + + it 'runs glance command to query valid id' do + subject.stub(:openstack_command).with('glance', 'image-show cirros', :env, {}) + subject.stub(:prettytable_to_array) + .and_return([{ 'id' => '87f38e15-9737-46cc-a612-7c67ee29a24f', 'name' => 'cirros' }]) + + result = subject.image_id('cirros', :env) + expect(result).to eq('87f38e15-9737-46cc-a612-7c67ee29a24f') + end + + it 'runs glance command to query invalid id' do + subject.stub(:openstack_command).with('glance', 'image-show test', :env, {}) + .and_raise("No image with a name or ID of 'test' exists. (1)") + + expect { subject.image_id('test', :env) }.to raise_error + end + end end end