Skip to content

Commit

Permalink
Merge PR #190 into 14.0-dev
Browse files Browse the repository at this point in the history
Signed-off-by ivs-cetmix
  • Loading branch information
CetmixGitBot committed Jan 22, 2025
2 parents b957088 + 597ed5e commit bc6c7d5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
58 changes: 52 additions & 6 deletions cetmix_tower_server/models/cx_tower_server_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions cetmix_tower_server/readme/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 = {
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions cetmix_tower_server/views/cx_tower_key_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
<group>
<group>
<field name="name" />
<field
name="reference"
attrs="{'invisible':[('key_type','=','k')]}"
/>
<field name="reference" widget="CopyClipboardChar" />
<field
name="reference_code"
widget="CopyClipboardChar"
Expand Down

0 comments on commit bc6c7d5

Please sign in to comment.