+def get_compute_quotas(nova, project_id):
+ """
+ Returns a list of all available keypairs
+ :param nova: the Nova client
+ :param project_id: the project's ID of the quotas to lookup
+ :return: an object of type ComputeQuotas or None if not found
+ """
+ quotas = nova.quotas.get(tenant_id=project_id)
+ if quotas:
+ return ComputeQuotas(quotas)
+
+
+def update_quotas(nova, project_id, compute_quotas):
+ """
+ Updates the compute quotas for a given project
+ :param nova: the Nova client
+ :param project_id: the project's ID that requires quota updates
+ :param compute_quotas: an object of type ComputeQuotas containing the
+ values to update
+ :return:
+ """
+ update_values = dict()
+ update_values['metadata_items'] = compute_quotas.metadata_items
+ update_values['cores'] = compute_quotas.cores
+ update_values['instances'] = compute_quotas.instances
+ update_values['injected_files'] = compute_quotas.injected_files
+ update_values['injected_file_content_bytes'] = (
+ compute_quotas.injected_file_content_bytes)
+ update_values['ram'] = compute_quotas.ram
+ update_values['fixed_ips'] = compute_quotas.fixed_ips
+ update_values['key_pairs'] = compute_quotas.key_pairs
+
+ return nova.quotas.update(project_id, **update_values)
+
+
+def attach_volume(nova, server, volume, timeout=None):
+ """
+ Attaches a volume to a server
+ :param nova: the nova client
+ :param server: the VMInst domain object
+ :param volume: the Volume domain object
+ :param timeout: denotes the amount of time to block to determine if the
+ has been properly attached. When None, do not wait.
+ :return: the value from the nova call
+ """
+ nova.volumes.create_server_volume(server.id, volume.id)
+
+ if timeout:
+ start_time = time.time()
+ while time.time() < start_time + timeout:
+ vm = get_server_object_by_id(nova, server.id)
+ for vol_dict in vm.volume_ids:
+ if volume.id == vol_dict['id']:
+ return vm
+
+ return None
+ else:
+ return get_server_object_by_id(nova, server.id)
+
+
+def detach_volume(nova, server, volume, timeout=None):
+ """
+ Attaches a volume to a server
+ :param nova: the nova client
+ :param server: the VMInst domain object
+ :param volume: the Volume domain object
+ :param timeout: denotes the amount of time to block to determine if the
+ has been properly detached. When None, do not wait.
+ :return: the value from the nova call
+ """
+ nova.volumes.delete_server_volume(server.id, volume.id)
+
+ if timeout:
+ start_time = time.time()
+ while time.time() < start_time + timeout:
+ vm = get_server_object_by_id(nova, server.id)
+ found = False
+ for vol_dict in vm.volume_ids:
+ if volume.id == vol_dict['id']:
+ found = True
+
+ if not found:
+ return vm
+
+ return None
+ else:
+ return get_server_object_by_id(nova, server.id)
+
+