Skip to content

Commit 01ab8ae

Browse files
committed
aaz update vm/vmss
1 parent 1cb74ac commit 01ab8ae

10 files changed

+14836
-18044
lines changed

src/azure-cli/azure/cli/command_modules/vm/custom.py

Lines changed: 112 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,26 +1681,6 @@ def _output(self, *args, **kwargs):
16811681
}))
16821682
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
16831683

1684-
if add_proxy_agent_extension is not None:
1685-
from .aaz.latest.vm import Update as _Update
1686-
1687-
class Update(_Update):
1688-
pass
1689-
1690-
args = {
1691-
'resource_group': resource_group_name,
1692-
'vm_name': vm_name,
1693-
'no_wait': no_wait,
1694-
'security_profile': {
1695-
'proxy_agent_settings': {
1696-
'add_proxy_agent_extension': add_proxy_agent_extension
1697-
}
1698-
}
1699-
}
1700-
1701-
LongRunningOperation(cmd.cli_ctx)(Update(cli_ctx=cmd.cli_ctx)(command_args=args))
1702-
vm = get_vm_to_update(cmd, resource_group_name, vm_name)
1703-
17041684
disk_name = None
17051685
if os_disk is not None:
17061686
if is_valid_resource_id(os_disk):
@@ -1916,12 +1896,66 @@ class Update(_Update):
19161896
vm.scheduled_events_policy.user_initiated_reboot = {
19171897
"automaticallyApprove": enable_user_reboot_scheduled_events
19181898
}
1919-
client = _compute_client_factory(cmd.cli_ctx, aux_subscriptions=aux_subscriptions)
1920-
if wire_server_access_control_profile_reference_id is not None or \
1921-
imds_access_control_profile_reference_id is not None or \
1922-
add_proxy_agent_extension is not None:
1923-
kwargs['parameters'] = vm
1924-
return sdk_no_wait(no_wait, client.virtual_machines.begin_create_or_update, resource_group_name, vm_name, **kwargs)
1899+
1900+
# ==============================================================================
1901+
# AAZ Update: Use SDK for complex logic, then apply via AAZ
1902+
# ==============================================================================
1903+
# All SDK modifications to 'vm' object have already happened above (storage, security, etc.)
1904+
# Now we just need to apply those changes using AAZ's Update command
1905+
1906+
class VMUpdateCustom(UpdateVM):
1907+
def pre_instance_update(self, instance):
1908+
"""
1909+
Copy SDK VM object properties into the AAZ instance before the update.
1910+
This ensures AAZ sees the current state, but does NOT override user args.
1911+
"""
1912+
vm_dict = vm.serialize()
1913+
vm_dict.pop('resources', None) # Remove conflicting resources
1914+
1915+
for key, value in vm_dict.items():
1916+
try:
1917+
instance[key] = value
1918+
except Exception as e:
1919+
logger.debug(f"Could not set {key}: {e}")
1920+
1921+
def post_instance_update(self, instance):
1922+
"""
1923+
Set AAZ-only fields after AAZ processes command_args.
1924+
This ensures user arguments (--enable-proxy-agent, etc.) take precedence.
1925+
"""
1926+
if add_proxy_agent_extension is not None:
1927+
props = getattr(instance, 'properties', None)
1928+
sp = getattr(props, 'security_profile', None) if props else None
1929+
pas = getattr(sp, 'proxy_agent_settings', None) if sp else None
1930+
1931+
if pas:
1932+
pas.add_proxy_agent_extension = add_proxy_agent_extension
1933+
else:
1934+
logger.debug("Cannot set add_proxy_agent_extension - proxy_agent_settings does not exist")
1935+
1936+
def _output(self, *args, **kwargs):
1937+
"""
1938+
Remove resources before flattening to avoid KeyError issues during output serialization.
1939+
"""
1940+
self.ctx.vars.instance.resources = None
1941+
return self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
1942+
1943+
args = {
1944+
'resource_group': resource_group_name,
1945+
'vm_name': vm_name,
1946+
'no_wait': no_wait
1947+
}
1948+
1949+
return VMUpdateCustom(cli_ctx=cmd.cli_ctx)(command_args=args)
1950+
1951+
1952+
1953+
# client = _compute_client_factory(cmd.cli_ctx, aux_subscriptions=aux_subscriptions)
1954+
# if wire_server_access_control_profile_reference_id is not None or \
1955+
# imds_access_control_profile_reference_id is not None or \
1956+
# add_proxy_agent_extension is not None:
1957+
# kwargs['parameters'] = vm
1958+
# return sdk_no_wait(no_wait, client.virtual_machines.begin_create_or_update, resource_group_name, vm_name, **kwargs)
19251959
# endregion
19261960

