def ListInstances( self, region: Optional[str] = None, filters: Optional[List[Dict[str, Any]]] = None, show_terminated: bool = False) -> Dict[str, ec2.AWSInstance]: """List instances of an AWS account. Example usage: ListInstances(region='us-east-1', filters=[ {'Name':'instance-id', 'Values':['some-instance-id']}]) Args: region (str): Optional. The region from which to list instances. If none provided, the default_region associated to the AWSAccount object will be used. filters (List[Dict]): Optional. Filters for the query. Filters are given as a list of dictionaries, e.g.: {'Name': 'someFilter', 'Values': ['value1', 'value2']}. show_terminated (bool): Optional. Include terminated instances in the list. Returns: Dict[str, AWInstance]: Dictionary mapping instance IDs (str) to their respective AWSInstance object. Raises: RuntimeError: If instances can't be listed. """ if not filters: filters = [] instances = {} client = self.ClientApi(common.EC2_SERVICE, region=region) responses = common.ExecuteRequest(client, 'describe_instances', {'Filters': filters}) for response in responses: for reservation in response['Reservations']: for instance in reservation['Instances']: # If reservation['Instances'] contains any entry, then the # instance's state is expected to be present in the API's response. if instance['State'][ 'Name'] == 'terminated' and not show_terminated: continue zone = instance['Placement']['AvailabilityZone'] instance_id = instance['InstanceId'] aws_instance = ec2.AWSInstance(self, instance_id, zone[:-1], zone) for tag in instance.get('Tags', []): if tag.get('Key') == 'Name': aws_instance.name = tag.get('Value') break instances[instance_id] = aws_instance return instances
def ListVolumes( self, region: Optional[str] = None, # pylint: disable=line-too-long filters: Optional[List[Dict[str, Any]]] = None ) -> Dict[str, ebs.AWSVolume]: # pylint: enable=line-too-long """List volumes of an AWS account. Example usage: # List volumes attached to the instance 'some-instance-id' ListVolumes(filters=[ {'Name':'attachment.instance-id', 'Values':['some-instance-id']}]) Args: region (str): Optional. The region from which to list the volumes. If none provided, the default_region associated to the AWSAccount object will be used. filters (List[Dict]): Optional. Filters for the query. Filters are given as a list of dictionaries, e.g.: {'Name': 'someFilter', 'Values': ['value1', 'value2']}. Returns: Dict[str, AWSVolume]: Dictionary mapping volume IDs (str) to their respective AWSVolume object. Raises: RuntimeError: If volumes can't be listed. """ if not filters: filters = [] volumes = {} client = self.ClientApi(common.EC2_SERVICE, region=region) responses = common.ExecuteRequest(client, 'describe_volumes', {'Filters': filters}) for response in responses: for volume in response['Volumes']: volume_id = volume['VolumeId'] aws_volume = ebs.AWSVolume(volume_id, self, self.default_region, volume['AvailabilityZone'], volume['Encrypted']) for tag in volume.get('Tags', []): if tag.get('Key') == 'Name': aws_volume.name = tag.get('Value') break for attachment in volume.get('Attachments', []): if attachment.get('State') == 'attached': aws_volume.device_name = attachment.get('Device') break volumes[volume_id] = aws_volume return volumes
def LookupEvents( self, qfilter: Optional[str] = None, starttime: Optional['datetime'] = None, endtime: Optional['datetime'] = None) -> List[Dict[str, Any]]: """Lookup events in the CloudTrail logs of this account. Example usage: # pylint: disable=line-too-long # qfilter = 'key,value' # starttime = datetime(2020,5,5,17,33,00) # LookupEvents(qfilter=qfilter, starttime=starttime) # Check documentation for qfilter details # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudtrail.html#CloudTrail.Client.lookup_events Args: qfilter (string): Optional. Filter for the query including 1 key and value. starttime (datetime): Optional. Start datetime to add to query filter. endtime (datetime): Optional. End datetime to add to query filter. Returns: List[Dict]: A list of events. E.g. [{'EventId': 'id', ...}, {'EventId': ...}] """ events = [] client = self.aws_account.ClientApi(common.CLOUDTRAIL_SERVICE) params = {} # type: Dict[str, Any] if qfilter: k, v = qfilter.split(',') filters = [{'AttributeKey': k, 'AttributeValue': v}] params = {'LookupAttributes': filters} if starttime: params['StartTime'] = starttime if endtime: params['EndTime'] = endtime responses = common.ExecuteRequest(client, 'lookup_events', params) for response in responses: for entry in response['Events']: events.append(entry) return events