""" This is a python function to call getsecret for Security Hub to Snow """

import base64
import json
import boto3
from botocore.exceptions import ClientError

def get_secret(secret_name, region_name):
    """ This a function to call getsecret """
    session = boto3.session.Session()
    results = dict()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )
    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as exception:
        if exception.response['Error']['Code'] == 'DecryptionFailureException':
            raise exception
        if exception.response['Error']['Code'] == 'InternalServiceErrorException':
            raise exception
        if exception.response['Error']['Code'] == 'InvalidParameterException':
            raise exception
        if exception.response['Error']['Code'] == 'InvalidRequestException':
            raise exception
        if exception.response['Error']['Code'] == 'ResourceNotFoundException':
            raise exception

    else:
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
            j = json.loads(secret)
            results["username"] = j['username']
            results["password"] = j['password']
        else:
            decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
            results["username"] = decoded_binary_secret.username
            results["password"] = decoded_binary_secret.password
    return results