19271961

@@ -4221,28 +4255,6 @@ def _output(self, *args, **kwargs):
42214255
}))
42224256
vmss = get_vmss_modified(cmd, resource_group_name, name, instance_id, security_type)
42234257

4224-
if add_proxy_agent_extension is not None:
4225-
from .aaz.latest.vmss import Update as _VMSSUpdate
4226-
4227-
class VMSSUpdate(_VMSSUpdate):
4228-
pass
4229-
4230-
args = {
4231-
'resource_group': resource_group_name,
4232-
'vm_scale_set_name': name,
4233-
'no_wait': no_wait,
4234-
'virtual_machine_profile': {
4235-
'security_profile': {
4236-
'proxy_agent_settings': {
4237-
'add_proxy_agent_extension': add_proxy_agent_extension
4238-
}
4239-
}
4240-
}
4241-
}
4242-
4243-
LongRunningOperation(cmd.cli_ctx)(VMSSUpdate(cli_ctx=cmd.cli_ctx)(command_args=args))
4244-
vmss = get_vmss_modified(cmd, resource_group_name, name, instance_id, security_type)
4245-
42464258
aux_subscriptions = None
42474259
# pylint: disable=too-many-boolean-expressions
42484260
if vmss and hasattr(vmss, 'virtual_machine_profile') and vmss.virtual_machine_profile and \
@@ -4586,14 +4598,58 @@ class VMSSUpdate(_VMSSUpdate):
45864598

45874599
if zone_balance is not None:
45884600
vmss.zone_balance = zone_balance
4589-
4590-
if wire_server_access_control_profile_reference_id is not None or \
4591-
imds_access_control_profile_reference_id is not None or \
4592-
add_proxy_agent_extension is not None:
4593-
kwargs['parameters'] = vmss
4594-
4595-
return sdk_no_wait(no_wait, client.virtual_machine_scale_sets.begin_create_or_update,
4596-
resource_group_name, name, **kwargs)
4601+
4602+
# if wire_server_access_control_profile_reference_id is not None or \
4603+
# imds_access_control_profile_reference_id is not None or \
4604+
# add_proxy_agent_extension is not None:
4605+
# kwargs['parameters'] = vmss
4606+
4607+
# return sdk_no_wait(no_wait, client.virtual_machine_scale_sets.begin_create_or_update,
4608+
# resource_group_name, name, **kwargs)
4609+
4610+
# ==============================================================================
4611+
# AAZ Update: Use SDK for complex logic, then apply via AAZ (same pattern as update_vm)
4612+
# ==============================================================================
4613+
from .aaz.latest.vmss import Update as UpdateVMSS
4614+
4615+
class VMSSUpdateCustom(UpdateVMSS):
4616+
def pre_instance_update(self, instance):
4617+
"""
4618+
Copy SDK VMSS object properties into the AAZ instance before the update.
4619+
This ensures AAZ sees the current state, but does NOT override user args.
4620+
"""
4621+
vmss_dict = vmss.serialize()
4622+
4623+
for key, value in vmss_dict.items():
4624+
try:
4625+
instance[key] = value
4626+
except Exception as e:
4627+
logger.debug(f"Could not set {key}: {e}")
4628+
4629+
def post_instance_update(self, instance):
4630+
"""
4631+
Set AAZ-only fields after AAZ processes command_args.
4632+
This ensures user arguments take precedence.
4633+
"""
4634+
if add_proxy_agent_extension is not None:
4635+
# Navigate through nested structure safely (VMSS has extra level)
4636+
props = getattr(instance, 'properties', None)
4637+
vm_profile = getattr(props, 'virtual_machine_profile', None) if props else None
4638+
sp = getattr(vm_profile, 'security_profile', None) if vm_profile else None
4639+
pas = getattr(sp, 'proxy_agent_settings', None) if sp else None
4640+
4641+
if pas:
4642+
pas.add_proxy_agent_extension = add_proxy_agent_extension
4643+
else:
4644+
logger.debug("Cannot set add_proxy_agent_extension - proxy_agent_settings does not exist")
4645+
4646+
args = {
4647+
'resource_group': resource_group_name,
4648+
'vm_scale_set_name': name,
4649+
'no_wait': no_wait
4650+
}
4651+
4652+
return VMSSUpdateCustom(cli_ctx=cmd.cli_ctx)(command_args=args)
45974653

45984654
# endregion
45994655

0 commit comments

Comments
 (0)