Skip to content

Commit

Permalink
Fix #306 - Retrieve parentId by realm name (#307)
Browse files Browse the repository at this point in the history
* Fix #306 - Retrieve parentId by realm name

* Fix unit tests

---------

Co-authored-by: Trey Dockendorf <[email protected]>
  • Loading branch information
TuningYourCode and treydock authored Apr 19, 2024
1 parent 1795982 commit 9a42b33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/puppet/provider/keycloak_ldap_user_provider/kcadm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,29 @@ def self.prefetch(resources)
end
end

def get_parent_id(realm)
parent_id = nil
output = kcadm('get', 'realms', realm, nil, ['id'])
Puppet.debug("#{realm} realms: #{output}")
begin
data = JSON.parse(output)
rescue JSON::ParserError
Puppet.debug('Unable to parse output from kcadm get realms')
data = []
end
data.each do |d|
parent_id = d['id']
end
parent_id
end

def create
raise(Puppet::Error, "Realm is mandatory for #{resource.type} #{resource.name}") if resource[:realm].nil?

data = {}
data[:id] = resource[:id] || name_uuid(resource[:name])
data[:name] = resource[:resource_name]
data[:parentId] = resource[:realm]
data[:parentId] = get_parent_id(resource[:realm]) || resource[:realm]
data[:providerId] = 'ldap'
data[:providerType] = 'org.keycloak.storage.UserStorageProvider'
data[:config] = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
it 'creates a realm' do
temp = Tempfile.new('keycloak_component')
allow(Tempfile).to receive(:new).with('keycloak_component').and_return(temp)
allow(resource.provider).to receive(:get_parent_id).with('test').and_return('test')
expect(resource.provider).to receive(:kcadm).with('create', 'components', 'test', temp.path)
resource.provider.create
property_hash = resource.provider.instance_variable_get('@property_hash')
Expand Down

0 comments on commit 9a42b33

Please sign in to comment.