Skip to content

Commit

Permalink
fixup! Add validation for s3
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaniszewska-dev committed May 31, 2022
1 parent 3e0f851 commit 122d400
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 36 deletions.
5 changes: 3 additions & 2 deletions storage_backend/models/storage_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ class StorageBackend(models.Model):
help="Relative path to the directory to store the file"
)
has_validation = fields.Boolean(compute="_compute_has_validation")

validated = fields.Boolean(string="Validated",compute="_compute_has_validation")

def _compute_has_validation(self):
for rec in self:
adapter = self._get_adapter()
rec.has_validation = hasattr(adapter, "validate_config")
rec.validated = rec.has_validation = hasattr(adapter, "validate_config")

@property
def _server_env_fields(self):
Expand Down
4 changes: 2 additions & 2 deletions storage_backend_s3/components/s3_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _get_or_create_bucket(self, s3, bucket_name, **params):
else:
create_params = self._get_create_bucket_params(bucket_name, params)
bucket = s3.create_bucket(**create_params)
self.collection.s3_bucket_exists = True
self.collection.validated = True
return bucket

def _check_bucket_exists(self, s3, bucket_name, force=False):
Expand All @@ -67,7 +67,7 @@ def _check_bucket_exists(self, s3, bucket_name, force=False):
try:
s3.meta.client.head_bucket(Bucket=bucket_name)
# The call above is expensive, avoid it when possible.
self.collection.s3_bucket_exists = True
self.collection.validated = True
return True
except ClientError as e:
# If a client error is thrown, then check that it was a 404 error.
Expand Down
33 changes: 1 addition & 32 deletions storage_backend_s3/models/storage_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging

from odoo import _, fields, models
from odoo import fields, models

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -43,7 +43,6 @@ class StorageBackend(models.Model):
"eg: Exoscale",
)
aws_bucket = fields.Char(string="Bucket")
validated = fields.Boolean(string="Bucket exists", compute="_compute_s3_validated")
aws_access_key_id = fields.Char(string="Access Key ID")
aws_secret_access_key = fields.Char(string="Secret Access Key")
aws_region = fields.Selection(selection="_selection_aws_region", string="Region")
Expand All @@ -62,12 +61,6 @@ class StorageBackend(models.Model):
]
)

def _compute_s3_validated(self):
for rec in self:
if rec.backend_type == "amazon_s3":
adapter = self._get_adapter()
rec.validated = hasattr(adapter, "validate_config")

@property
def _server_env_fields(self):
env_fields = super()._server_env_fields
Expand All @@ -91,27 +84,3 @@ def _selection_aws_region(self):
+ AWS_REGIONS
+ [("other", "Empty or Other (Manually specify below)")]
)

def action_test_config(self):
if not self.validated:
raise AttributeError("Validation not supported!")
adapter = self._get_adapter()
try:
adapter.validate_config()
title = _("Connection Test Succeeded!")
message = _("Everything seems properly set up!")
msg_type = "success"
except Exception as err:
title = _("Connection Test Failed!")
message = str(err)
msg_type = "danger"
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"title": title,
"message": message,
"type": msg_type,
"sticky": False,
},
}

0 comments on commit 122d400

Please sign in to comment.