Common: Replace protected attributes with read-only properties

This commit is contained in:
Mike Salvatore 2022-04-28 08:43:38 -04:00 committed by vakarisz
parent b58d847e22
commit 797482a172
6 changed files with 60 additions and 58 deletions

View File

@ -33,16 +33,24 @@ class AwsInstance(CloudInstance):
__metaclass__ = Singleton
def __init__(self):
self._is_instance, instance_info = AwsInstance._fetch_instance_info()
self._instance_id = instance_info.instance_id
self._region = instance_info.region
self._account_id = instance_info.account_id
self._is_instance, self._instance_info = AwsInstance._fetch_instance_info()
@property
def is_instance(self) -> bool:
return self._is_instance
@property
def instance_id(self) -> str:
return self._instance_info.instance_id
@property
def region(self) -> str:
return self._instance_info.region
@property
def account_id(self) -> str:
return self._instance_info.account_id
@staticmethod
def _fetch_instance_info() -> Tuple[bool, AwsInstanceInfo]:
try:
@ -93,12 +101,6 @@ class AwsInstance(CloudInstance):
else:
return None
def get_instance_id(self):
return self._instance_id
def get_region(self):
return self._region
@staticmethod
def _extract_account_id(instance_identity_document_response):
"""
@ -117,4 +119,4 @@ class AwsInstance(CloudInstance):
:return: the AWS account ID which "owns" this instance.
See https://docs.aws.amazon.com/general/latest/gr/acct-identifiers.html
"""
return self._account_id
return self.account_id

View File

@ -63,7 +63,7 @@ class AwsService(object):
:return: All visible instances from this instance
"""
current_instance = AwsInstance()
local_ssm_client = boto3.client("ssm", current_instance.get_region())
local_ssm_client = boto3.client("ssm", current_instance.region)
try:
response = local_ssm_client.describe_instance_information()

View File

@ -21,7 +21,7 @@ def _report_aws_environment(telemetry_messenger: LegacyTelemetryMessengerAdapter
if _running_on_aws(aws_instance):
logger.info("Machine is an AWS instance")
telemetry_messenger.send_telemetry(AWSInstanceTelemetry(aws_instance.get_instance_id()))
telemetry_messenger.send_telemetry(AWSInstanceTelemetry(aws_instance.instance_id))
else:
logger.info("Machine is NOT an AWS instance")

View File

@ -57,7 +57,7 @@ class RemoteRunAwsService:
"""
Updates the AWS region without auth params (via IAM role)
"""
AwsService.set_region(RemoteRunAwsService.aws_instance._region)
AwsService.set_region(RemoteRunAwsService.aws_instance.region)
@staticmethod
def _run_aws_monkey_cmd_async(instance_id, is_linux, island_ip):

View File

@ -36,7 +36,7 @@ class AWSExporter(Exporter):
return True
# Not suppressing error here on purpose.
current_aws_region = AwsInstance().get_region()
current_aws_region = AwsInstance().region
for machine in issues_list:
for issue in issues_list[machine]:

View File

@ -77,16 +77,16 @@ def test_is_instance_good_data(good_data_mock_instance):
assert good_data_mock_instance.is_instance
def test_get_instance_id_good_data(good_data_mock_instance):
assert good_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
def test_instance_id_good_data(good_data_mock_instance):
assert good_data_mock_instance.instance_id == EXPECTED_INSTANCE_ID
def test_get_region_good_data(good_data_mock_instance):
assert good_data_mock_instance.get_region() == EXPECTED_REGION
def test_region_good_data(good_data_mock_instance):
assert good_data_mock_instance.region == EXPECTED_REGION
def test_get_account_id_good_data(good_data_mock_instance):
assert good_data_mock_instance.get_account_id() == EXPECTED_ACCOUNT_ID
def test_account_id_good_data(good_data_mock_instance):
assert good_data_mock_instance.account_id == EXPECTED_ACCOUNT_ID
# 'region' bad data
@ -105,16 +105,16 @@ def test_is_instance_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.is_instance
def test_get_instance_id_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
def test_instance_id_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.instance_id == EXPECTED_INSTANCE_ID
def test_get_region_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.get_region() is None
def test_region_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.region is None
def test_get_account_id_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.get_account_id() == EXPECTED_ACCOUNT_ID
def test_account_id_bad_region_data(bad_region_data_mock_instance):
assert bad_region_data_mock_instance.account_id == EXPECTED_ACCOUNT_ID
# 'account_id' bad data
@ -133,16 +133,16 @@ def test_is_instance_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.is_instance
def test_get_instance_id_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
def test_instance_id_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.instance_id == EXPECTED_INSTANCE_ID
def test_get_region_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.get_region() == EXPECTED_REGION
def test_region_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.region == EXPECTED_REGION
def test_get_account_id_data_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.get_account_id() is None
def test_account_id_data_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.account_id is None
# 'instance_id' bad requests
@ -164,18 +164,18 @@ def test_is_instance_bad_instance_id_request(bad_instance_id_request_mock_instan
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_get_instance_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.get_instance_id() is None
def test_instance_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.instance_id is None
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_get_region_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.get_region() is None
def test_region_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.region is None
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_get_account_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.get_account_id() == EXPECTED_ACCOUNT_ID
def test_account_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.account_id == EXPECTED_ACCOUNT_ID
# 'region' bad requests
@ -197,18 +197,18 @@ def test_is_instance_bad_region_request(bad_region_request_mock_instance):
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_get_instance_id_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
def test_instance_id_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.instance_id == EXPECTED_INSTANCE_ID
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_get_region_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.get_region() is None
def test_region_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.region is None
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_get_account_id_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.get_account_id() == EXPECTED_ACCOUNT_ID
def test_account_id_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.account_id == EXPECTED_ACCOUNT_ID
# 'account_id' bad requests
@ -230,18 +230,18 @@ def test_is_instance_bad_account_id_request(bad_account_id_request_mock_instance
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_get_instance_id_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
def test_instance_id_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.instance_id == EXPECTED_INSTANCE_ID
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_get_region_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.get_region() == EXPECTED_REGION
def test_region_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.region == EXPECTED_REGION
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_get_account_id_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.get_account_id() is None
def test_account_id_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.account_id is None
# not found request
@ -268,13 +268,13 @@ def test_is_instance_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.is_instance is False
def test_get_instance_id_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.get_instance_id() is None
def test_instance_id_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.instance_id is None
def test_get_region_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.get_region() is None
def test_region_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.region is None
def test_get_account_id_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.get_account_id() is None
def test_account_id_not_found_request(not_found_request_mock_instance):
assert not_found_request_mock_instance.account_id is None