Skip to content

Commit e692793

Browse files
authored
Merge branch 'dev' into fixing_timeout_on_aws
2 parents 018865f + 9754025 commit e692793

File tree

10 files changed

+138
-15528
lines changed

10 files changed

+138
-15528
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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()

bumiworker/bumiworker/modules/recommendations/s3_intelligent_tiering.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def _aggregate_resources(self, cloud_account_id: str) -> List[Dict[str, Any]]:
149149
{"$match": {
150150
"resource_type": "Bucket",
151151
"cloud_account_id": cloud_account_id,
152+
"active": True,
152153
"deleted_at": 0
153154
}},
154155
{"$project": {
@@ -160,6 +161,8 @@ def _aggregate_resources(self, cloud_account_id: str) -> List[Dict[str, Any]]:
160161
"tiers": "$meta.tiers",
161162
"object_count": "$meta.object_count",
162163
"pool_id": 1,
164+
"owner_id": "$owner_id",
165+
"employee_id": "$employee_id",
163166
"region": 1,
164167
"last_checked": "$meta.last_checked",
165168
"has_lifecycle": "$meta.has_lifecycle",
@@ -267,6 +270,8 @@ def get(self, **kwargs) -> List[Dict[str, Any]]:
267270
excluded_pools = set((options.get("excluded_pools") or {}).keys())
268271
skip_accounts = set(options.get("skip_cloud_accounts") or [])
269272
ca_names = self._cloud_account_names()
273+
employees = self.get_employees()
274+
pools = self.get_pools()
270275

271276
items: List[Dict[str, Any]] = []
272277
for ca in self.get_cloud_accounts():
@@ -293,9 +298,11 @@ def get(self, **kwargs) -> List[Dict[str, Any]]:
293298
"region": d.get("region"),
294299
"cloud_account_id": d.get("cloud_account_id"),
295300
"cloud_type": "aws_cnr",
296-
"owner": {"id": None, "name": None},
297-
"pool": {"id": d.get("pool_id"), "name": None, "purpose": None},
298-
"is_excluded": False,
301+
"owner": self._extract_owner(
302+
d.get("owner_id") or d.get("employee_id"), employees),
303+
"pool": self._extract_pool(
304+
d.get("pool_id"), pools),
305+
"is_excluded": d.get("pool_id") in excluded_pools,
299306
"is_with_intelligent_tiering": eval_res["is_with_it"],
300307
"detected_at": self.created_at,
301308
"cloud_account_name": ca_names.get(d.get("cloud_account_id")),

0 commit comments

Comments
 (0)