def normalize_time_str(time_str): try: # local time zone datetime format return timeutils.normalize_time(timeutils.local_to_utc(timeutils.parse_strtime(time_str, "%Y-%m-%d %H:%M:%S"))) except ValueError: # UTC+0 time zone datetime format or others return timeutils.normalize_time(timeutils.parse_isotime(time_str))
def request_admin_token(search_cache=True): """Retrieve new token as admin user from keystone. :return token id upon success :raises ServerError when unable to communicate with keystone """ admin_user = CONF.get("admin_user") admin_password = CONF.get("admin_password") admin_tenant_name = CONF.get("admin_tenant_name") keystone_client = client.KeystonePublicClient() local_store = local.dict_store() if search_cache and hasattr(local_store, "admin_token"): token = local_store.admin_token expires = timeutils.parse_isotime(token["expires"]) if not timeutils.is_older_than(timeutils.normalize_time(expires), 0): LOG.debug(_("Get token from local store.")) return token params = { "auth": { "passwordCredentials": {"username": admin_user, "password": admin_password}, "tenantName": admin_tenant_name, } } data, headers = keystone_client.response( "POST", "/tokens", headers={"content-type": "application/json"}, body=params ) try: token = data["access"]["token"] assert token local_store.admin_token = token LOG.debug(_("Request for admin token and save to local store.")) return token except (AssertionError, KeyError): LOG.warn("Unexpected response from keystone service: %s", data) raise