Skip to content

Commit 04a4f24

Browse files
authored
fix: no attribute 'type' error in CloudInstance combiner (#4432)
Signed-off-by: Xiaoxue Wang <[email protected]>
1 parent e248e40 commit 04a4f24

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

insights/combiners/cloud_instance.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* :py:class:`insights.parsers.subscription_manager.SubscriptionManagerFacts`
1414
1515
"""
16+
1617
from insights.combiners.cloud_provider import CloudProvider
1718
from insights.core.exceptions import ContentException, SkipComponent
1819
from insights.core.filters import add_filter
@@ -22,7 +23,7 @@
2223
from insights.parsers.gcp_instance_type import GCPInstanceType
2324
from insights.parsers.subscription_manager import SubscriptionManagerFacts
2425

25-
add_filter(SubscriptionManagerFacts, 'instance_id')
26+
add_filter(SubscriptionManagerFacts, ['instance_id', 'instance_type'])
2627

2728

2829
@combiner(
@@ -33,7 +34,7 @@
3334
AzureInstanceType,
3435
GCPInstanceType,
3536
SubscriptionManagerFacts,
36-
]
37+
],
3738
)
3839
class CloudInstance(object):
3940
"""
@@ -65,10 +66,11 @@ class CloudInstance(object):
6566
>>> ci.size == 't2.micro'
6667
True
6768
"""
68-
def __init__(self, cp, aws=None, azure_id=None, azure_type=None,
69-
gcp=None, facts=None):
69+
70+
def __init__(self, cp, aws=None, azure_id=None, azure_type=None, gcp=None, facts=None):
7071
self.provider = cp.cloud_provider
7172
self.id = None
73+
self.type = None
7274
# 1. Get from the Cloud REST API at first
7375
if aws:
7476
self.id = aws.get('instanceId')
@@ -84,6 +86,8 @@ def __init__(self, cp, aws=None, azure_id=None, azure_type=None,
8486
if key not in facts:
8587
raise ContentException("Unmatched/unsupported types!")
8688
self.id = facts[key]
89+
if self.type is None:
90+
self.type = facts.get("{0}_instance_type".format(self.provider))
8791
# The instance id is the key attribute of this Combiner
8892
if self.id is None:
8993
raise SkipComponent

insights/tests/combiners/test_cloud_instance.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from insights.core.exceptions import ContentException, SkipComponent
88
from insights.parsers.aws_instance_id import AWSInstanceIdDoc
99
from insights.parsers.azure_instance import AzureInstanceID, AzureInstanceType
10+
from insights.parsers.dmidecode import DMIDecode
1011
from insights.parsers.gcp_instance_type import GCPInstanceType
1112
from insights.parsers.installed_rpms import InstalledRpms
1213
from insights.parsers.subscription_manager import SubscriptionManagerFacts
@@ -23,6 +24,41 @@
2324
uname.sysname: Linux
2425
""".strip()
2526

27+
AWS_RHSM_FACTS = """
28+
aws_instance_id: i-01234567890abcdef
29+
aws_instance_type: m5.large
30+
""".strip()
31+
32+
AWS_RHSM_FACTS_NO_TYPE = """
33+
aws_instance_id: i-01234567890abcdef
34+
""".strip()
35+
36+
AWS_DMIDECODE = """
37+
# dmidecode 3.3
38+
Getting SMBIOS data from sysfs.
39+
SMBIOS 2.7 present.
40+
13 structures occupying 568 bytes.
41+
Table at 0xBFFF0000.
42+
43+
Handle 0x0000, DMI type 0, 24 bytes
44+
BIOS Information
45+
Vendor: Amazon EC2
46+
Version: 1.0
47+
Release Date: 10/16/2017
48+
Address: 0xF0000
49+
Runtime Size: 64 kB
50+
ROM Size: 64 kB
51+
Characteristics:
52+
PCI is supported
53+
EDD is supported
54+
ACPI is supported
55+
System is a virtual machine
56+
BIOS Revision: 1.0
57+
58+
Handle 0x000C, DMI type 127, 4 bytes
59+
End Of Table
60+
""".strip()
61+
2662

2763
def test_cloud_instance_google():
2864
rpms = InstalledRpms(context_wrap(RPMS_GOOGLE))
@@ -59,6 +95,30 @@ def test_cloud_instance_azure():
5995
assert ret.size == "Standard_NV48s_v3"
6096

6197

98+
def test_cloud_instance_aws_from_submanfacts():
99+
rpms = InstalledRpms(context_wrap(RPMS_AWS))
100+
bios = DMIDecode(context_wrap(AWS_DMIDECODE))
101+
facts = SubscriptionManagerFacts(context_wrap(AWS_RHSM_FACTS))
102+
cp = CloudProvider(rpms, bios, None, None)
103+
ret = CloudInstance(cp, None, None, None, None, facts)
104+
assert ret.provider == CloudProvider.AWS
105+
assert ret.id == "i-01234567890abcdef"
106+
assert ret.type == "m5.large"
107+
assert ret.size == "m5.large"
108+
109+
110+
def test_cloud_instance_aws_from_submanfacts_no_type():
111+
rpms = InstalledRpms(context_wrap(RPMS_AWS))
112+
bios = DMIDecode(context_wrap(AWS_DMIDECODE))
113+
facts = SubscriptionManagerFacts(context_wrap(AWS_RHSM_FACTS_NO_TYPE))
114+
cp = CloudProvider(rpms, bios, None, None)
115+
ret = CloudInstance(cp, None, None, None, None, facts)
116+
assert ret.provider == CloudProvider.AWS
117+
assert ret.id == "i-01234567890abcdef"
118+
assert ret.type is None
119+
assert ret.size is None
120+
121+
62122
def test_cloud_instance_ex():
63123
rpms = InstalledRpms(context_wrap(RPMS_GOOGLE))
64124
_type = GCPInstanceType(context_wrap(GOOGLE_TYPE_1))

0 commit comments

Comments
 (0)