Skip to content

Commit 758d2b4

Browse files
authored
Merge pull request #429 from kairu-ms/feature-identity-subcommands-with-update-patch
Support generate identity subcommands when using patch only update
2 parents ea0344d + 997afb2 commit 758d2b4

File tree

2 files changed

+52
-54
lines changed

2 files changed

+52
-54
lines changed

src/aaz_dev/command/controller/workspace_cfg_editor.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,6 @@ def inherit_modification(self, ref_cfg: CfgReader):
869869
for cmd_names, ref_cmd_names in command_rename_list:
870870
self.rename_command(*cmd_names, new_cmd_names=ref_cmd_names)
871871

872-
# generate identity subcommand
873-
for command_group in self.cfg.command_groups:
874-
self.build_identity_subresource(command_group)
875-
876872
# inherit sub command
877873
sub_resources = set()
878874
array_sub_resources = set()
@@ -917,6 +913,11 @@ def inherit_modification(self, ref_cfg: CfgReader):
917913
if not schema:
918914
# schema not exist
919915
continue
916+
917+
# skip inherit identity subcommands, it will be generated in build_identity_subresource
918+
if isinstance(schema, CMDIdentityObjectSchemaBase):
919+
continue
920+
920921
assert isinstance(schema, CMDSchema)
921922

922923
# build ref_args_options
@@ -941,10 +942,6 @@ def inherit_modification(self, ref_cfg: CfgReader):
941942
ref_args_options[ref_arg.var] = [*ref_arg.options]
942943
assert cg_names is not None
943944

944-
# skip inherit identity subcommands
945-
if isinstance(schema, CMDIdentityObjectSchemaBase):
946-
continue
947-
948945
# generate sub commands
949946
sub_commands = self._generate_sub_commands(schema, subresource_idx, update_cmd, ref_args_options)
950947
for sub_command in sub_commands:
@@ -1059,25 +1056,25 @@ def remove_subresource_commands(self, resource_id, version, subresource):
10591056
self.reformat()
10601057
return commands
10611058

1062-
def build_identity_subresource(self, command_group):
1063-
update_cmd = None
1064-
identity_schema, identity_schema_idx = None, None
1065-
if command_group.commands:
1066-
for command in command_group.commands:
1067-
match = self.find_identity_schema_in_command(command)
1068-
if match:
1069-
update_cmd = command
1070-
update_op, _, identity_schema, identity_schema_idx = match
1071-
1072-
if update_cmd is None:
1059+
def build_identity_subresource(self, resource_id, temp_generic_update_cmd=None):
1060+
update_cmd_info = self.get_update_cmd(resource_id)
1061+
if not update_cmd_info:
1062+
return
1063+
update_cmd_names, update_cmd, update_by = update_cmd_info
1064+
if update_by == "PatchOnly" and temp_generic_update_cmd is not None:
1065+
# generate temp update command using generic update
1066+
update_cmd = temp_generic_update_cmd
1067+
update_cmd.name = ' '.join(update_cmd_names)
1068+
elif update_by != "GenericOnly":
10731069
return
1070+
update_op, _, identity_schema, identity_schema_idx = self.find_identity_schema_in_command(update_cmd)
10741071

10751072
subresource_idx = self.schema_idx_to_subresource_idx(identity_schema_idx)
10761073
assert subresource_idx
10771074

10781075
sub_commands = self._generate_identity_sub_commands(identity_schema, subresource_idx, update_cmd, update_op)
10791076

1080-
cg_names = command_group.name.split(' ') + [identity_schema.name]
1077+
cg_names = update_cmd_names[:-1] + [identity_schema.name]
10811078
for sub_command in sub_commands:
10821079
self._add_command(*cg_names, sub_command.name, command=sub_command)
10831080

