Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 56 additions & 11 deletions base_external_system/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Base External System
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:2a506ad3e94464097ed6d1a165abccb0f8151ad22ebfa20bf7aa521d308dff01
!! source digest: sha256:3e86769a6d488f4c2debd3cb5f076fa41da5b207404fab56d493718f66ba2473
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -50,15 +50,23 @@ Configuration

Configure external systems in Settings => Technical => External Systems

|Try me on Runbot|

.. |Try me on Runbot| image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:target: https://runbot.odoo-community.org/runbot/149/10.0

Usage
=====

Implementation
--------------

The credentials for systems are stored in the ``external.system`` model,
and are to be configured by the user. This model is the unified
interface for the underlying adapters.

Using the Interface
-------------------
~~~~~~~~~~~~~~~~~~~

Given an ``external.system`` singleton called ``external_system``, you
would do the following to get the underlying system client:
Expand All @@ -77,14 +85,38 @@ implemented in other modules, such as the file system interface in
`OCA/server-tools/external_file_location <https://github.com/OCA/server-tools/tree/9.0/external_file_location>`__.

Creating an Adapter
-------------------
~~~~~~~~~~~~~~~~~~~

Modules looking to add an external system adapter should inherit the
``external.system.adapter`` model and override the following methods:

- ``external_get_client``: Returns a usable client for the system
- ``external_destroy_client``: Destroy the connection, if applicable.
Does not need to be defined if the connection destroys itself.
- ``external_get_client``: Returns a usable client for the system
- ``external_destroy_client``: Destroy the connection, if applicable.
Does not need to be defined if the connection destroys itself.

Changelog
=========

Version 18.0.1.0.0
==================

Migration to 18.0

- All data will now be stored in the external.system model.
- Interface adapters can more easily be extended with functionality.
- Activated clients can be passed information from the main
external.system model.

Version 16.0.1.0.0
==================

Migration to 16.0

Version 10.0.2.0.0
------------------

Model base.external.system will be the only regular model, containing
all the data

Bug Tracker
===========
Expand All @@ -103,16 +135,21 @@ Authors
-------

* LasLabs
* Therp BV

Contributors
------------

- Dave Lasley <dave@laslabs.com>
- Ronald Portier <ronald@therp.nl>
- `Tecnativa <https://www.tecnativa.com>`__:
- Dave Lasley dave@laslabs.com

- Therp BV https://therp.nl:

- Ronald Portier ronald@therp.nl

- Alexandre Díaz
- César A. Sánchez
- Tecnativa https://www.tecnativa.com:

- Alexandre Díaz
- César A. Sánchez

Maintainers
-----------
Expand All @@ -127,6 +164,14 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

.. |maintainer-NL66278| image:: https://github.com/NL66278.png?size=40px
:target: https://github.com/NL66278
:alt: NL66278

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-NL66278|

This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/18.0/base_external_system>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 0 additions & 1 deletion base_external_system/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from . import models
5 changes: 4 additions & 1 deletion base_external_system/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2017 LasLabs Inc.
# Copyright 2024 Therp BV <https://therp.nl>.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

{
Expand All @@ -7,7 +8,8 @@
"version": "18.0.1.0.0",
"category": "Base",
"website": "https://github.com/OCA/server-backend",
"author": "LasLabs, " "Odoo Community Association (OCA)",
"author": "LasLabs, Therp BV, Odoo Community Association (OCA)",
"maintainers": ["NL66278"],
"license": "LGPL-3",
"application": False,
"installable": True,
Expand All @@ -16,5 +18,6 @@
"demo/external_system_os_demo.xml",
"security/ir.model.access.csv",
"views/external_system_view.xml",
"views/ir_ui_menu.xml",
],
}
7 changes: 4 additions & 3 deletions base_external_system/demo/external_system_os_demo.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2017 LasLabs Inc.
Copyright 2023 Therp BV.
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
-->
<odoo>
<record id="external_system_os" model="external.system.os">
<field name="name">Example OS Connection</field>
<field name="system_type">external.system.os</field>
<record id="external_system_os_demo" model="external.system">
<field name="name">Example OS Connection for Testing</field>
<field name="system_type">external.system.adapter.os</field>
<field name="remote_path">/tmp</field>
<field name="company_ids" eval="[(5, 0), (4, ref('base.main_company'))]" />
</record>
Expand Down
3 changes: 2 additions & 1 deletion base_external_system/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
from . import external_system
from . import external_system_adapter
from . import external_system_os
from . import external_system_adapter_os
131 changes: 86 additions & 45 deletions base_external_system/models/external_system.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2017 LasLabs Inc.
# Copyright 2023-2024 Therp BV.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from contextlib import contextmanager
Expand All @@ -12,42 +13,47 @@ class ExternalSystem(models.Model):
_description = "External System"

