Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add s3_bucket_exists on storage backend #1

Open
wants to merge 3 commits into
base: s3-improve-get-bucket
Choose a base branch
from

Conversation

ajaniszewska-dev
Copy link
Member

@ajaniszewska-dev ajaniszewska-dev commented May 30, 2022

specs:
1. remove global var
2. add flag s3_bucket_exists on the backend
3. btn action_ensure_bucket_exists to test if the bucket exists by calling _ensure_bucket_exists
4. modify _check_bucket_exists to accept a flag force=False
5. if the force flag is false and self.s3_bucket_exists is True return True
6. if test is ok, set the flag
7. if the bucket is not available, create it and set the flag
8. no more call to check if the bucket exists

@@ -84,3 +85,8 @@ def _selection_aws_region(self):
+ AWS_REGIONS
+ [("other", "Empty or Other (Manually specify below)")]
)


def action_ensure_bucket_exists(self):
Copy link
Member Author

@ajaniszewska-dev ajaniszewska-dev May 30, 2022

Choose a reason for hiding this comment

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

to clarify:

  1. btn action_ensure_bucket_exists to test if the bucket exists by calling _ensure_bucket_exists

I am not sure how do we want to test bucket connection here, I assumed that we want to start flow for getting bucket and forcing flag to True so we make sure call to head_bucket is made.

Copy link
Member

@simahawk simahawk May 30, 2022

Choose a reason for hiding this comment

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

TBH I forgot I added a generic way for defining validation of backends. Please check action_test_config in storage_backend -> it works if you define a validate_config method on the adapter.
Which means that we can:

  1. rename s3_bucket_exists to validated and move it to storage_backend and let action_test_config set it to True or False when needed (do this in its own commit pls)
  2. define validate_config and make it call _check_bucket_exists
  3. get rid of the custom btn

Copy link
Member Author

Choose a reason for hiding this comment

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

@simahawk just checking both modules, in storage_backend there is already a field 'has_validation'. Are we sure we want another field with basically same functionality there?

@ajaniszewska-dev ajaniszewska-dev force-pushed the s3-improve-get-bucket_add_flag branch 3 times, most recently from 7bc809f to 7c06954 Compare May 31, 2022 13:29
@codecov-commenter
Copy link

codecov-commenter commented May 31, 2022

Codecov Report

Merging #1 (122d400) into s3-improve-get-bucket (cc855ee) will decrease coverage by 0.16%.
The diff coverage is 54.54%.

@@                    Coverage Diff                    @@
##           s3-improve-get-bucket       #1      +/-   ##
=========================================================
- Coverage                  86.51%   86.34%   -0.17%     
=========================================================
  Files                         41       41              
  Lines                       1305     1311       +6     
=========================================================
+ Hits                        1129     1132       +3     
- Misses                       176      179       +3     
Impacted Files Coverage Δ
storage_backend_s3/components/s3_adapter.py 69.30% <44.44%> (-6.74%) ⬇️
storage_backend/models/storage_backend.py 80.20% <100.00%> (+0.20%) ⬆️
storage_media_product/models/product.py 87.09% <0.00%> (+16.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cc855ee...122d400. Read the comment docs.

@@ -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")
Copy link
Member

Choose a reason for hiding this comment

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

what is this? It should be a normal stored field, not computed :/

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")
Copy link
Member

Choose a reason for hiding this comment

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

pls rollback this

@@ -50,6 +46,7 @@ def _get_bucket(self):
s3 = boto3.resource("s3", **params)
bucket_name = self.collection.aws_bucket
bucket = self._get_or_create_bucket(s3, bucket_name, **params)
self.collection.validated = True
Copy link
Member

Choose a reason for hiding this comment

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

this writes on EVERY lookup for the bucket, pls drop it

try:
s3.meta.client.head_bucket(Bucket=bucket_name)
# The call above is expensive, avoid it when possible.
EXISTING_BUCKETS.append(bucket_name)
self.collection.validated = True
Copy link
Member

Choose a reason for hiding this comment

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

you are writing on this a second time because is done already on line 148.
Drop this and rely only on the flag that this method should return and update the flag only in validate_config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants