Skip to content

Commit

Permalink
Merge pull request #429 from kairu-ms/feature-identity-subcommands-wi…
Browse files Browse the repository at this point in the history
…th-update-patch

Support generate identity subcommands when using patch only update
  • Loading branch information
kairu-ms authored Nov 20, 2024
2 parents ea0344d + 997afb2 commit 758d2b4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 54 deletions.
37 changes: 17 additions & 20 deletions src/aaz_dev/command/controller/workspace_cfg_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,6 @@ def inherit_modification(self, ref_cfg: CfgReader):
for cmd_names, ref_cmd_names in command_rename_list:
self.rename_command(*cmd_names, new_cmd_names=ref_cmd_names)

# generate identity subcommand
for command_group in self.cfg.command_groups:
self.build_identity_subresource(command_group)

# inherit sub command
sub_resources = set()
array_sub_resources = set()
Expand Down Expand Up @@ -917,6 +913,11 @@ def inherit_modification(self, ref_cfg: CfgReader):
if not schema:
# schema not exist
continue

# skip inherit identity subcommands, it will be generated in build_identity_subresource
if isinstance(schema, CMDIdentityObjectSchemaBase):
continue

assert isinstance(schema, CMDSchema)

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

# skip inherit identity subcommands
if isinstance(schema, CMDIdentityObjectSchemaBase):
continue

# generate sub commands
sub_commands = self._generate_sub_commands(schema, subresource_idx, update_cmd, ref_args_options)
for sub_command in sub_commands:
Expand Down Expand Up @@ -1059,25 +1056,25 @@ def remove_subresource_commands(self, resource_id, version, subresource):
self.reformat()
return commands

def build_identity_subresource(self, command_group):
update_cmd = None
identity_schema, identity_schema_idx = None, None
if command_group.commands:
for command in command_group.commands:
match = self.find_identity_schema_in_command(command)
if match:
update_cmd = command
update_op, _, identity_schema, identity_schema_idx = match

if update_cmd is None:
def build_identity_subresource(self, resource_id, temp_generic_update_cmd=None):
update_cmd_info = self.get_update_cmd(resource_id)
if not update_cmd_info:
return
update_cmd_names, update_cmd, update_by = update_cmd_info
if update_by == "PatchOnly" and temp_generic_update_cmd is not None:
# generate temp update command using generic update
update_cmd = temp_generic_update_cmd
update_cmd.name = ' '.join(update_cmd_names)
elif update_by != "GenericOnly":
return
update_op, _, identity_schema, identity_schema_idx = self.find_identity_schema_in_command(update_cmd)

subresource_idx = self.schema_idx_to_subresource_idx(identity_schema_idx)
assert subresource_idx

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

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

Expand Down
69 changes: 35 additions & 34 deletions src/aaz_dev/command/controller/workspace_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,23 +660,7 @@ def _add_new_resources(self, command_generator, resources, resource_options):
cfg_editors = []
aaz_ref = {}
for resource, options in zip(resources, resource_options):
try:
command_group = command_generator.create_draft_command_group(
resource, instance_var=CMDBuildInVariants.Instance, **options)
except InvalidSwaggerValueError as err:
raise exceptions.InvalidAPIUsage(
message=str(err)
) from err
assert not command_group.command_groups, "The logic to support sub command groups is not supported"
if not isinstance(resource, CMDResource):
# Typespec use CMDResource directly, but swagger use swagger Resource
resource = resource.to_cmd()
cfg_editor = WorkspaceCfgEditor.new_cfg(
plane=self.ws.plane,
resources=[resource],
command_groups=[command_group]
)

cfg_editor = self._build_draft_cfg_editor(command_generator, resource, options)
# inherit modification from cfg in aaz
aaz_version = options.get('aaz_version', None)
if aaz_version:
Expand All @@ -689,7 +673,14 @@ def _add_new_resources(self, command_generator, resources, resource_options):
for cmd_names, _ in cfg_editor.iter_commands():
aaz_ref[' '.join(cmd_names)] = aaz_version

cfg_editor.build_identity_subresource(command_group)
update_cmd_info = cfg_editor.get_update_cmd(resource.id)
temp_generic_update_cmd = None
if update_cmd_info and options.get('update_by', None) == "PatchOnly":
temp_cfg_editor = self._build_draft_cfg_editor(command_generator, resource, {"update_by": "GenericOnly"})
temp_update_cmd_info = temp_cfg_editor.get_update_cmd(resource.id)
if temp_update_cmd_info:
_, temp_generic_update_cmd, _ = temp_update_cmd_info
cfg_editor.build_identity_subresource(resource.id, temp_generic_update_cmd)
cfg_editors.append(cfg_editor)

# add cfg_editors
Expand Down Expand Up @@ -824,23 +815,15 @@ def _reload_resources(self, command_generator, reload_resource_map):
if update_cmd_info:
_, _, update_by = update_cmd_info
options['update_by'] = update_by
try:
command_group = command_generator.create_draft_command_group(
resource, instance_var=CMDBuildInVariants.Instance, **options)
except InvalidSwaggerValueError as err:
raise exceptions.InvalidAPIUsage(
message=str(err)
) from err
assert not command_group.command_groups, "The logic to support sub command groups is not supported"
if not isinstance(resource, CMDResource):
# Typespec use CMDResource directly, but swagger use swagger Resource
resource = resource.to_cmd()
new_cfg_editor = WorkspaceCfgEditor.new_cfg(
plane=self.ws.plane,
resources=[resource],
command_groups=[command_group]
)
new_cfg_editor = self._build_draft_cfg_editor(command_generator, resource, options)
new_cfg_editor.inherit_modification(cfg_editor)
temp_generic_update_cmd = None
if update_cmd_info and update_by == "PatchOnly":
temp_cfg_editor = self._build_draft_cfg_editor(command_generator, resource, {"update_by": "GenericOnly"})
temp_update_cmd_info = temp_cfg_editor.get_update_cmd(resource_id)
if temp_update_cmd_info:
_, temp_generic_update_cmd, _ = temp_update_cmd_info
new_cfg_editor.build_identity_subresource(resource_id, temp_generic_update_cmd)
new_cfg_editors.append(new_cfg_editor)

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

# add cfg_editors
self._add_cfg_editors(new_cfg_editors)

def _build_draft_cfg_editor(self, command_generator, resource, options):
try:
command_group = command_generator.create_draft_command_group(
resource, instance_var=CMDBuildInVariants.Instance, **options)
except InvalidSwaggerValueError as err:
raise exceptions.InvalidAPIUsage(
message=str(err)
) from err
assert not command_group.command_groups, "The logic to support sub command groups is not supported"
if not isinstance(resource, CMDResource):
# Typespec use CMDResource directly, but swagger use swagger Resource
resource = resource.to_cmd()
return WorkspaceCfgEditor.new_cfg(
plane=self.ws.plane,
resources=[resource],
command_groups=[command_group]
)

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

0 comments on commit 758d2b4

Please sign in to comment.