From 960b48b884fda9b36d487c8bb42d38428ee36ad5 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sun, 27 Oct 2024 08:29:26 +0000 Subject: [PATCH] Add instance_life_cycle attribute (#535) Fixes #440. --- CHANGELOG.rst | 4 ++++ README.rst | 5 +++++ src/ec2_metadata/__init__.py | 4 ++++ tests/test_ec2_metadata.py | 7 +++++++ 4 files changed, 20 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 144e3d6..35cce14 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ Changelog ========= +* Add ``instance_life_cycle`` attribute. + + Thanks to Zeev Rotshtein for the feature request in `Issue #440 `__. + * Drop Python 3.8 support. * Support Python 3.13. diff --git a/README.rst b/README.rst index b070a6e..d01cc32 100644 --- a/README.rst +++ b/README.rst @@ -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`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/ec2_metadata/__init__.py b/src/ec2_metadata/__init__.py index 163d9b1..2571293 100644 --- a/src/ec2_metadata/__init__.py +++ b/src/ec2_metadata/__init__.py @@ -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 diff --git a/tests/test_ec2_metadata.py b/tests/test_ec2_metadata.py index c0eb0b6..fb6984e 100644 --- a/tests/test_ec2_metadata.py +++ b/tests/test_ec2_metadata.py @@ -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",