diff --git a/src/azure-cli/azure/cli/command_modules/vm/_template_builder.py b/src/azure-cli/azure/cli/command_modules/vm/_template_builder.py index f150a9bd96b..09ad19ae46d 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_template_builder.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_template_builder.py @@ -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 + 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, @@ -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', @@ -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'] = { diff --git a/src/azure-cli/azure/cli/command_modules/vm/_validators.py b/src/azure-cli/azure/cli/command_modules/vm/_validators.py index 32b3a6c587f..f96269ffa43 100644 --- a/src/azure-cli/azure/cli/command_modules/vm/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/vm/_validators.py @@ -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':