66from functools import partial
77from typing import ClassVar , Dict , Optional , List , Type , Any
88
9+ from boto3 .exceptions import Boto3Error
910from attrs import define , field
1011
1112from fix_plugin_aws .aws_client import AwsClient
2223from fix_plugin_aws .resource .kms import AwsKmsKey
2324from fix_plugin_aws .resource .s3 import AwsS3Bucket
2425from fix_plugin_aws .utils import ToDict , TagsValue
26+ from fix_plugin_aws .aws_client import AwsClient
2527from fixlib .baseresources import (
2628 BaseInstance ,
2729 BaseKeyPair ,
@@ -384,14 +386,14 @@ class AwsEc2InferenceAcceleratorInfo:
384386
385387@define (eq = False , slots = False )
386388class AwsEc2InstanceType (AwsResource , BaseInstanceType ):
389+ # collected via AwsEc2Instance
387390 kind : ClassVar [str ] = "aws_ec2_instance_type"
388391 _kind_display : ClassVar [str ] = "AWS EC2 Instance Type"
389392 _kind_description : ClassVar [str ] = "AWS EC2 Instance Types are predefined virtual server configurations offered by Amazon Web Services. Each type specifies the compute, memory, storage, and networking capacity of the virtual machine. Users select an instance type based on their application's requirements, balancing performance and cost. EC2 instances can be launched, stopped, and terminated as needed for various computing workloads." # fmt: skip
390393 _docs_url : ClassVar [str ] = "https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html"
391394 _kind_service : ClassVar [Optional [str ]] = service_name
392395 _metadata : ClassVar [Dict [str , Any ]] = {"icon" : "type" , "group" : "compute" }
393396 _aws_metadata : ClassVar [Dict [str , Any ]] = {"arn_tpl" : "arn:{partition}:ec2:{region}:{account}:instance/{id}" } # fmt: skip
394- api_spec : ClassVar [AwsApiSpec ] = AwsApiSpec (service_name , "describe-instance-types" , "InstanceTypes" )
395397 _reference_kinds : ClassVar [ModelReference ] = {
396398 "successors" : {
397399 "default" : ["aws_ec2_instance" ],
@@ -456,6 +458,29 @@ class AwsEc2InstanceType(AwsResource, BaseInstanceType):
456458 auto_recovery_supported : Optional [bool ] = field (default = None )
457459 supported_boot_modes : List [str ] = field (factory = list )
458460
461+ @classmethod
462+ def collect_resource_types (cls , builder : GraphBuilder , instance_types : List [str ]) -> None :
463+ spec = AwsApiSpec (service_name , "describe-instance-types" , "InstanceTypes" )
464+ log .debug (f"Collecting { cls .__name__ } in region { builder .region .name } " )
465+ try :
466+ filters = [{"Name" : "instance-type" , "Values" : instance_types }]
467+ items = builder .client .list (
468+ aws_service = spec .service ,
469+ action = spec .api_action ,
470+ result_name = spec .result_property ,
471+ expected_errors = spec .expected_errors ,
472+ Filters = filters ,
473+ )
474+ cls .collect (items , builder )
475+ except Boto3Error as e :
476+ msg = f"Error while collecting { cls .__name__ } in region { builder .region .name } : { e } "
477+ builder .core_feedback .error (msg , log )
478+ raise
479+ except Exception as e :
480+ msg = f"Error while collecting { cls .__name__ } in region { builder .region .name } : { e } "
481+ builder .core_feedback .info (msg , log )
482+ raise
483+
459484 @classmethod
460485 def collect (cls : Type [AwsResource ], json : List [Json ], builder : GraphBuilder ) -> None :
461486 for js in json :
@@ -467,6 +492,14 @@ def collect(cls: Type[AwsResource], json: List[Json], builder: GraphBuilder) ->
467492 # we collect instance types in all regions and make the data unique in the builder
468493 builder .global_instance_types [it .safe_name ] = it
469494
495+ @classmethod
496+ def service_name (cls ) -> Optional [str ]:
497+ return service_name
498+
499+ @classmethod
500+ def called_collect_apis (cls ) -> List [AwsApiSpec ]:
501+ return [AwsApiSpec (service_name , "describe-instance-types" )]
502+
470503
471504# endregion
472505
@@ -1375,6 +1408,17 @@ class AwsEc2Instance(EC2Taggable, AwsResource, BaseInstance):
13751408 instance_maintenance_options : Optional [str ] = field (default = None )
13761409 instance_user_data : Optional [str ] = field (default = None )
13771410
1411+ @classmethod
1412+ def collect_resources (cls , builder : GraphBuilder ) -> None :
1413+ super ().collect_resources (builder )
1414+ ec2_instance_types = set ()
1415+ for instance in builder .nodes (clazz = AwsEc2Instance ):
1416+ ec2_instance_types .add (instance .instance_type )
1417+ if ec2_instance_types :
1418+ builder .submit_work (
1419+ service_name , AwsEc2InstanceType .collect_resource_types , builder , list (ec2_instance_types )
1420+ )
1421+
13781422 @classmethod
13791423 def collect (cls : Type [AwsResource ], json : List [Json ], builder : GraphBuilder ) -> None :
13801424 def fetch_user_data (instance : AwsEc2Instance ) -> None :
0 commit comments