Skip to content

Commit

Permalink
feat(resource_options): Implement ignore_resource in provider handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Noahnc committed Dec 22, 2023
1 parent bcdc06b commit 68806b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
25 changes: 17 additions & 8 deletions infrapatch/core/provider_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging as log
from pathlib import Path
from typing import Sequence, Union
Expand All @@ -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
Expand All @@ -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]]:
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion infrapatch/core/provider_handler_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)

0 comments on commit 68806b0

Please sign in to comment.