|
| 1 | +from typing import Dict |
| 2 | +from bumiworker.bumiworker.consts import ArchiveReason |
| 3 | +from bumiworker.bumiworker.modules.base import ArchiveBase |
| 4 | +from bumiworker.bumiworker.modules.recommendations.constants import ( |
| 5 | + IT_POSITIVE_STATUS, |
| 6 | +) |
| 7 | +from bumiworker.bumiworker.modules.recommendations.s3_intelligent_tiering import ( |
| 8 | + S3IntelligentTiering as S3IntelligentTieringRecommendation, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class S3IntelligentTiering(ArchiveBase, S3IntelligentTieringRecommendation): |
| 13 | + def __init__(self, *args, **kwargs): |
| 14 | + super().__init__(*args, **kwargs) |
| 15 | + self.reason_description_map[ |
| 16 | + ArchiveReason.RECOMMENDATION_APPLIED |
| 17 | + ] = "intelligent-tiering enabled" |
| 18 | + self.reason_description_map[ |
| 19 | + ArchiveReason.RECOMMENDATION_IRRELEVANT |
| 20 | + ] = "bucket no longer matches intelligent-tiering criteria" |
| 21 | + |
| 22 | + @property |
| 23 | + def supported_cloud_types(self): |
| 24 | + return list(self.SUPPORTED_CLOUD_TYPES) |
| 25 | + |
| 26 | + def _is_it_enabled(self, doc: Dict) -> bool: |
| 27 | + status = doc.get("it_status_bucket") |
| 28 | + if status is None: |
| 29 | + status = (doc.get("meta") or {}).get("it_status_bucket") |
| 30 | + if status is None: |
| 31 | + return False |
| 32 | + return str(status).lower() in IT_POSITIVE_STATUS |
| 33 | + |
| 34 | + def _get(self, previous_options, optimizations, cloud_accounts_map, **kwargs): |
| 35 | + current_options = self.get_options() |
| 36 | + current_excluded_pools = set( |
| 37 | + (current_options.get("excluded_pools") or {}).keys() |
| 38 | + ) |
| 39 | + current_skip_accounts = set( |
| 40 | + current_options.get("skip_cloud_accounts") or [] |
| 41 | + ) |
| 42 | + docs_cache: Dict[str, Dict[str, Dict]] = {} |
| 43 | + resources_collection = self.mongo_client.restapi.resources |
| 44 | + result = [] |
| 45 | + for optimization in optimizations: |
| 46 | + cloud_account_id = optimization["cloud_account_id"] |
| 47 | + if cloud_account_id not in cloud_accounts_map: |
| 48 | + self._set_reason_properties( |
| 49 | + optimization, ArchiveReason.CLOUD_ACCOUNT_DELETED |
| 50 | + ) |
| 51 | + result.append(optimization) |
| 52 | + continue |
| 53 | + if cloud_account_id in current_skip_accounts: |
| 54 | + self._set_reason_properties( |
| 55 | + optimization, ArchiveReason.OPTIONS_CHANGED |
| 56 | + ) |
| 57 | + result.append(optimization) |
| 58 | + continue |
| 59 | + account_docs = docs_cache.get(cloud_account_id) |
| 60 | + if account_docs is None: |
| 61 | + docs = self._aggregate_resources(cloud_account_id) |
| 62 | + account_docs = {doc["resource_id"]: doc for doc in docs} |
| 63 | + docs_cache[cloud_account_id] = account_docs |
| 64 | + bucket_doc = account_docs.get(optimization["resource_id"]) |
| 65 | + if not bucket_doc: |
| 66 | + resource = resources_collection.find_one( |
| 67 | + {"_id": optimization["resource_id"]} |
| 68 | + ) |
| 69 | + if resource and self._is_it_enabled(resource): |
| 70 | + reason = ArchiveReason.RECOMMENDATION_APPLIED |
| 71 | + elif resource: |
| 72 | + reason = ArchiveReason.RESOURCE_DELETED |
| 73 | + else: |
| 74 | + reason = ArchiveReason.RESOURCE_DELETED |
| 75 | + self._set_reason_properties(optimization, reason) |
| 76 | + result.append(optimization) |
| 77 | + continue |
| 78 | + if bucket_doc.get("pool_id") in current_excluded_pools: |
| 79 | + self._set_reason_properties( |
| 80 | + optimization, ArchiveReason.OPTIONS_CHANGED |
| 81 | + ) |
| 82 | + result.append(optimization) |
| 83 | + continue |
| 84 | + if self._is_it_enabled(bucket_doc): |
| 85 | + reason = ArchiveReason.RECOMMENDATION_APPLIED |
| 86 | + else: |
| 87 | + eval_res = self._candidate_and_saving(bucket_doc) |
| 88 | + if eval_res.get("is_candidate"): |
| 89 | + reason = ArchiveReason.OPTIONS_CHANGED |
| 90 | + else: |
| 91 | + reason = ArchiveReason.RECOMMENDATION_IRRELEVANT |
| 92 | + self._set_reason_properties(optimization, reason) |
| 93 | + result.append(optimization) |
| 94 | + return result |
| 95 | + |
| 96 | + |
| 97 | +def main(organization_id, config_client, created_at, **kwargs): |
| 98 | + return S3IntelligentTiering( |
| 99 | + organization_id, config_client, created_at |
| 100 | + ).get() |
0 commit comments