예제 #1
0
    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
예제 #2
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")
예제 #3
0
파일: test_api.py 프로젝트: Nesrine85/nova
 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')
예제 #4
0
파일: test_api.py 프로젝트: pulchart/nova
 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')
예제 #5
0
파일: context.py 프로젝트: acomisario/nova
    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
예제 #6
0
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')
    }
예제 #7
0
파일: context.py 프로젝트: cp16net/reddwarf
 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
예제 #8
0
파일: context.py 프로젝트: yamahata/nova
 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
예제 #9
0
 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
예제 #10
0
    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
예제 #11
0
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"),
    }
예제 #12
0
 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
예제 #13
0
    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()
예제 #14
0
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'
    }
예제 #15
0
    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')
예제 #16
0
파일: versions.py 프로젝트: AsylumCorp/nova
    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')
예제 #17
0
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())
예제 #18
0
    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()
예제 #19
0
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())
예제 #20
0
파일: context.py 프로젝트: esirola/nova
    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
예제 #21
0
    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()
예제 #22
0
파일: context.py 프로젝트: cp16net/reddwarf
 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
예제 #23
0
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'}
예제 #24
0
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')}