Switch id setters to openstack client

Switch tempest_glance_id_setter and tempest_neutron_id_setter to
use openstack client with credentials provided in tempest config.
This will allow to run puppet-tempest on standalone node but adds
dependancy to openstacklib.

Change-Id: I27215a9b0f94db39774e20515eddce4cffd7cadf
This commit is contained in:
Lukas Bezdicka 2015-11-05 14:38:51 +01:00
parent e6edd08717
commit 4a61da58d6
6 changed files with 115 additions and 19 deletions

View File

@ -0,0 +1,44 @@
require 'puppet/util/inifile'
require 'puppet/provider/openstack'
require 'puppet/provider/openstack/auth'
require 'puppet/provider/openstack/credentials'
class Puppet::Provider::Tempest < Puppet::Provider::Openstack
extend Puppet::Provider::Openstack::Auth
def self.tempest_file
return @tempest_file if @tempest_file
@tempest_file = Puppet::Util::IniConfig::File.new
@tempest_file.read(@file_path)
@tempest_file
end
def self.request(service, action, properties=[], file_path)
@file_path = file_path
begin
super(service, action, properties)
rescue Puppet::Error::OpenstackAuthInputError => error
tempest_request(service, action, error, properties)
end
end
def self.tempest_request(service, action, error, properties=nil)
@credentials.username = tempest_credentials['admin_user']
@credentials.password = tempest_credentials['admin_password']
@credentials.project_name = tempest_credentials['admin_tenant_name']
@credentials.auth_url = tempest_credentials['auth_endpoint']
raise error unless @credentials.set?
Puppet::Provider::Openstack.request(service, action, properties, @credentials)
end
def self.tempest_credentials
t = {}
t['admin_user'] = tempest_file['identity']['admin_username']
t['admin_password'] = tempest_file['identity']['admin_password']
t['admin_tenant_name'] = tempest_file['identity']['admin_tenant_name']
t['auth_endpoint'] = tempest_file['identity']['uri']
return t
end
end

View File

@ -1,6 +1,11 @@
Puppet::Type.type(:tempest_glance_id_setter).provide(:ruby) do
require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/tempest')
# almost entirely lifted from stdlib's file_line
Puppet::Type.type(:tempest_glance_id_setter).provide(
:openstack,
:parent => Puppet::Provider::Tempest
) do
@credentials = Puppet::Provider::Openstack::CredentialsV2_0.new
def exists?
lines.find do |line|
@ -8,13 +13,32 @@ Puppet::Type.type(:tempest_glance_id_setter).provide(:ruby) do
end
end
def file_path
resource[:tempest_conf_path]
end
def create
handle_create_with_match
end
def destroy
handle_create_with_match
end
def get_image_id
@image_id ||= Puppet::Resource.indirection.find("Glance_image/#{resource[:image_name]}")[:id]
@image_id if @image_id != :absent
if resource[:ensure] == :present or resource[:ensure].nil?
if @image_id.nil?
images = self.class.request('image', 'list', file_path)
img = images.detect {|img| img[:name] == resource[:image_name]}
if img.nil?
raise(Puppet::Error, "Image #{resource[:image_name]} not found!")
end
@image_id = img[:id]
end
elsif resource[:ensure] != :absent
raise(Puppet::Error, "Cannot ensure to #{resource[:ensure]}")
end
@image_id
end
def should_line
@ -32,7 +56,7 @@ Puppet::Type.type(:tempest_glance_id_setter).provide(:ruby) do
file = lines
case match_count
when 1
File.open(resource[:tempest_conf_path], 'w') do |fh|
File.open(file_path, 'w') do |fh|
lines.each do |l|
fh.puts(regex.match(l) ? "#{should_line}" : l)
end
@ -44,10 +68,10 @@ Puppet::Type.type(:tempest_glance_id_setter).provide(:ruby) do
else
file.insert(block_pos+1, "#{should_line}\n")
end
File.write(resource[:tempest_conf_path], file.join)
File.write(file_path, file.join)
else # cannot be negative.
raise Puppet::Error, "More than one line in file \
'#{resource[:tempest_conf_path]}' matches pattern '#{regex}'"
'#{file_path}' matches pattern '#{regex}'"
end
end
@ -58,7 +82,7 @@ Puppet::Type.type(:tempest_glance_id_setter).provide(:ruby) do
# file; for now assuming that this type is only used on
# small-ish config files that can fit into memory without
# too much trouble.
@lines ||= File.readlines(resource[:tempest_conf_path])
@lines ||= File.readlines(file_path)
end
end

View File

