How to use create_transit_gateway method in localstack

Best Python code snippet using localstack_python

transit-gateway.py

Source:transit-gateway.py Github

copy

Full Screen

1import boto32import time3import sys4#create transit gateway5def create_transit_gateway():6 client = boto3.client('ec2', region_name='us-east-1')7 response = client.create_transit_gateway(8 Description='string',9 Options={10 'AmazonSideAsn': 64512,11 'AutoAcceptSharedAttachments': 'disable',12 'DefaultRouteTableAssociation': 'disable',13 'DefaultRouteTablePropagation': 'disable',14 'VpnEcmpSupport': 'enable',15 'DnsSupport': 'enable',16 'MulticastSupport': 'enable'17 },18 TagSpecifications=[19 {20 'ResourceType':'transit-gateway',21 'Tags': [22 {23 'Key': 'Name',24 'Value': 'terraform-boto3-tgw'25 },26 ]27 },28 ],29 DryRun=False30 )31 return response32#create transit gateway VPC attachment33def create_transit_gateway_vpc_attachment(transit_gateway_id, vpc_id, subnet_id):34 client = boto3.client('ec2', region_name='us-east-1')35 response = client.create_transit_gateway_vpc_attachment(36 TransitGatewayId=transit_gateway_id,37 VpcId=vpc_id,38 SubnetIds=[39 subnet_id,40 ],41 Options={42 'DnsSupport': 'enable',43 'Ipv6Support': 'disable'44 },45 TagSpecifications=[46 {47 'ResourceType': 'transit-gateway-attachment',48 'Tags': [49 {50 'Key': 'Name',51 'Value': 'terraform-boto3-tgw-attachment'52 },53 ]54 },55 ],56 DryRun=False57 )58 return response59#Create transit gateway route-table60def create_transit_gateway_rt(transit_gateway_id):61 client = boto3.client('ec2', region_name='us-east-1')62 response = client.create_transit_gateway_route_table(63 TransitGatewayId=transit_gateway_id,64 TagSpecifications=[65 {66 'ResourceType': 'transit-gateway-route-table',67 'Tags': [68 {69 'Key': 'Name',70 'Value': 'terraform-boto3-tgw-rtable'71 },72 ]73 },74 ],75 DryRun=False76 )77 return response78#Create transit gateway route79def create_transit_gateway_route(transit_gateway_rt_id, destination_ip_block, transit_gateway_attachment_id):80 client = boto3.client('ec2', region_name='us-east-1')81 response = client.create_transit_gateway_route(82 DestinationCidrBlock=destination_ip_block,83 TransitGatewayRouteTableId=transit_gateway_rt_id,84 TransitGatewayAttachmentId=transit_gateway_attachment_id,85 Blackhole=False,86 DryRun=False87 )88 return response89#Associate transit gateway route table90def associate_transit_gateway_route_table(tgw_rt_id, tgw_attach_id):91 client = boto3.client('ec2', region_name='us-east-1')92 response = client.associate_transit_gateway_route_table(93 TransitGatewayRouteTableId=tgw_rt_id,94 TransitGatewayAttachmentId=tgw_attach_id,95 DryRun=False96 )97 return response98#Enable transit gateway route table propogation99def enable_transit_gateway_route_table_propagation(tgw_rt_id, tgw_att_id):100 client = boto3.client('ec2', region_name='us-east-1')101 response = client.enable_transit_gateway_route_table_propagation(102 TransitGatewayRouteTableId=tgw_rt_id,103 TransitGatewayAttachmentId= tgw_att_id,104 DryRun=False105 )106 return response107#Create transit gateway multi-cast domain108def create_transit_gateway_multicast_domain(transit_gateway_id):109 client = boto3.client('ec2', region_name='us-east-1')110 response = client.create_transit_gateway_multicast_domain(111 TransitGatewayId=transit_gateway_id,112 TagSpecifications=[113 {114 'ResourceType': 'transit-gateway-multicast-domain',115 'Tags': [116 {117 'Key': 'Name',118 'Value': 'aquis-terraform-boto3-tgw-multicast-domain'119 },120 ]121 },122 ],123 DryRun=False124 )125 return response126#Associate transit gateway multi cast domain127def associate_transit_gateway_multicast_domain(tgw_multicast_domain_id, tgw_attachment_id, subnetID):128 client = boto3.client('ec2', region_name='us-east-1')129 response = client.associate_transit_gateway_multicast_domain(130 TransitGatewayMulticastDomainId=tgw_multicast_domain_id,131 TransitGatewayAttachmentId=tgw_attachment_id,132 SubnetIds=[133 subnetID,134 ],135 DryRun=False136 )137 return response138#Delete transit gateway multi-cast domain139def delete_transit_gateway_multicast_domain(tgwy_multicast_domain_id):140 client = boto3.client('ec2', region_name='us-east-1')141 response = client.delete_transit_gateway_multicast_domain(142 TransitGatewayMulticastDomainId=tgwy_multicast_domain_id,143 DryRun=False144 )145 return response146#Delete transit gateway vpc attachment147def delete_transit_gateway_vpc_attachment(transit_gateway_attachment_id):148 client = boto3.client('ec2', region_name='us-east-1')149 response = client.delete_transit_gateway_vpc_attachment(150 TransitGatewayAttachmentId=transit_gateway_attachment_id,151 DryRun=False152 )153 return response154#Delete transit gateway155def delete_transit_gateway(transit_gateway_id):156 client = boto3.client('ec2', region_name='us-east-1')157 response = client.delete_transit_gateway(158 TransitGatewayId=transit_gateway_id,159 DryRun=False160 )161 return response162# dissacotiate transit gateway multicast domain163def disassociate_transit_gateway_multicast_domain(tgw_multicast_domain_id, tgw_attachment_id, subnet_id):164 client = boto3.client('ec2', region_name='us-east-1')165 response = client.disassociate_transit_gateway_multicast_domain(166 TransitGatewayMulticastDomainId=tgw_multicast_domain_id,167 TransitGatewayAttachmentId=tgw_attachment_id,168 SubnetIds=[169 subnet_id,170 ],171 DryRun=False172 )173 return response174#register transit gateway multi-cast members175def register_transit_gateway_multicast_group_members(tgw_multicast_domain_id, group_address, network_interface3, network_interface4):176 client = boto3.client('ec2', region_name='us-east-1')177 response = client.register_transit_gateway_multicast_group_members(178 TransitGatewayMulticastDomainId = tgw_multicast_domain_id,179 GroupIpAddress = group_address,180 NetworkInterfaceIds = [181 network_interface3, network_interface4,182 ],183 DryRun = False184 )185 return response186#register transit gateway multi cast group sources187def register_transit_gateway_multicast_group_sources(tgw_multicast_domain_id, nic1id, group_address):188 client = boto3.client('ec2', region_name='us-east-1')189 response = client.register_transit_gateway_multicast_group_sources(190 TransitGatewayMulticastDomainId=tgw_multicast_domain_id,191 GroupIpAddress=group_address,192 NetworkInterfaceIds=[193 nic1id,194 ],195 DryRun=False196 )197 return response198#Delete transit gateway route table199def delete_transit_gateway_route_table(rtb_id):200 client = boto3.client('ec2', region_name='us-east-1')201 response = client.delete_transit_gateway_route_table(202 TransitGatewayRouteTableId=rtb_id,203 DryRun=False204 )205 return response206if __name__ == '__main__':207 action = sys.argv[1]208 if action == "apply":209 #Get values from python input when calling python and assign to variables using sys.argv210 vpc_id = sys.argv[2]211 subnet_id = sys.argv[3]212 processor1nic = sys.argv[4]213 processor2nic = sys.argv[5]214 processor3nic = sys.argv[6]215 #Create transit gateway216 tgw_response = create_transit_gateway()217 #Collect transit gateway id from response218 tgw_id = tgw_response['TransitGateway']['TransitGatewayId']219 time.sleep(120)220 #Create transit gateway attachment221 tgw_attachment_response = create_transit_gateway_vpc_attachment(tgw_id, vpc_id, subnet_id)222 #get tgw attachment id response223 tgw_attachment_id = tgw_attachment_response['TransitGatewayVpcAttachment']['TransitGatewayAttachmentId']224 time.sleep(90)225 #Create transit gateway route table and get response226 tgw_rt_response = create_transit_gateway_rt(tgw_id)227 #Get transit gateway route table id from the response228 tgw_rt_id = tgw_rt_response ['TransitGatewayRouteTable']['TransitGatewayRouteTableId']229 time.sleep(90)230 #associate transit gateway route table...

