Add network-related CLI commands

This change introduced the ability to query UUIDs for Neutron entities:
routers, (sub)networks, etc.

To do so, the identity_uuid method was generalised and the image_id
method rewritten to make use of the new get_uuid method.

Change-Id: Idf9f4f04a8ca482ab23c0c667c3546437829690a
This commit is contained in:
Stephan Renatus 2014-06-18 12:14:20 +02:00
parent f62f72ee7c
commit 1a3c689d6d
4 changed files with 67 additions and 28 deletions

@ -2,6 +2,9 @@
This file is used to list changes made in each version of cookbook-openstack-common.
## 9.7.0
* Add new network_uuid cli library method for obtaining ID from various neutron resources
## 9.6.1
* Add python_packages attribute for sqlite

@ -49,9 +49,6 @@ module ::Openstack # rubocop:disable Documentation
# @param [Hash] optional command argument/values pairs
# @return [String] stdout or fail
#
# TODO: this was taken from the identity register provider, will need to
# update the provider to use this.
#
def openstack_command(cmd, options = '', env = {}, args = {})
# NOTE: Here we split options (which creates an array) and then merge that
# array into [cmd]. This is done to accomdate cmd + options like:
@ -67,6 +64,29 @@ module ::Openstack # rubocop:disable Documentation
result.stdout
end
# return uuid for a resource.
#
# @param [String] client of resource (keystone, neutron, glance, ...)
# @param [String] type of resource (user, service, tenant, endpoint, role; net, subnet, router, ...)
# @param [String] key of resource to match
# @param [String] value of resource key to match
# @param [Hash] environment to use.
# @param [Hash] optional command argument/values pairs
# @param [String] optional uuid field to match
# @return [String] uuid or nil
#
def get_uuid(client, type, key, value, env, args = {}, uuid_field = 'id') # rubocop: disable ParameterLists
begin
output = openstack_command(client, "#{type}-list", env, args)
prettytable_to_array(output).each do |obj|
return obj[uuid_field] if obj.key?(uuid_field) && obj[key] == value
end
rescue RuntimeError => e
raise "Could not lookup uuid for #{type}:#{key}=>#{value}. Error was #{e.message}"
end
nil
end
# return uuid for an identity resource.
#
# @param [String] type of resource (user, service, tenant, endpoint, role)
@ -77,35 +97,33 @@ module ::Openstack # rubocop:disable Documentation
# @param [String] optional uuid field to match
# @return [String] uuid or nil
#
# TODO: this was taken from the identity register provider, will need to
# update the provider to use this.
# TODO: update openstack-identity register provider to use these functions.
#
def identity_uuid(type, key, value, env, args = {}, uuid_field = 'id') # rubocop: disable ParameterLists
begin
output = openstack_command('keystone', "#{type}-list", env, args)
prettytable_to_array(output).each do |obj|
return obj[uuid_field] if obj.key?(uuid_field) && obj[key] == value
end
rescue RuntimeError => e
raise "Could not lookup uuid for #{type}:#{key}=>#{value}. Error was #{e.message}"
end
nil
def identity_uuid(*args)
get_uuid('keystone', *args)
end
# return id for a glance image.
#
# @param [String] name of image
# @param [Hash] environment to use.
# @param [Hash] optional command argument/values pairs
# @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
get_uuid('glance', 'image', 'Name', name, env, args, 'ID')
end
# return uuid for a network resource.
#
# @param [String] type of resource (net, subnet, router, port, ...)
# @param [String] key of resource to match
# @param [String] value of resource key to match
# @param [Hash] environment to use.
# @param [Hash] optional command argument/values pairs
# @param [String] optional uuid field to match
# @return [String] uuid or nil
#
def network_uuid(*args)
get_uuid('neutron', *args)
end
end

@ -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.6.1'
version '9.7.0'
recipe 'openstack-common', 'Installs/Configures common recipes'
recipe 'openstack-common::set_endpoints_by_interface', 'Set endpoints by interface'

@ -112,20 +112,38 @@ describe 'openstack-common::default' do
end
it 'runs glance command to query valid id' do
subject.stub(:openstack_command).with('glance', 'image-show cirros', :env, {})
subject.stub(:openstack_command).with('glance', 'image-list', :env, {})
subject.stub(:prettytable_to_array)
.and_return([{ 'id' => '87f38e15-9737-46cc-a612-7c67ee29a24f', 'name' => 'cirros' }])
.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, {})
subject.stub(:openstack_command).with('glance', 'image-list', :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
describe 'network_uuid' do
it 'runs network command to query uuid' do
env =
{
'OS_USERNAME' => 'name',
'OS_PASSWORD' => 'pass',
'OS_TENANT_NAME' => 'tenant',
'OS_AUTH_URL' => 'http://127.0.0.1:35357/v2.0'
}
subject.stub(:openstack_command).with('neutron', 'net-list', env, {})
subject.stub(:prettytable_to_array)
.and_return([{ 'name' => 'net1', 'id' => '1234567890ABCDEFGH' }])
result = subject.network_uuid('net', 'name', 'net1', env)
expect(result).to eq('1234567890ABCDEFGH')
end
end
end
end