From f8f8a43c514d5cb8b3ca4868eccabdce3fd76b26 Mon Sep 17 00:00:00 2001 From: zghp Date: Tue, 11 Nov 2025 10:07:24 +0000 Subject: [PATCH] add logic for slot config --- src/xts_core/plugins/xts_allocator_client.py | 26 ++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/xts_core/plugins/xts_allocator_client.py b/src/xts_core/plugins/xts_allocator_client.py index 2234048..b90b605 100755 --- a/src/xts_core/plugins/xts_allocator_client.py +++ b/src/xts_core/plugins/xts_allocator_client.py @@ -76,7 +76,6 @@ def _send_request(method, url, data=None) -> dict: except requests.exceptions.RequestException as e: plugin_utils.error(f'Error during request: {e}') - @staticmethod def _format_slots_list_to_table(response:list[dict]) -> Table: '''Format the list of slot dictionaries into a rich table @@ -199,6 +198,7 @@ def _setup_allocator_args(self): add_slot_parser.add_argument('--rack-name', required=True, help='Rack name of the slot.', dest='rack_name') add_slot_parser.add_argument('--slot-name', required=True, help='Slot name.', dest='slot_name') add_slot_parser.add_argument('--state', choices=['free', 'allocated'], default='free', help='State of the slot.') + add_slot_parser.add_argument('--config', required=False, help='path to a YAML or JSON slot config file.') # update-slot update_slot_parser= allocator_subparsers.add_parser('update-slot', @@ -462,13 +462,31 @@ def _add_slot(self, owner_email:str|None, state:str='', description:str='', - tags:list[str]=[]): + tags:list[str]=[], + config: str|None=None): ''' Add a new slot to the allocator server. Args: args (list): Command-line arguments. ''' + + configuration = None + if config: + import json, yaml + try: + if config.endswith(('.yaml', '.yml')): + with open(config, 'r') as f: + configuration = yaml.safe_load(f) + elif config.endswith('.json'): + with open(config, 'r') as f: + configuration = json.load(f) + else: + plugin_utils.warning(f"Unsupported config format for file: {config}") + except Exception as e: + plugin_utils.error(f"Failed to load configuration file: {e}") + configuration = None + payload = { 'rackName': rack_name, 'slotName': slot_name, @@ -478,6 +496,10 @@ def _add_slot(self, 'state': state, 'owner_email': owner_email, } + + if configuration: + payload['configuration'] = configuration + response = self._send_request('POST', f'{server}/add_slot', payload) if response: plugin_utils.info(response.get('message'))