Skip to content

Commit 542f37a

Browse files
committed
[REF] fs_storage: server_environment should not be a required dependency
1 parent 4d047fa commit 542f37a

8 files changed

Lines changed: 36 additions & 168 deletions

File tree

fs_attachment/tests/test_fs_storage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ def test_url_for_image_dir_optimized_and_not_obfuscated(self):
387387
{
388388
"name": "FS Product Image Backend",
389389
"code": "file",
390+
"protocol": "odoofs",
390391
"base_url": "https://localhost/images",
391392
"optimizes_directory_path": True,
392393
"use_filename_obfuscation": False,

fs_storage/README.rst

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ When you create a new backend, you must specify the following:
121121
* The protocol options. These are the options that will be passed to the
122122
fsspec python package when creating the filesystem. These options depend
123123
on the protocol used and are described in the fsspec documentation.
124-
* Resolve env vars. This options resolves the protocol options values starting
125-
with $ from environment variables
126124
* Check Connection Method. If set, Odoo will always check the connection before using
127125
a storage and it will remove the fs connection from the cache if the check fails.
128126

@@ -153,29 +151,6 @@ the protocol options as follows:
153151
In this example, the SimpleCacheFileSystem protocol will be used as a wrapper
154152
around the odoofs protocol.
155153

156-
Server Environment
157-
~~~~~~~~~~~~~~~~~~
158-
159-
To ease the management of the filesystem storages configuration accross the different
160-
environments, the configuration of the filesystem storages can be defined in
161-
environment files or directly in the main configuration file. For example, the
162-
configuration of a filesystem storage with the code `fsprod` can be provided in the
163-
main configuration file as follows:
164-
165-
.. code-block:: ini
166-
167-
[fs_storage.fsprod]
168-
protocol=s3
169-
options={"endpoint_url": "https://my_s3_server/", "key": "KEY", "secret": "SECRET"}
170-
directory_path=my_bucket
171-
172-
To work, a `storage.backend` record must exist with the code `fsprod` into the database.
173-
In your configuration section, you can specify the value for the following fields:
174-
175-
* `protocol`
176-
* `options`
177-
* `directory_path`
178-
179154
Migration from storage_backend
180155
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181156

fs_storage/__manifest__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
{
66
"name": "Filesystem Storage Backend",
77
"summary": "Implement the concept of Storage with amazon S3, sftp...",
8-
"version": "16.0.1.3.6",
8+
"version": "16.0.1.3.7",
99
"category": "FS Storage",
1010
"website": "https://github.com/OCA/storage",
1111
"author": " ACSONE SA/NV, Odoo Community Association (OCA)",
1212
"license": "LGPL-3",
1313
"development_status": "Beta",
1414
"installable": True,
15-
"depends": ["base", "base_sparse_field", "server_environment"],
15+
"depends": ["base", "base_sparse_field"],
1616
"data": [
1717
"views/fs_storage_view.xml",
1818
"security/ir.model.access.csv",

fs_storage/models/fs_storage.py

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def wrapper(self, *args, **kwargs):
8888

8989
class FSStorage(models.Model):
9090
_name = "fs.storage"
91-
_inherit = "server.env.mixin"
9291
_description = "FS Storage"
9392

9493
__slots__ = ("__fs", "__odoo_storage_path")
@@ -142,15 +141,6 @@ def __init__(self, env, ids=(), prefetch_ids=()):
142141
inverse="_inverse_json_options",
143142
)
144143

145-
eval_options_from_env = fields.Boolean(
146-
string="Resolve env vars",
147-
help="""Resolve options values starting with $ from environment variables. e.g
148-
{
149-
"endpoint_url": "$AWS_ENDPOINT_URL",
150-
}
151-
""",
152-
)
153-
154144
directory_path = fields.Char(
155145
help="Relative path to the directory to store the file"
156146
)
@@ -191,24 +181,13 @@ def __init__(self, env, ids=(), prefetch_ids=()):
191181
),
192182
]
193183

194-
_server_env_section_name_field = "code"
195-
196184
@api.model
197185
def _get_check_connection_method_selection(self):
198186
return [
199187
("marker_file", _("Create Marker file")),
200188
("ls", _("List File")),
201189
]
202190