@ -1,6 +1,11 @@
Puppet::Type.type(:tempest_neutron_net_id_setter).provide(:ruby) do
require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/tempest')
# almost entirely lifted from stdlib's file_line
Puppet::Type.type(:tempest_neutron_net_id_setter).provide(
:openstack,
:parent => Puppet::Provider::Tempest
) do
@credentials = Puppet::Provider::Openstack::CredentialsV2_0.new
def exists?
lines.find do |line|
@ -8,13 +13,32 @@ Puppet::Type.type(:tempest_neutron_net_id_setter).provide(:ruby) do
end
end
def file_path
resource[:tempest_conf_path]
end
def create
handle_create_with_match
end
def destroy
handle_create_with_match
end
def get_network_id
@network_id ||= Puppet::Resource.indirection.find("Neutron_network/#{@resource[:network_name]}")[:id]
@network_id if @network_id != :absent
if resource[:ensure] == :present or resource[:ensure].nil?
if @network_id.nil?
nets = self.class.request('network', 'list', file_path)
net = nets.detect {|img| img[:name] == resource[:network_name]}
if net.nil?
raise(Puppet::Error, "Network #{resource[:network_name]} not found!")
end
@network_id = net[:id]
end
elsif resource[:ensure] != :absent
raise(Puppet::Error, "Cannot ensure to #{resource[:ensure]}")
end
@network_id
end
def should_line
@ -31,7 +55,7 @@ Puppet::Type.type(:tempest_neutron_net_id_setter).provide(:ruby) do
file = lines
case match_count
when 1
File.open(resource[:tempest_conf_path], 'w') do |fh|
File.open(file_path, 'w') do |fh|
lines.each do |l|
fh.puts(regex.match(l) ? should_line : l)
end
@ -43,10 +67,10 @@ Puppet::Type.type(:tempest_neutron_net_id_setter).provide(:ruby) do
else
file.insert(block_pos+1, "#{should_line}\n")
end
File.write(resource[:tempest_conf_path], file.join)
File.write(file_path, file.join)
else # cannot be negative.
raise Puppet::Error, "More than one line in file \
'#{resource[:tempest_conf_path]}' matches pattern '#{regex}'"
'#{file_path}' matches pattern '#{regex}'"
end
end
@ -57,7 +81,7 @@ Puppet::Type.type(:tempest_neutron_net_id_setter).provide(:ruby) do
# file; for now assuming that this type is only used on
# small-ish config files that can fit into memory without
# too much trouble.
@lines ||= File.readlines(resource[:tempest_conf_path])
@lines ||= File.readlines(file_path)
end
end

View File

@ -348,6 +348,7 @@ class tempest(
}
Glance_image<||> -> Tempest_glance_id_setter['image_ref']
Tempest_config<||> -> Tempest_glance_id_setter['image_ref']
Keystone_user_role<||> -> Tempest_glance_id_setter['image_ref']
} elsif ($image_name and $image_ref) or (! $image_name and ! $image_ref) {
fail('A value for either image_name or image_ref must be provided.')
}
@ -359,6 +360,7 @@ class tempest(
}
Glance_image<||> -> Tempest_glance_id_setter['image_ref_alt']
Tempest_config<||> -> Tempest_glance_id_setter['image_ref_alt']
Keystone_user_role<||> -> Tempest_glance_id_setter['image_ref_alt']
} elsif ($image_name_alt and $image_ref_alt) or (! $image_name_alt and ! $image_ref_alt) {
fail('A value for either image_name_alt or image_ref_alt must \
be provided.')
@ -374,6 +376,7 @@ be provided.')
}
Neutron_network<||> -> Tempest_neutron_net_id_setter['public_network_id']
Tempest_config<||> -> Tempest_neutron_net_id_setter['public_network_id']
Keystone_user_role<||> -> Tempest_neutron_net_id_setter['public_network_id']
} elsif ($public_network_name and $public_network_id) or (! $public_network_name and ! $public_network_id) {
fail('A value for either public_network_id or public_network_name \
must be provided.')

View File

@ -33,6 +33,7 @@
"dependencies": [
{ "name": "puppetlabs/inifile", "version_requirement": ">=1.0.0 <2.0.0" },
{ "name": "puppetlabs/stdlib", "version_requirement": ">=4.0.0 <5.0.0" },
{ "name": "puppetlabs/vcsrepo", "version_requirement": ">=0.1.2 <2.0.0"}
{ "name": "puppetlabs/vcsrepo", "version_requirement": ">=0.1.2 <2.0.0"},
{ "name": "openstack/openstacklib", "version_requirement": ">= 7.0.0 <8.0.0" }
]
}

View File

@ -3,9 +3,9 @@ require 'puppet'
describe 'Providers' do
glance_provider_class =
Puppet::Type.type(:tempest_glance_id_setter).provider(:ruby)
Puppet::Type.type(:tempest_glance_id_setter).provider(:openstack)
network_provider_class =
Puppet::Type.type(:tempest_neutron_net_id_setter).provider(:ruby)
Puppet::Type.type(:tempest_neutron_net_id_setter).provider(:openstack)
include PuppetlabsSpec::Files
let(:tmpfile) { tmpfilename('ini_setting_test') }