def has_enough_resources(self, cpu_count, machine_type):
   estimated_bandwidth = cloud_util.get_max_bandwidth_from_machine_type('GCP', machine_type)
   if (self.get_available_cpus(machine_type) >= cpu_count and self.quotas['IN_USE_ADDRESSES']['limit'] > self.quotas['IN_USE_ADDRESSES']['usage']):
     if not self.bandwidth_limit or estimated_bandwidth + self.bandwidth_usage <= self.bandwidth_limit:
       if not self.cloud.bandwidth_limit or estimated_bandwidth + self.cloud.bandwidth_usage <= self.cloud.bandwidth_limit:
         return True
 
   return False
 def has_enough_resources(self, cpu_count, machine_type):
   estimated_bandwidth = cloud_util.get_max_bandwidth_from_machine_type('AWS', machine_type)
   if (self.quotas['vm']['usage'] < self.quotas['vm']['limit']
     and self.quotas['elastic_ip']['usage'] < self.quotas['elastic_ip']['limit']
     and self.quotas['vpc']['usage'] < self.quotas['vpc']['limit']):
     # If we are checking bandwidth limits, and if this exceeds limit
     if not self.bandwidth_limit or estimated_bandwidth + self.bandwidth_usage <= self.bandwidth_limit:
       if not self.cloud.bandwidth_limit or estimated_bandwidth + self.cloud.bandwidth_usage <= self.cloud.bandwidth_limit:
         return True
 
   return False
  def remove_virtual_machine(self, vm):
    # TODO add safety checks here
    cpu_type = self._get_cpu_type(vm.machine_type)
    estimated_bandwidth = cloud_util.get_max_bandwidth_from_machine_type('GCP', vm.machine_type)
    self.virtual_machines.remove(vm)
    self.quotas[cpu_type]['usage'] -= vm.cpu_count
    self.quotas['IN_USE_ADDRESSES']['usage'] -= 1
    self.bandwidth_usage -= estimated_bandwidth
    self.cloud.bandwidth_usage -= estimated_bandwidth

    if self.quotas[cpu_type]['usage'] < 0:
      self.quotas[cpu_type]['usage'] = 0
    if self.quotas['IN_USE_ADDRESSES']['usage'] < 0:
      self.quotas['IN_USE_ADDRESSES']['usage'] = 0
 def add_virtual_machine_if_possible(self, vm):
   if self.has_enough_resources(vm.cpu_count, vm.machine_type):
     estimated_bandwidth = cloud_util.get_max_bandwidth_from_machine_type('AWS', vm.machine_type)
     self.virtual_machines.append(vm)
     self.quotas['vm']['usage'] += 1
     self.quotas['vpc']['usage'] += 1
     self.quotas['elastic_ip']['usage'] += 1
     self.bandwidth_usage += estimated_bandwidth
     self.cloud.bandwidth_usage += estimated_bandwidth
     print(f"AWS VM USAGE: {self.quotas['vm']['usage']}, QUOTA: {self.quotas['vm']['limit']}")
     print(f"AWS ELASTIC IP USAGE: {self.quotas['elastic_ip']['usage']}, QUOTA: {self.quotas['elastic_ip']['limit']}")
     return True
   else:
     print("Quota reached for region: " + self.name)
     return False
 def add_virtual_machine_if_possible(self, vm):
   if self.has_enough_resources(vm.cpu_count, vm.machine_type):
     cpu_type = self._get_cpu_type(vm.machine_type)
     estimated_bandwidth = cloud_util.get_max_bandwidth_from_machine_type('GCP', vm.machine_type)
     self.virtual_machines.append(vm)
     self.quotas[cpu_type]['usage'] += vm.cpu_count
     self.quotas['IN_USE_ADDRESSES']['usage'] += 1
     self.bandwidth_usage += estimated_bandwidth
     self.cloud.bandwidth_usage += estimated_bandwidth
     print(f"{cpu_type} CPU USAGE: {self.quotas[cpu_type]['usage']}, QUOTA: {self.quotas[cpu_type]['limit']}")
     print(f"ADDR USAGE: {self.quotas['IN_USE_ADDRESSES']['usage']}, QUOTA: {self.quotas['IN_USE_ADDRESSES']['limit']}")
     return True
   else:
     print("Quota reached for region: " + self.name)
     return False
  def remove_virtual_machine(self, vm):
    # TODO add safety checks here
    cpu_type = self._get_cpu_type(vm.machine_type)
    estimated_bandwidth = cloud_util.get_max_bandwidth_from_machine_type('GCP', vm.machine_type)
    self.virtual_machines.remove(vm)
    self.quotas['vm']['usage'] -= 1
    self.quotas['vpc']['usage'] -= 1
    self.quotas['elastic_ip']['usage'] -= 1
    self.bandwidth_usage -= estimated_bandwidth
    self.cloud.bandwidth_usage -= estimated_bandwidth

    if self.quotas['vm']['usage'] < 0:
      self.quotas['vm']['usage'] = 0 
    if self.quotas['vpc']['usage'] < 0:
      self.quotas['vm']['usage'] = 0
    if self.quotas['elastic_ip']['usage'] < 0:
      self.quotas['vm']['usage'] = 0