Skip to content

Commit

Permalink
Merge pull request #299 from rodrigonull/instance-create-time
Browse files Browse the repository at this point in the history
Add VM instance create time property for all providers
  • Loading branch information
nuwang committed Jul 29, 2022
2 parents f822c0c + 4151042 commit 7b1bcd1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cloudbridge/interfaces/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ def vm_type(self):
"""
pass

@abstractproperty
def create_time(self):
"""
Get the creation data and time for this instance.
:rtype: ``DateTime``
:return: Creation time for this instance as returned by the cloud
middleware.
"""
pass

@abstractmethod
def reboot(self):
"""
Expand Down
7 changes: 7 additions & 0 deletions cloudbridge/providers/aws/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ def vm_type(self):
return self._provider.compute.vm_types.find(
name=self._ec2_instance.instance_type)[0]

@property
def create_time(self):
"""
Get the instance creation time
"""
return self._ec2_instance.launch_time

def reboot(self):
self._ec2_instance.reboot()

Expand Down
7 changes: 7 additions & 0 deletions cloudbridge/providers/azure/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,13 @@ def vm_type(self):
return self._provider.compute.vm_types.find(
name=self.vm_type_id)[0]

@property
def create_time(self):
"""
Get the instance creation time
"""
return self._vm.time_created

def reboot(self):
"""
Reboot this instance (using the cloud middleware API).
Expand Down
8 changes: 8 additions & 0 deletions cloudbridge/providers/gcp/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
import uuid
from collections import namedtuple
from datetime import datetime

import googleapiclient

Expand Down Expand Up @@ -882,6 +883,13 @@ def vm_type(self):
parsed_uri = self._provider.parse_url(machine_type_uri)
return GCPVMType(self._provider, parsed_uri.get_resource())

@property
def create_time(self):
"""
Get the instance creation time
"""
return datetime.fromisoformat(self._gcp_instance.get("creationTimestamp"))

@property
def subnet_id(self):
"""
Expand Down
9 changes: 9 additions & 0 deletions cloudbridge/providers/openstack/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from urlparse import urlparse
from urlparse import urljoin

from datetime import datetime

from keystoneclient.v3.regions import Region

import novaclient.exceptions as novaex
Expand Down Expand Up @@ -361,6 +363,13 @@ def vm_type(self):
self._os_instance.flavor.get('id'))
return OpenStackVMType(self._provider, flavor)

@property
def create_time(self):
"""
Get the instance creation time
"""
return datetime.strptime(self._os_instance.created, '%Y-%m-%dT%H:%M:%SZ')

def reboot(self):
"""
Reboot this instance (using the cloud middleware API).
Expand Down
7 changes: 7 additions & 0 deletions tests/test_compute_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import ipaddress

import six
Expand Down Expand Up @@ -168,6 +169,12 @@ def test_instance_properties(self):
self.assertEqual(len(find_zone), 1,
"Instance's placement zone could not be "
" found in zones list")
self.assertIsNotNone(
test_instance.create_time,
"Instance must have its creation time")
self.assertIsInstance(
test_instance.create_time,
datetime.datetime)

@helpers.skipIfNoService(['compute.instances', 'compute.images',
'compute.vm_types'])
Expand Down

0 comments on commit 7b1bcd1

Please sign in to comment.