From 68806b0ee1187d578c6615b6d16efffebcd3c27e Mon Sep 17 00:00:00 2001 From: Noah Canadea Date: Fri, 22 Dec 2023 15:11:25 +0000 Subject: [PATCH] feat(resource_options): Implement ignore_resource in provider handler. --- infrapatch/core/provider_handler.py | 25 ++++++++++++++------- infrapatch/core/provider_handler_builder.py | 5 ++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/infrapatch/core/provider_handler.py b/infrapatch/core/provider_handler.py index 851c677..0b6ec0c 100644 --- a/infrapatch/core/provider_handler.py +++ b/infrapatch/core/provider_handler.py @@ -1,4 +1,3 @@ -import json import logging as log from pathlib import Path from typing import Sequence, Union @@ -11,10 +10,13 @@ from infrapatch.core.models.statistics import ProviderStatistics, Statistics from infrapatch.core.models.versioned_resource import ResourceStatus, VersionedResource, VersionedResourceReleaseNotes from infrapatch.core.providers.base_provider_interface import BaseProviderInterface +from infrapatch.core.utils.options_processor import OptionsProcessorInterface class ProviderHandler: - def __init__(self, providers: Sequence[BaseProviderInterface], console: Console, statistics_file: Path, repo: Union[Repo, None] = None) -> None: + def __init__( + self, providers: Sequence[BaseProviderInterface], console: Console, statistics_file: Path, options_processor: OptionsProcessorInterface, repo: Union[Repo, None] = None + ) -> None: self.providers: dict[str, BaseProviderInterface] = {} for provider in providers: self.providers[provider.get_provider_name()] = provider @@ -23,19 +25,26 @@ def __init__(self, providers: Sequence[BaseProviderInterface], console: Console, self.console = console self.statistics_file = statistics_file self.repo = repo + self.options_processor = options_processor def get_resources(self, disable_cache: bool = False) -> dict[str, Sequence[VersionedResource]]: for provider_name, provider in self.providers.items(): if provider_name not in self._resource_cache: log.debug(f"Fetching resources for provider {provider.get_provider_name()} since cache is empty.") - self._resource_cache[provider.get_provider_name()] = provider.get_resources() - continue elif disable_cache: log.debug(f"Fetching resources for provider {provider.get_provider_name()} since cache is disabled.") - self._resource_cache[provider.get_provider_name()] = provider.get_resources() - continue else: log.debug(f"Using cached resources for provider {provider.get_provider_name()}.") + continue + resources = provider.get_resources() + for resource in resources: + self.options_processor.process_options_for_resource(resource) + ignored_resources = [resource for resource in resources if resource.options.ignore_resource] + un_ignored_resources = [resource for resource in resources if not resource.options.ignore_resource] + for resource in ignored_resources: + log.debug(f"Ignoring resource '{resource.name}' from provider {provider.get_provider_display_name()}since its marked as ignored.") + + self._resource_cache[provider.get_provider_name()] = un_ignored_resources return self._resource_cache def get_patched_resources(self) -> dict[str, Sequence[VersionedResource]]: @@ -127,9 +136,9 @@ def dump_statistics(self, disable_cache: bool = False): log.debug(f"Deleting existing statistics file {self.statistics_file.absolute().as_posix()}.") self.statistics_file.unlink() log.debug(f"Writing statistics to {self.statistics_file.absolute().as_posix()}.") - statistics_dict = self._get_statistics(disable_cache).to_dict() + statistics = self._get_statistics(disable_cache) with open(self.statistics_file, "w") as f: - f.write(json.dumps(statistics_dict)) + f.write(statistics.model_dump_json()) def print_statistics_table(self, disable_cache: bool = False): table = self._get_statistics(disable_cache).get_rich_table() diff --git a/infrapatch/core/provider_handler_builder.py b/infrapatch/core/provider_handler_builder.py index 0db5175..ae87611 100644 --- a/infrapatch/core/provider_handler_builder.py +++ b/infrapatch/core/provider_handler_builder.py @@ -12,6 +12,7 @@ import infrapatch.core.constants as const import infrapatch.core.constants as cs from infrapatch.core.provider_handler import ProviderHandler +from infrapatch.core.utils.options_processor import OptionsProcessor from infrapatch.core.utils.terraform.hcl_edit_cli import HclEditCli from infrapatch.core.utils.terraform.hcl_handler import HclHandler from infrapatch.core.utils.terraform.registry_handler import RegistryHandler @@ -61,4 +62,6 @@ def build(self) -> ProviderHandler: if len(self.providers) == 0: raise Exception("No providers added to ProviderHandlerBuilder.") statistics_file = self.working_directory.joinpath(f"{cs.APP_NAME}_Statistics.json") - return ProviderHandler(providers=self.providers, console=Console(width=const.CLI_WIDTH), statistics_file=statistics_file, repo=self.git_repo) + return ProviderHandler( + providers=self.providers, console=Console(width=const.CLI_WIDTH), options_processor=OptionsProcessor(), statistics_file=statistics_file, repo=self.git_repo + )