Best Python code snippet using localstack_python
eni.py
Source:eni.py
...99 % (self.type_name))100 res = self.client.detach_network_interface(**params)101 self.logger.debug('Response: %s' % res)102 return res103 def modify_network_interface_attribute(self, params):104 '''105 Modify an AWS EC2 NetworkInterface attribute.106 '''107 self.logger.debug('Modifying %s with: %s'108 % (self.type_name, params))109 res = self.client.modify_network_interface_attribute(**params)110 self.logger.debug('Response: %s' % res)111 return res112@decorators.aws_resource(resource_type=RESOURCE_TYPE)113def prepare(ctx, resource_config, **_):114 """Prepares an AWS EC2 NetworkInterface"""115 # Save the parameters116 ctx.instance.runtime_properties['resource_config'] = resource_config117@decorators.aws_resource(EC2NetworkInterface, RESOURCE_TYPE)118@decorators.wait_for_status(status_good=['available'])119@decorators.tag_resources120def create(ctx, iface, resource_config, **_):121 """Creates an AWS EC2 NetworkInterface"""122 # Create a copy of the resource config for clean manipulation.123 params = \124 dict() if not resource_config else resource_config.copy()125 subnet_id = params.get(SUBNET_ID)126 if not subnet_id:127 targ = \128 utils.find_rel_by_node_type(ctx.instance, SUBNET_TYPE) or \129 utils.find_rel_by_node_type(ctx.instance, SUBNET_TYPE_DEPRECATED)130 # Attempt to use the VPC ID from parameters.131 # Fallback to connected VPC.132 params[SUBNET_ID] = \133 subnet_id or \134 targ.target.instance.runtime_properties.get(EXTERNAL_RESOURCE_ID)135 groups = params.get(SEC_GROUPS, [])136 for targ in utils.find_rels_by_node_type(ctx.instance, SEC_GROUP_TYPE):137 group_id = \138 targ.target.instance.runtime_properties.get(139 EXTERNAL_RESOURCE_ID)140 if group_id and group_id not in groups:141 groups.append(group_id)142 params[SEC_GROUPS] = groups143 # Actually create the resource144 create_response = iface.create(params)['NetworkInterface']145 cleaned_create_response = utils.JsonCleanuper(create_response).to_dict()146 ctx.instance.runtime_properties['create_response'] = \147 utils.JsonCleanuper(cleaned_create_response).to_dict()148 eni_id = cleaned_create_response.get(NETWORKINTERFACE_ID, '')149 iface.update_resource_id(eni_id)150 utils.update_resource_id(ctx.instance, eni_id)151 ctx.instance.runtime_properties['device_index'] = \152 cleaned_create_response.get(153 'Attachment', {}).get(154 'DeviceIndex',155 ctx.instance.runtime_properties.get('device_index'))156 modify_network_interface_attribute_args = \157 _.get('modify_network_interface_attribute_args')158 if modify_network_interface_attribute_args:159 modify_network_interface_attribute_args[NETWORKINTERFACE_ID] = \160 eni_id161 iface.modify_network_interface_attribute(162 modify_network_interface_attribute_args)163@decorators.aws_resource(EC2NetworkInterface, RESOURCE_TYPE,164 ignore_properties=True)165@decorators.untag_resources166def delete(ctx, iface, resource_config, **_):167 """Deletes an AWS EC2 NetworkInterface"""168 # Create a copy of the resource config for clean manipulation.169 params = \170 dict() if not resource_config else resource_config.copy()171 eni_id = params.get(NETWORKINTERFACE_ID)172 if not eni_id:173 params[NETWORKINTERFACE_ID] = \174 iface.resource_id or \175 ctx.instance.runtime_properties.get(EXTERNAL_RESOURCE_ID)176 iface.delete(params)177 for prop in ['resource_config',178 'aws_resource_id',179 'device_index',180 'create_response']:181 try:182 del ctx.instance.runtime_properties[prop]183 except KeyError:184 pass185@decorators.aws_resource(EC2NetworkInterface, RESOURCE_TYPE)186def attach(ctx, iface, resource_config, **_):187 '''Attaches an AWS EC2 NetworkInterface to a Subnet'''188 params = dict() if not resource_config else resource_config.copy()189 eni_id = params.get(NETWORKINTERFACE_ID)190 if not eni_id:191 eni_id = iface.resource_id192 device_index = ctx.instance.runtime_properties.get('device_index', 1)193 ctx.instance.runtime_properties['device_index'] = device_index194 params.update({NETWORKINTERFACE_ID: eni_id})195 params.update({'DeviceIndex': device_index})196 instance_id = params.get(INSTANCE_ID)197 if not instance_id:198 targ = \199 utils.find_rel_by_node_type(ctx.instance, INSTANCE_TYPE_DEPRECATED)200 # Attempt to use the SUBNET ID from parameters.201 # Fallback to connected SUBNET.202 if not targ:203 return204 params[INSTANCE_ID] = \205 instance_id or \206 targ.target.instance.runtime_properties.get(EXTERNAL_RESOURCE_ID)207 # Actually attach the resources208 eni_attachment_id = iface.attach(params)209 ctx.instance.runtime_properties['attachment_id'] = \210 eni_attachment_id[ATTACHMENT_ID]211@decorators.aws_resource(EC2NetworkInterface, RESOURCE_TYPE,212 ignore_properties=True)213def detach(ctx, iface, resource_config, **_):214 '''Detach an AWS EC2 NetworkInterface from a Subnet'''215 params = dict() if not resource_config else resource_config.copy()216 attachment_id = ctx.instance.runtime_properties.get('attachment_id', None)217 if not attachment_id:218 return219 params.update({ATTACHMENT_ID: attachment_id})220 iface.detach(params)221@decorators.aws_resource(EC2NetworkInterface, RESOURCE_TYPE)222def modify_network_interface_attribute(ctx, iface, resource_config, **_):223 params = \224 dict() if not resource_config else resource_config.copy()225 eni_id = \226 ctx.instance.runtime_properties.get(227 NETWORKINTERFACE_ID, iface.resource_id)228 params[NETWORKINTERFACE_ID] = eni_id...
ultimate_attach_lambda.py
Source:ultimate_attach_lambda.py
...59 NetworkInterfaceId=eni60 )61 62 eni_attach_id=response1['AttachmentId']63 responsex = ec2.modify_network_interface_attribute(Attachment={'AttachmentId': eni_attach_id,'DeleteOnTermination': True},64NetworkInterfaceId=eni,)65 #print(response1)66 67 68 ##Associate free EIP to ENI69 70 response3 = ec2.associate_address(71 AllocationId=eip,72 NetworkInterfaceId=eni,73 )74 ##Create ENI75 response = ec2.create_network_interface(76 Description='PythonENILam2',77 Groups=[78 'sg-084d045efc41c9924',79 ],80 SubnetId=subnet2,81 )82 eni2=response['NetworkInterface']['NetworkInterfaceId']83 84 ##Attach ENI to instance85 response1 = ec2.attach_network_interface(86 DeviceIndex=2,87 InstanceId=EC2InstanceId,88 NetworkInterfaceId=eni289 )90 eni_attach_id2=response1['AttachmentId']91 responsex = ec2.modify_network_interface_attribute(Attachment={'AttachmentId': eni_attach_id2,'DeleteOnTermination': True},92NetworkInterfaceId=eni2,)93 94 autoscaling = boto3.client('autoscaling')95 response = autoscaling.complete_lifecycle_action(96 LifecycleHookName='LaunchLC',97 AutoScalingGroupName='MyASG',98 LifecycleActionResult='CONTINUE',99 InstanceId=EC2InstanceId100 ) 101 102 ##Detach from instance##103 ##Disassociate EIP##104 ##Delete ENI##105 ...
ultimate_lambda.py
Source:ultimate_lambda.py
...48 NetworkInterfaceId=eni49 )50 51 eni_attach_id=response1['AttachmentId']52 responsex = ec2.modify_network_interface_attribute(Attachment={'AttachmentId': eni_attach_id,'DeleteOnTermination': True},NetworkInterfaceId=eni,)53 #print(response1)54 55 56 ##Associate free EIP to ENI57 ec2 = boto3.client('ec2')58 response3 = ec2.associate_address(59 AllocationId=eip_list[0],60 NetworkInterfaceId=eni,61 )62 63 64 ##Detach from instance##65 ##Disassociate EIP##66 ##Delete ENI##67 68 69 response6 = ec2.create_network_interface(70 Description='PythonENILam2',71 Groups=[72 'sg-084d045efc41c9924',73 ],74 SubnetId=subnet_id,75 )76 eni2=response6['NetworkInterface']['NetworkInterfaceId']77 78 ##Attach ENI to instance79 response7 = ec2.attach_network_interface(80 DeviceIndex=2,81 InstanceId=EC2InstanceId,82 NetworkInterfaceId=eni283 )84 85 eni_attach_id2=response7['AttachmentId']86 response8 = ec2.modify_network_interface_attribute(Attachment={'AttachmentId': eni_attach_id2,'DeleteOnTermination': True},NetworkInterfaceId=eni2,)...
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!!