Skip to content
Merged
96 changes: 96 additions & 0 deletions base_external_system/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
.. image:: https://img.shields.io/badge/license-LGPL--3-blue.svg
:target: http://www.gnu.org/licenses/lgpl.html
:alt: License: LGPL-3

======================
Base - External System
======================

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.

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:

.. code-block:: 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 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.

Configuration
=============

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

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

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/server-tools/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

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

* Dave Lasley <dave@laslabs.com>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

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.

To contribute to this module, please visit https://odoo-community.org.
5 changes: 5 additions & 0 deletions base_external_system/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from . import models
23 changes: 23 additions & 0 deletions base_external_system/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

{
"name": "Base External System",
"summary": "Data models allowing for connection to external systems.",
"version": "10.0.1.0.0",
"category": "Base",
"website": "https://laslabs.com/",
"author": "LasLabs, Odoo Community Association (OCA)",
"license": "LGPL-3",
"application": False,
"installable": True,
'depends': [
'base',
],
'data': [
'demo/external_system_os_demo.xml',
'security/ir.model.access.csv',
'views/external_system_view.xml',
],
}
17 changes: 17 additions & 0 deletions base_external_system/demo/external_system_os_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Copyright 2017 LasLabs Inc.
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>
<field name="remote_path">/tmp</field>
<field name="company_ids" eval="[(5, 0), (4, ref('base.main_company'))]" />
</record>

</odoo>
4 changes: 4 additions & 0 deletions base_external_system/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
from . import external_system
from . import external_system_adapter
from . import external_system_os
125 changes: 125 additions & 0 deletions base_external_system/models/external_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from contextlib import contextmanager

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class ExternalSystem(models.Model):

_name = 'external.system'
_description = 'External System'

name = fields.Char(
required=True,
help='This is the canonical (humanized) name for the system.',
)
host = fields.Char(
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.',
)
username = fields.Char(
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.',
)
private_key = fields.Text(
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.',
)
fingerprint = fields.Text(
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.',
)
remote_path = fields.Char(
help='Restrict to this directory path on the remote, if applicable.',
)
company_ids = fields.Many2many(
string='Companies',
comodel_name='res.company',
required=True,
default=lambda s: [(6, 0, s.env.user.company_id.ids)],
help='Access to this system is restricted to these companies.',
)
system_type = fields.Selection(
selection='_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.'),
]

@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
]

@api.multi
@api.constrains('fingerprint', 'ignore_fingerprint')
def check_fingerprint_ignore_fingerprint(self):
"""Do not allow a blank fingerprint if not set to ignore."""
for record in self:
if not record.ignore_fingerprint and not record.fingerprint:
raise ValidationError(_(
'Fingerprint cannot be empty when Ignore Fingerprint is '
'not checked.',
))

@api.multi
@contextmanager
def client(self):
"""Client object usable as a context manager to include destruction.

Yields the result from ``external_get_client``, then calls
``external_destroy_client`` to cleanup the client.

Yields:
mixed: An object representing the client connection to the remote
system.
"""
with self.interface.client() as client:
yield client

@api.model
def create(self, vals):
"""Create the interface for the record and assign to ``interface``."""
record = super(ExternalSystem, self).create(vals)
interface = self.env[vals['system_type']].create({
'system_id': record.id,
})
record.interface = interface
return record

@api.multi
def action_test_connection(self):
"""Test the connection to the external system."""
self.ensure_one()
self.interface.external_test_connection()
71 changes: 71 additions & 0 deletions base_external_system/models/external_system_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from contextlib import contextmanager

from odoo import api, fields, models, _
from odoo.exceptions import UserError


class ExternalSystemAdapter(models.AbstractModel):
"""This is the model that should be inherited for new external systems.

Methods provided are prefixed with ``external_`` in order to keep from
"""

_name = 'external.system.adapter'
_description = 'External System Adapter'
_inherits = {'external.system': 'system_id'}

system_id = fields.Many2one(
string='System',
comodel_name='external.system',
required=True,
ondelete='cascade',
)

@api.multi
@contextmanager
def client(self):
"""Client object usable as a context manager to include destruction.

Yields the result from ``external_get_client``, then calls
``external_destroy_client`` to cleanup the client.

Yields:
mixed: An object representing the client connection to the remote
system.
"""
client = self.external_get_client()
try:
yield client
finally:
self.external_destroy_client(client)

@api.multi
def external_get_client(self):
"""Return a usable client representing the remote system."""
self.ensure_one()

@api.multi
def external_destroy_client(self, client):
"""Perform any logic necessary to destroy the client connection.

Args:
client (mixed): The client that was returned by
``external_get_client``.
"""
self.ensure_one()

@api.multi
def external_test_connection(self):
"""Adapters should override this method, then call super if valid.

If the connection is invalid, adapters should raise an instance of
``odoo.ValidationError``.

Raises:
odoo.exceptions.UserError: In the event of a good connection.
"""
raise UserError(_('The connection was a success.'))
43 changes: 43 additions & 0 deletions base_external_system/models/external_system_os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

import os

from odoo import api, models


class ExternalSystemOs(models.Model):
"""This is an Interface implementing the OS module.

For the most part, this is just a sample of how to implement an external
system interface. This is still a fully usable implementation, however.
"""

_name = 'external.system.os'
_inherit = 'external.system.adapter'
_description = 'External System OS'

previous_dir = None

@api.multi
def external_get_client(self):
"""Return a usable client representing the remote system."""
super(ExternalSystemOs, self).external_get_client()
if self.system_id.remote_path:
self.previous_dir = os.getcwd()
os.chdir(self.system_id.remote_path)
return os

@api.multi
def external_destroy_client(self, client):
"""Perform any logic necessary to destroy the client connection.

Args:
client (mixed): The client that was returned by
``external_get_client``.
"""
super(ExternalSystemOs, self).external_destroy_client(client)
if self.previous_dir:
os.chdir(self.previous_dir)
self.previous_dir = None
3 changes: 3 additions & 0 deletions base_external_system/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_external_system_os_admin,access_external_system_os_admin,model_external_system_os,base.group_system,1,1,1,1
access_external_system_admin,access_external_system_admin,model_external_system,base.group_system,1,1,1,1
Binary file added base_external_system/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading