class LandscapeInstall:

    def __init__(self, opts, display_controller):
        self.config = Config()
        self.opts = opts
        self.display_controller = display_controller
        # Sets install type
        utils.spew(os.path.join(self.config.cfg_path,
                                'landscape'),
                   'auto-generated')

        self.landscape_tasks = ["Preparing Landscape",
                                "Deploying Landscape",
                                "Registering against Landscape"]

    def _do_install_existing_maas(self):
        """ Performs the landscape deployment with existing MAAS
        """
        MultiInstallExistingMaas(
            self.opts, self.display_controller,
            post_tasks=self.landscape_tasks).run()

    def _do_install_new_maas(self):
        """ Prepare new maas environment for landscape
        """
        MultiInstallNewMaas(self.opts, self.display_controller,
                            post_tasks=self.landscape_tasks).run()

    def _save_lds_creds(self, creds):
        admin_name = creds['admin_name'].value
        admin_email = creds['admin_email'].value
        system_email = creds['system_email'].value
        maas_server = creds['maas_server'].value
        maas_apikey = creds['maas_apikey'].value
        self.config.save_landscape_creds(
            admin_name, admin_email, system_email,
            maas_server, maas_apikey)

        self.display_controller.ui.hide_widget_on_top()
        self.display_controller.info_message("Running ..")
        if not maas_server:
            log.debug("No maas credentials entered, doing a new MAAS install")
            self._do_install_new_maas()
        else:
            log.debug("Existing MAAS defined, doing a LDS "
                      "installation with existing MAAS.")
            self.config.save_maas_creds(maas_server,
                                        maas_apikey)
            self._do_install_existing_maas()

    def run(self):
        self.display_controller.info_message(
            "Please enter your Landscape information and "
            "optionally an existing MAAS Server IP. If MAAS "
            "is not defined a new one will be created for you.")
        self.display_controller.show_landscape_input("Landscape Setup",
                                                     self._save_lds_creds)
class LandscapeInstall:

    def __init__(self, opts, display_controller):
        self.config = Config()
        self.opts = opts
        self.display_controller = display_controller
        # Sets install type
        utils.spew(os.path.join(self.config.cfg_path,
                                'landscape'),
                   'auto-generated')

        self.landscape_tasks = ["Preparing Landscape",
                                "Deploying Landscape",
                                "Registering against Landscape"]

    def _do_install_existing_maas(self):
        """ Performs the landscape deployment with existing MAAS
        """
        MultiInstallExistingMaas(
            self.opts, self.display_controller,
            post_tasks=self.landscape_tasks).run()

    def _do_install_new_maas(self):
        """ Prepare new maas environment for landscape
        """
        MultiInstallNewMaas(self.opts, self.display_controller,
                            post_tasks=self.landscape_tasks).run()

    def _save_lds_creds(self, creds):
        admin_name = creds['admin_name'].value
        admin_email = creds['admin_email'].value
        system_email = creds['admin_email'].value
        maas_server = creds['maas_server'].value
        maas_apikey = creds['maas_apikey'].value
        self.config.save_landscape_creds(
            admin_name, admin_email, system_email,
            maas_server, maas_apikey)

        self.display_controller.ui.hide_widget_on_top()

        # Validate
        if not maas_server:
            log.debug("No MAAS server entered.")
            self.display_controller.flash("Missing required MAAS Server")
            return self.run()

        if not maas_apikey:
            log.debug("No MAAS api key entered.")
            self.display_controller.flash("Missing required MAAS API Key")
            return self.run()

        # self.display_controller.flash_reset()
        log.debug("Existing MAAS defined, doing a LDS "
                  "installation with existing MAAS.")
        self.config.save_maas_creds(maas_server,
                                    maas_apikey)
        self._do_install_existing_maas()

    def run(self):
        self.display_controller.info_message(
            "Please enter your Landscape information and "
            "MAAS Server IP and API Key. Use the MAAS web UI or 'maas list' "
            "to find your API Key")
        self.display_controller.show_landscape_input(
            "Landscape OpenStack Autopilot Setup",
            self._save_lds_creds)