Source code for util.application_dependencies

import json
import logging.config
from json import JSONDecodeError

from google.cloud import bigquery
from google.oauth2.service_account import Credentials
from pymorri.util.config_reader_2 import ConfigReader2
from pymorri.util.ssm import SSM

from exceptions.configuration_exception import GoogleCredentialError, ConfigurationException
from exceptions.task_exception import NotExecutedError, ConfigTypeError


[docs]class ApplicationDependencies: not_found_message = "was not found in configuration retrieved from configuration service"
[docs] def __init__(self, application_credentials_name='storepick_edit_order', config_credentials_name='storepick_config_service'): """ Application Dependencies class Args: application_credentials_name (str): Default=storepick_edit_order name of parameter store on ssm for the app config_credentials_name (str): Default=storepick_config_service name of parameter store on ssm for config service """ self.configuration_credentials = None self.application_configuration = None self.application_credentials = None self.environment = None self.application_credentials_name = application_credentials_name self.config_credentials_name = config_credentials_name
[docs] def execute(self, environment="dev", name_of_configuration="storepick-edit-order-notification.json"): """ Method to load all the initialise the dependencies can raise many errors always use in try catch Args: environment (str): Default=dev The environment the application is running in name_of_configuration(str): Default=storepick-edit-order-notification.json The name of the configuration to be loaded from config service Returns: bool: True on successful execution """ self.configuration_credentials = self._get_config_ssm() self.application_credentials = self._get_app_ssm() self.environment = environment self.application_configuration = self.get_configuration_from_config_service(name_of_configuration) return True
def _get_app_ssm(self): """ Private method to fetch the ssm config for osp Returns: json: SSM Keystore """ ssm_credentials = SSM(self.application_credentials_name) return ssm_credentials.get_ssm_credentials() def _get_config_ssm(self): """ Private method to fetch the ssm config for config service Returns: json: SSM Keystore """ ssm_config = SSM(self.config_credentials_name) return ssm_config.get_ssm_credentials()
[docs] def get_configuration_from_config_service(self, name_of_configuration): """ Gets the configuration from the config service using the credentials created when executed Returns: json: Configuration from config service Raises: NotExecutedError: If the dependencies have not been initialised """ if self.configuration_credentials: config_service = ConfigReader2(self.configuration_credentials, self.environment) configuration = config_service.load_config(name_of_configuration) if isinstance(configuration, dict) is False: raise ConfigTypeError return configuration else: raise NotExecutedError
[docs] def get_google_big_query_client(self): """ Enables the big query client to be initialised and configured for use with the table from config Returns: google.cloud.bigquery.client.Client: Initialised bigquery client with credentials Raises: NotExecutedError: If the dependencies have not been initialised """ if self.application_configuration is None or self.application_credentials is None: raise NotExecutedError return self._create_big_query_client_with_credentials()
def _create_big_query_client_with_credentials(self): """ Initialises the big query client and configures it for use with the table from config Returns: google.cloud.bigquery.client.Client: Initialised bigquery client with credentials Raises: ConfigurationException: If google credentials are not found within the application credentials config GoogleCredentialError: If the google credentials found are invalid """ google_credential_name_with_env = "GOOGLE_CREDENTIALS_{env}".format(env=self.environment.upper()) google_service_account = self.application_credentials.get(google_credential_name_with_env) if google_service_account is None: raise ConfigurationException( "{variable} {default_message}".format(variable=google_credential_name_with_env, default_message=self.not_found_message)) project_id = google_service_account.get("project_id") if project_id is None: raise GoogleCredentialError("Credentials missing project_id") try: credentials = Credentials.from_service_account_info(google_service_account) return bigquery.Client(project=project_id, credentials=credentials, location="EU") except (JSONDecodeError, ValueError, AttributeError) as e: raise GoogleCredentialError( "Credentials are not valid json or missing attributes, error: {error}".format(error=e))
[docs] def get_default_logger(self): """ Enables the default/console logger to use within the app for logging processes Returns: logging.Logger: default/console logger """ logging_config = self._get_logging_configuration() logging.config.dictConfig(logging_config) logger = logging.getLogger("console") return logger
[docs] def get_monitoring_logger(self): """ Enables the monitoring logger to use within the app for logging monitoring processes Returns: logging.Logger: monitor logger """ logging_config = self._get_logging_configuration() logging.config.dictConfig(logging_config) monitor = logging.getLogger("monitoring") return monitor
def _get_logging_configuration(self): """ Finds the logging configuration settings from the application configuration Returns: json: Logging configuration from application config Raises: ConfigurationException: If logging config is not found within the application configuration """ logging_config = self.application_configuration.get("EON_LOGGING_CONFIG") if logging_config is None: raise ConfigurationException( "EON_LOGGING_CONFIG {default_message}".format(default_message=self.not_found_message)) return logging_config
[docs] def get_cut_off_tomorrow_data_set(self): """ Finds the bigquery data set name from the application configuration Returns: str: Name of the bigquery data set found in the config Raises: NotExecutedError: If dependencies have not been initialised ConfigurationException: If data set is not found within the config """ if self.application_configuration is None: raise NotExecutedError data_set = self.application_configuration.get("DATASET") if data_set is None: raise ConfigurationException( "DATASET {default_message}".format(default_message=self.not_found_message)) return data_set
[docs] def get_cut_off_tomorrow_table(self): """ Finds the bigquery table name from the application configuration Returns: str: Name of the bigquery table found in the config Raises: NotExecutedError: If dependencies have not been initialised ConfigurationException: If table is not found within the config """ if self.application_configuration is None: raise NotExecutedError table = self.application_configuration.get("TABLE") if table is None: raise ConfigurationException( "TABLE {default_message}".format(default_message=self.not_found_message)) return table
[docs] def get_application_credentials(self): """ Finds the ssm application credentials for osp Returns: json: The ssm key value pairs stored in aws for the application Raises: NotExecutedError: If dependencies have not been initialised """ if self.application_credentials is None: raise NotExecutedError return self.application_credentials
[docs] def get_report_table(self): """ Finds the bigquery report table name from the application configuration Returns: str: Name of the bigquery report table found in the config Raises: NotExecutedError: If dependencies have not been initialised ConfigurationException: If report table is not found within the config """ if self.application_configuration is None: raise NotExecutedError report_table = self.application_configuration.get("REPORT_TABLE") if report_table is None: raise ConfigurationException( "REPORT_TABLE {default_message}".format(default_message=self.not_found_message)) return report_table
[docs] def get_email_template_id(self): """ Finds the marketing email template id from the application configuration Returns: str: Template id of the marketing email Raises: NotExecutedError: If dependencies have not been initialised ConfigurationException: If template id is not found within the config """ if self.application_configuration is None: raise NotExecutedError template = self.application_configuration.get("EMAIL_TEMPLATE_ID") if template is None: raise ConfigurationException( "EMAIL_TEMPLATE_ID {default_message}".format(default_message=self.not_found_message)) return template
[docs] def get_sms_message(self): """ Finds the sms message to send from the application configuration Returns: str: message content to send by sms to the customer Raises: NotExecutedError: If dependencies have not been initialised ConfigurationException: If sms message is not found within the config """ if self.application_configuration is None: raise NotExecutedError sms_message = self.application_configuration.get("SMS_MESSAGE") if sms_message is None: raise ConfigurationException( "SMS_MESSAGE {default_message}".format(default_message=self.not_found_message)) return sms_message