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, 0, 8, 13), 'dotenv.load_dotenv', 'load_dotenv', ({}, {}), '()', False, 'from dotenv import load_dotenv\n'), ((11, 12, 11, 39), 'os.environ.get', 'os.environ.get', ({(11, 27, 11, 38): '"""CLIENT_ID"""'}, {}), "('CLIENT_ID')", False, 'import os\n'), ((12, 16, 12, 47), 'os.environ.get', 'os.environ.get', ({(12, 31, 12, 46): '"""CLIENT_SECRET"""'}, {}), "('CLIENT_SECRET')", False, 'import os\n'), ((13, 11, 13, 37), 'os.environ.get', 'os.environ.get', ({(13, 26, 13, 36): '"""USERNAME"""'}, {}), "('USERNAME')", False, 'import os\n'), ((14, 14, 14, 43), 'os.environ.get', 'os.environ.get', ({(14, 29, 14, 42): '"""BUCKET_NAME"""'}, {}), "('BUCKET_NAME')", False, 'import os\n'), ((19, 10, 22, 5), 'subprocess.Popen', 'subprocess.Popen', (), '', False, 'import subprocess\n'), ((33, 4, 37, 6), 'subprocess.run', 'subprocess.run', ({(33, 19, 37, 5): "['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    ]"}, {}), "(['aws', 'lambda', 'update-function-configuration',\n    '--function-name', 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, 25, 24, 46), 'json.loads', 'json.loads', ({(24, 36, 24, 45): 'output[0]'}, {}), '(output[0])', False, 'import json\n')]