diff --git a/cetmix_tower_server/models/cx_tower_server_template.py b/cetmix_tower_server/models/cx_tower_server_template.py index 54487437..88066fb0 100644 --- a/cetmix_tower_server/models/cx_tower_server_template.py +++ b/cetmix_tower_server/models/cx_tower_server_template.py @@ -162,9 +162,7 @@ def create_server_from_template(self, template_reference, server_name, **kwargs) ipv6 (Char, optional): IP v6 address. Must be provided in case IP v4 is not. Defaults to None. ssh_password (Char, optional): SSH password. Defaults to None. - ssh_private_key_value (Char, optional): SSH private key content. - Defaults to None. - ssh_private_key_value (cx.tower.key(), optional): SSH private key record. + ssh_key (Char, optional): SSH private key record reference. Defaults to None. configuration_variables (Dict, optional): Custom configuration variable. Following format is used: @@ -190,9 +188,7 @@ def _create_new_server(self, name, **kwargs): ipv6 (Char, optional): IP v6 address. Must be provided in case IP v4 is not. Defaults to None. ssh_password (Char, optional): SSH password. Defaults to None. - ssh_private_key_value (Char, optional): SSH private key content. - Defaults to None. - ssh_private_key_value (cx.tower.key(), optional): SSH private key record. + ssh_key (Char, optional): SSH private key record reference. Defaults to None. configuration_variables (Dict, optional): Custom configuration variable. Following format is used: @@ -276,6 +272,9 @@ def _prepare_server_values(self, **kwargs): # read all values required to create a new server from the template vals_list = self.read(self._get_fields_tower_server(), load=False) + # prepare server config values from kwargs + server_config_values = self._parse_server_config_values(kwargs) + # process each template record for values in vals_list: template = self.browse(values["id"]) @@ -375,9 +374,56 @@ def _prepare_server_values(self, **kwargs): del values["id"] # update the values with additional arguments from kwargs values.update(kwargs) + # update server configs + values.update(server_config_values) return vals_list + def _parse_server_config_values(self, config_values): + """ + Prepares server configuration values. + + Args: + config_values (dict): A dictionary containing server configuration values. + Keys and their expected values: + - partner (res.partner, optional): The partner this server + belongs to. + - ipv4 (str, optional): IPv4 address. Defaults to None. + - ipv6 (str, optional): IPv6 address. Must be provided if IPv4 is + not specified. Defaults to None. + - ssh_key (str, optional): Reference to an SSH private key record. + Defaults to None. + + Returns: + dict: A dictionary containing parsed server configuration values with the + following keys: + - partner_id (int, optional): ID of the partner. + - ssh_key_id (int, optional): ID of the associated SSH key. + - ip_v4_address (str, optional): Parsed IPv4 address. + - ip_v6_address (str, optional): Parsed IPv6 address. + """ + values = {} + + partner = config_values.pop("partner", None) + if partner: + values["partner_id"] = partner.id + + ssh_key_reference = config_values.pop("ssh_key", None) + if ssh_key_reference: + ssh_key = self.env["cx.tower.key"].get_by_reference(ssh_key_reference) + if ssh_key: + values["ssh_key_id"] = ssh_key.id + + ipv4 = config_values.pop("ipv4", None) + if ipv4: + values["ip_v4_address"] = ipv4 + + ipv6 = config_values.pop("ipv6", None) + if ipv6: + values["ip_v6_address"] = ipv6 + + return values + def _validate_required_variables(self, configuration_variables): """ Validate that all required variables are present, not empty, diff --git a/cetmix_tower_server/readme/USAGE.md b/cetmix_tower_server/readme/USAGE.md index 9a2c17fc..6d738e45 100644 --- a/cetmix_tower_server/readme/USAGE.md +++ b/cetmix_tower_server/readme/USAGE.md @@ -18,8 +18,7 @@ This function takes the following arguments: - ipv4 (Char, optional): IP v4 address. Defaults to None. - ipv6 (Char, optional): IP v6 address. Must be provided in case IP v4 is not. Defaults to None. - ssh_password (Char, optional): SSH password. Defaults to None. Defaults to None. - - ssh_private_key_value (Char, optional): SSH private key content. - - ssh_private_key_value (cx.tower.key(), optional): SSH private key record. Defaults to None. + - ssh_key (Char, optional): SSH private key record reference. Defaults to None. - configuration_variables (Dict, optional): Custom configuration variable. Following format is used: 'variable_reference': 'variable_value_char' @@ -33,7 +32,7 @@ Here is a short example of an Odoo automated action that creates a new server wh ```python for record in records: - + # Check confirmed orders if record.state == "sale": params = { @@ -46,14 +45,14 @@ for record in records: "odoo_version": "16.0" }, } - - # Create a new server from template with the 'demo_template' reference + + # Create a new server from template with the 'demo_template' reference env["cetmix.tower"].server_create_from_template( template_reference="demo_template", server_name=record.name, **params ) - + ``` ## Run a Command diff --git a/cetmix_tower_server/views/cx_tower_key_view.xml b/cetmix_tower_server/views/cx_tower_key_view.xml index 81a41efe..ce22f1f4 100644 --- a/cetmix_tower_server/views/cx_tower_key_view.xml +++ b/cetmix_tower_server/views/cx_tower_key_view.xml @@ -10,10 +10,7 @@ - +