Esempio n. 1
0
 def test_timestamp_to_epoch(self):
     """
     ``timestamp_to_epoch`` returns an epoch as a float with microseconds.
     """
     self.assertEqual(timestamp.timestamp_to_epoch('1970-01-01T00:00:00Z'),
                      0.0)
     self.assertEqual(
         timestamp.timestamp_to_epoch('2015-05-01T04:51:12.078580Z'),
         1430455872.078580)
Esempio n. 2
0
 def test_timestamp_to_epoch(self):
     """
     ``timestamp_to_epoch`` returns an epoch as a float with microseconds.
     """
     self.assertEqual(
         timestamp.timestamp_to_epoch('1970-01-01T00:00:00Z'),
         0.0)
     self.assertEqual(
         timestamp.timestamp_to_epoch('2015-05-01T04:51:12.078580Z'),
         1430455872.078580)
Esempio n. 3
0
 def test_first_entry(self):
     """
     Takes the first entry only
     """
     feed = self.feed.format(self.summary, self.updated)
     self.assertEqual(extract_CLB_drained_at(feed),
                      timestamp_to_epoch(self.updated))
Esempio n. 4
0
 def test_first_entry(self):
     """
     Takes the first entry only
     """
     feed = self.feed.format(self.summary, self.updated)
     self.assertEqual(extract_CLB_drained_at(feed),
                      timestamp_to_epoch(self.updated))
Esempio n. 5
0
    def from_server_details_json(cls, server_json):
        """
        Create a :obj:`NovaServer` instance from a server details JSON
        dictionary, although without any 'server' or 'servers' initial resource
        key.

        See
        http://docs.rackspace.com/servers/api/v2/cs-devguide/content/
        Get_Server_Details-d1e2623.html

        :return: :obj:`NovaServer` instance
        """
        try:
            server_state = ServerState.lookupByName(server_json['status'])
        except ValueError:
            server_state = ServerState.UNKNOWN_TO_OTTER
        if server_json.get("OS-EXT-STS:task_state", "") == "deleting":
            server_state = ServerState.DELETED
        metadata = server_json.get('metadata', {})

        return cls(id=server_json['id'],
                   state=server_state,
                   created=timestamp_to_epoch(server_json['created']),
                   image_id=get_in(["image", "id"], server_json),
                   flavor_id=server_json['flavor']['id'],
                   links=freeze(server_json['links']),
                   desired_lbs=_lbs_from_metadata(metadata),
                   servicenet_address=_servicenet_address(server_json),
                   json=freeze(server_json))
Esempio n. 6
0
    def from_server_details_json(cls, server_json):
        """
        Create a :obj:`NovaServer` instance from a server details JSON
        dictionary, although without any 'server' or 'servers' initial resource
        key.

        See
        http://docs.rackspace.com/servers/api/v2/cs-devguide/content/
        Get_Server_Details-d1e2623.html

        :return: :obj:`NovaServer` instance
        """
        try:
            server_state = ServerState.lookupByName(server_json['status'])
        except ValueError:
            server_state = ServerState.UNKNOWN_TO_OTTER
        if server_json.get("OS-EXT-STS:task_state", "") == "deleting":
            server_state = ServerState.DELETED
        metadata = server_json.get('metadata', {})

        return cls(
            id=server_json['id'],
            state=server_state,
            created=timestamp_to_epoch(server_json['created']),
            image_id=server_json.get('image', {}).get('id'),
            flavor_id=server_json['flavor']['id'],
            links=freeze(server_json['links']),
            desired_lbs=_lbs_from_metadata(metadata),
            servicenet_address=_servicenet_address(server_json),
            json=freeze(server_json))
Esempio n. 7
0
 def test_created(self):
     """
     Returns time matched from first "Node created" entry found in feeds
     """
     feed = self.parsed_feed(
         ("summary", "2000-10-01Z"), (self.created_summary, self.updated2))
     self.assertEqual(
         extract_clb_drained_at(feed), timestamp_to_epoch(self.updated2))
