Skip to content

Commit

Permalink
never use suppress without logging/feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Jan 3, 2024
1 parent 6243003 commit fc6e3c3
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 23 deletions.
3 changes: 1 addition & 2 deletions plugins/aws/resoto_plugin_aws/resource/acm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from contextlib import suppress
from datetime import datetime
from typing import ClassVar, Dict, Optional, List, Type

Expand Down Expand Up @@ -128,7 +127,7 @@ class AwsAcmCertificate(AwsResource):
@classmethod
def collect_resources(cls: Type[AwsResource], builder: GraphBuilder) -> None:
def fetch_certificate(arn: str) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.describe-certificate"):
if res := builder.client.get(service_name, "describe-certificate", "Certificate", CertificateArn=arn):
AwsAcmCertificate.collect([res], builder)

Expand Down
5 changes: 4 additions & 1 deletion plugins/aws/resoto_plugin_aws/resource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
ModelReference,
)
from resotolib.config import Config, current_config
from resotolib.core.actions import CoreFeedback
from resotolib.core.actions import CoreFeedback, SuppressWithFeedback
from resotolib.graph import ByNodeId, BySearchCriteria, EdgeKey, Graph, NodeSelector
from resotolib.json import from_json, value_in_path
from resotolib.json_bender import Bender, bend
Expand Down Expand Up @@ -411,6 +411,9 @@ def __init__(
self.metrics_start = start
self.metrics_delta = delta

def suppress(self, message: str) -> SuppressWithFeedback:
return SuppressWithFeedback(message, self.core_feedback, log)

def submit_work(self, service: str, fn: Callable[..., T], *args: Any, **kwargs: Any) -> Future[T]:
"""
Use this method for work that can be done in parallel.
Expand Down
3 changes: 1 addition & 2 deletions plugins/aws/resoto_plugin_aws/resource/ecr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
from contextlib import suppress
from typing import ClassVar, Dict, Optional, List, Type

from attrs import define, field
Expand Down Expand Up @@ -52,7 +51,7 @@ class AwsEcrRepository(AwsResource):
@classmethod
def collect_resources(cls, builder: GraphBuilder) -> None:
def fetch_lifecycle_policy(repository: AwsEcrRepository) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-lifecycle-policy"):
if policy := builder.client.get(
service_name,
"get-lifecycle-policy",
Expand Down
3 changes: 1 addition & 2 deletions plugins/aws/resoto_plugin_aws/resource/efs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from contextlib import suppress
from typing import Optional, ClassVar, Dict, List, Type

import math
Expand Down Expand Up @@ -139,7 +138,7 @@ def collect_mount_points(fs: AwsEfsFileSystem) -> None:
builder.add_edge(fs, node=mt)

def fetch_file_system_policy(fs: AwsEfsFileSystem) -> None:
with suppress(Exception):
with builder.suppress("describe-file-system-policy"):
if policy := builder.client.get(
service_name,
"describe-file-system-policy",
Expand Down
3 changes: 1 addition & 2 deletions plugins/aws/resoto_plugin_aws/resource/kms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from contextlib import suppress
from typing import ClassVar, Dict, List, Optional, Type
from attrs import define, field
from resoto_plugin_aws.aws_client import AwsClient
Expand Down Expand Up @@ -135,7 +134,7 @@ def add_instance(key: Dict[str, str]) -> None:
builder.submit_work(service_name, add_rotation_status, instance)

def add_rotation_status(key: AwsKmsKey) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-key-rotation-status"):
key.kms_key_rotation_enabled = builder.client.get( # type: ignore
service_name, "get-key-rotation-status", result_name="KeyRotationEnabled", KeyId=key.id
)
Expand Down
15 changes: 8 additions & 7 deletions plugins/aws/resoto_plugin_aws/resource/s3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from contextlib import suppress
import logging
from json import loads as json_loads
from typing import ClassVar, Dict, List, Type, Optional, cast, Any

Expand All @@ -15,6 +15,7 @@
from resotolib.types import Json

service_name = "s3"
log = logging.getLogger("resoto.plugins.aws")


@define(eq=False, slots=False)
Expand Down Expand Up @@ -201,7 +202,7 @@ def add_tags(bucket: AwsS3Bucket) -> None:
bucket.tags = cast(Dict[str, Optional[str]], tags)

def add_bucket_encryption(bck: AwsS3Bucket) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-bucket-encryption"):
bck.bucket_encryption_rules = []
for raw in builder.client.list(
service_name,
Expand All @@ -215,7 +216,7 @@ def add_bucket_encryption(bck: AwsS3Bucket) -> None:
bck.bucket_encryption_rules.append(rule)

def add_bucket_policy(bck: AwsS3Bucket) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-bucket-policy"):
if raw_policy := builder.client.get(
service_name,
"get-bucket-policy",
Expand All @@ -226,7 +227,7 @@ def add_bucket_policy(bck: AwsS3Bucket) -> None:
bck.bucket_policy = json_loads(raw_policy) # type: ignore # this is a string

def add_bucket_versioning(bck: AwsS3Bucket) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-bucket-versioning"):
if raw_versioning := builder.client.get(
service_name, "get-bucket-versioning", None, Bucket=bck.name, expected_errors=["NoSuchBucket"]
):
Expand All @@ -237,7 +238,7 @@ def add_bucket_versioning(bck: AwsS3Bucket) -> None:
bck.bucket_mfa_delete = False

def add_public_access(bck: AwsS3Bucket) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-public-access-block"):
if raw_access := builder.client.get(
service_name,
"get-public-access-block",
Expand All @@ -251,15 +252,15 @@ def add_public_access(bck: AwsS3Bucket) -> None:
)

def add_acls(bck: AwsS3Bucket) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-bucket-acl"):
if raw := builder.client.get(
service_name, "get-bucket-acl", Bucket=bck.name, expected_errors=["NoSuchBucket"]
):
mapped = bend(AwsS3BucketAcl.mapping, raw)
bck.bucket_acl = parse_json(mapped, AwsS3BucketAcl, builder)

def add_bucket_logging(bck: AwsS3Bucket) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-bucket-logging"):
if raw := builder.client.get(
service_name,
"get-bucket-logging",
Expand Down
3 changes: 1 addition & 2 deletions plugins/aws/resoto_plugin_aws/resource/ssm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
from contextlib import suppress
from datetime import datetime
from typing import ClassVar, Dict, Optional, List, Type

Expand Down Expand Up @@ -214,7 +213,7 @@ class AwsSSMDocument(AwsResource):
@classmethod
def collect_resources(cls, builder: GraphBuilder) -> None:
def collect_document(name: str) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.describe-document"):
js = builder.client.get(service_name, "describe-document", "Document", Name=name)
doc = builder.client.get(service_name, "get-document", Name=name)
share = builder.client.get(
Expand Down
6 changes: 3 additions & 3 deletions plugins/aws/resoto_plugin_aws/resource/waf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import logging
from contextlib import suppress
from typing import ClassVar, Dict, Optional, List, Type

from attrs import define, field
Expand Down Expand Up @@ -847,17 +846,18 @@ class AwsWafWebACL(AwsResource):
@classmethod
def collect_resources(cls: Type[AwsResource], builder: GraphBuilder) -> None:
def fetch_acl_resources(acl: AwsWafWebACL) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.list-resources-for-web-acl"):
acl._associated_resources = builder.client.list(
service_name, "list-resources-for-web-acl", "ResourceArns", WebACLArn=acl.arn
)

def fetch_logging_configuration(acl: AwsWafWebACL) -> None:
with suppress(Exception):
with builder.suppress(f"{service_name}.get-logging-configuration"):
if logging_configuration := builder.client.get(
aws_service=service_name,
action="get-logging-configuration",
result_name="LoggingConfiguration",
expected_errors=["WAFNonexistentItemException"],
ResourceArn=acl.arn,
):
acl.logging_configuration = parse_json(
Expand Down
15 changes: 13 additions & 2 deletions resotolib/resotolib/core/actions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import threading
import time
from contextlib import suppress
from contextlib import suppress, AbstractContextManager
from logging import Logger
from queue import Queue

Expand Down Expand Up @@ -56,7 +56,7 @@ def info(self, message: str, logger: Optional[Logger] = None) -> None:

def error(self, message: str, logger: Optional[Logger] = None) -> None:
if logger:
logger.error(self.context_str + message)
logger.error(self.context_str + message, exc_info=True)
self._info_message("error", message)

@property
Expand Down Expand Up @@ -84,6 +84,17 @@ def child_context(self, *context: str) -> "CoreFeedback":
return self.with_context(*(self.context + list(context)))


class SuppressWithFeedback(AbstractContextManager):
def __init__(self, message: str, feedback: CoreFeedback, logger: Optional[Logger] = None) -> None:
self.message = message
self.feedback = feedback
self.logger = logger

def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
if exc_type is not None:
self.feedback.error(f"{self.message}: {exc_val}", self.logger)


class CoreActions(threading.Thread):
def __init__(
self,
Expand Down

0 comments on commit fc6e3c3

Please sign in to comment.