Skip to content

Commit

Permalink
support generate update command by inherenting th existing command mo…
Browse files Browse the repository at this point in the history
…des in aaz
  • Loading branch information
kairu-ms committed Nov 20, 2024
1 parent 758d2b4 commit a4cb91b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/pages/usage/workspace_editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ There are two mechanisms to implement `Update` in azure-cli.

There's three modes to control the generation of `Update` commands while add the resources.

- Default: If inherent from existing command models in aaz, it will follow the method used in the existing update command model, else it will follow the _Generic(Get&Put) First_ mode.
- Generic(Get&Put) First: If the resource supports `Get` and `Put` methods, generate the _Generic Update_, else if it supports `Patch` method, it generate the _Patch Update_.
- Patch First: If the resource supports `Patch` method, it generate the _Patch Update_, else if it supports `Get` and `Put` methods.
- No update command: Never generate `Update` commands for the resource.
Expand Down
3 changes: 2 additions & 1 deletion src/aaz_dev/command/controller/workspace_cfg_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ def inherit_modification(self, ref_cfg: CfgReader):
update_cmd_info = self.get_update_cmd(resource_id)
if not update_cmd_info:
continue
update_cmd_names, update_cmd, update_by = update_cmd_info
_, update_cmd, update_by = update_cmd_info
if update_by != "GenericOnly":
continue

Expand Down Expand Up @@ -1079,6 +1079,7 @@ def build_identity_subresource(self, resource_id, temp_generic_update_cmd=None):
self._add_command(*cg_names, sub_command.name, command=sub_command)

self.reformat()
return cg_names

def build_subresource_commands_by_arg_var(self, resource_id, arg_var, cg_names, ref_args_options=None):
"""
Expand Down
13 changes: 10 additions & 3 deletions src/aaz_dev/command/controller/workspace_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,23 +660,30 @@ def _add_new_resources(self, command_generator, resources, resource_options):
cfg_editors = []
aaz_ref = {}
for resource, options in zip(resources, resource_options):
cfg_editor = self._build_draft_cfg_editor(command_generator, resource, options)
# inherit modification from cfg in aaz
aaz_cfg_reader = None
aaz_version = options.get('aaz_version', None)
if aaz_version:
try:
aaz_cfg_reader = self.aaz_specs.load_resource_cfg_reader(
self.ws.plane, resource.id, aaz_version)
except ValueError as err:
raise exceptions.InvalidAPIUsage(message=str(err)) from err
# inherit update_by from existing cfg in aaz
if "update_by" not in options:
aaz_update_cmd_info = aaz_cfg_reader.get_update_cmd(resource.id)
if aaz_update_cmd_info:
options["update_by"] = aaz_update_cmd_info[2]
cfg_editor = self._build_draft_cfg_editor(command_generator, resource, options)
if aaz_cfg_reader:
cfg_editor.inherit_modification(aaz_cfg_reader)
for cmd_names, _ in cfg_editor.iter_commands():
aaz_ref[' '.join(cmd_names)] = aaz_version

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_cfg_editor = self._build_draft_cfg_editor(command_generator, resource, {"update_by": "GenericOnly", "methods": ('get', 'put')})
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
Expand Down Expand Up @@ -819,7 +826,7 @@ def _reload_resources(self, command_generator, reload_resource_map):
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_cfg_editor = self._build_draft_cfg_editor(command_generator, resource, {"update_by": "GenericOnly", "methods": ('get', 'put')})
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
Expand Down
22 changes: 20 additions & 2 deletions src/web/src/views/workspace/WSEditorSwaggerPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const MiddlePadding2 = styled(Box)(() => ({
height: '8vh'
}));

const UpdateOptions = ["Generic(Get&Put) First", "Patch First", "No update command"];
const UpdateOptions = ["Default", "Generic(Get&Put) First", "Patch First", "No update command"];

class WSEditorSwaggerPicker extends React.Component<WSEditorSwaggerPickerProps, WSEditorSwaggerPickerState> {

Expand Down Expand Up @@ -366,6 +366,24 @@ class WSEditorSwaggerPicker extends React.Component<WSEditorSwaggerPickerProps,
},
}
if (updateOption === UpdateOptions[1]) {
// generic first
const resource = resourceMap[resourceId];
const operations = resource.versions.find(v => v.version === selectedVersion)?.operations;
if (operations) {
let hasGet = false;
let hasPut = false;
for (const opName in operations) {
if (operations[opName].toLowerCase() === "put") {
hasPut = true;
} else if (operations[opName].toLowerCase() === "get") {
hasGet = true;
}
}
if (hasGet && hasPut) {
res.options.update_by = "GenericOnly"
}
}
} else if (updateOption === UpdateOptions[2]) {
// patch first
const resource = resourceMap[resourceId];
const operations = resource.versions.find(v => v.version === selectedVersion)?.operations;
Expand All @@ -377,7 +395,7 @@ class WSEditorSwaggerPicker extends React.Component<WSEditorSwaggerPickerProps,
}
}
}
} else if (updateOption === UpdateOptions[2]) {
} else if (updateOption === UpdateOptions[3]) {
// No update command generation
res.options.update_by = "None"
}
Expand Down

0 comments on commit a4cb91b

Please sign in to comment.