2014-11-16 16:23:00 -08:00
|
|
|
require 'puppet/provider/openstack'
|
2015-05-08 15:45:16 +10:00
|
|
|
require 'puppet/provider/openstack/auth'
|
|
|
|
require 'puppet/provider/openstack/credentials'
|
|
|
|
|
2014-11-16 16:23:00 -08:00
|
|
|
class Puppet::Provider::Keystone < Puppet::Provider::Openstack
|
|
|
|
|
2015-05-08 15:45:16 +10:00
|
|
|
extend Puppet::Provider::Openstack::Auth
|
2014-11-16 16:23:00 -08:00
|
|
|
|
2015-11-18 18:38:50 +01:00
|
|
|
DEFAULT_DOMAIN = 'Default'
|
2014-11-16 16:23:00 -08:00
|
|
|
|
2015-11-18 18:38:50 +01:00
|
|
|
@@default_domain_id = nil
|
2015-09-05 15:55:09 -06:00
|
|
|
|
2019-11-02 12:32:24 +01:00
|
|
|
def self.get_auth_endpoint
|
2021-08-29 00:14:52 +09:00
|
|
|
configs = self.request('configuration', 'show')
|
2023-03-10 15:39:41 +01:00
|
|
|
"#{configs[:'auth.auth_url']}"
|
2021-08-29 00:14:52 +09:00
|
|
|
rescue Puppet::Error::OpenstackAuthInputError
|
|
|
|
nil
|
2019-11-02 12:32:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.auth_endpoint
|
|
|
|
@auth_endpoint ||= get_auth_endpoint
|
2012-03-29 14:29:04 -07:00
|
|
|
end
|
|
|
|
|
2015-11-18 18:38:50 +01:00
|
|
|
def self.default_domain_from_ini_file
|
|
|
|
default_domain_from_conf = Puppet::Resource.indirection
|
|
|
|
.find('Keystone_config/identity/default_domain_id')
|
|
|
|
if default_domain_from_conf[:ensure] == :present
|
|
|
|
# get from ini file
|
2021-07-13 17:43:00 +09:00
|
|
|
default_domain_from_conf[:value][0]
|
2015-11-18 18:38:50 +01:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_domain_id
|
|
|
|
if @@default_domain_id
|
|
|
|
# cached
|
|
|
|
@@default_domain_id
|
|
|
|
else
|
|
|
|
@@default_domain_id = default_domain_from_ini_file
|
|
|
|
end
|
|
|
|
@@default_domain_id = @@default_domain_id.nil? ? 'default' : @@default_domain_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_domain_changed
|
|
|
|
default_domain_id != 'default'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_domain_deprecation_message
|
|
|
|
'Support for a resource without the domain ' \
|
|
|
|
'set is deprecated in Liberty cycle. ' \
|
|
|
|
'It will be dropped in the M-cycle. ' \
|
|
|
|
"Currently using '#{default_domain}' as default domain name " \
|
|
|
|
"while the default domain id is '#{default_domain_id}'."
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_domain
|
|
|
|
DEFAULT_DOMAIN
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.resource_to_name(domain, name, check_for_default = true)
|
Add composite namevar for tenant, user, user_role.
There are two sides on this patch, the user facing one, and the
developer's one.
It gives more flexibility for the interface used by the user for the
Keystone_tenant, Keystone_user and Keystone_user_roles resources. For
instance to specify a user and give the admin role, currently you have
to:
keystone_user { 'new_admin::admin_domain':
ensure => present,
enabled => true,
tenant => 'openstackv3::admin_domain',
email => 'test@example.tld',
password => 'a_big_secret',
}
keystone_user_role { 'new_admin::admin_domain@openstackv3::admin_domain':
ensure => present,
roles => ['admin'],
}
Now you can specify it like this:
keystone_user { 'new_admin':
ensure => present,
enabled => true,
domain => 'admin_domain',
tenant => 'openstackv3::admin_domain',
email => 'test@example.tld',
password => 'a_big_secret',
}
keystone_user_role { 'new_admin@openstackv3':
ensure => present,
user_domain => 'admin_domain',
project_domain => 'admin_domain',
roles => ['admin'],
}
For the developer this simplify the code. Puppet is using composite
namevar to make all the resources unique. So guessing what pattern is
used in the title is no longer required. For instance this :
keystone_tenant { 'project_one': ensure => present }
keystone_tenant { 'meaningless': name => 'project_one', domain => 'Default', ensure => present }
is detected as the same tenant by puppet.
The same is true for dependencies. This is working correctly:
keystone_tenant { 'meaningless': name => 'project_one', domain => 'domain_one', ensure => present }
file {'/tmp/needed': ensure => present, require => Keystone_tenant['project_one::domain_one'] }
In autorequire term in type definition, you just have to pass the fully
qualified name (with the domain suffix for user and tenant) of the
resource and puppet will do the matching, whatever the original title
is. See the examples in user and tenant in keystone_user_role type.
Change-Id: I4deb27dc6f71fb7a7ec6a9c72bd0e1412c2e9a30
2015-09-23 20:17:31 +02:00
|
|
|
raise Puppet::Error, "Domain cannot be nil for project '#{name}'. " \
|
|
|
|
'Please report a bug.' if domain.nil?
|
|
|
|
join_str = '::'
|
|
|
|
name_display = [name]
|
|
|
|
unless check_for_default && domain == default_domain
|
|
|
|
name_display << domain
|
|
|
|
end
|
|
|
|
name_display.join(join_str)
|
2015-09-22 11:21:36 +10:00
|
|
|
end
|
|
|
|
|
Add composite namevar for tenant, user, user_role.
There are two sides on this patch, the user facing one, and the
developer's one.
It gives more flexibility for the interface used by the user for the
Keystone_tenant, Keystone_user and Keystone_user_roles resources. For
instance to specify a user and give the admin role, currently you have
to:
keystone_user { 'new_admin::admin_domain':
ensure => present,
enabled => true,
tenant => 'openstackv3::admin_domain',
email => 'test@example.tld',
password => 'a_big_secret',
}
keystone_user_role { 'new_admin::admin_domain@openstackv3::admin_domain':
ensure => present,
roles => ['admin'],
}
Now you can specify it like this:
keystone_user { 'new_admin':
ensure => present,
enabled => true,
domain => 'admin_domain',
tenant => 'openstackv3::admin_domain',
email => 'test@example.tld',
password => 'a_big_secret',
}
keystone_user_role { 'new_admin@openstackv3':
ensure => present,
user_domain => 'admin_domain',
project_domain => 'admin_domain',
roles => ['admin'],
}
For the developer this simplify the code. Puppet is using composite
namevar to make all the resources unique. So guessing what pattern is
used in the title is no longer required. For instance this :
keystone_tenant { 'project_one': ensure => present }
keystone_tenant { 'meaningless': name => 'project_one', domain => 'Default', ensure => present }
is detected as the same tenant by puppet.
The same is true for dependencies. This is working correctly:
keystone_tenant { 'meaningless': name => 'project_one', domain => 'domain_one', ensure => present }
file {'/tmp/needed': ensure => present, require => Keystone_tenant['project_one::domain_one'] }
In autorequire term in type definition, you just have to pass the fully
qualified name (with the domain suffix for user and tenant) of the
resource and puppet will do the matching, whatever the original title
is. See the examples in user and tenant in keystone_user_role type.
Change-Id: I4deb27dc6f71fb7a7ec6a9c72bd0e1412c2e9a30
2015-09-23 20:17:31 +02:00
|
|
|
def self.name_to_resource(name)
|
|
|
|
uniq = name.split('::')
|
|
|
|
if uniq.count == 1
|
|
|
|
uniq.insert(0, default_domain)
|
|
|
|
else
|
|
|
|
uniq.reverse!
|
2015-09-22 11:21:36 +10:00
|
|
|
end
|
Add composite namevar for tenant, user, user_role.
There are two sides on this patch, the user facing one, and the
developer's one.
It gives more flexibility for the interface used by the user for the
Keystone_tenant, Keystone_user and Keystone_user_roles resources. For
instance to specify a user and give the admin role, currently you have
to:
keystone_user { 'new_admin::admin_domain':
ensure => present,
enabled => true,
tenant => 'openstackv3::admin_domain',
email => 'test@example.tld',
password => 'a_big_secret',
}
keystone_user_role { 'new_admin::admin_domain@openstackv3::admin_domain':
ensure => present,
roles => ['admin'],
}
Now you can specify it like this:
keystone_user { 'new_admin':
ensure => present,
enabled => true,
domain => 'admin_domain',
tenant => 'openstackv3::admin_domain',
email => 'test@example.tld',
password => 'a_big_secret',
}
keystone_user_role { 'new_admin@openstackv3':
ensure => present,
user_domain => 'admin_domain',
project_domain => 'admin_domain',
roles => ['admin'],
}
For the developer this simplify the code. Puppet is using composite
namevar to make all the resources unique. So guessing what pattern is
used in the title is no longer required. For instance this :
keystone_tenant { 'project_one': ensure => present }
keystone_tenant { 'meaningless': name => 'project_one', domain => 'Default', ensure => present }
is detected as the same tenant by puppet.
The same is true for dependencies. This is working correctly:
keystone_tenant { 'meaningless': name => 'project_one', domain => 'domain_one', ensure => present }
file {'/tmp/needed': ensure => present, require => Keystone_tenant['project_one::domain_one'] }
In autorequire term in type definition, you just have to pass the fully
qualified name (with the domain suffix for user and tenant) of the
resource and puppet will do the matching, whatever the original title
is. See the examples in user and tenant in keystone_user_role type.
Change-Id: I4deb27dc6f71fb7a7ec6a9c72bd0e1412c2e9a30
2015-09-23 20:17:31 +02:00
|
|
|
uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prefix with default domain if missing from the name.
|
|
|
|
def self.make_full_name(name)
|
|
|
|
resource_to_name(*name_to_resource(name), false)
|
|
|
|
end
|
|
|
|
|
2016-03-30 13:00:58 +02:00
|
|
|
def self.user_id_from_name_and_domain_name(name, domain_name)
|
|
|
|
@users_name ||= {}
|
|
|
|
id_str = "#{name}_#{domain_name}"
|
|
|
|
unless @users_name.keys.include?(id_str)
|
|
|
|
user = fetch_user(name, domain_name)
|
2021-09-22 10:02:27 +02:00
|
|
|
if user && user.key?(:id)
|
|
|
|
@users_name[id_str] = user[:id]
|
|
|
|
else
|
|
|
|
err("Could not find user with name [#{name}] and domain [#{domain_name}]")
|
|
|
|
end
|
2016-03-30 13:00:58 +02:00
|
|
|
end
|
|
|
|
@users_name[id_str]
|
|
|
|
end
|
|
|
|
|
2015-08-17 14:10:42 +10:00
|
|
|
def self.domain_name_from_id(id)
|
2015-09-05 15:55:09 -06:00
|
|
|
unless @domain_hash
|
2021-08-29 00:14:52 +09:00
|
|
|
list = system_request('domain', 'list')
|
2021-09-22 10:02:27 +02:00
|
|
|
if list.nil?
|
|
|
|
err("Could not list domains")
|
|
|
|
else
|
|
|
|
@domain_hash = Hash[list.collect{|domain| [domain[:id], domain[:name]]}]
|
|
|
|
end
|
2015-09-05 15:55:09 -06:00
|
|
|
end
|
|
|
|
unless @domain_hash.include?(id)
|
2021-08-29 00:14:52 +09:00
|
|
|
domain = system_request('domain', 'show', id)
|
2021-09-22 10:02:27 +02:00
|
|
|
if domain && domain.key?(:name)
|
|
|
|
@domain_hash[id] = domain[:name]
|
|
|
|
else
|
|
|
|
err("Could not find domain with id [#{id}]")
|
|
|
|
end
|
2015-09-05 15:55:09 -06:00
|
|
|
end
|
|
|
|
@domain_hash[id]
|
2012-03-29 14:29:04 -07:00
|
|
|
end
|
|
|
|
|
2016-01-18 16:54:35 +03:00
|
|
|
def self.domain_id_from_name(name)
|
|
|
|
unless @domain_hash_name
|
2021-08-29 00:14:52 +09:00
|
|
|
list = system_request('domain', 'list')
|
2016-01-18 16:54:35 +03:00
|
|
|
@domain_hash_name = Hash[list.collect{|domain| [domain[:name], domain[:id]]}]
|
|
|
|
end
|
|
|
|
unless @domain_hash_name.include?(name)
|
2021-08-29 00:14:52 +09:00
|
|
|
domain = system_request('domain', 'show', name)
|
2021-09-22 10:02:27 +02:00
|
|
|
if domain && domain.key?(:id)
|
|
|
|
@domain_hash_name[name] = domain[:id]
|
|
|
|
else
|
|
|
|
err("Could not find domain with name [#{name}]")
|
|
|
|
end
|
2016-01-18 16:54:35 +03:00
|
|
|
end
|
|
|
|
@domain_hash_name[name]
|
2015-09-28 11:14:30 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.fetch_user(name, domain)
|
|
|
|
domain ||= default_domain
|
2024-09-30 21:04:20 +09:00
|
|
|
user = system_request(
|
|
|
|
'user', 'show',
|
|
|
|
[name, '--domain', domain],
|
|
|
|
{
|
|
|
|
# TODO(tkajinam): Remove the first item after 2024.2 release.
|
2024-10-01 15:56:29 +02:00
|
|
|
:no_retry_exception_msgs => [/No user with a name or ID/, /No User found for/]
|
2024-09-30 21:04:20 +09:00
|
|
|
})
|
2022-04-06 09:30:29 +00:00
|
|
|
# The description key is only set if it exists
|
|
|
|
if user and user.key?(:id) and !user.key?(:description)
|
|
|
|
user[:description] = ''
|
|
|
|
end
|
|
|
|
user
|
2015-09-28 11:14:30 +10:00
|
|
|
rescue Puppet::ExecutionFailure => e
|
2024-10-01 15:56:29 +02:00
|
|
|
raise e unless (e.message =~ /No user with a name or ID/ or e.message =~ /No User found for/)
|
2015-09-28 11:14:30 +10:00
|
|
|
end
|
|
|
|
|
2015-08-17 15:32:03 +10:00
|
|
|
def self.get_auth_url
|
|
|
|
auth_url = nil
|
2015-08-17 14:10:42 +10:00
|
|
|
if ENV['OS_AUTH_URL']
|
2015-08-17 15:32:03 +10:00
|
|
|
auth_url = ENV['OS_AUTH_URL'].dup
|
|
|
|
elsif auth_url = get_os_vars_from_rcfile(rc_filename)['OS_AUTH_URL']
|
2015-08-17 14:10:42 +10:00
|
|
|
else
|
2019-11-02 12:32:24 +01:00
|
|
|
auth_url = auth_endpoint
|
2015-08-17 15:32:03 +10:00
|
|
|
end
|
|
|
|
return auth_url
|
|
|
|
end
|
|
|
|
|
2021-08-29 00:14:52 +09:00
|
|
|
def self.project_request(service, action, properties=nil, options={})
|
|
|
|
self.request(service, action, properties, options, 'project')
|
2015-08-17 14:10:42 +10:00
|
|
|
end
|
|
|
|
|
2021-08-29 00:14:52 +09:00
|
|
|
def self.system_request(service, action, properties=nil, options={})
|
|
|
|
self.request(service, action, properties, options, 'system')
|
2015-08-17 14:10:42 +10:00
|
|
|
end
|
|
|
|
|
2015-09-28 11:14:30 +10:00
|
|
|
def self.set_domain_for_name(name, domain_name)
|
|
|
|
if domain_name.nil? || domain_name.empty?
|
|
|
|
raise(Puppet::Error, "Missing domain name for resource #{name}")
|
|
|
|
end
|
2016-01-18 16:54:35 +03:00
|
|
|
domain_id = self.domain_id_from_name(domain_name)
|
2015-09-28 11:14:30 +10:00
|
|
|
case domain_id
|
|
|
|
when default_domain_id
|
|
|
|
name
|
|
|
|
when nil
|
|
|
|
name
|
|
|
|
else
|
|
|
|
name << "::#{domain_name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-16 16:23:00 -08:00
|
|
|
# Helper functions to use on the pre-validated enabled field
|
|
|
|
def bool_to_sym(bool)
|
|
|
|
bool == true ? :true : :false
|
|
|
|
end
|
|
|
|
|
|
|
|
def sym_to_bool(sym)
|
|
|
|
sym == :true ? true : false
|
|
|
|
end
|
2012-03-29 14:29:04 -07:00
|
|
|
end
|