Remove unused functions from base provider
These functions in the base provider has been unused. Change-Id: I265ddb8236335abfabc01abd30eafaa2cda6712d
This commit is contained in:
@@ -1,157 +1,9 @@
|
|||||||
require 'csv'
|
|
||||||
require 'puppet/util/inifile'
|
|
||||||
require 'puppet/provider/openstack'
|
require 'puppet/provider/openstack'
|
||||||
require 'puppet/provider/openstack/auth'
|
require 'puppet/provider/openstack/auth'
|
||||||
require 'puppet/provider/openstack/credentials'
|
require 'puppet/provider/openstack/credentials'
|
||||||
|
|
||||||
class Puppet::Provider::Ironic < Puppet::Provider
|
class Puppet::Provider::Ironic < Puppet::Provider
|
||||||
|
|
||||||
def self.conf_filename
|
|
||||||
'/etc/ironic/ironic.conf'
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.withenv(hash, &block)
|
|
||||||
saved = ENV.to_hash
|
|
||||||
hash.each do |name, val|
|
|
||||||
ENV[name.to_s] = val
|
|
||||||
end
|
|
||||||
|
|
||||||
yield
|
|
||||||
ensure
|
|
||||||
ENV.clear
|
|
||||||
saved.each do |name, val|
|
|
||||||
ENV[name] = val
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.ironic_credentials
|
|
||||||
@ironic_credentials ||= get_ironic_credentials
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.get_ironic_credentials
|
|
||||||
auth_keys = ['auth_url', 'project_name', 'username', 'password']
|
|
||||||
conf = ironic_conf
|
|
||||||
if conf and conf['keystone_authtoken'] and
|
|
||||||
auth_keys.all?{|k| !conf['keystone_authtoken'][k].nil?}
|
|
||||||
creds = Hash[ auth_keys.map \
|
|
||||||
{ |k| [k, conf['keystone_authtoken'][k].strip] } ]
|
|
||||||
if !conf['keystone_authtoken']['project_domain_name'].nil?
|
|
||||||
creds['project_domain_name'] = conf['keystone_authtoken']['project_domain_name'].strip
|
|
||||||
else
|
|
||||||
creds['project_domain_name'] = 'Default'
|
|
||||||
end
|
|
||||||
if !conf['keystone_authtoken']['user_domain_name'].nil?
|
|
||||||
creds['user_domain_name'] = conf['keystone_authtoken']['user_domain_name'].strip
|
|
||||||
else
|
|
||||||
creds['user_domain_name'] = 'Default'
|
|
||||||
end
|
|
||||||
return creds
|
|
||||||
else
|
|
||||||
raise(Puppet::Error, "File: #{conf_filename} does not contain all \
|
|
||||||
required sections. Ironic types will not work if ironic is not \
|
|
||||||
correctly configured.")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def ironic_credentials
|
|
||||||
self.class.ironic_credentials
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.ironic_conf
|
|
||||||
return @ironic_conf if @ironic_conf
|
|
||||||
@ironic_conf = Puppet::Util::IniConfig::File.new
|
|
||||||
@ironic_conf.read(conf_filename)
|
|
||||||
@ironic_conf
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.auth_ironic(*args)
|
|
||||||
q = ironic_credentials
|
|
||||||
authenv = {
|
|
||||||
:OS_AUTH_URL => q['auth_url'],
|
|
||||||
:OS_USERNAME => q['username'],
|
|
||||||
:OS_PROJECT_NAME => q['project_name'],
|
|
||||||
:OS_PASSWORD => q['password'],
|
|
||||||
:OS_PROJECT_DOMAIN_NAME => q['project_domain_name'],
|
|
||||||
:OS_USER_DOMAIN_NAME => q['user_domain_name'],
|
|
||||||
}
|
|
||||||
begin
|
|
||||||
withenv authenv do
|
|
||||||
ironic(args)
|
|
||||||
end
|
|
||||||
rescue Exception => e
|
|
||||||
if (e.message =~ /\[Errno 111\] Connection refused/) or
|
|
||||||
(e.message =~ /\(HTTP 400\)/)
|
|
||||||
sleep 10
|
|
||||||
withenv authenv do
|
|
||||||
ironic(args)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
raise(e)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def auth_ironic(*args)
|
|
||||||
self.class.auth_ironic(args)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.reset
|
|
||||||
@ironic_conf = nil
|
|
||||||
@ironic_credentials = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.list_ironic_resources(type)
|
|
||||||
ids = []
|
|
||||||
list = auth_ironic("#{type}-list", '--format=csv',
|
|
||||||
'--column=id', '--quote=none')
|
|
||||||
(list.split("\n")[1..-1] || []).compact.collect do |line|
|
|
||||||
ids << line.strip
|
|
||||||
end
|
|
||||||
return ids
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.get_ironic_resource_attrs(type, id)
|
|
||||||
attrs = {}
|
|
||||||
net = auth_ironic("#{type}-show", '--format=shell', id)
|
|
||||||
last_key = nil
|
|
||||||
(net.split("\n") || []).compact.collect do |line|
|
|
||||||
if line.include? '='
|
|
||||||
k, v = line.split('=', 2)
|
|
||||||
attrs[k] = v.gsub(/\A"|"\Z/, '')
|
|
||||||
last_key = k
|
|
||||||
else
|
|
||||||
# Handle the case of a list of values
|
|
||||||
v = line.gsub(/\A"|"\Z/, '')
|
|
||||||
attrs[last_key] = [attrs[last_key], v]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return attrs
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.get_tenant_id(catalog, name)
|
|
||||||
instance_type = 'keystone_tenant'
|
|
||||||
instance = catalog.resource("#{instance_type.capitalize!}[#{name}]")
|
|
||||||
if ! instance
|
|
||||||
instance = Puppet::Type.type(instance_type).instances.find do |i|
|
|
||||||
i.provider.name == name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if instance
|
|
||||||
return instance.provider.id
|
|
||||||
else
|
|
||||||
fail("Unable to find #{instance_type} for name #{name}")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.parse_creation_output(data)
|
|
||||||
hash = {}
|
|
||||||
data.split("\n").compact.each do |line|
|
|
||||||
if line.include? '='
|
|
||||||
hash[line.split('=').first] = line.split('=', 2)[1].gsub(/\A"|"\Z/, '')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
hash
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Puppet::Provider::Ironic::OpenstackRequest
|
class Puppet::Provider::Ironic::OpenstackRequest
|
||||||
|
@@ -1,105 +0,0 @@
|
|||||||
require 'puppet'
|
|
||||||
require 'spec_helper'
|
|
||||||
require 'puppet/provider/ironic'
|
|
||||||
require 'tempfile'
|
|
||||||
|
|
||||||
describe Puppet::Provider::Ironic do
|
|
||||||
|
|
||||||
def klass
|
|
||||||
described_class
|
|
||||||
end
|
|
||||||
|
|
||||||
let :credential_hash do
|
|
||||||
{
|
|
||||||
'project_name' => 'admin_tenant',
|
|
||||||
'username' => 'admin',
|
|
||||||
'password' => 'password',
|
|
||||||
'auth_url' => 'https://192.168.56.210:5000/',
|
|
||||||
'project_domain_name' => 'admin_tenant_domain',
|
|
||||||
'user_domain_name' => 'admin_domain',
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
let :credential_error do
|
|
||||||
/Ironic types will not work/
|
|
||||||
end
|
|
||||||
|
|
||||||
after :each do
|
|
||||||
klass.reset
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'when determining credentials' do
|
|
||||||
|
|
||||||
it 'should fail if config is empty' do
|
|
||||||
conf = {}
|
|
||||||
expect(klass).to receive(:ironic_conf).and_return(conf)
|
|
||||||
expect do
|
|
||||||
klass.ironic_credentials
|
|
||||||
end.to raise_error(Puppet::Error, credential_error)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should fail if config does not have keystone_authtoken section.' do
|
|
||||||
conf = {'foo' => 'bar'}
|
|
||||||
expect(klass).to receive(:ironic_conf).and_return(conf)
|
|
||||||
expect do
|
|
||||||
klass.ironic_credentials
|
|
||||||
end.to raise_error(Puppet::Error, credential_error)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should fail if config does not contain all auth params' do
|
|
||||||
conf = {'keystone_authtoken' => {'invalid_value' => 'foo'}}
|
|
||||||
expect(klass).to receive(:ironic_conf).and_return(conf)
|
|
||||||
expect do
|
|
||||||
klass.ironic_credentials
|
|
||||||
end.to raise_error(Puppet::Error, credential_error)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'when invoking the ironic cli' do
|
|
||||||
|
|
||||||
it 'should set auth credentials in the environment' do
|
|
||||||
authenv = {
|
|
||||||
:OS_AUTH_URL => credential_hash['auth_url'],
|
|
||||||
:OS_USERNAME => credential_hash['username'],
|
|
||||||
:OS_PROJECT_NAME => credential_hash['project_name'],
|
|
||||||
:OS_PASSWORD => credential_hash['password'],
|
|
||||||
:OS_PROJECT_DOMAIN_NAME => credential_hash['project_domain_name'],
|
|
||||||
:OS_USER_DOMAIN_NAME => credential_hash['user_domain_name'],
|
|
||||||
}
|
|
||||||
expect(klass).to receive(:get_ironic_credentials).with(no_args).and_return(credential_hash)
|
|
||||||
expect(klass).to receive(:withenv).with(authenv)
|
|
||||||
klass.auth_ironic('test_retries')
|
|
||||||
end
|
|
||||||
|
|
||||||
['[Errno 111] Connection refused',
|
|
||||||
'(HTTP 400)'].reverse.each do |valid_message|
|
|
||||||
it "should retry when ironic cli returns with error #{valid_message}" do
|
|
||||||
expect(klass).to receive(:get_ironic_credentials).with(no_args).and_return({})
|
|
||||||
expect(klass).to receive(:sleep).with(10).and_return(nil)
|
|
||||||
expect(klass).to receive(:ironic).with(['test_retries']).and_invoke(
|
|
||||||
lambda { |x| raise valid_message },
|
|
||||||
lambda { |x| return '' }
|
|
||||||
)
|
|
||||||
klass.auth_ironic('test_retries')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'when listing ironic resources' do
|
|
||||||
|
|
||||||
it 'should exclude the column header' do
|
|
||||||
output = <<-EOT
|
|
||||||
id
|
|
||||||
net1
|
|
||||||
net2
|
|
||||||
EOT
|
|
||||||
expect(klass).to receive(:auth_ironic).and_return(output)
|
|
||||||
result = klass.list_ironic_resources('foo')
|
|
||||||
expect(result).to eql(['net1', 'net2'])
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
Reference in New Issue
Block a user