From 4914498877f486accf61e11e2391a9effa343f82 Mon Sep 17 00:00:00 2001 From: Mike Dorman Date: Fri, 14 Nov 2014 16:06:46 -0700 Subject: [PATCH] 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 --- .../nova_admin_tenant_id_setter/ini_setting.rb | 13 +++++++++++-- .../nova_admin_tenant_id_setter/neutron_spec.rb | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/puppet/provider/nova_admin_tenant_id_setter/ini_setting.rb b/lib/puppet/provider/nova_admin_tenant_id_setter/ini_setting.rb index f5decb4a7..ea64359d7 100644 --- a/lib/puppet/provider/nova_admin_tenant_id_setter/ini_setting.rb +++ b/lib/puppet/provider/nova_admin_tenant_id_setter/ini_setting.rb @@ -12,6 +12,7 @@ require 'rubygems' require 'net/http' require 'net/https' require 'json' +require 'puppet/util/inifile' class KeystoneError < Puppet::Error end @@ -126,6 +127,8 @@ def keystone_v2_tenants(auth_url, end Puppet::Type.type(:nova_admin_tenant_id_setter).provide(:ruby) do + @tenant_id = nil + def authenticate keystone_v2_authenticate( @resource[:auth_url], @@ -144,13 +147,19 @@ Puppet::Type.type(:nova_admin_tenant_id_setter).provide(:ruby) do end 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 def create config end + def tenant_id + @tenant_id ||= get_tenant_id + end + # This looks for the tenant specified by the 'tenant_name' parameter to # the resource and returns the corresponding UUID if there is a single # match. @@ -174,7 +183,7 @@ Puppet::Type.type(:nova_admin_tenant_id_setter).provide(:ruby) do def config 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 end diff --git a/spec/unit/provider/nova_admin_tenant_id_setter/neutron_spec.rb b/spec/unit/provider/nova_admin_tenant_id_setter/neutron_spec.rb index 4e729a3e8..feb6c1864 100644 --- a/spec/unit/provider/nova_admin_tenant_id_setter/neutron_spec.rb +++ b/spec/unit/provider/nova_admin_tenant_id_setter/neutron_spec.rb @@ -77,6 +77,19 @@ describe 'Puppet::Type.type(:nova_admin_tenant_id_setter)' do expect(provider.exists?).to be_false expect(provider.create).to be_nil 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 # What happens if we ask for a tenant that does not exist?