Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions chi/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,23 @@ def add_fip_reservation(self, amount: int):
"""
add_fip_reservation(reservation_list=self.fip_reservations, count=amount)

def add_flavor_reservation(self, id, amount=1):
def add_flavor_reservation(self, id=None, name=None, amount=1):
"""
Add a reservation for a KVM flavor to the list of reservations.
Add a reservation for a KVM flavor to the list of reservations by name or ID.

Args:
id (str): The ID of the flavor to reserve
count (int): The number of floating IPs to reserve.
name (str): The name of the flavor to reserve
amount (int): The number of instances of this flavor to reserve
"""
# Do not permit both or neither name and id.
if (name and id) or not (name or id):
raise CHIValueError("You must specify exactly one of id or name")

# Blazar API requires flavor ID, so we must convert from name
if not id:
id = server.get_flavor(name).id

self.flavor_reservations.append(
{
"resource_type": "flavor:instance",
Expand Down Expand Up @@ -783,9 +792,7 @@ def get_reserved_flavors(self):
"""
flavors = []
for res in self.flavor_reservations:
flavors.extend(
server.list_flavors(reservable=True, reservation_id=res.get("id"))
)
flavors.extend(server.list_flavors(reservation_id=res.get("id")))
return flavors


Expand Down
25 changes: 16 additions & 9 deletions chi/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,11 +699,12 @@ def __repr__(self):
return f"<{self.__class__.__name__} '{self.name}' {self.description} (disk={self.disk}) (ram={self.ram}) (vcpus={self.vcpus})>"


def list_flavors(reservable=None, reservation_id=None) -> List[Flavor]:
"""Get a list of all available flavors.
def list_flavors(reservable=None, gpu=None, reservation_id=None) -> List[Flavor]:
"""Get a list of flavors

Args:
reservable (bool): Whether to filter by reservable flavors. Defaults to True.
reservable (bool): If given, whether to filter by reservable flavors.
gpu (bool): If given, whether to filter by GPU flavors.
reservation_id (str, optional): The reservation ID to filter by. Defaults to None.

Returns:
Expand All @@ -715,13 +716,19 @@ def list_flavors(reservable=None, reservation_id=None) -> List[Flavor]:
chi_flavors = []
for f in flavors:
extras = f.get_keys()
# include a flavor if:
# - not filtering by reservable
# - is reservable in blazar & not an active reservation flavor
if not reservable or (
extras.get("aggregate_instance_extra_specs:reservation")
is_reservable = not any(
e.startswith("resources:CUSTOM_RESERVATION_") for e in extras
)
is_gpu = f.extra_specs.get("trait:CUSTOM_GPU") == "required"
matching_reservation = (
reservation_id is None
or extras.get("aggregate_instance_extra_specs:reservation", None)
== reservation_id
and extras.get("trait:CUSTOM_BLAZAR_FLAVOR_RESERVATION") == "required"
)
if (
(reservable is None or reservable == is_reservable)
and (gpu is None or gpu == is_gpu)
and matching_reservation
):
chi_flavors.append(
Flavor(
Expand Down