Implement better nova_admin_tenant_id_setter exists? method

Actually do some checking in the exists? method here to see if the
neutron.conf nova_admin_tenant_id setting is correct already.  That
way Puppet can properly recognize when the resource is already in
the right state.

Change-Id: I9692f4689683298cba4af1cacd700975f7e50b17
Closes-bug: 1392862
This commit is contained in:
Mike Dorman 2014-11-14 16:06:46 -07:00
parent 663b4b16d2
commit 4914498877
2 changed files with 24 additions and 2 deletions

View File

@ -12,6 +12,7 @@ require 'rubygems'
require 'net/http' require 'net/http'
require 'net/https' require 'net/https'
require 'json' require 'json'
require 'puppet/util/inifile'
class KeystoneError < Puppet::Error class KeystoneError < Puppet::Error
end end
@ -126,6 +127,8 @@ def keystone_v2_tenants(auth_url,
end end
Puppet::Type.type(:nova_admin_tenant_id_setter).provide(:ruby) do Puppet::Type.type(:nova_admin_tenant_id_setter).provide(:ruby) do
@tenant_id = nil
def authenticate def authenticate
keystone_v2_authenticate( keystone_v2_authenticate(
@resource[:auth_url], @resource[:auth_url],
@ -144,13 +147,19 @@ Puppet::Type.type(:nova_admin_tenant_id_setter).provide(:ruby) do
end end
def exists? def exists?
false ini_file = Puppet::Util::IniConfig::File.new
ini_file.read("/etc/neutron/neutron.conf")
ini_file['DEFAULT'] && ini_file['DEFAULT']['nova_admin_tenant_id'] && ini_file['DEFAULT']['nova_admin_tenant_id'] == tenant_id
end end
def create def create
config config
end end
def tenant_id
@tenant_id ||= get_tenant_id
end
# This looks for the tenant specified by the 'tenant_name' parameter to # This looks for the tenant specified by the 'tenant_name' parameter to
# the resource and returns the corresponding UUID if there is a single # the resource and returns the corresponding UUID if there is a single
# match. # match.
@ -174,7 +183,7 @@ Puppet::Type.type(:nova_admin_tenant_id_setter).provide(:ruby) do
def config def config
Puppet::Type.type(:neutron_config).new( Puppet::Type.type(:neutron_config).new(
{:name => 'DEFAULT/nova_admin_tenant_id', :value => "#{get_tenant_id}"} {:name => 'DEFAULT/nova_admin_tenant_id', :value => "#{tenant_id}"}
).create ).create
end end

View File

@ -77,6 +77,19 @@ describe 'Puppet::Type.type(:nova_admin_tenant_id_setter)' do
expect(provider.exists?).to be_false expect(provider.exists?).to be_false
expect(provider.create).to be_nil expect(provider.create).to be_nil
end end
context 'when tenant id already set' do
it 'should create a resource, with exists? true' do
mock = { 'DEFAULT' => { 'nova_admin_tenant_id' => 'UUID_SERVICES' } }
Puppet::Util::IniConfig::File.expects(:new).returns(mock)
mock.expects(:read).with('/etc/neutron/neutron.conf')
resource = Puppet::Type::Nova_admin_tenant_id_setter.new(params)
provider = provider_class.new(resource)
expect(provider.exists?).to be_true
expect(provider.create).to be_nil
end
end
end end
# What happens if we ask for a tenant that does not exist? # What happens if we ask for a tenant that does not exist?