diff --git a/lib/puppet/provider/keycloak_ldap_user_provider/kcadm.rb b/lib/puppet/provider/keycloak_ldap_user_provider/kcadm.rb index 96aed003..960f5c1a 100644 --- a/lib/puppet/provider/keycloak_ldap_user_provider/kcadm.rb +++ b/lib/puppet/provider/keycloak_ldap_user_provider/kcadm.rb @@ -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] = {} diff --git a/spec/unit/puppet/provider/keycloak_ldap_user_provider/kcadm_spec.rb b/spec/unit/puppet/provider/keycloak_ldap_user_provider/kcadm_spec.rb index cbbdc90a..3a73b833 100644 --- a/spec/unit/puppet/provider/keycloak_ldap_user_provider/kcadm_spec.rb +++ b/spec/unit/puppet/provider/keycloak_ldap_user_provider/kcadm_spec.rb @@ -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')