Esempio n. 8
0
 def test_created(self):
     """
     Returns time matched from first "Node created" entry found in feeds
     """
     feed = self.parsed_feed(("summary", "2000-10-01Z"),
                             (self.created_summary, self.updated2))
     self.assertEqual(extract_clb_drained_at(feed),
                      timestamp_to_epoch(self.updated2))
Esempio n. 9
0
def extract_clb_drained_at(feed):
    """
    Extract time when node was changed to DRAINING from a CLB atom feed. Will
    return node's creation time if node was created with DRAINING. Return None
    if couldnt find for any reason.

    :param list feed: ``list`` of atom entry :class:`Elements`

    :returns: drained_at EPOCH in seconds
    :rtype: float
    """
    for entry in feed:
        if _DRAINING_RE.match(atom.summary(entry)):
            return timestamp_to_epoch(atom.updated(entry))
    return None
Esempio n. 10
0
def extract_clb_drained_at(feed):
    """
    Extract time when node was changed to DRAINING from a CLB atom feed. Will
    return node's creation time if node was created with DRAINING. Return None
    if couldnt find for any reason.

    :param list feed: ``list`` of atom entry :class:`Elements`

    :returns: drained_at EPOCH in seconds
    :rtype: float
    """
    for entry in feed:
        if _DRAINING_RE.match(atom.summary(entry)):
            return timestamp_to_epoch(atom.updated(entry))
    return None
Esempio n. 11
0
def validate_datetime(dt_str):
    """
    Validate date-time string in json. Return True if valid and raise
    ValueError if invalid
    """
    if dt_str and dt_str[-1] != 'Z':
        raise ValueError('Expecting Zulu-format UTC time')
    try:
        ep = timestamp_to_epoch(dt_str)
    except ParseError:
        raise ValueError('Error parsing datetime str')
    # Ensure time is in future - can't just parse into a datetime and compare
    # because we cannot compare naive datetimes with timezone-aware datetimes
    if datetime.utcfromtimestamp(ep) <= datetime.utcnow():
        raise ValidationError(
            'Invalid "{0}" datetime: It must be in the future'.format(dt_str))
    return True
Esempio n. 12
0
def validate_datetime(dt_str):
    """
    Validate date-time string in json. Return True if valid and raise
    ValueError if invalid
    """
    if dt_str and dt_str[-1] != 'Z':
        raise ValueError('Expecting Zulu-format UTC time')
    try:
        ep = timestamp_to_epoch(dt_str)
    except ParseError:
        raise ValueError('Error parsing datetime str')
    # Ensure time is in future - can't just parse into a datetime and compare
    # because we cannot compare naive datetimes with timezone-aware datetimes
    if datetime.utcfromtimestamp(ep) <= datetime.utcnow():
        raise ValidationError(
            'Invalid "{0}" datetime: It must be in the future'.format(dt_str))
    return True
Esempio n. 13
0
def extract_CLB_drained_at(feed):
    """
    Extract time when node was changed to DRAINING from a CLB atom feed.

    :param str feed: Atom feed of the node

    :returns: EPOCH in seconds
    :rtype: float
    """
    # TODO: This function temporarily only looks at last entry assuming that
    # it was draining operation. May need to look at all entries in reverse
    # order and check for draining operation. This could include paging to
    # further entries
    entry = atom.entries(atom.parse(feed))[0]
    summary = atom.summary(entry)
    if 'Node successfully updated' in summary and 'DRAINING' in summary:
        return timestamp_to_epoch(atom.updated(entry))
    else:
        raise ValueError('Unexpected summary: {}'.format(summary))
Esempio n. 14
0
def extract_CLB_drained_at(feed):
    """
    Extract time when node was changed to DRAINING from a CLB atom feed.

    :param str feed: Atom feed of the node

    :returns: EPOCH in seconds
    :rtype: float
    """
    # TODO: This function temporarily only looks at last entry assuming that
    # it was draining operation. May need to look at all entries in reverse
    # order and check for draining operation. This could include paging to
    # further entries
    entry = atom.entries(atom.parse(feed))[0]
    summary = atom.summary(entry)
    if 'Node successfully updated' in summary and 'DRAINING' in summary:
        return timestamp_to_epoch(atom.updated(entry))
    else:
        raise ValueError('Unexpected summary: {}'.format(summary))