Skip to content

Commit

Permalink
Add instance_life_cycle attribute (#535)
Browse files Browse the repository at this point in the history
Fixes #440.
  • Loading branch information
adamchainz authored Oct 27, 2024
1 parent 6c7e09c commit 960b48b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* Add ``instance_life_cycle`` attribute.

Thanks to Zeev Rotshtein for the feature request in `Issue #440 <https://github.com/adamchainz/ec2-metadata/issues/440>`__.

* Drop Python 3.8 support.

* Support Python 3.13.
Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ The dict has this type, based on that document:
region: str
version: str
``instance_life_cycle: str``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The purchasing option of this instance, for example ``'on-demand'``.

``instance_profile_arn: str | None``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 4 additions & 0 deletions src/ec2_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def instance_identity_document(self) -> InstanceIdentityDocumentDict:
).json()
return result

@cached_property
def instance_life_cycle(self) -> str:
return self._get_url(f"{self.metadata_url}instance-life-cycle").text

@property
def instance_profile_arn(self) -> str | None:
iam_info = self.iam_info
Expand Down
7 changes: 7 additions & 0 deletions tests/test_ec2_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ def test_instance_identity(em_requests_mock):
assert ec2_metadata.instance_identity_document == identity_doc


def test_instance_life_cycle(em_requests_mock):
em_requests_mock.get(
"http://169.254.169.254/latest/meta-data/instance-life-cycle", text="on-demand"
)
assert ec2_metadata.instance_life_cycle == "on-demand"


def test_instance_profile_arn(em_requests_mock):
em_requests_mock.get(
"http://169.254.169.254/latest/meta-data/iam/info",
Expand Down

0 comments on commit 960b48b

Please sign in to comment.