Best Python code snippet using localstack_python
TGW-Static-Route-VPN-Failover.py
Source:TGW-Static-Route-VPN-Failover.py
...81 else:82 SecondaryVPN = 'UP'83 print('Secondary VPN ' + StandbyVPN + ' is UP')84 # check the existing routes which are for primary VPN store the CIDR85 response_route = client.search_transit_gateway_routes(TransitGatewayRouteTableId=TGWRouteTableId,86 Filters=[87 {88 'Name': 'attachment.transit-gateway-attachment-id',89 'Values': [90 ActiveVPN_Ass_Id91 ]92 }93 ], )94 for route in range(0, len(response_route['Routes'])):95 a = response_route['Routes'][route]['DestinationCidrBlock']96 CIDR.append(a)97 response_route = client.search_transit_gateway_routes(TransitGatewayRouteTableId=TGWRouteTableId,98 Filters=[99 {100 'Name': 'attachment.transit-gateway-attachment-id',101 'Values': [102 PassiveVPN_Ass_Id103 ]104 }105 ], )106 for route in range(0, len(response_route['Routes'])):107 a = response_route['Routes'][route]['DestinationCidrBlock']108 CIDR.append(a)109# print(CIDR)110 # If primary VPN is UP, ensure that the routes are pointing towards primary VPN111 if PrimaryVPN == 'UP': # change it to UP for real test112 for i in CIDR:113 response = client.search_transit_gateway_routes(114 TransitGatewayRouteTableId=TGWRouteTableId,115 Filters=[116 {117 'Name': 'route-search.exact-match',118 'Values': [i]119 },120 ], )121 if response['Routes'][0]['TransitGatewayAttachments'][0]['TransitGatewayAttachmentId'] == ActiveVPN_Ass_Id:122 break123 else:124 response = client.replace_transit_gateway_route(125 DestinationCidrBlock=i,126 TransitGatewayRouteTableId=TGWRouteTableId,127 TransitGatewayAttachmentId=ActiveVPN_Ass_Id,128 )129 print('Route is pointing towards ' + ActiveVPN)130 elif (PrimaryVPN == 'DOWN') and (SecondaryVPN == 'UP'):131 for i in CIDR:132 response = client.search_transit_gateway_routes(133 TransitGatewayRouteTableId=TGWRouteTableId,134 Filters=[135 {136 'Name': 'route-search.exact-match',137 'Values': [i]138 },139 ], )140 if response['Routes'][0]['TransitGatewayAttachments'][0]['TransitGatewayAttachmentId'] == PassiveVPN_Ass_Id:141 break142 else:143 response = client.replace_transit_gateway_route(144 DestinationCidrBlock=i,145 TransitGatewayRouteTableId=TGWRouteTableId,146 TransitGatewayAttachmentId=PassiveVPN_Ass_Id,147 )148 print('Route is pointing towards ' + StandbyVPN)149 # when both vpn are UP150 elif (PrimaryVPN == 'UP') and (SecondaryVPN == 'UP'):151 for i in CIDR:152 response = client.search_transit_gateway_routes(153 TransitGatewayRouteTableId=TGWRouteTableId,154 Filters=[155 {156 'Name': 'route-search.exact-match',157 'Values': [i]158 },159 ], )160 print(161 CIDR[i] + 'is poinging towards ' + response['Routes'][0]['TransitGatewayAttachments'][0]['ResourceId'])162 #when both vpn are DOWN163 elif (PrimaryVPN == 'DOWN') and (SecondaryVPN == 'DOWN'):...
handler.py
Source:handler.py
...26 # Search transit gateways27 for transit_gateway_route_table in get_transit_gateway_route_tables['TransitGatewayRouteTables']:28 logger.info("Transit Gateway Route Table: " + transit_gateway_route_table['TransitGatewayRouteTableId'])29 # Loop over transit gateway routes filtering on active state30 search_active_transit_gateway_routes = ec2.search_transit_gateway_routes(31 TransitGatewayRouteTableId = transit_gateway_route_table['TransitGatewayRouteTableId'],32 Filters=[33 {34 'Name': 'state',35 'Values': [36 'active',37 ]38 }39 ]40 )41 if len(search_active_transit_gateway_routes['Routes']) == 0:42 logger.info("No Transit Gateway Attachements in active state")43 else:44 for active_transit_gateway_route in search_active_transit_gateway_routes['Routes']:45 logger.info("Transit Gateway Attachment (Active) " + active_transit_gateway_route['TransitGatewayAttachments'][0]['TransitGatewayAttachmentId'] + " Route: " + active_transit_gateway_route['DestinationCidrBlock'])46 put_cloudwatch_metric_data = cloudwatch.put_metric_data(47 Namespace='HMPPS/EMS/SharedNetworking',48 MetricData=[49 {50 'MetricName': 'TransitGatewayAttachmentStatus',51 'Dimensions': [52 {53 'Name': 'TransitGatewayAttachmentId',54 'Value': active_transit_gateway_route['TransitGatewayAttachments'][0]['TransitGatewayAttachmentId']55 },56 {57 'Name': 'DestinationCidrBlock',58 'Value': active_transit_gateway_route['DestinationCidrBlock']59 },60 {61 'Name': 'ResourceType',62 'Value': active_transit_gateway_route['TransitGatewayAttachments'][0]['ResourceType']63 },64 ],65 'Unit': 'None',66 'Value': 167 },68 ]69 )70 # Loop over transit gateway routes filtering on blackholed state71 search_blackholed_transit_gateway_routes = ec2.search_transit_gateway_routes(72 TransitGatewayRouteTableId = transit_gateway_route_table['TransitGatewayRouteTableId'],73 Filters=[74 {75 'Name': 'state',76 'Values': [77 'blackhole',78 ]79 }80 ]81 )82 if len(search_blackholed_transit_gateway_routes['Routes']) == 0:83 logger.info("No Transit Gateway Attachements in blackholed state")84 else:85 for blackholed_transit_gateway_route in search_blackholed_transit_gateway_routes['Routes']:...
main.py
Source:main.py
...9 cidr_blocks = event['cidr_blocks']10 tgw_route_table_id = event['tgw_route_table_id']11 for cidr_block in cidr_blocks:12 # find all routes that belong in a specific CIDR block or its subnets13 routes = ec2.search_transit_gateway_routes(14 TransitGatewayRouteTableId=tgw_route_table_id,15 Filters=[16 {17 'Name': 'route-search.subnet-of-match',18 'Values': [19 cidr_block,20 ]21 },22 {23 'Name': 'attachment.resource-type',24 'Values': [25 'vpc',26 ]27 }28 ]29 )30 # array with the CIDR blocks that were identified31 cidr_blocks_for_routes = (32 [d['DestinationCidrBlock'] for d in routes['Routes']]33 )34 # if a static route does not already exis35 if len(cidr_blocks_for_routes) > 0:36 if cidr_block not in cidr_blocks_for_routes:37 tgw_attachements = (38 [d['TransitGatewayAttachments'] for d in routes['Routes']]39 )40 tgw_vpc_attachements = (41 [d for d in tgw_attachements if d[0]['ResourceType'] in ['vpc']] # noqa: E50142 )43 # identify attachment id for VPC44 tgw_attach_id = tgw_vpc_attachements[0][0]['TransitGatewayAttachmentId'] # noqa: E50145 logger.info('Creating route for: %s' % str(cidr_block))46 ec2.create_transit_gateway_route(47 DestinationCidrBlock=cidr_block,48 TransitGatewayRouteTableId=tgw_route_table_id,49 TransitGatewayAttachmentId=tgw_attach_id50 )51 ####################52 # start - cleanup53 # find all static routes to vpcs54 all_static_routes = ec2.search_transit_gateway_routes(55 TransitGatewayRouteTableId=tgw_route_table_id,56 Filters=[57 {58 'Name': 'type',59 'Values': [60 'static',61 ]62 },63 {64 'Name': 'attachment.resource-type',65 'Values': [66 'vpc',67 ]68 }...
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!!