Skip to content

Commit d8fec83

Browse files
committed
[Fix] - Fixed schema output issue
1 parent b2367ba commit d8fec83

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -884,8 +884,8 @@ def setter(vm, external_identities=external_identities):
884884
if key not in command_args['mi_user_assigned']:
885885
command_args['mi_user_assigned'].append(key)
886886

887-
from .aaz.latest.vm import Patch
888-
update_vm_identity = Patch(cli_ctx=cmd.cli_ctx)(command_args=command_args)
887+
from .operations.vm import VMPatch
888+
update_vm_identity = VMPatch(cli_ctx=cmd.cli_ctx)(command_args=command_args)
889889
LongRunningOperation(cmd.cli_ctx)(update_vm_identity)
890890
result = update_vm_identity.result()
891891
return result
@@ -1387,7 +1387,7 @@ def get_instance_view(cmd, resource_group_name, vm_name, include_user_data=False
13871387

13881388

13891389
def get_vm_by_aaz(cmd, resource_group_name, vm_name, expand=None):
1390-
from .aaz.latest.vm import Show
1390+
from .operations.vm import VMShow
13911391
command_args = {
13921392
'resource_group': resource_group_name,
13931393
'vm_name': vm_name,
@@ -1396,7 +1396,7 @@ def get_vm_by_aaz(cmd, resource_group_name, vm_name, expand=None):
13961396
if expand:
13971397
command_args['expand'] = expand
13981398

1399-
return Show(cli_ctx=cmd.cli_ctx)(command_args=command_args)
1399+
return VMShow(cli_ctx=cmd.cli_ctx)(command_args=command_args)
14001400

14011401

14021402
def get_vm(cmd, resource_group_name, vm_name, expand=None):
@@ -2568,6 +2568,7 @@ def _remove_identities_by_aaz(cmd, resource_group_name, name, identities, getter
25682568
return None
25692569

25702570
existing_emsis = [x.lower() for x in (existing_identity.get('userAssignedIdentities') or {}).keys()]
2571+
existing_identity['userAssignedIdentities'] = {}
25712572

25722573
if identities:
25732574
emsis_to_remove = [x.lower() for x in identities]
@@ -2584,11 +2585,8 @@ def _remove_identities_by_aaz(cmd, resource_group_name, name, identities, getter
25842585
elif existing_identity['type'] == IdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED.value:
25852586
existing_identity['type'] = IdentityType.SYSTEM_ASSIGNED.value
25862587

2587-
existing_identity['userAssignedIdentities'] = {}
25882588
for emsis in identities:
25892589
existing_identity['userAssignedIdentities'][emsis] = {}
2590-
else:
2591-
existing_identity['userAssignedIdentities'] = None
25922590

25932591
if remove_system_assigned_identity:
25942592
if existing_identity['type'] == IdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED.value \
@@ -2611,15 +2609,15 @@ def setter(resource_group_name, vm_name, vm):
26112609
from ._vm_utils import IdentityType
26122610
if vm.get('identity') and vm.get('identity').get('type') == IdentityType.USER_ASSIGNED.value:
26132611
command_args['mi_user_assigned'] = \
2614-
vm.get('identity', {}).get('userAssignedIdentities', {}).keys() + ['UserAssigned']
2612+
list(vm.get('identity', {}).get('userAssignedIdentities', {}).keys()) + ['UserAssigned']
26152613
# NOTE: The literal 'UserAssigned' is intentionally appended as a marker for
26162614
# VMIdentityRemove._format_content, which uses it to apply special handling
26172615
# for purely user-assigned identities. It is not a real identity resource ID.
26182616
elif vm.get('identity') and vm.get('identity').get('type') == IdentityType.SYSTEM_ASSIGNED.value:
26192617
command_args['mi_user_assigned'] = []
26202618
command_args['mi_system_assigned'] = 'True'
26212619
elif vm.get('identity') and vm.get('identity').get('type') == IdentityType.SYSTEM_ASSIGNED_USER_ASSIGNED.value:
2622-
command_args['mi_user_assigned'] = vm.get('identity', {}).get('userAssignedIdentities', {}).keys()
2620+
command_args['mi_user_assigned'] = list(vm.get('identity', {}).get('userAssignedIdentities', {}).keys())
26232621
command_args['mi_system_assigned'] = 'True'
26242622
else:
26252623
command_args['mi_user_assigned'] = []

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,44 @@ def _output(self, *args, **kwargs):
158158
return result
159159

160160

161+
class VMPatch(_VMPatch):
162+
class VirtualMachinesUpdate(_VMPatch.VirtualMachinesUpdate):
163+
# Override to solve key conflict of _schema_on_200.resources.Element.properties.type when deserializing
164+
@classmethod
165+
def _build_schema_on_200(cls):
166+
schema = super()._build_schema_on_200()
167+
168+
del schema.resources.Element.properties._fields['type']
169+
schema.resources.Element.properties.type = AAZStrType(
170+
serialized_name="typePropertiesType",
171+
)
172+
return schema
173+
174+
def _output(self, *args, **kwargs):
175+
from azure.cli.core.aaz import AAZUndefined, has_value
176+
177+
# Resolve flatten conflict
178+
# When the type field conflicts, the type in inner layer is ignored and the outer layer is applied
179+
if has_value(self.ctx.vars.instance.resources):
180+
for resource in self.ctx.vars.instance.resources:
181+
if has_value(resource.type):
182+
resource.type = AAZUndefined
183+
184+
result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
185+
return result
186+
187+
161188
class VMIdentityRemove(_VMPatch):
162189
def _output(self, *args, **kwargs):
190+
from azure.cli.core.aaz import AAZUndefined, has_value
191+
192+
# Resolve flatten conflict
193+
# When the type field conflicts, the type in inner layer is ignored and the outer layer is applied
194+
if has_value(self.ctx.vars.instance.resources):
195+
for resource in self.ctx.vars.instance.resources:
196+
if has_value(resource.type):
197+
resource.type = AAZUndefined
198+
163199
result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
164200

165201
identity = result.get('identity')
@@ -178,6 +214,17 @@ def _output(self, *args, **kwargs):
178214
return result
179215

180216
class VirtualMachinesUpdate(_VMPatch.VirtualMachinesUpdate):
217+
# Override to solve key conflict of _schema_on_200.resources.Element.properties.type when deserializing
218+
@classmethod
219+
def _build_schema_on_200(cls):
220+
schema = super()._build_schema_on_200()
221+
222+
del schema.resources.Element.properties._fields['type']
223+
schema.resources.Element.properties.type = AAZStrType(
224+
serialized_name="typePropertiesType",
225+
)
226+
return schema
227+
181228
def _format_content(self, content):
182229
if isinstance(content, str):
183230
content = json.loads(content)

0 commit comments

Comments
 (0)