203-
@property
204-
def _server_env_fields(self):
205-
return {
206-
"protocol": {},
207-
"options": {},
208-
"directory_path": {},
209-
"eval_options_from_env": {},
210-
}
211-
212191
@api.model_create_multi
213192
@prevent_call_from_safe_eval("create")
214193
def create(self, vals_list):
@@ -406,32 +385,10 @@ def _recursive_add_odoo_storage_path(self, options: dict) -> dict:
406385
self._recursive_add_odoo_storage_path(target_options)
407386
return options
408387

409-
def _eval_options_from_env(self, options):
410-
values = {}
411-
for key, value in options.items():
412-
if isinstance(value, dict):
413-
values[key] = self._eval_options_from_env(value)
414-
elif isinstance(value, str) and value.startswith("$"):
415-
env_variable_name = value[1:]
416-
env_variable_value = os.getenv(env_variable_name)
417-
if env_variable_value is not None:
418-
values[key] = env_variable_value
419-
else:
420-
values[key] = value
421-
_logger.warning(
422-
"Environment variable %s is not set for fs_storage %s.",
423-
env_variable_name,
424-
self.display_name,
425-
)
426-
else:
427-
values[key] = value
428-
return values
429-
430388
def _get_fs_options(self):
431-
options = self.json_options
432-
if not self.eval_options_from_env:
433-
return options
434-
return self._eval_options_from_env(self.json_options)
389+
# We need this hook to be able to override
390+
# the options in the dependent modules
391+
return self.json_options
435392

