Hi I'm a very new python programmer and I have encountered a problem with quota query code.
def get_metric_statistics(instance_args):
CMD = (**querying the quota***)
print (CMD)
output = run_command_str(command=CMD, shell_command=True)
applied_quota_value = json.loads(output)
max_quota_metric = applied_quota_value ['Quota']['Value']
print ("max_quota:", max_quota_metric)
CMD1 = (***querying metric**)
print (CMD1)
output1 = run_command_str(command=CMD1, shell_command=True)
print ("output:", output1)
utilized_quota_value = json.loads(output1)
used_quota_metric = utilized_quota_value ['Datapoints'][0]['Maximum']
print ("used:", used_quota_metric)
available_quota = max_quota_metric - used_quota_metric
instance_args["no_of_vcpu"] = some value
while available_quota < instance_args["no_of_vcpu"]:
print ("Waiting for vcpu to be available")
time.sleep(30)
else:
print ("available quota is greater than requested vcpu, continues to launch the instance")
aws_metric_data = get_metric_statistics(instance_args)
print(aws_metric_data)
so here im trying to check the condition and print the subsequent statements, But when the variable with the value in the condition enters the loop, it checks the condition is true and the true condition is looping over even after the variable has been updated by a different value. The variable in the condition is basically not getting updated. Not sure how to get that piece of code which does the update inside the loop.
The variable is not being updated in the while loop. I've moved your calculations into a seperate function. When the while loop runs after the sleep the available quota will be recalculated
def calc_available_quota():
CMD = (**querying the quota***)
print (CMD)
output = run_command_str(command=CMD, shell_command=True)
applied_quota_value = json.loads(output)
max_quota_metric = applied_quota_value ['Quota']['Value']
print ("max_quota:", max_quota_metric)
CMD1 = (***querying metric**)
print (CMD1)
output1 = run_command_str(command=CMD1, shell_command=True)
print ("output:", output1)
utilized_quota_value = json.loads(output1)
used_quota_metric = utilized_quota_value ['Datapoints'][0]['Maximum']
print ("used:", used_quota_metric)
return max_quota_metric - used_quota_metric
def get_metric_statistics(instance_args):
instance_args["no_of_vcpu"] = some_value
available_quota = calc_available_quota()
while available_quota < instance_args["no_of_vcpu"]:
print ("Waiting for vcpu to be available")
time.sleep(30)
available_quota = calc_available_quota()
else:
print ("available quota is greater than requested vcpu, continues to launch the instance")
aws_metric_data = get_metric_statistics(instance_args)
print(aws_metric_data)