Skip to content
Closed
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
96 changes: 66 additions & 30 deletions base_external_system/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Base External System
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:538c9c995cdcc3f95d664975e426c7a54638c5a728cef8253014ef178d29ec3f
!! source digest: sha256:199072a1b536548cfc88a3f85e558966096b960920cc11d93bdcbabcefc66bf3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -28,13 +28,13 @@ Base External System

|badge1| |badge2| |badge3| |badge4| |badge5|

This module provides an interface/adapter mechanism for the definition of remote
systems.
This module provides an interface/adapter mechanism for the definition
of remote systems.

Note that this module stores everything in plain text. In the interest of security,
it is recommended you use another module (such as `keychain` or `red_october` to
encrypt things like the password and private key). This is not done here in order
to not force a specific security method.
Note that this module stores everything in plain text. In the interest
of security, it is recommended you use another module (such as keychain
or red_october to encrypt things like the password and private key).
This is not done here in order to not force a specific security method.

**Table of contents**

Expand All @@ -49,38 +49,61 @@ Configure external systems in Settings => Technical => External Systems
Usage
=====

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.
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:
Given an ``external.system`` singleton called ``external_system``, you
would do the following to get the underlying system client:

.. code-block:: python
.. code:: python

with external_system.client() as client:
client.do_something()

The client will be destroyed once the context has completed. Destruction takes place
in the adapter's ``external_destroy_client`` method.
The client will be destroyed once the context has completed. Destruction
takes place in the adapter's ``external_destroy_client`` method.

The only unified aspect of this interface is the client connection itself. Other more
opinionated interface/adapter mechanisms can be 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>`_.
The only unified aspect of this interface is the client connection
itself. Other more opinionated interface/adapter mechanisms can be
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 16.0.1.0.0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given all the major breaking changes done here, should they also be explained with a 16.0.2.0.0 changelog comment, with a rationale and a migration path? If someone had made custom dependent modules that add fields on base.external.system, this will now stop working and someone will need to redo the customization in a new way.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sorry I'm talking nonsense here, it's not base.external.system that went to become an AbstractModel. Still, I would appreciate an explanation why this is better, if only to be able to review better, and for users to understand.

------------------

Migration to 16.0

Version 16.0.2.0.0
------------------

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

If people have used this module "as is" to store connection data, no
changes will be needed. In case anybody has implemented a specific
interface, this would need to be changed to an abstract class that
inherits from external.system.adapter, using the example of
``external.system.adapter.os``.

Bug Tracker
===========
Expand All @@ -96,22 +119,27 @@ Credits
=======

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

* Alexandre Díaz
* César A. Sánchez
- Therp BV https://therp.nl:

- Ronald Portier ronald@therp.nl

- Tecnativa https://www.tecnativa.com:

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

Maintainers
~~~~~~~~~~~
-----------

This module is maintained by the OCA.

Expand All @@ -123,6 +151,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/16.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": "16.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",
],
}
9 changes: 6 additions & 3 deletions base_external_system/demo/external_system_os_demo.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?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>

</odoo>
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
93 changes: 66 additions & 27 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-2026 Therp BV.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from contextlib import contextmanager
Expand All @@ -16,8 +17,11 @@ class ExternalSystem(models.Model):
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.",
Expand All @@ -36,7 +40,10 @@ class ExternalSystem(models.Model):
)
private_key_password = fields.Text(
help="This is the password to unlock the private key that was "
"provided for this sytem.",
"provided for this system.",
)
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 "
Expand All @@ -58,15 +65,10 @@ 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."),
Expand All @@ -76,7 +78,16 @@ class ExternalSystem(models.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 @@ -101,24 +112,52 @@ 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(ExternalSystem, self).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)s",
exception=exc,
system=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