diff options
author | 2017-01-31 13:57:24 +0100 | |
---|---|---|
committer | 2017-01-31 14:55:11 +0100 | |
commit | 2e984294ac28c6f2ede290c38164c5d536ccaf4a (patch) | |
tree | 5eba5a929b7a961c98749fa69e03cfea58e1a724 /keystone-client/src/main/java/com/woorea/openstack/keystone/utils/KeystoneTokenProvider.java | |
parent | 86c0f28c8ed469486b64d6422dc53e3a7bcc8adb (diff) |
Initial OpenECOMP MSO OpenStack SDK lib commit
Change-Id: Ieaacb2b2c0dcc469669880e73f0cda9fa59a6c5a
Signed-off-by: ChrisC <cc697w@intl.att.com>
Diffstat (limited to 'keystone-client/src/main/java/com/woorea/openstack/keystone/utils/KeystoneTokenProvider.java')
-rw-r--r-- | keystone-client/src/main/java/com/woorea/openstack/keystone/utils/KeystoneTokenProvider.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/keystone-client/src/main/java/com/woorea/openstack/keystone/utils/KeystoneTokenProvider.java b/keystone-client/src/main/java/com/woorea/openstack/keystone/utils/KeystoneTokenProvider.java new file mode 100644 index 0000000..fb5127a --- /dev/null +++ b/keystone-client/src/main/java/com/woorea/openstack/keystone/utils/KeystoneTokenProvider.java @@ -0,0 +1,56 @@ +package com.woorea.openstack.keystone.utils; + +import java.util.concurrent.ConcurrentHashMap; + +import com.woorea.openstack.base.client.OpenStackTokenProvider; +import com.woorea.openstack.keystone.Keystone; +import com.woorea.openstack.keystone.model.Access; +import com.woorea.openstack.keystone.model.authentication.UsernamePassword; + +public class KeystoneTokenProvider { + + protected Keystone keystone; + + protected String username; + + protected String password; + + ConcurrentHashMap<String, Access> hashTenantAccess; + + public KeystoneTokenProvider(String endpoint, String username, String password) { + this.keystone = new Keystone(endpoint); + this.username = username; + this.password = password; + this.hashTenantAccess = new ConcurrentHashMap<String, Access>(); + } + + public Access getAccessByTenant(String tenantName) { + Access access = hashTenantAccess.get(tenantName); + if (access == null) { + access = keystone.tokens().authenticate(new UsernamePassword(username, password)) + .withTenantName(tenantName) + .execute(); + hashTenantAccess.put(tenantName, access); + } + return access; + } + + public void expireAccessByTenant(String tenantName) { + hashTenantAccess.remove(tenantName); + } + + public OpenStackTokenProvider getProviderByTenant(final String tenantName) { + final KeystoneTokenProvider keystoneTokenProvider = this; + return new OpenStackTokenProvider() { + @Override + public String getToken() { + return keystoneTokenProvider.getAccessByTenant(tenantName) + .getToken().getId(); + } + @Override + public void expireToken() { + keystoneTokenProvider.expireAccessByTenant(tenantName); + } + }; + } +} |