src/aaz_dev/command/controller/workspace_manager.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -660,23 +660,7 @@ def _add_new_resources(self, command_generator, resources, resource_options):
660660
cfg_editors = []
661661
aaz_ref = {}
662662
for resource, options in zip(resources, resource_options):
663-
try:
664-
command_group = command_generator.create_draft_command_group(
665-
resource, instance_var=CMDBuildInVariants.Instance, **options)
666-
except InvalidSwaggerValueError as err:
667-
raise exceptions.InvalidAPIUsage(
668-
message=str(err)
669-
) from err
670-
assert not command_group.command_groups, "The logic to support sub command groups is not supported"
671-
if not isinstance(resource, CMDResource):
672-
# Typespec use CMDResource directly, but swagger use swagger Resource
673-
resource = resource.to_cmd()
674-
cfg_editor = WorkspaceCfgEditor.new_cfg(
675-
plane=self.ws.plane,
676-
resources=[resource],
677-
command_groups=[command_group]
678-
)
679-
663+
cfg_editor = self._build_draft_cfg_editor(command_generator, resource, options)
680664
# inherit modification from cfg in aaz
681665
aaz_version = options.get('aaz_version', None)
682666
if aaz_version:
@@ -689,7 +673,14 @@ def _add_new_resources(self, command_generator, resources, resource_options):
689673
for cmd_names, _ in cfg_editor.iter_commands():
690674
aaz_ref[' '.join(cmd_names)] = aaz_version
691675

692-
cfg_editor.build_identity_subresource(command_group)
676+
update_cmd_info = cfg_editor.get_update_cmd(resource.id)
677+
temp_generic_update_cmd = None
678+
if update_cmd_info and options.get('update_by', None) == "PatchOnly":
679+
temp_cfg_editor = self._build_draft_cfg_editor(command_generator, resource, {"update_by": "GenericOnly"})
680+
temp_update_cmd_info = temp_cfg_editor.get_update_cmd(resource.id)
681+
if temp_update_cmd_info:
682+
_, temp_generic_update_cmd, _ = temp_update_cmd_info
683+
cfg_editor.build_identity_subresource(resource.id, temp_generic_update_cmd)
693684
cfg_editors.append(cfg_editor)
694685

695686
# add cfg_editors
@@ -824,23 +815,15 @@ def _reload_resources(self, command_generator, reload_resource_map):
824815
if update_cmd_info:
825816
_, _, update_by = update_cmd_info
826817
options['update_by'] = update_by
827-
try:
828-
command_group = command_generator.create_draft_command_group(
829-
resource, instance_var=CMDBuildInVariants.Instance, **options)
830-
except InvalidSwaggerValueError as err:
831-
raise exceptions.InvalidAPIUsage(
832-
message=str(err)
833-
) from err
834-
assert not command_group.command_groups, "The logic to support sub command groups is not supported"
835-
if not isinstance(resource, CMDResource):
836-
# Typespec use CMDResource directly, but swagger use swagger Resource
837-
resource = resource.to_cmd()
838-
new_cfg_editor = WorkspaceCfgEditor.new_cfg(
839-
plane=self.ws.plane,
840-
resources=[resource],
841-
command_groups=[command_group]
842-
)
818+
new_cfg_editor = self._build_draft_cfg_editor(command_generator, resource, options)
843819
new_cfg_editor.inherit_modification(cfg_editor)
820+
temp_generic_update_cmd = None
821+
if update_cmd_info and update_by == "PatchOnly":
822+
temp_cfg_editor = self._build_draft_cfg_editor(command_generator, resource, {"update_by": "GenericOnly"})
823+
temp_update_cmd_info = temp_cfg_editor.get_update_cmd(resource_id)
824+
if temp_update_cmd_info:
825+
_, temp_generic_update_cmd, _ = temp_update_cmd_info
826+
new_cfg_editor.build_identity_subresource(resource_id, temp_generic_update_cmd)
844827
new_cfg_editors.append(new_cfg_editor)
845828

846829
# remove old cfg editor
@@ -850,6 +833,24 @@ def _reload_resources(self, command_generator, reload_resource_map):
850833

851834
# add cfg_editors
852835
self._add_cfg_editors(new_cfg_editors)
836+
837+
def _build_draft_cfg_editor(self, command_generator, resource, options):
838+
try:
839+
command_group = command_generator.create_draft_command_group(
840+
resource, instance_var=CMDBuildInVariants.Instance, **options)
841+
except InvalidSwaggerValueError as err:
842+
raise exceptions.InvalidAPIUsage(
843+
message=str(err)
844+
) from err
845+
assert not command_group.command_groups, "The logic to support sub command groups is not supported"
846+
if not isinstance(resource, CMDResource):
847+
# Typespec use CMDResource directly, but swagger use swagger Resource
848+
resource = resource.to_cmd()
849+
return WorkspaceCfgEditor.new_cfg(
850+
plane=self.ws.plane,
851+
resources=[resource],
852+
command_groups=[command_group]
853+
)
853854

854855
def add_new_command_by_aaz(self, *cmd_names, version):
855856
# TODO: add support to load from aaz

0 commit comments

Comments
 (0)