Class: Puppet::Provider::Keystone

Inherits:
Openstack
  • Object
show all
Extended by:
Openstack::Auth
Defined in:
lib/puppet/provider/keystone.rb

Constant Summary collapse

DEFAULT_DOMAIN =
'Default'
@@default_domain_id =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auth_endpointObject



20
21
22
# File 'lib/puppet/provider/keystone.rb', line 20

def self.auth_endpoint
  @auth_endpoint ||= get_auth_endpoint
end

.default_domainObject



59
60
61
# File 'lib/puppet/provider/keystone.rb', line 59

def self.default_domain
  DEFAULT_DOMAIN
end

.default_domain_changedObject



47
48
49
# File 'lib/puppet/provider/keystone.rb', line 47

def self.default_domain_changed
  default_domain_id != 'default'
end

.default_domain_deprecation_messageObject



51
52
53
54
55
56
57
# File 'lib/puppet/provider/keystone.rb', line 51

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

.default_domain_from_ini_fileObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet/provider/keystone.rb', line 24

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
    default_domain_from_conf[:value][0]
  else
    nil
  end
rescue
  nil
end

.default_domain_idObject



37
38
39
40
41
42
43
44
45
# File 'lib/puppet/provider/keystone.rb', line 37

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

.domain_id_from_name(name) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/puppet/provider/keystone.rb', line 123

def self.domain_id_from_name(name)
  unless @domain_hash_name
    list = system_request('domain', 'list')
    @domain_hash_name = Hash[list.collect{|domain| [domain[:name], domain[:id]]}]
  end
  unless @domain_hash_name.include?(name)
    domain = system_request('domain', 'show', name)
    if domain && domain.key?(:id)
      @domain_hash_name[name] = domain[:id]
    else
      err("Could not find domain with name [#{name}]")
    end
  end
  @domain_hash_name[name]
end

.domain_name_from_id(id) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/puppet/provider/keystone.rb', line 103

def self.domain_name_from_id(id)
  unless @domain_hash
    list = system_request('domain', 'list')
    if list.nil?
      err("Could not list domains")
    else
      @domain_hash = Hash[list.collect{|domain| [domain[:id], domain[:name]]}]
    end
  end
  unless @domain_hash.include?(id)
    domain = system_request('domain', 'show', id)
    if domain && domain.key?(:name)
      @domain_hash[id] = domain[:name]
    else
      err("Could not find domain with id [#{id}]")
    end
  end
  @domain_hash[id]
end

.fetch_user(name, domain) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/puppet/provider/keystone.rb', line 139

def self.fetch_user(name, domain)
  domain ||= default_domain
  user = system_request(
    'user', 'show',
    [name, '--domain', domain],
    {
      # TODO(tkajinam): Remove the first item after 2024.2 release.
      :no_retry_exception_msgs => [/No user with a name or ID/, /No User found for/]
    })
  # The description key is only set if it exists
  if user and user.key?(:id) and !user.key?(:description)
      user[:description] = ''
  end
  user
rescue Puppet::ExecutionFailure => e
  raise e unless (e.message =~ /No user with a name or ID/ or e.message =~ /No User found for/)
end

.get_auth_endpointObject



13
14
15
16
17
18
# File 'lib/puppet/provider/keystone.rb', line 13

def self.get_auth_endpoint
  configs = self.request('configuration', 'show')
  "#{configs[:'auth.auth_url']}"
rescue Puppet::Error::OpenstackAuthInputError
  nil
end

.get_auth_urlObject



157
158
159
160
161
162
163
164
165
166
# File 'lib/puppet/provider/keystone.rb', line 157

def self.get_auth_url
  auth_url = nil
  if ENV['OS_AUTH_URL']
    auth_url = ENV['OS_AUTH_URL'].dup
  elsif auth_url = get_os_vars_from_rcfile(rc_filename)['OS_AUTH_URL']
  else
    auth_url = auth_endpoint
  end
  return auth_url
end

.make_full_name(name) ⇒ Object

Prefix with default domain if missing from the name.



85
86
87
# File 'lib/puppet/provider/keystone.rb', line 85

def self.make_full_name(name)
  resource_to_name(*name_to_resource(name), false)
end

.name_to_resource(name) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/puppet/provider/keystone.rb', line 74

def self.name_to_resource(name)
  uniq = name.split('::')
  if uniq.count == 1
    uniq.insert(0, default_domain)
  else
    uniq.reverse!
  end
  uniq
end

.project_request(service, action, properties = nil, options = {}) ⇒ Object



168
169
170
# File 'lib/puppet/provider/keystone.rb', line 168

def self.project_request(service, action, properties=nil, options={})
  self.request(service, action, properties, options, 'project')
end

.resource_to_name(domain, name, check_for_default = true) ⇒ Object

Raises:

  • (Puppet::Error)


63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet/provider/keystone.rb', line 63

def self.resource_to_name(domain, name, check_for_default = true)
  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)
end

.set_domain_for_name(name, domain_name) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/puppet/provider/keystone.rb', line 176

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
  domain_id = self.domain_id_from_name(domain_name)
  case domain_id
  when default_domain_id
    name
  when nil
    name
  else
    name << "::#{domain_name}"
  end
end

.system_request(service, action, properties = nil, options = {}) ⇒ Object



172
173
174
# File 'lib/puppet/provider/keystone.rb', line 172

def self.system_request(service, action, properties=nil, options={})
  self.request(service, action, properties, options, 'system')
end

.user_id_from_name_and_domain_name(name, domain_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/puppet/provider/keystone.rb', line 89

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)
    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
  end
  @users_name[id_str]
end

Instance Method Details

#bool_to_sym(bool) ⇒ Object

Helper functions to use on the pre-validated enabled field



192
193
194
# File 'lib/puppet/provider/keystone.rb', line 192

def bool_to_sym(bool)
  bool == true ? :true : :false
end

#sym_to_bool(sym) ⇒ Object



196
197
198
# File 'lib/puppet/provider/keystone.rb', line 196

def sym_to_bool(sym)
  sym == :true ? true : false
end