import petname

from conjureup import async, controllers, juju, utils
from conjureup.app_config import app
from conjureup.controllers.clouds.common import list_clouds
from conjureup.ui.views.cloud import CloudView


class CloudsController:

    def __init__(self):
        self.view = None

    def __handle_exception(self, exc):
        utils.pollinate(app.session_id, "E004")
        app.ui.show_exception_message(exc)

    def __add_model(self):
        juju.add_model(app.current_model, app.current_controller)

    def finish(self, cloud):
        """ Load the Model controller passing along the selected cloud.

        Arguments:
        cloud: Cloud to create the controller/model on.
        """
        utils.pollinate(app.session_id, 'CS')
        existing_controller = juju.get_controller_in_cloud(cloud)

        if existing_controller is None:
            return controllers.use('newcloud').render(cloud)

        app.current_controller = existing_controller
        app.current_model = petname.Name()
        async.submit(self.__add_model,
                     self.__handle_exception,
                     queue_name=juju.JUJU_ASYNC_QUEUE)

        return controllers.use('deploy').render()

    def render(self):
        clouds = list_clouds()
        excerpt = app.config.get(
            'description',
            "Please select from a list of available clouds")
        view = CloudView(app,
                         clouds,
                         self.finish)

        app.ui.set_header(
            title="Choose a Cloud",
            excerpt=excerpt
        )
        app.ui.set_body(view)
        app.ui.set_footer('Please press [ENTER] on highlighted '
                          'Cloud to proceed.')


_controller_class = CloudsController

@@ -1,20 +1,4 @@
-import petname%0A%0A
 from
@@ -18,15 +18,8 @@
 port
- async,
  con
@@ -30,21 +30,8 @@
 lers
-, juju, utils
 %0Afro
@@ -260,36 +260,26 @@
 def 
-__handle_exception
+finish
 (self, 
-exc
+cloud
 ):%0A 
@@ -289,285 +289,149 @@
     
-utils.pollinate(app.session_id, %22E004%22)%0A        app.ui.show_exception_message(exc)%0A%0A    def __add_model(self):%0A        juju.add_model(app.current_model, app.current_controller)%0A%0A    def finish(self, cloud):%0A        %22%22%22 Load the Model controller passing along the selected cloud
+%22%22%22Show the 'newcloud' screen to enter credentials for a new%0A        controller on 'cloud'.  There will not be an existing%0A        controller
 .%0A%0A 
@@ -504,16 +504,17 @@
 del on.%0A
+%0A
         
@@ -529,524 +529,145 @@
     
-utils.pollinate(app.session_id, 'CS')%0A        existing_controller = juju.get_controller_in_cloud(cloud)%0A%0A        if existing_controller is None:%0A            return controllers.use('newcloud').render(cloud)%0A%0A        app.current_controller = existing_controller%0A        app.current_model = petname.Name()%0A        async.submit(self.__add_model,%0A                     self.__handle_exception,%0A                     queue_name=juju.JUJU_ASYNC_QUEUE)%0A%0A        return controllers.use('deploy').render()%0A%0A    def render(self):
+return controllers.use('newcloud').render(cloud)%0A%0A    def render(self):%0A        %22Pick or create a cloud to bootstrap a new controller on%22
 %0A   

Simplify clouds gui controller