def _hours_for(self, instance, period_start, period_stop): launched_at = instance['launched_at'] terminated_at = instance['terminated_at'] if terminated_at is not None: if not isinstance(terminated_at, datetime.datetime): terminated_at = utils.parse_strtime(terminated_at, "%Y-%m-%d %H:%M:%S.%f") if launched_at is not None: if not isinstance(launched_at, datetime.datetime): launched_at = utils.parse_strtime(launched_at, "%Y-%m-%d %H:%M:%S.%f") if terminated_at and terminated_at < period_start: return 0 # nothing if it started after the usage report ended if launched_at and launched_at > period_stop: return 0 if launched_at: # if instance launched after period_started, don't charge for first start = max(launched_at, period_start) if terminated_at: # if instance stopped before period_stop, don't charge after stop = min(period_stop, terminated_at) else: # instance is still running, so charge them up to current time stop = period_stop dt = stop - start seconds = (dt.days * 3600 * 24 + dt.seconds + dt.microseconds / 100000.0) return seconds / 3600.0 else: # instance hasn't launched, so no charge return 0
def _parse_datetime(self, dtstr): if not dtstr: return utils.utcnow() elif isinstance(dtstr, datetime.datetime): return dtstr try: return utils.parse_strtime(dtstr, "%Y-%m-%dT%H:%M:%S") except Exception: try: return utils.parse_strtime(dtstr, "%Y-%m-%dT%H:%M:%S.%f") except Exception: return utils.parse_strtime(dtstr, "%Y-%m-%d %H:%M:%S.%f")
def test_return_valid_isoformat(self): """ Ensure that the ec2 api returns datetime in xs:dateTime (which apparently isn't datetime.isoformat()) NOTE(ken-pepple): https://bugs.launchpad.net/nova/+bug/721297 """ conv = apirequest._database_to_isoformat # sqlite database representation with microseconds time_to_convert = utils.parse_strtime("2011-02-21 20:14:10.634276", "%Y-%m-%d %H:%M:%S.%f") self.assertEqual(conv(time_to_convert), '2011-02-21T20:14:10.634Z') # mysqlite database representation time_to_convert = utils.parse_strtime("2011-02-21 19:56:18", "%Y-%m-%d %H:%M:%S") self.assertEqual(conv(time_to_convert), '2011-02-21T19:56:18.000Z')
def test_return_valid_isoformat(self): """ Ensure that the ec2 api returns datetime in xs:dateTime (which apparently isn't datetime.isoformat()) NOTE(ken-pepple): https://bugs.launchpad.net/nova/+bug/721297 """ conv = apirequest._database_to_isoformat # sqlite database representation with microseconds time_to_convert = utils.parse_strtime("2011-02-21 20:14:10.634276", "%Y-%m-%d %H:%M:%S.%f") self.assertEqual(conv(time_to_convert), '2011-02-21T20:14:10.634Z') # mysqlite database representation time_to_convert = utils.parse_strtime("2011-02-21 19:56:18", "%Y-%m-%d %H:%M:%S") self.assertEqual(conv(time_to_convert), '2011-02-21T19:56:18.000Z')
def __init__(self, user_id, project_id, is_admin=None, read_deleted="no", roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, strategy='noauth', overwrite=True): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that *only* deleted records are visible. :param overwrite: Set to False to ensure that the greenthread local copy of the index is not overwritten. """ self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = 'admin' in [x.lower() for x in self.roles] elif self.is_admin and 'admin' not in self.roles: self.roles.append('admin') self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = 'req-' + str(utils.gen_uuid()) self.request_id = request_id self.auth_token = auth_token self.strategy = strategy if overwrite or not hasattr(local.store, 'context'): local.store.context = self
def fake_vpn_instance_low_id(): return { 'id': 4, 'image_id': FLAGS.vpn_image_id, 'vm_state': 'active', 'created_at': utils.parse_strtime('1981-10-20T00:00:00.000000') }
def __init__(self, user_id, project_id, is_admin=None, read_deleted=False, roles=None, remote_address=None, timestamp=None, request_id=None): self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.admin = 'admin' in self.roles self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = unicode(uuid.uuid4()) self.request_id = request_id
def __init__(self, user_id, project_id, is_admin=None, read_deleted="no", roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, strategy='noauth'): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that *only* deleted records are visible. """ self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = 'admin' in [x.lower() for x in self.roles] self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = unicode(uuid.uuid4()) self.request_id = request_id self.auth_token = auth_token self.strategy = strategy local.store.context = self
def __init__( self, user_id, project_id, is_admin=None, read_deleted=False, roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, strategy="noauth", ): self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = "admin" in [x.lower() for x in self.roles] self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = unicode(uuid.uuid4()) self.request_id = request_id self.auth_token = auth_token self.strategy = strategy
def __init__(self, user_id, project_id, is_admin=None, read_deleted="no", roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, strategy='noauth', overwrite=True): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that *only* deleted records are visible. :param overwrite: Set to False to ensure that the greenthread local copy of the index is not overwritten. """ self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = 'admin' in [x.lower() for x in self.roles] self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = 'req-' + str(utils.gen_uuid()) self.request_id = request_id self.auth_token = auth_token self.strategy = strategy if overwrite or not hasattr(local.store, 'context'): local.store.context = self
def fake_vpn_instance_low_id(): return { "id": 4, "image_id": FLAGS.vpn_image_id, "vm_state": "active", "created_at": utils.parse_strtime("1981-10-20T00:00:00.000000"), }
def __init__(self, user_id, project_id, is_admin=None, read_deleted=False, roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, strategy='noauth'): self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = 'admin' in [x.lower() for x in self.roles] self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = unicode(uuid.uuid4()) self.request_id = request_id self.auth_token = auth_token self.strategy = strategy local.store.context = self
def __init__(self, user_id, project_id, is_admin=None, read_deleted="no", roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, overwrite=True, quota_class=None, user_name=None, project_name=None, **kwargs): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that *only* deleted records are visible. :param overwrite: Set to False to ensure that the greenthread local copy of the index is not overwritten. :param kwargs: Extra arguments that might be present, but we ignore because they possibly came in from older rpc messages. """ if kwargs: LOG.warn( _('Arguments dropped when creating context: %s') % str(kwargs)) self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = 'admin' in [x.lower() for x in self.roles] elif self.is_admin and 'admin' not in self.roles: self.roles.append('admin') self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = generate_request_id() self.request_id = request_id self.auth_token = auth_token # NOTE(markmc): this attribute is currently only used by the # rs_limits turnstile pre-processor. # See https://lists.launchpad.net/openstack/msg12200.html self.quota_class = quota_class self.user_name = user_name self.project_name = project_name if overwrite or not hasattr(local.store, 'context'): self.update_store()
def fake_vpn_instance(): return { 'id': 7, 'image_ref': FLAGS.vpn_image_id, 'vm_state': 'active', 'created_at': utils.parse_strtime('1981-10-20T00:00:00.000000'), 'uuid': 7777, 'project_id': 'other' }
def _get_most_recent_update(self, versions): recent = None for version in versions: updated = utils.parse_strtime(version['updated'], '%Y-%m-%dT%H:%M:%SZ') if not recent: recent = updated elif updated > recent: recent = updated return recent.strftime('%Y-%m-%dT%H:%M:%SZ')
def _get_most_recent_update(self, versions): recent = None for version in versions: updated = utils.parse_strtime(version['updated'], '%Y-%m-%dT%H:%M:%SZ') if not recent: recent = updated elif updated > recent: recent = updated return recent.strftime('%Y-%m-%dT%H:%M:%SZ')
def _parse_glance_iso8601_timestamp(timestamp): """Parse a subset of iso8601 timestamps into datetime objects.""" iso_formats = ['%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S'] for iso_format in iso_formats: try: return utils.parse_strtime(timestamp, iso_format) except ValueError: pass raise ValueError(_('%(timestamp)s does not follow any of the ' 'signatures: %(iso_formats)s') % locals())
def __init__(self, user_id, project_id, is_admin=None, read_deleted="no", roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, overwrite=True, **kwargs): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that *only* deleted records are visible. :param overwrite: Set to False to ensure that the greenthread local copy of the index is not overwritten. :param kwargs: Extra arguments that might be present, but we ignore because they possibly came in from older rpc messages. """ if read_deleted not in ('no', 'yes', 'only'): raise ValueError( _("read_deleted can only be one of 'no', " "'yes' or 'only', not %r") % read_deleted) if kwargs: LOG.warn( _('Arguments dropped when creating context: %s') % str(kwargs)) self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = 'admin' in [x.lower() for x in self.roles] elif self.is_admin and 'admin' not in self.roles: self.roles.append('admin') self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = generate_request_id() self.request_id = request_id self.auth_token = auth_token if overwrite or not hasattr(local.store, 'context'): self.update_store()
def _parse_glance_iso8601_timestamp(timestamp): """Parse a subset of iso8601 timestamps into datetime objects.""" iso_formats = ['%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S'] for iso_format in iso_formats: try: return utils.parse_strtime(timestamp, iso_format) except ValueError: pass raise ValueError( _('%(timestamp)s does not follow any of the ' 'signatures: %(iso_formats)s') % locals())
def __init__( self, user_id, project_id, is_admin=None, read_deleted="no", roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, overwrite=True, **kwargs ): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that *only* deleted records are visible. :param overwrite: Set to False to ensure that the greenthread local copy of the index is not overwritten. :param kwargs: Extra arguments that might be present, but we ignore because they possibly came in from older rpc messages. """ if read_deleted not in ("no", "yes", "only"): raise ValueError(_("read_deleted can only be one of 'no', " "'yes' or 'only', not %r") % read_deleted) if kwargs: LOG.warn(_("Arguments dropped when creating context: %s") % str(kwargs)) self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = "admin" in [x.lower() for x in self.roles] elif self.is_admin and "admin" not in self.roles: self.roles.append("admin") self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = generate_request_id() self.request_id = request_id self.auth_token = auth_token if overwrite or not hasattr(local.store, "context"): local.store.context = self
def __init__(self, user_id, project_id, is_admin=None, read_deleted="no", roles=None, remote_address=None, timestamp=None, request_id=None, auth_token=None, overwrite=True, quota_class=None, user_name=None, project_name=None, **kwargs): """ :param read_deleted: 'no' indicates deleted records are hidden, 'yes' indicates deleted records are visible, 'only' indicates that *only* deleted records are visible. :param overwrite: Set to False to ensure that the greenthread local copy of the index is not overwritten. :param kwargs: Extra arguments that might be present, but we ignore because they possibly came in from older rpc messages. """ if kwargs: LOG.warn(_('Arguments dropped when creating context: %s') % str(kwargs)) self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.is_admin = 'admin' in [x.lower() for x in self.roles] elif self.is_admin and 'admin' not in self.roles: self.roles.append('admin') self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = generate_request_id() self.request_id = request_id self.auth_token = auth_token # NOTE(markmc): this attribute is currently only used by the # rs_limits turnstile pre-processor. # See https://lists.launchpad.net/openstack/msg12200.html self.quota_class = quota_class self.user_name = user_name self.project_name = project_name if overwrite or not hasattr(local.store, 'context'): self.update_store()
def __init__(self, user_id, project_id, is_admin=None, read_deleted=False, roles=None, remote_address=None, timestamp=None, request_id=None): self.user_id = user_id self.project_id = project_id self.roles = roles or [] self.is_admin = is_admin if self.is_admin is None: self.admin = 'admin' in self.roles self.read_deleted = read_deleted self.remote_address = remote_address if not timestamp: timestamp = utils.utcnow() if isinstance(timestamp, basestring): timestamp = utils.parse_strtime(timestamp) self.timestamp = timestamp if not request_id: request_id = unicode(uuid.uuid4()) self.request_id = request_id
def fake_vpn_instance(): return {'id': 7, 'image_ref': FLAGS.vpn_image_id, 'vm_state': 'active', 'created_at': utils.parse_strtime('1981-10-20T00:00:00.000000'), 'uuid': 7777, 'project_id': 'fake'}
def fake_vpn_instance_low_id(): return {'id': 4, 'image_id': FLAGS.vpn_image_id, 'vm_state': 'active', 'created_at': utils.parse_strtime('1981-10-20T00:00:00.000000')}