name = fields.Char(
required=True,
help="This is the canonical (humanized) name for the system.",
required=True, help="This is the canonical (humanized) name for the system."
)
scheme = fields.Char(
help="This is the protocol used to communicate with the system."
)
host = fields.Char(
help="This is the domain or IP address that the system can be reached " "at.",
help="This is the domain or IP address that the system can be reached at."
)
port = fields.Integer(
help="This is the port number that the system is listening on.",
help="This is the port number that the system is listening on."
)
username = fields.Char(
help="This is the username that is used for authenticating to this "
"system, if applicable.",
help="This is the username that is used for authenticating to this"
" system, if applicable."
)
password = fields.Char(
help="This is the password that is used for authenticating to this "
"system, if applicable.",
help="This is the password that is used for authenticating to this"
" system, if applicable."
)
private_key = fields.Text(
help="This is the private key that is used for authenticating to "
"this system, if applicable.",
help="This is the private key that is used for authenticating to"
" this system, if applicable."
)
private_key_password = fields.Text(
help="This is the password to unlock the private key that was "
"provided for this sytem.",
help="This is the password to unlock the private key that was"
" provided for this sytem."
)
private_key_thumbprint = fields.Text(
help="The thumbprint generated by AAD when you upload your public cert"
)
fingerprint = fields.Text(
help="This is the fingerprint that is advertised by this system in "
"order to validate its identity.",
help="This is the fingerprint that is advertised by this system in"
" order to validate its identity."
)
ignore_fingerprint = fields.Boolean(
default=True,
help="Set this to `True` in order to ignore an invalid/unknown "
"fingerprint from the system.",
help="Set this to `True` in order to ignore an invalid/unknown"
" fingerprint from the system.",
)
remote_path = fields.Char(
help="Restrict to this directory path on the remote, if applicable.",
help="Restrict to this directory path on the remote, if applicable."
)
company_ids = fields.Many2many(
string="Companies",
Expand All @@ -57,25 +63,29 @@ class ExternalSystem(models.Model):
help="Access to this system is restricted to these companies.",
)
system_type = fields.Selection(
selection="_get_system_types",
# Use lambda selection, otherwise subclasses loaded later will not be found.
selection=lambda self: self._get_system_types(),
required=True,
)
interface = fields.Reference(
selection="_get_system_types",
readonly=True,
help="This is the interface that this system represents. It is "
"created automatically upon creation of the external system.",
)

_sql_constraints = [
("name_uniq", "UNIQUE(name)", "Connection name must be unique."),
("name_uniq", "UNIQUE(name)", "Connection name must be unique.")
]

@api.model
def _get_system_types(self):
"""Return the adapter interface models that are installed."""
adapter = self.env["external.system.adapter"]
return [(m, self.env[m]._description) for m in adapter._inherit_children]
subclasses = set()
work = [adapter]
while work:
parent = work.pop()
for child in parent._inherit_children:
subclass = self.env[child]
if subclass not in subclasses:
subclasses.add(subclass)
work.append(subclass)
return [(m._name, m._description) for m in subclasses]

@api.constrains("fingerprint", "ignore_fingerprint")
def check_fingerprint_ignore_fingerprint(self):
Expand All @@ -84,8 +94,8 @@ def check_fingerprint_ignore_fingerprint(self):
if not record.ignore_fingerprint and not record.fingerprint:
raise ValidationError(
_(
"Fingerprint cannot be empty when Ignore Fingerprint is "
"not checked.",
"Fingerprint cannot be empty when Ignore Fingerprint is"
" not checked."
)
)

Expand All @@ -100,24 +110,55 @@ def client(self):
mixed: An object representing the client connection to the remote
system.
"""
with self.interface.client() as client:
self.ensure_one()
adapter = None
client = None
try:
adapter = self._get_adapter()
client = adapter.external_get_client()
yield client

@api.model_create_multi
def create(self, vals_list):
"""Create the interface for the record and assign to ``interface``."""
records = self.browse([])
for vals in vals_list:
record = super().create(vals)
if not self.env.context.get("no_create_interface"):
interface = self.env[vals["system_type"]].create(
{"system_id": record.id}
)
record.interface = interface
records += record
return records
finally:
if client:
adapter.external_destroy_client(client)

def action_test_connection(self):
"""Test the connection to the external system."""
"""Test the connection to the external system.

Any unexpected exception will be transformed into a
ValidationError. A ValidationError will also be raised
if no client is returned.
"""
self.ensure_one()
self.interface.external_test_connection()
try:
with self.client() as client:
if client is None:
raise ValidationError(
_("Client connection failed for system %s") % self.name
)
return True
except Exception as exc:
raise ValidationError(
_(
"Unexpected error %(exception)s"
" when connecting to %(system_name)s"
)
% {
"exception": exc,
"system_name": self.name,
}
) from None

def _get_adapter(self):
"""Trivial method to get adapter from system type.

Adding the system to the context allows the AbstractModel for the adapter,
that can not be instantiated, to still hold information, through a class
property, that can be accessed by all methods in derived classes.

An alternative would be to use standard python classes, but that would take
away the possibility to extend them in the Odoo way.

To even further extend the possibility of the adapter to have its own
runtime memory, we add an (at first) empty dictionary to the context.
"""
return self.with_context(system=self, adapter_memory={}).env[self.system_type]
Loading
Loading