import json

from app import app
from app.lists.mirror_mapping import mirror_mapping
from app.lists.bc2 import mirror_sites
from app.lists.bridgelines import bridgelines
from app.models.base import MirrorList
from app.terraform import BaseAutomation


class ListAutomation(BaseAutomation):
    def generate_terraform(self):
        self.write_terraform_config(
            self.template,
            lists=MirrorList.query.filter(
                MirrorList.destroyed == None,
                MirrorList.provider == self.provider,
            ).all(),
            global_namespace=app.config['GLOBAL_NAMESPACE'],
            **{
                k: app.config[k.upper()]
                for k in self.template_parameters
            }
        )
        with open(self.working_directory('bc2.json'), 'w') as out:
            json.dump(mirror_sites(), out, indent=2, sort_keys=True)
        with open(self.working_directory('bca.json'), 'w') as out:
            json.dump(mirror_mapping(), out, indent=2, sort_keys=True)
        with open(self.working_directory('bridgelines.json'), 'w') as out:
            json.dump(bridgelines(), out, indent=2, sort_keys=True)