Full Screen

Full Screen

test_transit_gateway.py

Source:test_transit_gateway.py Github

copy

Full Screen

...18 def test__success(self):19 """success"""20 tgw = TGWPeering()21 # mock setup22 sample_tgw1 = tgw.ec2_client.create_transit_gateway()23 peers = tgw.get_tgw_peers(24 tgw_id=sample_tgw1["TransitGateway"]["TransitGatewayId"],25 states=[AttachmentState.AVAILABLE],26 )27 assert len(peers) == 028 # clean-up29 tgw.ec2_client.delete_transit_gateway(30 TransitGatewayId=sample_tgw1["TransitGateway"]["TransitGatewayId"]31 )32 def test__fail__client_error(self):33 """fail with client error"""34 tgw = TGWPeering()35 stubber = Stubber(tgw.ec2_client)36 stubber.add_client_error(37 "describe_transit_gateway_peering_attachments",38 service_error_code="InternalException",39 service_message="this is test error",40 )41 stubber.activate()42 peers = tgw.get_tgw_peers(43 tgw_id="",44 states=[AttachmentState.AVAILABLE],45 )46 assert len(peers) == 047 stubber.deactivate()48@pytest.mark.BDD49@mock_ec250class TestCreatePeeringAttachment:51 """BDD test class for TGW create peering attachment methods"""52 owner = "123456789012"53 def test__success(self):54 """success"""55 tgw = TGWPeering()56 # mock setup57 sample_tgw1 = tgw.ec2_client.create_transit_gateway()58 sample_tgw2 = tgw.ec2_client.create_transit_gateway()59 sample_peer = TGWPeer(60 transit_gateway=sample_tgw2["TransitGateway"]["TransitGatewayId"],61 aws_region=environ.get("AWS_DEFAULT_REGION"),62 )63 attachment = tgw.create_tgw_peering_attachment(64 tgw_id=sample_tgw1["TransitGateway"]["TransitGatewayId"],65 peer=sample_peer,66 peer_account_id=self.owner,67 )68 sample_peer.attachment_id = attachment["TransitGatewayAttachmentId"]69 # test attachment is tagged with solution identifier70 assert attachment["Tags"][0]["Key"] == "SolutionId"71 assert attachment["Tags"][0]["Value"] == environ.get("SOLUTION_ID")72 peers = tgw.get_tgw_peers(...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful