import json
import os
import subprocess

from dotenv import load_dotenv
from subprocess import check_output, Popen, PIPE

load_dotenv()

# Accessing variables.
CLIENT_ID = os.environ.get('CLIENT_ID')
CLIENT_SECRET = os.environ.get('CLIENT_SECRET')
USERNAME = os.environ.get('USERNAME')
BUCKET_NAME = os.environ.get('BUCKET_NAME')


def get_lambda_functions():
    function_dict = {}
    res = subprocess.Popen(
        ["aws", "lambda", "list-functions"],
        stdout=subprocess.PIPE
    )
    output = res.communicate()
    function_dict.update(json.loads(output[0]))

    return function_dict['Functions']


lambda_functions = get_lambda_functions()
for lambda_function in lambda_functions:
    function_name = lambda_function['FunctionName']

    subprocess.run([
        "aws", "lambda", "update-function-configuration",
        "--function-name", f"{function_name}", "--environment",
        f"Variables={{CLIENT_ID={CLIENT_ID},CLIENT_SECRET={CLIENT_SECRET},USERNAME={USERNAME},BUCKET_NAME={BUCKET_NAME}}}"
    ])

[(8, 'dotenv.load_dotenv', 'load_dotenv', ([], {}), False, 'from dotenv import load_dotenv\n'), (11, 'os.environ.get', 'os.environ.get', (['"""CLIENT_ID"""'], {}), False, 'import os\n'), (12, 'os.environ.get', 'os.environ.get', (['"""CLIENT_SECRET"""'], {}), False, 'import os\n'), (13, 'os.environ.get', 'os.environ.get', (['"""USERNAME"""'], {}), False, 'import os\n'), (14, 'os.environ.get', 'os.environ.get', (['"""BUCKET_NAME"""'], {}), False, 'import os\n'), (19, 'subprocess.Popen', 'subprocess.Popen', (["['aws', 'lambda', 'list-functions']"], {'stdout': 'subprocess.PIPE'}), False, 'import subprocess\n'), (33, 'subprocess.run', 'subprocess.run', (["['aws', 'lambda', 'update-function-configuration', '--function-name',\n    f'{function_name}', '--environment',\n    f'Variables={{CLIENT_ID={CLIENT_ID},CLIENT_SECRET={CLIENT_SECRET},USERNAME={USERNAME},BUCKET_NAME={BUCKET_NAME}}}'\n    ]"], {}), False, 'import subprocess\n'), (24, 'json.loads', 'json.loads', (['output[0]'], {}), False, 'import json\n')]