/** * This method determines the local time that the current token (held in the Access object) will expire. * * @param accessKey * The access to be checked * @return The local time the token expires */ private static long getLocalExpiration(Access accessKey) { Date now = Time.getCurrentUTCDate(); if (accessKey != null && accessKey.getToken() != null) { Calendar issued = accessKey.getToken().getIssued_at(); Calendar expires = accessKey.getToken().getExpires(); if (issued != null && expires != null) { long tokenLife = expires.getTimeInMillis() - issued.getTimeInMillis(); return now.getTime() + tokenLife; } } return now.getTime(); }
@Override public String getToken() { return keystoneTokenProvider.getAccessByTenant(tenantName) .getToken().getId(); } @Override
@Override public String getToken() { return keystoneTokenProvider.getAccessByTenant(tenantName) .getToken().getId(); } @Override
/** * @see com.woorea.openstack.base.client.OpenStackTokenProvider#getToken() */ @Override public String getToken() throws OpenStackConnectException, OpenStackResponseException { return keystoneTokenProvider.getAccessByTenant(tenantName).getToken().getId(); }
/** * @param connector * The connector to be updated with the new token */ public synchronized void refreshIfStale(Connector connector) { String logId = "PAL-5558"; Logger appLogger = getLogger(); appLogger.info(new Date().toString()+ " " + logId+":OpenStackContext.refreshIfStale - in"); if (isStale()) { appLogger.info(new Date().toString()+ " " +logId+":OpenStackContext.refreshIfStale - Connection is stale"); try { logout(); relogin(); } catch (IllegalStateException | IllegalArgumentException | ZoneException e) { e.printStackTrace(); } } appLogger.info(new Date().toString()+ " " +logId+":OpenStackContext.refreshIfStale - old token -"+connector.getAccess().getToken().getId()); connector.updateToken(); appLogger.info(new Date().toString()+ " " +logId+":OpenStackContext.refreshIfStale - new token -"+connector.getAccess().getToken().getId()); }
/** * Updates the expired token by re-authenticating, getting a new unexpired token (in the access object), and then * updating the token held by this client wrapped by this connector to use the new token. * * @see com.att.cdp.openstack.connectors.Connector#updateToken() */ @Override public void updateToken() { super.updateToken(); keystone.token(getAccess().getToken().getId()); } }
/** * Updates the expired token by re-authenticating, getting a new unexpired token (in the access object), and then * updating the token held by this client wrapped by this connector to use the new token. * * @see com.att.cdp.openstack.connectors.Connector#updateToken() */ @Override public void updateToken() { super.updateToken(); glance.token(getAccess().getToken().getId()); } }
/** * Updates the expired token by re-authenticating, getting a new unexpired token (in the access object), and then * updating the token held by this client wrapped by this connector to use the new token. * * @see com.att.cdp.openstack.connectors.Connector#updateToken() */ @Override public void updateToken() { super.updateToken(); heat.token(getAccess().getToken().getId()); }
/** * Updates the expired token by re-authenticating, getting a new unexpired token (in the access object), and then * updating the token held by this client wrapped by this connector to use the new token. * * @see com.att.cdp.openstack.connectors.Connector#updateToken() */ @Override public void updateToken() { super.updateToken(); quantum.token(getAccess().getToken().getId()); } }
/** * Updates the expired token by re-authenticating, getting a new unexpired token (in the access object), and then * updating the token held by this client wrapped by this connector to use the new token. * * @see com.att.cdp.openstack.connectors.Connector#updateToken() */ @Override public void updateToken() { super.updateToken(); cinder.token(getAccess().getToken().getId()); } }
/** * Updates the expired token by re-authenticating, getting a new unexpired token (in the access object), and then * updating the token held by this client wrapped by this connector to use the new token. * * @see com.att.cdp.openstack.connectors.Connector#updateToken() */ @Override public void updateToken() { super.updateToken(); nova.token(getAccess().getToken().getId()); } }
/** * @param args */ public static void main(String[] args) { Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); Access access = keystone.tokens().authenticate(new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) .withTenantName("demo") .execute(); //use the token in the following requests keystone.token(access.getToken().getId()); //NovaClient novaClient = new NovaClient(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "compute", null, "public"), access.getToken().getId()); Nova novaClient = new Nova(ExamplesConfiguration.NOVA_ENDPOINT.concat("/").concat(access.getToken().getTenant().getId())); novaClient.token(access.getToken().getId()); //novaClient.enableLogging(Logger.getLogger("nova"), 100 * 1024); Servers servers = novaClient.servers().list(true).execute(); for(Server server : servers) { System.out.println(server); } }
public static void main(String[] args) throws InterruptedException { Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); Access access = keystone.tokens().authenticate(new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) .withTenantName(ExamplesConfiguration.TENANT_NAME) .execute(); //use the token in the following requests keystone.token(access.getToken().getId()); Nova novaClient = new Nova(ExamplesConfiguration.NOVA_ENDPOINT.concat("/").concat(access.getToken().getTenant().getId())); novaClient.token(access.getToken().getId()); Servers servers = novaClient.servers().list(true).execute(); if(servers.getList().size() > 0) { // Server has to be in activated state. ServersResource.StopServer stopServer = novaClient.servers().stop(servers.getList().get(0).getId()); stopServer.endpoint(ExamplesConfiguration.NOVA_ENDPOINT); stopServer.execute(); // Wait until server shutdown. Or 400 error occurs. Thread.sleep(5000); ServersResource.StartServer startServer = novaClient.servers().start(servers.getList().get(0).getId()); startServer.endpoint(ExamplesConfiguration.NOVA_ENDPOINT); startServer.execute(); } } }
/** * Creates a new Keystone connector and configures it. * * @param context * The context we are servicing */ public KeystoneConnector(OpenStackContext context) { super(context); String version = context.getProviderMetadata().getNetworkVersion(); setEndpoint(context.getProperties().getProperty(ContextFactory.PROPERTY_NETWORK_URL)); getLogger().debug( String.format("Initializing Quantum connector for version [%s] and url [%s]", version, getEndpoint())); AbstractOpenStackIdentityService service = (AbstractOpenStackIdentityService) context.getIdentityService(); keystone = service.getKeystone(); keystone.setLogger(getLogger()); keystone.token(getAccess().getToken().getId()); /* * It is not necessary to setup the proxy and certificate options because the keystone client has already been * created and is authenticated during login, where all this is already set up. Keystone is special because we * had to set these properties before logging in, so lets just keep using them in that client, making the * "connector" just a proxy to the existing keystone client. */ }
/** * Creates the super class from the specified context object. This is then used to obtain the Access object. * * @param context * The OpenStackCOntext object to obtain the Access object */ public Connector(OpenStackContext context) { /* * Allow the specification of a client connector to override the default mechanism of the service * loader. This is needed to support use within an OSGi container. */ clientConnector = context.getClientConnector(); configuration = ConfigurationFactory.getConfiguration(); logger = configuration.getApplicationLogger(); identity = (CommonIdentityService) context.getIdentityService(); access = identity.getAccess(); access.getToken().getTenant().getId(); }
/** * @param args */ public static void main(String[] args) { Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); // access with unscoped token Access access = keystone.tokens().authenticate( new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) .execute(); // use the token in the following requests keystone.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); Tenants tenants = keystone.tenants().list().execute(); // try to exchange token using the first tenant if (tenants.getList().size() > 0) { // access with tenant access = keystone.tokens().authenticate(new TokenAuthentication(access.getToken().getId())).withTenantId(tenants.getList().get(0).getId()).execute(); Quantum quantum = new Quantum(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "network", null, "public")); quantum.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); Networks networks = quantum.networks().list().execute(); for (Network network : networks) { System.out.println(network); } } else { System.out.println("No tenants found!"); } } }
@Override public void execute(Console console, CommandLine args) { if(args.getArgs().length == 1) { Keystone keystone = new Keystone((String) console.getProperty("keystone.endpoint")); Access access = keystone.tokens().authenticate( new UsernamePassword( console.getProperty("keystone.username"), console.getProperty("keystone.password") ) ) .withTenantName(console.getProperty("keystone.tenant_name")) .execute(); System.out.println(console.getProperty("nova.endpoint")); Nova client = new Nova(console.getProperty("nova.endpoint")+args.getArgs()[0]); client.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); NovaEnvironment environment = new NovaEnvironment(console.getEnvironment(), client); environment.register(new NovaServerList()); console.setEnvironment(environment); } }
/** * @param args */ public static void main(String[] args) { Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); //access with unscoped token Access access = keystone.tokens().authenticate( new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) .execute(); access = keystone.tokens().authenticate(new TokenAuthentication(access.getToken().getId())).withTenantName("admin").execute(); Tenant tenant = new Tenant(); tenant.setName("benn.cs"); tenant.setDescription("benn.cs"); tenant.setEnabled(true); //Get the adminURL client and use the token got above keystone = new Keystone("http://keystone.x.org/v2.0"); keystone.token(access.getToken().getId()); tenant = keystone.tenants().create(tenant).execute(); System.out.println(tenant); keystone.tenants().delete(tenant.getId()); } }
/** * @param args */ public static void main(String[] args) { Keystone keystone = new Keystone(ExamplesConfiguration.KEYSTONE_AUTH_URL); //access with unscoped token Access access = keystone.tokens() .authenticate(new UsernamePassword(ExamplesConfiguration.KEYSTONE_USERNAME, ExamplesConfiguration.KEYSTONE_PASSWORD)) .withTenantName("admin") .execute(); User user = new User(); user.setEmail("luis@woorea.es"); user.setUsername("luis.gervaso"); user.setPassword("password.0"); user.setName("Luis"); user.setEnabled(Boolean.TRUE); keystone = new Keystone("http://keystone.x.org/v2.0"); keystone.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); //keystone.enableLogging(Logger.getLogger("keystone"), 10000); user = keystone.users().create(user).execute(); System.out.println(user); keystone.users().delete(user.getId()).execute(); } }
@Override public void execute(Console console, CommandLine args) { Keystone client = new Keystone(console.getProperty("keystone.endpoint")); Access access = client.tokens() .authenticate(new UsernamePassword( console.getProperty("keystone.username"), console.getProperty("keystone.password") )) .withTenantName(console.getProperty("keystone.tenant_name")) .execute(); client.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); KeystoneEnvironment environment = new KeystoneEnvironment(console.getEnvironment(), client); environment.register(new KeystoneTenantList()); environment.register(new KeystoneTenantCreate()); environment.register(new KeystoneTenantDelete()); environment.register(new KeystoneUserList()); environment.register(new KeystoneUserCreate()); environment.register(new KeystoneUserDelete()); environment.register(new KeystoneRoleList()); environment.register(new KeystoneRoleDelete()); environment.register(new KeystoneServiceList()); console.setEnvironment(environment); }