Import first type & provider for gnocchi_config

gnocchi_config will be used to configure /etc/gnocchi/gnocchi.conf file.
This commit is contained in:
Emilien Macchi 2014-12-29 13:48:56 +01:00
parent 7acdab3657
commit 54b3b8487a
3 changed files with 182 additions and 0 deletions

View File

@ -0,0 +1,113 @@
require 'json'
require 'puppet/util/inifile'
class Puppet::Provider::Gnocchi < Puppet::Provider
def self.conf_filename
'/etc/gnocchi/gnocchi.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.gnocchi_credentials
@gnocchi_credentials ||= get_gnocchi_credentials
end
def self.get_gnocchi_credentials
auth_keys = ['auth_host', 'auth_port', 'auth_protocol',
'admin_tenant_name', 'admin_user', 'admin_password']
conf = gnocchi_conf
if conf and conf['keystone_authtoken'] and
auth_keys.all?{|k| !conf['keystone_authtoken'][k].nil?}
return Hash[ auth_keys.map \
{ |k| [k, conf['keystone_authtoken'][k].strip] } ]
else
raise(Puppet::Error, "File: #{conf_filename} does not contain all \
required sections. Gnocchi types will not work if gnocchi is not \
correctly configured.")
end
end
def gnocchi_credentials
self.class.gnocchi_credentials
end
def self.auth_endpoint
@auth_endpoint ||= get_auth_endpoint
end
def self.get_auth_endpoint
q = gnocchi_credentials
"#{q['auth_protocol']}://#{q['auth_host']}:#{q['auth_port']}/v2.0/"
end
def self.gnocchi_conf
return @gnocchi_conf if @gnocchi_conf
@gnocchi_conf = Puppet::Util::IniConfig::File.new
@gnocchi_conf.read(conf_filename)
@gnocchi_conf
end
def self.auth_gnocchi(*args)
q = gnocchi_credentials
authenv = {
:OS_AUTH_URL => self.auth_endpoint,
:OS_USERNAME => q['admin_user'],
:OS_TENANT_NAME => q['admin_tenant_name'],
:OS_PASSWORD => q['admin_password']
}
begin
withenv authenv do
gnocchi(args)
end
rescue Exception => e
if (e.message =~ /\[Errno 111\] Connection refused/) or
(e.message =~ /\(HTTP 400\)/)
sleep 10
withenv authenv do
gnocchi(args)
end
else
raise(e)
end
end
end
def auth_gnocchi(*args)
self.class.auth_gnocchi(args)
end
def gnocchi_manage(*args)
cmd = args.join(" ")
output = `#{cmd}`
$?.exitstatus
end
def self.reset
@gnocchi_conf = nil
@gnocchi_credentials = nil
end
def self.list_gnocchi_resources(type, *args)
json = auth_gnocchi("--json", "#{type}-list", *args)
return JSON.parse(json)
end
def self.get_gnocchi_resource_attrs(type, id)
json = auth_gnocchi("--json", "#{type}-show", id)
return JSON.parse(json)
end
end

View File

@ -0,0 +1,27 @@
Puppet::Type.type(:gnocchi_config).provide(
:ini_setting,
:parent => Puppet::Type.type(:ini_setting).provider(:ruby)
) do
def section
resource[:name].split('/', 2).first
end
def setting
resource[:name].split('/', 2).last
end
def separator
'='
end
def self.file_path
'/etc/gnocchi/gnocchi.conf'
end
# added for backwards compatibility with older versions of inifile
def file_path
self.class.file_path
end
end

View File

@ -0,0 +1,42 @@
Puppet::Type.newtype(:gnocchi_config) do
ensurable
newparam(:name, :namevar => true) do
desc 'Section/setting name to manage from /etc/gnocchi/gnocchi.conf'
newvalues(/\S+\/\S+/)
end
newproperty(:value) do
desc 'The value of the setting to be defined.'
munge do |value|
value = value.to_s.strip
value.capitalize! if value =~ /^(true|false)$/i
value
end
def is_to_s( currentvalue )
if resource.secret?
return '[old secret redacted]'
else
return currentvalue
end
end
def should_to_s( newvalue )
if resource.secret?
return '[new secret redacted]'
else
return newvalue
end
end
end
newparam(:secret, :boolean => true) do
desc 'Whether to hide the value from Puppet logs. Defaults to `false`.'
newvalues(:true, :false)
defaultto false
end
end