#NEWLINE# def allowed_instances( context , requested_instances , instance_type ) : #NEWLINE# #INDENT# 'Check quota and return min(requested_instances, allowed_instances).' #NEWLINE# project_id = context . project_id #NEWLINE# context = context . elevated ( ) #NEWLINE# requested_cores = ( requested_instances * instance_type [ 'vcpus' ] ) #NEWLINE# requested_ram = ( requested_instances * instance_type [ 'memory_mb' ] ) #NEWLINE# usage = db . instance_data_get_for_project ( context , project_id ) #NEWLINE# ( used_instances , used_cores , used_ram ) = usage #NEWLINE# quota = get_project_quotas ( context , project_id ) #NEWLINE# allowed_instances = _get_request_allotment ( requested_instances , used_instances , quota [ 'instances' ] ) #NEWLINE# allowed_cores = _get_request_allotment ( requested_cores , used_cores , quota [ 'cores' ] ) #NEWLINE# allowed_ram = _get_request_allotment ( requested_ram , used_ram , quota [ 'ram' ] ) #NEWLINE# allowed_instances = min ( allowed_instances , ( allowed_cores // instance_type [ 'vcpus' ] ) , ( allowed_ram // instance_type [ 'memory_mb' ] ) ) #NEWLINE# return min ( requested_instances , allowed_instances )