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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user