Best Python code snippet using localstack_python
compute.py
Source:compute.py  
1import boto32import botocore3import json4import config5import res.utils as utils6import res.glob  as glob7# to do : autoscaling, security groups8# =======================================================================================================================9#10#  Supported services   : EC2 (instances, EBS, Network interfaces, vpc), lambda, lightsail (full), 11#                           Elastic Container Service (ECS), Elastic Beanstalk, EKS, Batch12#  Unsupported services : None13#14# =======================================================================================================================15#  ------------------------------------------------------------------------16#17#    EC2 18#19#  ------------------------------------------------------------------------20def get_ec2_inventory(oId):21    """22        Returns ec2 inventory, without any analysis or any formatting23        :param oId: ownerId (AWS account)24        :type oId: string25        :return: ec2 inventory26        :rtype: json27        .. note:: http://boto3.readthedocs.io/en/latest/reference/services/ec2.html28    """29    30    return glob.get_inventory(31        ownerId = oId,32        aws_service = "ec2", 33        aws_region = "all", 34        function_name = "describe_instances", 35        key_get = "Reservations",36        pagination = True37    )38def get_interfaces_inventory(oId):39    """40        Returns network interfaces detailed inventory41        :param oId: ownerId (AWS account)42        :type oId: string43        :return: network interfaces inventory44        :rtype: json45    """46    return glob.get_inventory(47        ownerId = oId,48        aws_service = "ec2", 49        aws_region = "all", 50        function_name = "describe_network_interfaces", 51        key_get = "NetworkInterfaces"52    )53def get_vpc_inventory(oId):54    """55        Returns VPC detailed inventory56        :param oId: ownerId (AWS account)57        :type oId: string58        :return: VPC inventory59        :rtype: json60    """61    return glob.get_inventory(62        ownerId = oId,63        aws_service = "ec2", 64        aws_region = "all", 65        function_name = "describe_vpcs", 66        key_get = "Vpcs"67    )68def get_subnet_inventory(oId):69    """70        Returns VPC subnets inventory71        :param oId: ownerId (AWS account)72        :type oId: string73        :return: VPC subnets inventory74        :rtype: json75    """76    return glob.get_inventory(77        ownerId = oId,78        aws_service = "ec2", 79        aws_region = "all", 80        function_name = "describe_subnets", 81        key_get = "Subnets"82    )83def get_ebs_inventory(oId):84    """85        Returns EBS detailed inventory86        :param oId: ownerId (AWS account)87        :type oId: string88        :return: EBS inventory89        :rtype: json90    """91    return glob.get_inventory(92        ownerId = oId,93        aws_service = "ec2", 94        aws_region = "all", 95        function_name = "describe_volumes", 96        key_get = "Volumes",97        pagination = True98    )99def get_eips_inventory(oId):100    """101        Returns Elastic IPs inventory102        :param oId: ownerId (AWS account)103        :type oId: string104        :return: Elastic IPs inventory105        :rtype: json106    """107    return glob.get_inventory(108        ownerId = oId,109        aws_service = "ec2", 110        aws_region = "all", 111        function_name = "describe_addresses", 112        key_get = "Addresses"113    )114def get_egpus_inventory(oId):115    """116        Returns Elastic GPUs inventory117        :param oId: ownerId (AWS account)118        :type oId: string119        :return: Elastic GPUs inventory120        :rtype: json121    """122    return glob.get_inventory(123        ownerId = oId,124        aws_service = "ec2", 125        aws_region = "all", 126        function_name = "describe_elastic_gpus", 127        key_get = "ElasticGpuSet"128    )129def get_sg_inventory(oId):130    """131        Returns Security Groups inventory132        :param oId: ownerId (AWS account)133        :type oId: string134        :return: Security Groups inventory135        :rtype: json136    """137    return glob.get_inventory(138        ownerId = oId,139        aws_service = "ec2", 140        aws_region = "all", 141        function_name = "describe_security_groups", 142        key_get = "SecurityGroups",143        pagination = True144    )145def get_igw_inventory(oId):146    """147        Returns Internet Gateways inventory148        :param oId: ownerId (AWS account)149        :type oId: string150        :return: Internet Gateways inventory151        :rtype: json152    """153    return glob.get_inventory(154        ownerId = oId,155        aws_service = "ec2", 156        aws_region = "all", 157        function_name = "describe_internet_gateways", 158        key_get = "InternetGateways"159    )160def get_ngw_inventory(oId):161    """162        Returns Nat Gateways inventory163        :param oId: ownerId (AWS account)164        :type oId: string165        :return: Nat Gateways inventory166        :rtype: json167    """168    return glob.get_inventory(169        ownerId = oId,170        aws_service = "ec2", 171        aws_region = "all", 172        function_name = "describe_nat_gateways", 173        key_get = "NatGateways"174    )175#  ------------------------------------------------------------------------176#177#    Elastic Beanstalk 178#179#  ------------------------------------------------------------------------180def get_elasticbeanstalk_environments_inventory(oId):181    """182        Returns Elastic Beanstalk detailed inventory183        :param oId: ownerId (AWS account)184        :type oId: string185        :return: Elastic Beanstalk inventory (environments)186        :rtype: json187        .. note:: http://boto3.readthedocs.io/en/latest/reference/services/elasticbeanstalk.html188    """189    return glob.get_inventory(190        ownerId = oId,191        aws_service = "elasticbeanstalk", 192        aws_region = "all", 193        function_name = "describe_environments", 194        key_get = "Environments"195    )196def get_elasticbeanstalk_applications_inventory(oId):197    """198        Returns Elastic Beanstalk detailed inventory199        :param oId: ownerId (AWS account)200        :type oId: string201        :return: Elastic Beanstalk inventory (applications)202        :rtype: json203    """204    return glob.get_inventory(205        ownerId = oId,206        aws_service = "elasticbeanstalk", 207        aws_region = "all", 208        function_name = "describe_applications", 209        key_get = "Applications"210    )211#  ------------------------------------------------------------------------212#213#    EC2 Container Service (ECS)214#215#  ------------------------------------------------------------------------216def get_ecs_inventory(oId):217    """218        Returns ECS detailed inventory219        :param oId: ownerId (AWS account)220        :type oId: string221        :return: ECS inventory222        :rtype: json223        .. note:: http://boto3.readthedocs.io/en/latest/reference/services/ecs.html224    """225    return glob.get_inventory(226        ownerId = oId,227        aws_service = "ecs", 228        aws_region = "all", 229        function_name = "describe_clusters", 230        key_get = "clusters"231    )232    #detail_function = "list_container_instances",233    #join_key = "clusterName", 234    #detail_join_key = "cluster", 235    #detail_get_key = ""236    237def get_ecs_services_inventory(oId):238    """239        Returns ECS tasks inventory  NOT WORKING YET240        :param oId: ownerId (AWS account)241        :type oId: string242        :return: ECS tasks inventory243        :rtype: json244    """245    return glob.get_inventory(246        ownerId = oId,247        aws_service = "ecs", 248        aws_region = "all", 249        function_name = "list_services", 250        key_get = "serviceArns",251        detail_function = "describe_services", 252        join_key = "", 253        detail_join_key = "services", 254        detail_get_key = "services",255        pagination = True,256        pagination_detail = True257    )258    259def get_ecs_tasks_inventory(oId):260    """261        Returns ECS tasks inventory262        :param oId: ownerId (AWS account)263        :type oId: string264        :return: ECS tasks inventory265        :rtype: json266    """267    return glob.get_inventory(268        ownerId = oId,269        aws_service = "ecs", 270        aws_region = "all", 271        function_name = "list_task_definitions", 272        key_get = "taskDefinitionArns",273        detail_function = "describe_task_definition", 274        join_key = "taskDefinitionArn", 275        detail_join_key = "taskDefinition", 276        detail_get_key = "taskDefinition",277        pagination = True,278        pagination_detail = True279    )280#  ------------------------------------------------------------------------281#282#    EKS283#284#  ------------------------------------------------------------------------285def get_eks_inventory(oId):286    """287        Returns eks inventory (if the region is avalaible)288        :param oId: ownerId (AWS account)289        :type oId: string290        :return: eks inventory291        :rtype: json292        .. note:: http://boto3.readthedocs.io/en/latest/reference/services/eks.html293    """294    inv = glob.get_inventory(295        ownerId = oId,296        aws_service = "eks", 297        aws_region = "all", 298        function_name = "list_clusters", 299        key_get = "clusters",300        detail_function = "describe_cluster", 301        join_key = "", 302        detail_join_key = "name", 303        detail_get_key = "cluster"304    )305    return inv306#  ------------------------------------------------------------------------307#308#    Autoscaling309#310#  ------------------------------------------------------------------------311def get_autoscaling_inventory(oId):312    """313        Returns eks inventory (if the region is avalaible)314        :param oId: ownerId (AWS account)315        :type oId: string316        :return: eks inventory317        :rtype: json318        .. note:: https://boto3.readthedocs.io/en/latest/reference/services/autoscaling.html319    """320    autoscaling_inventory = {}321    autoscaling_inventory['autoscaling-groups'] = glob.get_inventory(322        ownerId = oId,323        aws_service = "autoscaling", 324        aws_region = "all", 325        function_name = "describe_auto_scaling_groups", 326        key_get = "AutoScalingGroups",327        pagination = True328    )329    autoscaling_inventory['autoscaling-launch-configuration'] = glob.get_inventory(330        ownerId = oId,331        aws_service = "autoscaling", 332        aws_region = "all", 333        function_name = "describe_launch_configurations", 334        key_get = "LaunchConfigurations",335        pagination = True336    )337    autoscaling_inventory['autoscaling-plans'] = glob.get_inventory(338        ownerId = oId,339        aws_service = "autoscaling-plans", 340        aws_region = "all", 341        function_name = "describe_scaling_plans", 342        key_get = "ScalingPlans"343    )344    return autoscaling_inventory345#  ------------------------------------------------------------------------346#347#    Lambda348#349#  ------------------------------------------------------------------------350def get_lambda_inventory(oId):351    """352        Returns lambda inventory.353        :param oId: ownerId (AWS account)354        :type oId: string355        :return: lambda inventory356        :rtype: json357        .. note:: http://boto3.readthedocs.io/en/latest/reference/services/lambda.html358    """359    return glob.get_inventory(360        ownerId = oId,361        aws_service = "lambda", 362        aws_region = "all", 363        function_name = "list_functions", 364        key_get = "Functions",365        pagination = True366    )367#  ------------------------------------------------------------------------368#369#    Batch370#371#  ------------------------------------------------------------------------372def get_batch_inventory(oId):373    """374        Returns batch jobs inventory.375        :param oId: ownerId (AWS account)376        :type oId: string377        :return: batch inventory378        :rtype: json379        .. note:: http://boto3.readthedocs.io/en/latest/reference/services/batch.html380    """381    inventory = {}382    inventory['job-definitions'] = glob.get_inventory(383        ownerId = oId,384        aws_service = "batch", 385        aws_region = "all", 386        function_name = "describe_job_definitions", 387        key_get = "jobDefinitions"388    )389    inventory['job-queues'] = glob.get_inventory(390        ownerId = oId,391        aws_service = "batch", 392        aws_region = "all", 393        function_name = "describe_job_queues", 394        key_get = "jobQueues"395    )396    inventory['compute-environements'] = glob.get_inventory(397        ownerId = oId,398        aws_service = "batch", 399        aws_region = "all", 400        function_name = "describe_compute_environments", 401        key_get = "computeEnvironments"402    )403    return inventory404#  ------------------------------------------------------------------------405#406#    Lightsail407#408#  ------------------------------------------------------------------------409def get_lightsail_inventory(oId):410    """411        Returns lightsail inventory, with loadbalancers and IPs412        :param oId: ownerId (AWS account)413        :type oId: string414        :return: lightsail inventory415        :rtype: json416        .. note:: http://boto3.readthedocs.io/en/latest/reference/services/lightsail.html417    """418    lightsail_inventory = {}419    lightsail_inventory['lightsail-instances'] = glob.get_inventory(420        ownerId = oId,421        aws_service = "lightsail", 422        aws_region = "all", 423        function_name = "get_instances", 424        key_get = "instances",425        pagination = True426    )427    lightsail_inventory['lightsail-loadbalancers'] = glob.get_inventory(428        ownerId = oId,429        aws_service = "lightsail", 430        aws_region = "all", 431        function_name = "get_load_balancers", 432        key_get = "loadBalancers"433    )434    lightsail_inventory['lightsail-ip'] = glob.get_inventory(435        ownerId = oId,436        aws_service = "lightsail", 437        aws_region = "all", 438        function_name = "get_static_ips", 439        key_get = "staticIps"440    )441    lightsail_inventory['lightsail-disks'] = glob.get_inventory(442        ownerId = oId,443        aws_service = "lightsail", 444        aws_region = "all", 445        function_name = "get_disks", 446        key_get = "disks"447    )    448    return lightsail_inventory449#450# Hey, doc: we're in a module!451#452if (__name__ == '__main__'):...ec2.py
Source:ec2.py  
1# All the ec2 related functions2import filter_keys3import boto34from botocore.exceptions import ClientError5def get_service(function, dict_key, fields, extra_options={}):6    return filter_keys.filter_key('ec2', function, dict_key, fields, extra_options)7def get_all(remove_empty=False):8    # Return all resources9    resources = {}10    resources["connection_notifications"] = get_connection_notifications()11    resources["dedicated"] = get_dedicated()12    resources["dhcp_options"] = get_dhcp_options()13    resources["ec2_amis"] = get_ec2_amis()14    resources["egress_igs"] = get_egress_igs()15    resources["eips"] = get_eips()16    resources["elastic_gpus"] = get_elastic_gpus()17    resources["endpoint_connections"] = get_endpoint_connections()18    resources["endpoint_service_configs"] = get_endpoint_service_configs()19    resources["endpoints"] = get_endpoints()20    resources["flow_logs"] = get_flow_logs()21    # For some reason FPGA is not valid for the ec2 service but is documented22    # resources["fpga_images"] = get_fpga_images()23    resources["igs"] = get_igs()24    resources["instances"] = get_instances()25    resources["keys"] = get_keys()26    resources["launch_templates"] = get_launch_templates()27    resources["nacls"] = get_nacls()28    resources["nat_gateways"] = get_nat_gateways()29    resources["network_interfaces"] = get_network_interfaces()30    resources["peering_connections"] = get_peering_connections()31    resources["placement_groups"] = get_placement_groups()32    resources["prefix_lists"] = get_prefix_lists()33    resources["reserved"] = get_reserved()34    resources["reserved_listings"] = get_reserved_listings()35    resources["route_tables"] = get_route_tables()36    resources["scheduled"] = get_scheduled()37    resources["sgs"] = get_sgs()38    resources["snapshots"] = get_snapshots()39    resources["spot_fleets"] = get_spot_fleets()40    resources["spot_instances"] = get_spot_instances()41    resources["subnets"] = get_subnets()42    resources["tags"] = get_tags()43    resources["volumes"] = get_volumes()44    resources["vpcs"] = get_vpcs()45    resources["vpn_connections"] = get_vpn_connections()46    resources["vpn_gateways"] = get_vpn_gateways()47    resources["vpns"] = get_vpns()48    resources["valid_regions"] = filter_keys.get_regions("ec2")49    if remove_empty:50        resources = dict((key, value) for key, value in resources.items() if value)51    return resources52def get_eips():53    fields = ["InstanceId", "PublicIP", "PrivateIpAddress", "Domain"]54    return get_service("describe_addresses", "Addresses", fields)55def get_vpns():56    fields = ["CustomerGatewayId", "IpAddress", "Type", "BgpAsn"]57    return get_service("describe_customer_gateways", "CustomerGateways", fields)58def get_dhcp_options():59    fields = ["DhcpOptionsId"]60    return get_service("describe_dhcp_options", "DhcpOptions", fields)61def get_egress_igs():62    fields = ["EgressOnlyInternetGatewayId"]63    return get_service("describe_egress_only_internet_gateways", "EgressOnlyInternetGateways", fields)64def get_elastic_gpus():65    fields = ["ElasticGpuId", "ElasticGpuType", "InstanceId", "ElasticGpuState"]66    return get_service("describe_elastic_gpus", "ElasticGpuSet", fields)67def get_flow_logs():68    fields = ["FlowLogId", "LogGroupName", "ResourceId"]69    return get_service("describe_flow_logs", "FlowLogs", fields)70def get_fpga_images():71    fields = ["Name", "FpgaImageId", "FpgaImageGlobalId"]72    return get_service("describe_fpga_images", "FpgaImages", fields, {"Owners": ["self"]})73def get_dedicated():74    fields = ["HostId", "HostReservationId", "Instances"]75    return get_service("describe_hosts", "Hosts", fields)76def get_ec2_amis():77    fields = ["Name", "ImageId"]78    return get_service("describe_images", "Images", fields, {"Owners": ["self"]})79def get_instances():80    fields = ["InstanceId", "InstanceType", "ImageId", "LaunchTime", "Tags"]81    resources = []82    for region in filter_keys.get_regions("ec2"):83        res = boto3.client("ec2", region).describe_instances()84        res = res["Reservations"]85        instances = []86        for item in res:87            instances.extend(item["Instances"])88        instances[:] = [{key: value for key, value in i.items() if key in fields}89                        for i in instances]90        resources.extend(instances)91    return resources92def get_igs():93    fields = ["InternetGatewayId", "Attachments"]94    return get_service("describe_internet_gateways", "InternetGateways", fields)95def get_keys():96    fields = ["KeyFingerprint", "KeyName"]97    return get_service("describe_key_pairs", "KeyPairs", fields)98def get_launch_templates():99    fields = ["LaunchTemplateId", "LaunchTemplateName"]100    return get_service("describe_launch_templates", "LaunchTemplates", fields)101def get_nat_gateways():102    fields = ["NatGatewayId", "NatGatewayAddresses"]103    return get_service("describe_nat_gateways", "NatGateways", fields)104def get_nacls():105    fields = ["NetworkAclId", "VpcId"]106    return get_service("describe_network_acls", "NetworkAcls", fields)107def get_network_interfaces():108    fields = ["NetworkInterfaceId", "VpcId", "OwnerId", "RequesterId"]109    return get_service("describe_network_interfaces", "NetworkInterfaces", fields)110def get_placement_groups():111    fields = ["GroupName", "Strategy"]112    return get_service("describe_placement_groups", "PlacementGroups", fields)113def get_prefix_lists():114    fields = ["PrefixListId", "PrefixListName"]115    return get_service("describe_prefix_lists", "PrefixLists", fields)116def get_reserved():117    fields = ["ReservedInstancesId", "FixedPrice", "UsagePrice", "InstanceCount", "InstanceType"]118    return get_service("describe_reserved_instances", "ReservedInstances", fields)119def get_reserved_listings():120    fields = ["ReservedInstancesId", "ReservedInstancesListingId"]121    return get_service("describe_reserved_instances_listings", "ReservedInstancesListings", fields)122def get_route_tables():123    fields = ["RouteTableId", "VpcId", "Routes"]124    return get_service("describe_route_tables", "RouteTables", fields)125def get_scheduled():126    fields = ["ScheduledInstanceId", "InstanceCount", "HourlyPrice"]127    return get_service("describe_scheduled_instances", "ScheduledInstanceSet",128                       fields)129def get_sgs():130    fields = ["GroupName", "GroupId", "VpcId"]131    return get_service("describe_security_groups", "SecurityGroups", fields)132def get_snapshots():133    fields = ["VolumeId", "SnapshotId", "VolumeSize"]134    return get_service("describe_snapshots", "Snapshots", fields,135                       {"OwnerIds": ["self"]})136def get_spot_fleets():137    fields = ["ActivityStatus", "SpotFleetRequestId", "SpotFleetRequestState"]138    return get_service("describe_spot_fleet_requests", "SpotFleetRequestConfigs",139                       fields)140def get_spot_instances():141    fields = ["SpotInstanceRequestId", "SpotPrice", "State"]142    return get_service("describe_spot_instance_requests", "SpotInstanceRequests", fields)143def get_subnets():144    fields = ["SubnetId", "VpcId", "CidrBlock"]145    return get_service("describe_subnets", "Subnets", fields)146def get_tags():147    fields = ["Key", "ResourceId", "ResourceType", "Value"]148    return get_service("describe_tags", "Tags", fields)149def get_volumes():150    fields = ["VolumeId", "SnapshotId", "Size"]151    return get_service("describe_volumes", "Volumes", fields)152def get_connection_notifications():153    fields = ["ConnectionNotificationArn", "ConnectionNotificationId",154              "VpcEndpointId", "ServiceId"]155    return get_service("describe_vpc_endpoint_connection_notifications",156                       "ConnectionNotificationSet", fields)157def get_endpoint_connections():158    fields = ["VpcEndpointId", "VpcEndpointOwner", "ServiceId",159              "VpcEndpointState"]160    return get_service("describe_vpc_endpoint_connections",161                       "VpcEndpointConnections", fields)162def get_endpoint_service_configs():163    fields = ["ServiceId", "ServiceName", "ServiceState"]164    return get_service("describe_vpc_endpoint_service_configurations",165                       "ServiceConfigurations", fields)166def get_endpoints():167    fields = ["VpcEndpointId", "VpcId", "VpcEndpointType"]168    return get_service("describe_vpc_endpoints", "VpcEndpoints", fields)169def get_peering_connections():170    fields = ["VpcPeeringConnectionId", "Tags", "Status"]171    return get_service("describe_vpc_peering_connections", "VpcPeeringConnections", fields)172def get_vpcs():173    fields = ["VpcId", "CidrBlock"]174    return get_service("describe_vpcs", "Vpcs", fields)175def get_vpn_connections():176    fields = ["VpnConnectionId", "VpnGatewayId", "CustomerGatewayId"]177    return get_service("describe_vpn_connections", "VpnConnections", fields)178def get_vpn_gateways():179    fields = ["VpnGatewayId", "AmazonSideAsn", "Type"]...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
