Best Python code snippet using localstack_python
index.py
Source:index.py
...69 )70 capacity_reservation_id = capacity_reservations[0]['CapacityReservationId']71 else:72 print("Creating capacity reservation")73 capacity_reservation = client.create_capacity_reservation(74 ClientToken=str(random.random())[2:],75 InstanceType=instance['InstanceType'],76 InstancePlatform=platform,77 AvailabilityZone=instance['Placement']['AvailabilityZone'],78 Tenancy=instance['Placement']['Tenancy'],79 InstanceCount=1,80 EndDateType='unlimited',81 InstanceMatchCriteria='open',82 TagSpecifications=[83 {84 'ResourceType': 'capacity-reservation',85 'Tags': [86 {87 'Key': 'AutoCapacityReservation',...
a.py
Source:a.py
...8def lambda_handler(event, context):9 # Boto3 clients10 ec2_client = boto3.client('ec2')11 get_instances = ec2_client.describe_instances()12 create_capacity_reservation(get_instances, ec2_client)131415# Get All the running instances and call the function to reserve capacity16def create_capacity_reservation(get_instances, client):17 running_instances = False18 cancel_reservations = []19 #Total Count of instances running20 t2_micro = t3_2xlarge = r6i_2xlarge = r6i_4xlarge = r6i_8xlarge = 021 22 23 for reservations in get_instances['Reservations']:24 #Create Capacity Reservation for running instance types25 for instances in reservations['Instances']:26 if instances['State']['Name'] == 'running':27 running_instances = True28 type = instances['InstanceType']29 if type == "t2.micro":30 t2_micro += 131 elif type == "t3.2xlarge":32 t3_2xlarge += 133 elif type == "r6i.2xlarge":34 r6i_2xlarge += 135 elif type == "r6i.4xlarge":36 r6i_4xlarge += 137 elif type == "r6i_8xlarge":38 r6i_8xlarge += 139 40 41 get_reservation = get_reservations(client, instances['InstanceType'])42 print(get_reservation)43 if not get_reservation['CapacityReservations']:44 #print("No Capacity")45 #Call reserver capacity function46 47 48 49 reserve_capacity(client, instances['InstanceType'])50 51 52 53 elif get_reservation:54 for reservation in get_reservation['CapacityReservations']:55 cancel_reservations.remove(reservation['CapacityReservationId'])56 print("It is here")57 # modify_capacity_reservation(client, reservation['TotalInstanceCount'], 20, reservation['CapacityReservationId'])58 modify_capacity_reservation(client, reservation['TotalInstanceCount'], reservation['AvailableInstanceCount'], reservation['CapacityReservationId'])59 60 else:61 get_reservation = get_reservations(client, instances['InstanceType'])62 if get_reservation:63 for reservation in get_reservation['CapacityReservations']:64 if(reservation['CapacityReservationId'] not in cancel_reservations):65 cancel_reservations.append(reservation['CapacityReservationId'])66 67 68 for reservations in get_instances['Reservations']:69 #Cancel Cpacity Reservation for stopped instance types70 for instances in reservations['Instances']: 71 get_reservation = get_reservations(client, instances['InstanceType'])72 if get_reservation:73 for reservation in cancel_reservations:74 cancel_reservation(client, reservation)75 76 if running_instances == False:77 print('No Running Instances found!')78798081def cancel_reservation(client, reservation_id):82 response = client.cancel_capacity_reservation(83 CapacityReservationId=reservation_id84 )85 return response86 878889def modify_capacity_reservation(ec2_client, total_count, available_count, reservation_id):90 print ("Invoked!")91 default_count = config.default_count92 capacity_chg_percentage = config.capacity_chg_percentage93 94 #Calculate 20% capacity of total capacity95 capacity_percentage = int((config.capacity_chg_percentage / 100 * total_count))96 #Calculate available capacity percentage97 available_percentage = int((available_count / total_count * 100))98 99 print (capacity_percentage)100 print (available_percentage)101102 #If capacity is less than 0 for example total count is low like 2 then round103 #the value to 1104 if (capacity_percentage == 0):105 capacity_percentage = 1106 elif (available_percentage == 0):107 available_percentage = 1108 109 #Increment 20% Capacity110 if (available_percentage <= capacity_chg_percentage):111 new_count = total_count + capacity_percentage112 113 #Decrement 20% Capacity114 elif (available_percentage > capacity_chg_percentage and total_count > default_count):115 new_count = total_count -capacity_percentage116 117 else:118 #Else don't change capacity119 new_count = total_count120 121 122 print (new_count)123 modify = ec2_client.modify_capacity_reservation(124 CapacityReservationId=reservation_id,125 InstanceCount=new_count126 )127 128129130131def get_reservations(client, instance_type):132 133 response = client.describe_capacity_reservations(Filters=[134 {135 'Name': 'state',136 'Values': [137 'active',138 ]139 },140 {141 'Name': 'instance-type',142 'Values': [143 instance_type,144 ]145 },146 ]147 148 )149 150 return response151152153# Create Capacity Reservation154def reserve_capacity(client, instance_type):155 156 default_count = config.default_count157 158 response = client.create_capacity_reservation(159 InstanceType=instance_type,160 InstancePlatform=config.InstancePlatform,161 AvailabilityZone=config.AvailabilityZone,162 Tenancy=config.Tenancy,163 InstanceCount=default_count,164 EbsOptimized=config.EbsOptimized,165 EphemeralStorage=config.EphemeralStorage,166 EndDateType=config.EndDateType,167 InstanceMatchCriteria=config.InstanceMatchCriteria168 )169170171172
...
Capacity.Py
Source:Capacity.Py
...1112def checkCapacity(InstanceType, InstancePlatform, AvailabilityZone, InstanceCount, EndDate, EndDateType): 1314 ec2 = boto3.client('ec2', region_name='us-west-2')15 response = ec2.create_capacity_reservation(16 InstanceType=InstanceType,17 InstancePlatform=InstancePlatform,18 AvailabilityZone=AvailabilityZone,19 InstanceCount=InstanceCount,20 EndDate=EndDate,21 EndDateType=EndDateType,22 )23 cr = response['CapacityReservation']24 cr_status = cr['State']25 print (cr_status)26 return cr_status2728def main():29
...
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!!