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 _normalize_time(self, request): params = request.params.copy() start = params.pop("start", None) end = params.pop("end", None) try: start = ( timeutils.local_to_utc(timeutils.timestamp_to_datetime(start)) if start is not None else timeutils.seconds_ago(DEFAULT_CHANGE_SINCE_SECONDS) ) end = timeutils.local_to_utc(timeutils.timestamp_to_datetime(end)) if end is not None else None except ValueError: raise exception.Invalid(_("Not invalid datetime format.")) query_params = {"start": start, "end": end} return query_params
def _repack_server_data(self, server, req, tenant_id): server.update({"OS-EXT-STS:power_state": self._get_power_state(server['OS-EXT-STS:power_state'])}) auth_token = req.headers.get("x-auth-token") # get flavor info flavor_id = server['flavor']['id'] flavor = ops_api.get_flavor(tenant_id, auth_token, flavor_id) if flavor is not None: for k, v in flavor['flavor'].iteritems(): if k == 'links': continue server.update({"flavor-%s" % k: v}) server.pop('flavor') server_owner_id = server['tenant_id'] # get tenant name tenant = ops_api.get_tenant(server_owner_id, auth_token) tenant_name = tenant['name'] server.update(tenant_name=tenant_name) fixed_ips = [] private_floating_ips = [] public_floating_ips = [] # recognize IP type for ip in server['addresses'].get('private', {}): is_floating_ip, ip_type = _recog_ip_type(ip['addr']) if is_floating_ip and ip_type == 'private': private_floating_ips.append(ip) elif is_floating_ip and ip_type == 'public': public_floating_ips.append(ip) else: fixed_ips.append(ip) server.update(fixed_ips=fixed_ips) server.update(private_floating_ips=private_floating_ips) server.update(public_floating_ips=public_floating_ips) # get image info image_id = server['image']['id'] image = ops_api.get_image(auth_token, image_id) if image is not None: server.update(image) server.pop('image') # get running time (now - created_at) # FIXME(hzzhoushaoyu): created_at should transform to utc+0 datetime, # but as list show required nova return utc+8 datetime. # If nova return utc+0 datetime, running time may be ERROR data. created_at = server['created'] created_datetime = timeutils.normalize_time( timeutils.local_to_utc( timeutils.parse_strtime(created_at, '%Y-%m-%d %H:%M:%S'))) running_time = timeutils.seconds_from_now(created_datetime) server.update({"running_seconds": running_time}) return server