use of org.openstack4j.openstack.identity.v2.domain.KeystoneAccess in project openstack4j by ContainX.
the class AbstractTest method osv2.
protected OSClientV2 osv2() {
if (osv2 == null) {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
try {
String json = new String(Streams.readAll(getClass().getResourceAsStream(JSON_ACCESS)));
LOG.info(getClass().getName());
//LOG.info(getClass().getName() + ", JSON Access = " + json);
json = json.replaceAll("127.0.0.1", getHost());
//LOG.info("JSON Access = " + json);
KeystoneAccess a = mapper.readValue(json, KeystoneAccess.class);
a.applyContext(authURL("/v2.0"), new org.openstack4j.openstack.identity.v2.domain.Credentials("test", "test"));
osv2 = OSFactory.clientFromAccess(a);
} catch (Exception e) {
e.printStackTrace();
}
}
return osv2;
}
use of org.openstack4j.openstack.identity.v2.domain.KeystoneAccess in project openstack4j by ContainX.
the class OSAuthenticator method authenticateV2.
private static OSClientV2 authenticateV2(org.openstack4j.openstack.identity.v2.domain.Auth auth, SessionInfo info, Config config) {
HttpRequest<KeystoneAccess> request = HttpRequest.builder(KeystoneAccess.class).header(ClientConstants.HEADER_OS4J_AUTH, TOKEN_INDICATOR).endpoint(info.endpoint).method(HttpMethod.POST).path("/tokens").config(config).entity(auth).build();
HttpResponse response = HttpExecutor.create().execute(request);
if (response.getStatus() >= 400) {
try {
throw mapException(response.getStatusMessage(), response.getStatus());
} finally {
HttpEntityHandler.closeQuietly(response);
}
}
KeystoneAccess access = response.getEntity(KeystoneAccess.class);
if (auth.getType() == Type.CREDENTIALS) {
access = access.applyContext(info.endpoint, (Credentials) auth);
} else if (auth.getType() == Type.RAX_APIKEY) {
access = access.applyContext(info.endpoint, (RaxApiKeyCredentials) auth);
} else {
access = access.applyContext(info.endpoint, (org.openstack4j.openstack.identity.v2.domain.TokenAuth) auth);
}
if (!info.reLinkToExistingSession)
return OSClientSession.OSClientSessionV2.createSession(access, info.perspective, info.provider, config);
OSClientSession.OSClientSessionV2 current = (OSClientSessionV2) OSClientSession.getCurrent();
current.access = access;
return current;
}
use of org.openstack4j.openstack.identity.v2.domain.KeystoneAccess in project openstack4j by ContainX.
the class OSAuthenticator method reAuthenticate.
/**
* Re-authenticates/renews the token for the current Session
*/
@SuppressWarnings("rawtypes")
public static void reAuthenticate() {
LOG.debug("Re-Authenticating session due to expired Token or invalid response");
OSClientSession session = OSClientSession.getCurrent();
switch(session.getAuthVersion()) {
case V2:
KeystoneAccess access = ((OSClientSessionV2) session).getAccess().unwrap();
SessionInfo info = new SessionInfo(access.getEndpoint(), session.getPerspective(), true, session.getProvider());
Auth auth = (Auth) ((access.isCredentialType()) ? access.getCredentials() : access.getTokenAuth());
authenticateV2((org.openstack4j.openstack.identity.v2.domain.Auth) auth, info, session.getConfig());
break;
case V3:
default:
Token token = ((OSClientSessionV3) session).getToken();
info = new SessionInfo(token.getEndpoint(), session.getPerspective(), true, session.getProvider());
authenticateV3((KeystoneAuth) token.getCredentials(), info, session.getConfig());
break;
}
}
Aggregations