import argparse import sys import click from fansifter_common.adapters.aws.secretsmanager import SecretsManager from fansifter_common.m2m_token import M2MTokenManager from owsclient import OwsClient secrets_manager = SecretsManager(region_name="us-east-1") APP_CONNECTION_PLATFORM = ("meta", "tiktok", "shopify", "google") def create_ows_client(env: str) -> OwsClient: secret_key_name = f"{env}/lambda-jwt-refresh/jwt_token" secret_expiry_key_name = f"{env}/lambda-jwt-refresh/jwt_token_expiration" m2m_token_manager = M2MTokenManager( secrets_manager=secrets_manager, secret_name_key=secret_key_name, secret_expire_name_key=secret_expiry_key_name, ) return OwsClient( environment=env, service_name="ows-dmp", m2m_token_manager=m2m_token_manager, ) def sync_platform(ows_client: OwsClient, platform: str) -> bool: click.echo(f"Starting sync for platform '{platform}'") updated_connections: list[str] = [] has_errors = False try: response = ows_client.get( service_name="ows-dmp", path=f"/{platform}/fivetran-connectors", timeout=60, ) response.raise_for_status() connections = response.json() click.echo( f"Retrieved {len(connections)} connection(s) for platform '{platform}'" ) except Exception as exc: click.echo( f"Failed to get Fivetran connection ids for platform '{platform}': {exc}" ) return True for connection_id in connections: click.echo( "Triggering tables state sync for " f"platform '{platform}' connection '{connection_id}'" ) try: response = ows_client.post( service_name="ows-dmp", path=f"/{platform}/sync-fivetran-tables-state", timeout=300, json={"fivetranConnectorId": connection_id}, ) response.raise_for_status() except Exception as exc: click.echo( f"Failed to sync Fivetran tables state for platform '{platform}' " f"connection '{connection_id}': {exc}" ) has_errors = True else: updated_connections.append(connection_id) click.echo( f"Successfully synced tables state for platform '{platform}' " f"connection '{connection_id}'" ) if updated_connections: click.echo( f"Finished sync for platform '{platform}'. Updated connections: " f"{updated_connections}" ) else: click.echo( f"Finished sync for platform '{platform}'. No connections were updated" ) return has_errors def sync_fivetran_tables_state(env: str, platform: str | None = None) -> bool: ows_client = create_ows_client(env) platforms = [platform] if platform else APP_CONNECTION_PLATFORM has_errors = False for platform in platforms: has_errors = sync_platform(ows_client, platform) or has_errors return has_errors if __name__ == "__main__": parser = argparse.ArgumentParser(description="Sync OWS DMP Fivetran tables state") parser.add_argument( "-e", "--environment", type=str, help="Environment (qa, prod)", default="qa", choices=["qa", "prod"], ) parser.add_argument( "-p", "--platform", help="Fivetran platform name", choices=APP_CONNECTION_PLATFORM, ) args = parser.parse_args() if args.platform: click.echo(f"Syncing Fivetran tables state for single platform {args.platform}") has_errors = sync_fivetran_tables_state( env=args.environment, platform=args.platform, ) # Exit with error code if any sync operations failed if has_errors: click.echo("Sync completed with errors. Check logs above for details.") sys.exit(1) click.echo("Sync completed successfully.")