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
35 changes: 17 additions & 18 deletions src/azure-cli/azure/cli/command_modules/vm/_template_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,15 @@ def build_msi_role_assignment(vm_vmss_name, vm_vmss_resource_id, role_definition
}


def _build_capacity_reservation_profile(capacity_reservation_group, disable_assignment):
profile = {}
if capacity_reservation_group and capacity_reservation_group != 'None':
profile['capacityReservationGroup'] = {'id': capacity_reservation_group}
if disable_assignment is not None:
profile['disableCapacityReservationAssignment'] = disable_assignment
Comment on lines +296 to +301
return profile


def build_vm_resource( # pylint: disable=too-many-locals, too-many-statements, too-many-branches
name, location, tags, size, storage_profile, nics, admin_username,
availability_set_id=None, admin_password=None, ssh_key_values=None, ssh_key_path=None,
Expand Down Expand Up @@ -756,15 +765,10 @@ def _build_storage_profile():
if user_data:
vm_properties['userData'] = b64encode(user_data)

if capacity_reservation_group or disable_capacity_reservation_assignment is not None:
vm_properties['capacityReservation'] = {}
if capacity_reservation_group:
vm_properties['capacityReservation']['capacityReservationGroup'] = {
'id': capacity_reservation_group
}
if disable_capacity_reservation_assignment is not None:
vm_properties['capacityReservation']['disableCapacityReservationAssignment'] = \
disable_capacity_reservation_assignment
capacity_reservation = _build_capacity_reservation_profile(
capacity_reservation_group, disable_capacity_reservation_assignment)
if capacity_reservation:
vm_properties['capacityReservation'] = capacity_reservation

vm = {
'apiVersion': '2026-04-01',
Expand Down Expand Up @@ -1692,15 +1696,10 @@ def build_vmss_resource(cmd, name, computer_name_prefix, location, tags, overpro
if network_profile:
virtual_machine_profile['networkProfile'] = network_profile

if capacity_reservation_group or disable_capacity_reservation_assignment is not None:
virtual_machine_profile['capacityReservation'] = {}
if capacity_reservation_group:
virtual_machine_profile['capacityReservation']['capacityReservationGroup'] = {
'id': capacity_reservation_group
}
if disable_capacity_reservation_assignment is not None:
virtual_machine_profile['capacityReservation']['disableCapacityReservationAssignment'] = \
disable_capacity_reservation_assignment
capacity_reservation = _build_capacity_reservation_profile(
capacity_reservation_group, disable_capacity_reservation_assignment)
if capacity_reservation:
virtual_machine_profile['capacityReservation'] = capacity_reservation

if security_posture_reference_id:
virtual_machine_profile['securityPostureReference'] = {
Expand Down
9 changes: 5 additions & 4 deletions src/azure-cli/azure/cli/command_modules/vm/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2831,11 +2831,12 @@ def validate_edge_zone(cmd, namespace): # pylint: disable=unused-argument


def _validate_capacity_reservation_group(cmd, namespace):
if getattr(namespace, 'capacity_reservation_group', None) is not None and \
getattr(namespace, 'disable_capacity_reservation_assignment', None) is True:
if (namespace.disable_capacity_reservation_assignment is True and
namespace.capacity_reservation_group is not None and
namespace.capacity_reservation_group != 'None'):
raise MutuallyExclusiveArgumentError(
"--capacity-reservation-group cannot be used when --disable-capacity-reservation-assignment is set to true."
)
"--capacity-reservation-group must be omitted or set to None when "
"--disable-capacity-reservation-assignment is true.")

if namespace.capacity_reservation_group and namespace.capacity_reservation_group != 'None':

Expand Down
Loading