436393
def _get_filesystem(self) -> fsspec.AbstractFileSystem:
437394
"""Get the fsspec filesystem for this backend.

fs_storage/readme/USAGE.rst

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ When you create a new backend, you must specify the following:
1515
* The protocol options. These are the options that will be passed to the
1616
fsspec python package when creating the filesystem. These options depend
1717
on the protocol used and are described in the fsspec documentation.
18-
* Resolve env vars. This options resolves the protocol options values starting
19-
with $ from environment variables
2018
* Check Connection Method. If set, Odoo will always check the connection before using
2119
a storage and it will remove the fs connection from the cache if the check fails.
2220

@@ -47,29 +45,6 @@ the protocol options as follows:
4745
In this example, the SimpleCacheFileSystem protocol will be used as a wrapper
4846
around the odoofs protocol.
4947

50-
Server Environment
51-
~~~~~~~~~~~~~~~~~~
52-
53-
To ease the management of the filesystem storages configuration accross the different
54-
environments, the configuration of the filesystem storages can be defined in
55-
environment files or directly in the main configuration file. For example, the
56-
configuration of a filesystem storage with the code `fsprod` can be provided in the
57-
main configuration file as follows:
58-
59-
.. code-block:: ini
60-
61-
[fs_storage.fsprod]
62-
protocol=s3
63-
options={"endpoint_url": "https://my_s3_server/", "key": "KEY", "secret": "SECRET"}
64-
directory_path=my_bucket
65-
66-
To work, a `storage.backend` record must exist with the code `fsprod` into the database.
67-
In your configuration section, you can specify the value for the following fields:
68-
69-
* `protocol`
70-
* `options`
71-
* `directory_path`
72-
7348
Migration from storage_backend
7449
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7550

fs_storage/static/description/index.html

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -438,23 +438,22 @@ <h1>Filesystem Storage Backend</h1>
438438
<ul class="simple">
439439
<li><a class="reference internal" href="#usage" id="toc-entry-1">Usage</a><ul>
440440
<li><a class="reference internal" href="#configuration" id="toc-entry-2">Configuration</a></li>
441-
<li><a class="reference internal" href="#server-environment" id="toc-entry-3">Server Environment</a></li>
442-
<li><a class="reference internal" href="#migration-from-storage-backend" id="toc-entry-4">Migration from storage_backend</a></li>
441+
<li><a class="reference internal" href="#migration-from-storage-backend" id="toc-entry-3">Migration from storage_backend</a></li>
443442
</ul>
444443
</li>
445-
<li><a class="reference internal" href="#known-issues-roadmap" id="toc-entry-5">Known issues / Roadmap</a></li>
446-
<li><a class="reference internal" href="#changelog" id="toc-entry-6">Changelog</a><ul>
447-
<li><a class="reference internal" href="#section-1" id="toc-entry-7">16.0.1.2.0 (2024-02-06)</a></li>
448-
<li><a class="reference internal" href="#section-2" id="toc-entry-8">16.0.1.1.0 (2023-12-22)</a></li>
449-
<li><a class="reference internal" href="#section-3" id="toc-entry-9">16.0.1.0.3 (2023-10-17)</a></li>
450-
<li><a class="reference internal" href="#section-4" id="toc-entry-10">16.0.1.0.2 (2023-10-09)</a></li>
444+
<li><a class="reference internal" href="#known-issues-roadmap" id="toc-entry-4">Known issues / Roadmap</a></li>
445+
<li><a class="reference internal" href="#changelog" id="toc-entry-5">Changelog</a><ul>
446+
<li><a class="reference internal" href="#section-1" id="toc-entry-6">16.0.1.2.0 (2024-02-06)</a></li>
447+
<li><a class="reference internal" href="#section-2" id="toc-entry-7">16.0.1.1.0 (2023-12-22)</a></li>
448+
<li><a class="reference internal" href="#section-3" id="toc-entry-8">16.0.1.0.3 (2023-10-17)</a></li>
449+
<li><a class="reference internal" href="#section-4" id="toc-entry-9">16.0.1.0.2 (2023-10-09)</a></li>
451450
</ul>
452451
</li>
453-
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-11">Bug Tracker</a></li>
454-
<li><a class="reference internal" href="#credits" id="toc-entry-12">Credits</a><ul>
455-
<li><a class="reference internal" href="#authors" id="toc-entry-13">Authors</a></li>
456-
<li><a class="reference internal" href="#contributors" id="toc-entry-14">Contributors</a></li>
457-
<li><a class="reference internal" href="#maintainers" id="toc-entry-15">Maintainers</a></li>
452+
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-10">Bug Tracker</a></li>
453+
<li><a class="reference internal" href="#credits" id="toc-entry-11">Credits</a><ul>
454+
<li><a class="reference internal" href="#authors" id="toc-entry-12">Authors</a></li>
455+
<li><a class="reference internal" href="#contributors" id="toc-entry-13">Contributors</a></li>
456+
<li><a class="reference internal" href="#maintainers" id="toc-entry-14">Maintainers</a></li>
458457
</ul>
459458
</li>
460459
</ul>
@@ -477,8 +476,6 @@ <h3><a class="toc-backref" href="#toc-entry-2">Configuration</a></h3>
477476
<li>The protocol options. These are the options that will be passed to the
478477
fsspec python package when creating the filesystem. These options depend
479478
on the protocol used and are described in the fsspec documentation.</li>
480-
<li>Resolve env vars. This options resolves the protocol options values starting
481-
with $ from environment variables</li>
482479
<li>Check Connection Method. If set, Odoo will always check the connection before using
483480
a storage and it will remove the fs connection from the cache if the check fails.<ul>
484481
<li><tt class="docutils literal">Create Marker file</tt> : create a hidden file on remote and then check it exists with
@@ -507,29 +504,8 @@ <h3><a class="toc-backref" href="#toc-entry-2">Configuration</a></h3>
507504
<p>In this example, the SimpleCacheFileSystem protocol will be used as a wrapper
508505
around the odoofs protocol.</p>
509506
</div>
510-
<div class="section" id="server-environment">
511-
<h3><a class="toc-backref" href="#toc-entry-3">Server Environment</a></h3>
512-
<p>To ease the management of the filesystem storages configuration accross the different
513-
environments, the configuration of the filesystem storages can be defined in
514-
environment files or directly in the main configuration file. For example, the
515-
configuration of a filesystem storage with the code <cite>fsprod</cite> can be provided in the
516-
main configuration file as follows:</p>
517-
<pre class="code ini literal-block">
518-
<span class="k">[fs_storage.fsprod]</span><span class="w">
519-
</span><span class="na">protocol</span><span class="o">=</span><span class="s">s3</span><span class="w">
520-
</span><span class="na">options={&quot;endpoint_url&quot;</span><span class="o">:</span><span class="w"> </span><span class="s">&quot;https://my_s3_server/&quot;</span><span class="na">, &quot;key&quot;</span><span class="o">:</span><span class="w"> </span><span class="s">&quot;KEY&quot;</span><span class="na">, &quot;secret&quot;</span><span class="o">:</span><span class="w"> </span><span class="s">&quot;SECRET&quot;</span><span class="na">}</span><span class="w">
521-
</span><span class="na">directory_path</span><span class="o">=</span><span class="s">my_bucket</span>
522-
</pre>
523-
<p>To work, a <cite>storage.backend</cite> record must exist with the code <cite>fsprod</cite> into the database.
524-
In your configuration section, you can specify the value for the following fields:</p>
525-
<ul class="simple">
526-
<li><cite>protocol</cite></li>
527-
<li><cite>options</cite></li>
528-
<li><cite>directory_path</cite></li>
529-
</ul>
530-
</div>
531507
<div class="section" id="migration-from-storage-backend">
532-
<h3><a class="toc-backref" href="#toc-entry-4">Migration from storage_backend</a></h3>
508+
<h3><a class="toc-backref" href="#toc-entry-3">Migration from storage_backend</a></h3>
533509
<p>The fs_storage addon can be used to replace the storage_backend addon. (It has
534510
been designed to be a drop-in replacement for the storage_backend addon). To
535511
ease the migration, the <cite>fs.storage</cite> model defines the high-level methods
@@ -552,7 +528,7 @@ <h3><a class="toc-backref" href="#toc-entry-4">Migration from storage_backend</a
552528
</div>
553529
</div>
554530
<div class="section" id="known-issues-roadmap">
555-
<h2><a class="toc-backref" href="#toc-entry-5">Known issues / Roadmap</a></h2>
531+
<h2><a class="toc-backref" href="#toc-entry-4">Known issues / Roadmap</a></h2>
556532
<ul class="simple">
557533
<li>Transactions: fsspec comes with a transactional mechanism that once started,
558534
gathers all the files created during the transaction, and if the transaction
@@ -566,30 +542,30 @@ <h2><a class="toc-backref" href="#toc-entry-5">Known issues / Roadmap</a></h2>
566542
</ul>
567543
</div>
568544
<div class="section" id="changelog">
569-
<h2><a class="toc-backref" href="#toc-entry-6">Changelog</a></h2>
545+
<h2><a class="toc-backref" href="#toc-entry-5">Changelog</a></h2>
570546
<div class="section" id="section-1">
571-
<h3><a class="toc-backref" href="#toc-entry-7">16.0.1.2.0 (2024-02-06)</a></h3>
547+
<h3><a class="toc-backref" href="#toc-entry-6">16.0.1.2.0 (2024-02-06)</a></h3>
572548
<p><strong>Features</strong></p>
573549
<ul class="simple">
574550
<li>Invalidate FS filesystem object cache when the connection fails, forcing a reconnection. (<a class="reference external" href="https://github.com/OCA/storage/issues/320">#320</a>)</li>
575551
</ul>
576552
</div>
577553
<div class="section" id="section-2">
578-
<h3><a class="toc-backref" href="#toc-entry-8">16.0.1.1.0 (2023-12-22)</a></h3>
554+
<h3><a class="toc-backref" href="#toc-entry-7">16.0.1.1.0 (2023-12-22)</a></h3>
579555
<p><strong>Features</strong></p>
580556
<ul class="simple">
581557
<li>Add parameter on storage backend to resolve protocol options values starting with $ from environment variables (<a class="reference external" href="https://github.com/OCA/storage/issues/303">#303</a>)</li>
582558
</ul>
583559
</div>
584560
<div class="section" id="section-3">
585-
<h3><a class="toc-backref" href="#toc-entry-9">16.0.1.0.3 (2023-10-17)</a></h3>
561+
<h3><a class="toc-backref" href="#toc-entry-8">16.0.1.0.3 (2023-10-17)</a></h3>
586562
<p><strong>Bugfixes</strong></p>
587563
<ul class="simple">
588564
<li>Fix access to technical models to be able to upload attachments for users with basic access (<a class="reference external" href="https://github.com/OCA/storage/issues/289">#289</a>)</li>
589565
</ul>
590566
</div>
591567
<div class="section" id="section-4">
592-
<h3><a class="toc-backref" href="#toc-entry-10">16.0.1.0.2 (2023-10-09)</a></h3>
568+
<h3><a class="toc-backref" href="#toc-entry-9">16.0.1.0.2 (2023-10-09)</a></h3>
593569
<p><strong>Bugfixes</strong></p>
594570
<ul class="simple">
595571
<li>Avoid config error when using the webdav protocol. The auth option is expected
@@ -600,30 +576,30 @@ <h3><a class="toc-backref" href="#toc-entry-10">16.0.1.0.2 (2023-10-09)</a></h3>
600576
</div>
601577
</div>
602578
<div class="section" id="bug-tracker">
603-
<h2><a class="toc-backref" href="#toc-entry-11">Bug Tracker</a></h2>
579+
<h2><a class="toc-backref" href="#toc-entry-10">Bug Tracker</a></h2>
604580
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/storage/issues">GitHub Issues</a>.
605581
In case of trouble, please check there if your issue has already been reported.
606582
If you spotted it first, help us to smash it by providing a detailed and welcomed
607583
<a class="reference external" href="https://github.com/OCA/storage/issues/new?body=module:%20fs_storage%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
608584
<p>Do not contact contributors directly about support or help with technical issues.</p>
609585
</div>
610586
<div class="section" id="credits">
611-
<h2><a class="toc-backref" href="#toc-entry-12">Credits</a></h2>
587+
<h2><a class="toc-backref" href="#toc-entry-11">Credits</a></h2>
612588
<div class="section" id="authors">
613-
<h3><a class="toc-backref" href="#toc-entry-13">Authors</a></h3>
589+
<h3><a class="toc-backref" href="#toc-entry-12">Authors</a></h3>
614590
<ul class="simple">
615591
<li>ACSONE SA/NV</li>
616592
</ul>
617593
</div>
618594
<div class="section" id="contributors">
619-
<h3><a class="toc-backref" href="#toc-entry-14">Contributors</a></h3>
595+
<h3><a class="toc-backref" href="#toc-entry-13">Contributors</a></h3>
620596
<ul class="simple">
621597
<li>Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt;</li>
622598
<li>Sébastien BEAU &lt;<a class="reference external" href="mailto:sebastien.beau&#64;akretion.com">sebastien.beau&#64;akretion.com</a>&gt;</li>
623599
</ul>
624600
</div>
625601
<div class="section" id="maintainers">
626-
<h3><a class="toc-backref" href="#toc-entry-15">Maintainers</a></h3>
602+
<h3><a class="toc-backref" href="#toc-entry-14">Maintainers</a></h3>
627603
<p>This module is maintained by the OCA.</p>
628604
<a class="reference external image-reference" href="https://odoo-community.org">
629605
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />

fs_storage/tests/test_fs_storage.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Copyright 2023 ACSONE SA/NV (http://acsone.eu).
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
33
import warnings
4-
from unittest import mock
54

65
from odoo.exceptions import ValidationError
76
from odoo.tests import Form
@@ -70,7 +69,12 @@ def test_ensure_one_fs_by_record(self):
7069
for i in range(4):
7170
backend_ids.append(
7271
self.backend.create(
73-
{"name": f"name{i}", "directory_path": f"{i}", "code": f"code{i}"}
72+
{
73+
"name": f"name{i}",
74+
"directory_path": f"{i}",
75+
"code": f"code{i}",
76+
"protocol": "odoofs",
77+
}
7478
).id
7579
)
7680
records = self.backend.browse(backend_ids)
@@ -133,25 +137,6 @@ def test_interface_values(self):
133137
# this is still true after saving
134138
self.assertEqual(new_storage.options_protocol, protocol)
135139

136-
def test_options_env(self):
137-
self.backend.json_options = {"key": {"sub_key": "$KEY_VAR"}}
138-
eval_json_options = {"key": {"sub_key": "TEST"}}
139-
options = self.backend._get_fs_options()
140-
self.assertDictEqual(options, self.backend.json_options)
141-
self.backend.eval_options_from_env = True
142-
with mock.patch.dict("os.environ", {"KEY_VAR": "TEST"}):
143-
options = self.backend._get_fs_options()
144-
self.assertDictEqual(options, eval_json_options)
145-
with self.assertLogs(level="WARNING") as log:
146-
options = self.backend._get_fs_options()
147-
self.assertIn(
148-
(
149-
f"Environment variable KEY_VAR is not set for "
150-
f"fs_storage {self.backend.display_name}."
151-
),
152-
log.output[0],
153-
)
154-
155140
def test_no_create_in_safe_eval(self):
156141
# check that we can't create a file in safe_eval
157142
with self.assertRaisesRegex(

0 commit comments

Comments
 (0)