Best Python code snippet using localstack_python
SG2PL-Onboard.py
Source:SG2PL-Onboard.py
...212 # If the length of the list of CIDRs to add is less than 100, we create the Prefix List with the CIDRs populated213 # on create.214 if len(cidrstoadd) < 100:215 # Create the Prefix List populated with the CIDRs216 response = ec2remote.create_managed_prefix_list(217 PrefixListName=sg,218 Entries=cidrstoadd,219 MaxEntries=pllen,220 AddressFamily='IPv4'221 )222 message = "create_managed_prefix_list call result: "+str(response)223 log_handler(message, 1, False, False)224 # Return the Prefix List ID225 return response['PrefixList']['PrefixListId']226 # If the length of the list of CIDRs is greater than 100, we create a Prefix List, setting the size227 # appropriately but with no entries and call the Batch Sync Lambda Function to populate it. This is228 # because the Create_managed_prefix_list and modify_managed_prefix_list only allow up to 100 entries229 # to add per call. Batch Sync has an iterator to account for this API limit.230 else:231 # Create the empty Prefix List232 response = ec2remote.create_managed_prefix_list(233 PrefixListName=sg,234 MaxEntries=pllen,235 AddressFamily='IPv4'236 )237 message = "create_managed_prefix_list call result: " + str(response)238 log_handler(message, 1, False, False)239 # Get the ID for the Prefix List that was created240 pl = response['PrefixList']['PrefixListId']241 # Create the payload structure required to call the Batch Sync Lambda Function242 payload = {"sg": sg, "pl": pl, "region": region}243 message = "Invoking lambda " + str(batchsyncfn) + " with payload of " + str(payload)244 log_handler(message, 1, False, False)245 # Create the Lambda Client object246 lambdaclient = boto3.client('lambda')...
test_prefix_lists.py
Source:test_prefix_lists.py
...4from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID5@mock_ec26def test_create():7 ec2 = boto3.client("ec2", region_name="us-west-1")8 prefix_list = ec2.create_managed_prefix_list(9 PrefixListName="examplelist", MaxEntries=2, AddressFamily="?"10 )["PrefixList"]11 prefix_list.should.have.key("PrefixListId").match("pl-[a-z0-9]+")12 prefix_list.should.have.key("AddressFamily").equals("?")13 prefix_list.should.have.key("State").equals("create-complete")14 prefix_list.should.have.key("PrefixListArn").equals(15 "arn:aws:ec2:us-west-1:{}:prefix-list/{}".format(16 ACCOUNT_ID, prefix_list["PrefixListId"]17 )18 )19 prefix_list.should.have.key("PrefixListName").equals("examplelist")20 prefix_list.should.have.key("MaxEntries").equals(2)21 prefix_list.should.have.key("Version").equals(1)22 prefix_list.should.have.key("Tags").equals([])23 prefix_list.should.have.key("OwnerId").equals(ACCOUNT_ID)24@mock_ec225def test_create_with_tags():26 ec2 = boto3.client("ec2", region_name="us-west-1")27 prefix_list = ec2.create_managed_prefix_list(28 PrefixListName="examplelist",29 MaxEntries=2,30 AddressFamily="?",31 TagSpecifications=[32 {"ResourceType": "", "Tags": [{"Key": "key1", "Value": "val1"}]}33 ],34 )["PrefixList"]35 prefix_list.should.have.key("PrefixListId").match("pl-[a-z0-9]+")36 prefix_list.should.have.key("State").equals("create-complete")37 prefix_list.should.have.key("Version").equals(1)38 prefix_list.should.have.key("Tags").equals([{"Key": "key1", "Value": "val1"}])39@mock_ec240def test_describe_managed_prefix_lists():41 ec2 = boto3.client("ec2", region_name="us-west-1")42 prefix_list = ec2.create_managed_prefix_list(43 PrefixListName="examplelist", MaxEntries=2, AddressFamily="?"44 )45 pl_id = prefix_list["PrefixList"]["PrefixListId"]46 all_lists = ec2.describe_managed_prefix_lists()["PrefixLists"]47 [pl["PrefixListId"] for pl in all_lists].should.contain(pl_id)48 set([pl["OwnerId"] for pl in all_lists]).should.equal({"aws", ACCOUNT_ID})49@mock_ec250def test_describe_managed_prefix_lists_with_prefix():51 ec2 = boto3.client("ec2", region_name="us-west-1")52 default_lists = ec2.describe_managed_prefix_lists()["PrefixLists"]53 if not settings.TEST_SERVER_MODE:54 # ServerMode is not guaranteed to only have AWS prefix lists55 set([pl["OwnerId"] for pl in default_lists]).should.equal({"aws"})56 random_list_id = default_lists[0]["PrefixListId"]57 lists_by_id = ec2.describe_managed_prefix_lists(PrefixListIds=[random_list_id])[58 "PrefixLists"59 ]60 lists_by_id.should.have.length_of(1)61 if not settings.TEST_SERVER_MODE:62 lists_by_id[0]["OwnerId"].should.equal("aws")63@mock_ec264def test_describe_managed_prefix_lists_with_tags():65 ec2 = boto3.client("ec2", region_name="us-west-1")66 untagged_prefix_list = ec2.create_managed_prefix_list(67 PrefixListName="examplelist", MaxEntries=2, AddressFamily="?"68 )69 untagged_pl_id = untagged_prefix_list["PrefixList"]["PrefixListId"]70 tagged_prefix_list = ec2.create_managed_prefix_list(71 PrefixListName="examplelist",72 MaxEntries=2,73 AddressFamily="?",74 TagSpecifications=[75 {"ResourceType": "prefix-list", "Tags": [{"Key": "key", "Value": "value"}]}76 ],77 )78 tagged_pl_id = tagged_prefix_list["PrefixList"]["PrefixListId"]79 tagged_lists = ec2.describe_managed_prefix_lists(80 Filters=[{"Name": "tag:key", "Values": ["value"]}]81 )["PrefixLists"]82 [pl["PrefixListId"] for pl in tagged_lists].should.contain(tagged_pl_id)83 [pl["PrefixListId"] for pl in tagged_lists].should_not.contain(untagged_pl_id)84@mock_ec285def test_get_managed_prefix_list_entries():86 ec2 = boto3.client("ec2", region_name="us-west-1")87 resp = ec2.create_managed_prefix_list(88 PrefixListName="examplelist",89 MaxEntries=2,90 AddressFamily="?",91 Entries=[92 {"Cidr": "10.0.0.1", "Description": "entry1"},93 {"Cidr": "10.0.0.2", "Description": "entry2"},94 ],95 )96 ec2.create_managed_prefix_list(97 PrefixListName="examplelist2",98 MaxEntries=2,99 AddressFamily="?",100 Entries=[101 {"Cidr": "10.0.0.3", "Description": "entry3"},102 {"Cidr": "10.0.0.4", "Description": "entry4"},103 ],104 )105 prefix_list_id = resp["PrefixList"]["PrefixListId"]106 entries = ec2.get_managed_prefix_list_entries(PrefixListId=prefix_list_id)[107 "Entries"108 ]109 entries.should.have.length_of(2)110 entries.should.contain({"Cidr": "10.0.0.1", "Description": "entry1"})111 entries.should.contain({"Cidr": "10.0.0.2", "Description": "entry2"})112@mock_ec2113def test_get_managed_prefix_list_entries_0_entries():114 ec2 = boto3.client("ec2", region_name="us-west-1")115 resp = ec2.create_managed_prefix_list(116 PrefixListName="examplelist", MaxEntries=2, AddressFamily="?"117 )118 prefix_list_id = resp["PrefixList"]["PrefixListId"]119 entries = ec2.get_managed_prefix_list_entries(PrefixListId=prefix_list_id)[120 "Entries"121 ]122 entries.should.equal([])123@mock_ec2124def test_delete_managed_prefix_list():125 ec2 = boto3.client("ec2", region_name="us-west-1")126 id1 = ec2.create_managed_prefix_list(127 PrefixListName="examplelist1", MaxEntries=2, AddressFamily="?"128 )["PrefixList"]["PrefixListId"]129 id2 = ec2.create_managed_prefix_list(130 PrefixListName="examplelist2", MaxEntries=2, AddressFamily="?"131 )["PrefixList"]["PrefixListId"]132 lists_by_id = ec2.describe_managed_prefix_lists(PrefixListIds=[id1, id2])[133 "PrefixLists"134 ]135 lists_by_id.should.have.length_of(2)136 ec2.delete_managed_prefix_list(PrefixListId=id1)137 lists_by_id = ec2.describe_managed_prefix_lists(PrefixListIds=[id1, id2])[138 "PrefixLists"139 ]140 lists_by_id.should.have.length_of(2)141 set([pl["State"] for pl in lists_by_id]).should.equal(142 {"create-complete", "delete-complete"}143 )144@mock_ec2145def test_describe_prefix_lists():146 ec2 = boto3.client("ec2", region_name="us-west-1")147 default_lists = ec2.describe_prefix_lists()["PrefixLists"]148 default_lists.should.have.length_of(2)149 ec2.create_managed_prefix_list(150 PrefixListName="examplelist", MaxEntries=2, AddressFamily="?"151 )152 all_lists = ec2.describe_prefix_lists()["PrefixLists"]153 all_lists.should.have.length_of(2)154 for pl in all_lists:155 pl["PrefixListName"].should.contain("com.amazonaws")156@mock_ec2157def test_modify_manage_prefix_list():158 ec2 = boto3.client("ec2", region_name="us-west-1")159 resp = ec2.create_managed_prefix_list(160 PrefixListName="examplelist",161 MaxEntries=2,162 AddressFamily="?",163 Entries=[164 {"Cidr": "10.0.0.1", "Description": "entry1"},165 {"Cidr": "10.0.0.2", "Description": "entry2"},166 ],167 )168 prefix_list_id = resp["PrefixList"]["PrefixListId"]169 prefix_list = ec2.modify_managed_prefix_list(170 PrefixListId=prefix_list_id,171 AddEntries=[{"Cidr": "10.0.0.3", "Description": "entry3"}],172 RemoveEntries=[{"Cidr": "10.0.0.2"}],173 )["PrefixList"]174 prefix_list["PrefixListId"].should.equal(prefix_list_id)175 prefix_list["State"].should.equal("modify-complete")176 prefix_list["Version"].should.equal(2)177 described = ec2.describe_managed_prefix_lists(PrefixListIds=[prefix_list_id])[178 "PrefixLists"179 ][0]180 described.should.equal(prefix_list)181 entries = ec2.get_managed_prefix_list_entries(PrefixListId=prefix_list_id)[182 "Entries"183 ]184 entries.should.have.length_of(2)185 entries.should.contain({"Cidr": "10.0.0.1", "Description": "entry1"})186 entries.should.contain({"Cidr": "10.0.0.3", "Description": "entry3"})187 entries.shouldnt.contain({"Cidr": "10.0.0.2", "Description": "entry2"})188@mock_ec2189def test_modify_manage_prefix_list_add_to_empty_list():190 ec2 = boto3.client("ec2", region_name="us-west-1")191 resp = ec2.create_managed_prefix_list(192 PrefixListName="examplelist", MaxEntries=2, AddressFamily="?"193 )194 prefix_list_id = resp["PrefixList"]["PrefixListId"]195 prefix_list = ec2.modify_managed_prefix_list(196 PrefixListId=prefix_list_id,197 AddEntries=[{"Cidr": "10.0.0.3", "Description": "entry3"}],198 RemoveEntries=[{"Cidr": "10.0.0.2"}],199 )["PrefixList"]200 prefix_list["PrefixListId"].should.equal(prefix_list_id)201 prefix_list["State"].should.equal("modify-complete")202 prefix_list["Version"].should.equal(2)203 described = ec2.describe_managed_prefix_lists(PrefixListIds=[prefix_list_id])[204 "PrefixLists"205 ][0]206 described.should.equal(prefix_list)207 entries = ec2.get_managed_prefix_list_entries(PrefixListId=prefix_list_id)[208 "Entries"209 ]210 entries.should.have.length_of(1)211 entries.should.contain({"Cidr": "10.0.0.3", "Description": "entry3"})212 entries.shouldnt.contain({"Cidr": "10.0.0.2", "Description": "entry2"})213@mock_ec2214def test_modify_manage_prefix_list_name_only():215 ec2 = boto3.client("ec2", region_name="us-west-1")216 resp = ec2.create_managed_prefix_list(217 PrefixListName="examplelist", MaxEntries=2, AddressFamily="?"218 )219 prefix_list_id = resp["PrefixList"]["PrefixListId"]220 prefix_list = ec2.modify_managed_prefix_list(221 PrefixListId=prefix_list_id, PrefixListName="new name"222 )["PrefixList"]223 prefix_list["PrefixListId"].should.equal(prefix_list_id)224 prefix_list["PrefixListName"].should.equal("new name")225 prefix_list["State"].should.equal("modify-complete")226 prefix_list["Version"].should.equal(1)227 described = ec2.describe_managed_prefix_lists(PrefixListIds=[prefix_list_id])[228 "PrefixLists"229 ][0]230 described.should.equal(prefix_list)231@mock_ec2232def test_modify_manage_prefix_list_specifying_version():233 ec2 = boto3.client("ec2", region_name="us-west-1")234 resp = ec2.create_managed_prefix_list(235 PrefixListName="examplelist",236 MaxEntries=2,237 AddressFamily="?",238 Entries=[239 {"Cidr": "10.0.0.1", "Description": "entry1"},240 {"Cidr": "10.0.0.2", "Description": "entry2"},241 ],242 )243 prefix_list_id = resp["PrefixList"]["PrefixListId"]244 prefix_list = ec2.modify_managed_prefix_list(245 PrefixListId=prefix_list_id,246 AddEntries=[{"Cidr": "10.0.0.3", "Description": "entry3"}],247 RemoveEntries=[{"Cidr": "10.0.0.2"}],248 )["PrefixList"]...
update-ipranges.py
Source:update-ipranges.py
...15 if response['PrefixLists'] == []:16 print("No existing Prefix List. Creating new one")17 for ip in ec2_ips:18 entry_list.append({'Cidr': ip})19 ec2.create_managed_prefix_list(20 DryRun=False,21 PrefixListName='EC2-SYD',22 MaxEntries=max_prefixes,23 Entries=entry_list,24 AddressFamily='IPv4'25 )26 return {27 'statusCode': 200,28 'body': "Created new Prefix List"29 }30 else:31 print("Prefix List exists. Updating prefixes")32 to_remove = []33 prefix_list_id = response['PrefixLists'][0]['PrefixListId']...
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!!