Custom Search
Showing posts with label Horizon Dev Tips. Show all posts
Showing posts with label Horizon Dev Tips. Show all posts

Monday, May 26, 2014

OpenStack Horizon Dashboard How to enable IdentityPanels group and Users Panel to Project Dashboard

1)
Horizon "Admin" Dashboard and "Identity" Panel Group
------------------------------------------------------

vim ./openstack_dashboard/dashboards/admin/dashboard.py
https://github.com/openstack/horizon/blob/master/openstack_dashboard/dashboards/admin/dashboard.py

#Identity Panel group
class IdentityPanels(horizon.PanelGroup): 
    slug = "identity"
    name = _("Identity Panel")
    ##Panels of "Identity" Panel group.
    panels = ('domains', 'projects', 'users', 'groups', 'roles')


#Admin Dashboard
class Admin(horizon.Dashboard):
    name = _("Admin")
    slug = "admin"

    ##panel groups of "Admin" dashboard.
    panels = (SystemPanels, IdentityPanels)
    default_panel = 'overview'
    ##In havana we can't set multiple role here, only first one will takes.
    permissions = ('openstack.roles.admin',)

#Register "Admin" Dashboard
horizon.register(Admin)




2)
Horizon "Project" Dashboard
------------------------------------------------------

vim ./openstack_dashboard/dashboards/project/dashboard.py
https://github.com/openstack/horizon/blob/master/openstack_dashboard/dashboards/project/dashboard.py

#Dashboard
class Project(horizon.Dashboard):
    name = _("Project")
    slug = "project"

    ##Panel groups of "Project" dashboard.
    panels = (
        BasePanels,
        NetworkPanels,
        ObjectStorePanels,
        OrchestrationPanels,
        DatabasePanels,)
    default_panel = 'overview'
    supports_tenants = True


#Register "Project" Dashboard
horizon.register(Project)



3)
How to bring "Identity" panel group in "Project" dashboard.
-----------------------------------------------------------


vim ./openstack_dashboard/dashboards/project/dashboard.py
https://github.com/openstack/horizon/blob/master/openstack_dashboard/dashboards/project/dashboard.py

##Add new "Identity" panel group
class IdentityPanels(horizon.PanelGroup):
    slug = "identity"
    name = _("Identity Panel")

    ##panels of "Identity" panel group
    panels = ('domains', 'projects', 'users', 'groups', 'roles')

##Include new "Identity" panel group in "Project" dashboard
class Project(horizon.Dashboard):
    name = _("Project")
    slug = "project"
    panels = (
        BasePanels,
        NetworkPanels,
        ObjectStorePanels,
        OrchestrationPanels,
        DatabasePanels,
        ##New "Identity" panel group.
        IdentityPanels)
    default_panel = 'overview'
    supports_tenants = True


#Register "Project" Dashboard
horizon.register(Project)

4)
Base classes of horizon.Dashboard, horizon.PanelGroup and horizon.Panel
------------------------------------------------------------------------

vim ./horizon/base.py
https://github.com/openstack/horizon/blob/master/horizon/base.py
 

class Dashboard(Registry, HorizonComponent):

5)
"Users" panel of "Identity" panel group.
How to enable/add "Users" panel in "Project" Dashboard.
---------------------------------------------------------

vim ./openstack_dashboard/dashboards/admin/users/panel.py
https://github.com/openstack/horizon/blob/master/openstack_dashboard/dashboards/admin/users/panel.py

from openstack_dashboard.dashboards.admin import dashboard
from openstack_dashboard.dashboards.project import dashboard as project_dashboard

class Users(horizon.Panel):
    name = _("Users")
    slug = 'users'

    ##uncomment line "permissions = ('openstack.roles.admin',)",
    ##If we want to show this panel only for admin user.
    ##We can also set multiple roles, then that will work like "AND".
    #permissions = ('openstack.roles.admin',)

##Register "Users" panel to "Admin" Dashboard
dashboard.Admin.register(Users)
##Register "Users" panel to "Project" Dashboard
project_dashboard.Project.register(Users)




6)
Create new panel "ProjectUsers" for "Project" Dashboard
So we can apply separate permissions for that
---------------------------------------------------------


https://github.com/openstack/horizon/blob/master/openstack_dashboard/dashboards/admin/users/panel.py

import horizon

from openstack_dashboard.dashboards.admin import dashboard
from openstack_dashboard.dashboards.project import dashboard as project_dashboard

class Users(horizon.Panel):
    name = _("Users")
    slug = 'users'
    permissions = ('openstack.roles.admin',)


##New panel

class ProjectUsers(horizon.Panel):
    name = _("Users")
    slug = 'users'
    permissions = ('openstack.roles.tenantadmin1', 'openstack.roles.admin')


dashboard.Admin.register(Users)
project_dashboard.Project.register(ProjectUsers)


keystone CLI to assign multiple roles to a user
-------------------------------------------------


#keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin role-create --name tenantadmin1

##This command will create a new user and automatically add the role "_member_" to that user.
#keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin user-create --name saju5 --tenant 4da17230c6d24b7795d120943cbfd05c --pass saju5 --enabled true

##This command will add a particular role to user
#keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin user-role-add --user cf86e3655cc940c3ae0388a0e0a1b2dc --role ff198db3fd7b4c0ca4804e97f9d8ddb3 --tenant 4da17230c6d24b7795d120943cbfd05c

7)
Optional
---------

keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin role-create --name tenantadmin1

keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin role-list

keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin tenant-list

keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin tenant-create --name mytenant

##This command will create a new user and automatically add the role "_member_" to that user.
keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin user-create --name saju5 --tenant 4da17230c6d24b7795d120943cbfd05c --pass saju5 --enabled true

##This command will add a particular role to user
keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin user-role-add --user cf86e3655cc940c3ae0388a0e0a1b2dc --role ff198db3fd7b4c0ca4804e97f9d8ddb3 --tenant 4da17230c6d24b7795d120943cbfd05c

8)
Optional
---------

$ vim /etc/keystone/policy.json
 "admin_required": [["role:admin"], ["is_admin:1"], ["role:tenantadmin1"]],


Thursday, May 22, 2014

OpenStack Horizon dashboard How choice field or dropdown get populated

How flavor choice field or dropdown of create instance UI get populated

1)
https://github.com/openstack/horizon/blob/master/openstack_dashboard/dashboards/project/instances/workflows/create_instance.py

class SetInstanceDetailsAction(workflows.Action):
    flavor = forms.ChoiceField(label=_("Flavor"), help_text=_("Size of image to launch."))

    def populate_flavor_choices(self, request, context):
        ----
        return flavor_list

2)
https://github.com/openstack/horizon/blob/master/horizon/workflows/base.py

class Action(forms.Form):

    def __init__(self, request, context, *args, **kwargs):
        self._populate_choices(request, context)
       
    def _populate_choices(self, request, context):
        for field_name, bound_field in self.fields.items():
            meth = getattr(self, "populate_%s_choices" % field_name, None)
            if meth is not None and callable(meth):
                bound_field.choices = meth(request, context)



Wednesday, May 21, 2014

OpenStack Horizon Enable Identity Dashboard for non admin user

http://docs.openstack.org/developer/keystone/architecture.html#approach-to-authorization-policy

http://docs.openstack.org/developer/keystone/configuration.html#keystone-api-protection-with-role-based-access-control-rbac <===

http://www.mirantis.com/blog/manage-openstack-projects-using-domains-havana/ <===

1)
* List all Roles

#keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin role-list

2)
* Create a new role named "test-role1"

#keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin role-create --name test-role1

* The name of the role can't be arbitrary/random. The "test-role1" role has been set in /etc/keystone/policy.json.
http://my.safaribooksonline.com/book/operating-systems-and-server-administration/virtualization/9781849517324/3dot-keystone-openstack-identity-service/id286763411

3)
* List all Roles

#keystone --os-username admin --os-password nova --os-auth-url http://localhost:5000/v2.0/ --os-tenant-name admin role-list

4)
vim /etc/keystone/policy.json







5)
At the moment the Identity panel is only available for users with the admin role, once https://blueprints.launchpad.net/horizon/+spec/separate-identity-dash is implemented in Juno it will be possible to allow more finer grained permissions






Tuesday, March 25, 2014

OpenStack Horizon How to Prevent popup from closing when you click outside popup

1)
There are two files

horizon/static/bootstrap/js/bootstrap.js <=== for dev
https://github.com/openstack/horizon/blob/6954c02f4dc1d3ee98c56cc2a53512b4b625478a/horizon/static/bootstrap/js/bootstrap.js
and
horizon/static/bootstrap/js/bootstrap.min.js <=== for production (compressed)
https://github.com/openstack/horizon/blob/6954c02f4dc1d3ee98c56cc2a53512b4b625478a/horizon/static/bootstrap/js/bootstrap.min.js

bootstrap.js is not included in any html file
bootstrap.min.js is included in horizon/templates/horizon/_scripts.html

2)
http://getbootstrap.com/javascript/

Make following changes in bootstrap.js

Change

  $.fn.modal.defaults = {
      backdrop: true
    , keyboard: true
    , show: true
  }

To

  $.fn.modal.defaults = {
      backdrop: 'static'
    , keyboard: true
    , show: true
  } 

3)
http://jscompress.com/
http://stackoverflow.com/questions/9123112/twitter-bootstrap-make-from-source

* Compress the modified bootstrap.js and create bootstrap.min.js
* From the "makefile", find which compression tool was used to create bootstrap.min.js : https://github.com/twbs/bootstrap/

#sudo apt-get install node-uglify
*Modify bootstrap.js (Step-2) and run following command to create a compressed bootstrap.min.js file

#uglifyjs horizon/static/bootstrap/js/bootstrap.js -o horizon/static/bootstrap/js/bootstrap.min.js
* Run "#git diff" or check bootstrap.min.js and ensure that has Changes.

4)
Commit following files
horizon/static/bootstrap/js/bootstrap.js
horizon/static/bootstrap/js/bootstrap.min.js

=======Ref========

#sudo apt-get install yui-compressor
#yui-compressor horizon/static/bootstrap/js/bootstrap.js -o horizon/static/bootstrap/js/bootstrap.min.js

Saturday, February 15, 2014

OpenStack Horizon Dashboard How to customize header sidebar and stylesheet

OpenStack Horizon Dashboard How to customize header sidebar and stylesheet

A)
Existing Code
==========
1)
I could see two _header.html in Openstack Havana horizon projects

a)
horizon/openstack_dashboard/templates/_header.html
This "_header.html" contains username, Settings, Help and Signout links.

{% load i18n %}
{% load url from future %}
  {% blocktrans with username=request.user.username %}Logged in as: {{ username }}{% endblocktrans %}
  {% trans "Settings" %}
  {% if HORIZON_CONFIG.help_url %}
  {% trans "Help" %}
  {% endif %}
  {% trans "Sign Out" %}
  {% include "horizon/common/_region_selector.html" %}

b)
horizon/horizon/templates/_header.html
I don't know, where we using this "_header.html" template.

2)
horizon/horizon/templates/base.html
* The "horizon/openstack_dashboard/templates/_header.html" is included in the "base.html".
* The "horizon/horizon/templates/horizon/common/_sidebar.html" is included in the "base.html".

{% load branding i18n %}
DOCTYPE html>
html>
  head>
    meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
    title>{% block title %}{% endblock %} - {% site_branding %}

    {% comment %} Load CSS sheets before Javascript  {% endcomment %}
    {% block css %}
      {% include "_stylesheets.html" %}
    {% endblock %}
    {% include "horizon/_conf.html" %}
    {% include "horizon/client_side/_script_loader.html" %}
  /head>
  body id="{% block body_id %}{% endblock %}">
    {% block content %}
      div id="container">
        {% block sidebar %}
          {% include 'horizon/common/_sidebar.html' %}
        {% endblock %}
        div id='main_content'>
          div class='topbar'>
          {% include "_header.html" %}
          {% block page_header %}{% endblock %}
          /div>
          {% include "horizon/_messages.html" %}
          {% block main %}{% endblock %}
        /div>
      /div>
    {% endblock %}
    div id="footer">
      {% block footer %}{% endblock %}
    /div>
    {% block js %}
      {% include "horizon/_scripts.html" %}
    {% endblock %}
    div id="modal_wrapper" />
  /body>
/html>

3)

B)
Customized Code
===========


1)
Create a new folder like
horizon/horizon/templates/custom_cloud/common/

2)
Copy existing "_header.html" from horizon/openstack_dashboard/templates/ to
horizon/horizon/templates/custom_cloud/common/

3)
Copy existing "_sidebar.html" from horizon/horizon/templates/horizon/common/ to
horizon/horizon/templates/custom_cloud/common/

4)
So you copied two html files to new folder
horizon/horizon/templates/custom_cloud/common/_header.html
horizon/horizon/templates/custom_cloud/common/_sidebar.html

5)
* First take a copy of horizon/horizon/templates/base.html and rename to
horizon/horizon/templates/base.html.backup.

* Then edit the original horizon/horizon/templates/base.html and update with following lines
{% include "custom_cloud/common/_sidebar.html" %}
{% include "custom_cloud/common/_header.html" %}


6)
Modify "custom_cloud/common/_sidebar.html" and "custom_cloud/common/_header.html"
based on your requirement.

7)
How to Modify CSS
http://fosshelp.blogspot.in/2014/02/openstack-horizon-how-to-include-custom.html

Friday, February 14, 2014

OpenStack Horizon How to include custom stylesheets css less

Method 1 (Tested with OpenStack Havana)
===========================

1)
Create a folder for css/less. like,
horizon/openstack_dashboard/static/custom_dashboard/less

2)
Goto that folder and create a css/less file dashboard.less
#cd horizon/openstack_dashboard/static/custom_dashboard/less
#vim dashboard.less

3)
Open _stylesheets.html file and include your stylesheet there.
horizon/openstack_dashboard/templates/_stylesheets.html

{% load compress %}

{% compress css %}
link href='{{ STATIC_URL }}dashboard/less/horizon.less' type='text/less' media='screen' rel='stylesheet' />
link href='{{ STATIC_URL }}dashboard/less/rickshaw.css' type='text/css' media='screen' rel='stylesheet' />
link href='{{ STATIC_URL }}dashboard/less/horizon_charts.less' type='text/less' media='screen' rel='stylesheet' />
link href='{{ STATIC_URL }}custom_dashboard/less/dashboard.less' type='text/less' media='screen' rel='stylesheet' />
{% endcompress %}

link rel="shortcut icon" href="{{ STATIC_URL }}dashboard/img/favicon.ico"/>

4)
Restart apache
#sudo service apache2 restart

Notes:
----------

* In horizon you can find two _stylesheets.html files
a)
horizon/openstack_dashboard/templates/_stylesheets.html
Included in horizon/horizon/templates/base.html
Here, base.html is the base of all dashboard pages.

b)
horizon/horizon/templates/_stylesheets.html
Included in horizon/horizon/templates/splash.html
Here, splash.html is the Login Page.


Method 2 (Tested with OpenStack Havana)
===========================

1)
Create a folder for css/less. like,
horizon/openstack_dashboard/static/custom_dashboard/less

2)
Goto that folder and create a css/less file dashboard.less
#cd horizon/openstack_dashboard/static/custom_dashboard/less
#vim dashboard.less

3)
a)
Goto horizon/horizon/templates
#cd horizon/horizon/templates

b)
Rename "base.html" to "horizon_base.html"

c)
Create a new html file named "base.html" and add following lines.
{% extends 'horizon_base.html' %}

{% block css %}
  {% include "_stylesheets.html" %}

  {% load compress %}
  {% compress css %}
  link href='{{ STATIC_URL }}custom_dashboard/less/dashboard.less' type='text/less' media='screen' rel='stylesheet' />
  {% endcompress %}
{% endblock %}

4)
Restart apache
#sudo service apache2 restart

Notes:
---------
a)
If you are planning to implement like following link, then you have to edit all dashboard templates
http://docs.openstack.org/developer/horizon/topics/customizing.html

b)
You can also try following method (recomended)
http://docs.openstack.org/grizzly/openstack-compute/install/yum/content/dashboard-custom-brand.html

Tuesday, January 28, 2014

Git How to keep all our commits on top of upstream commits

1)
Goto new devstack env
#cd /opt/stack/horizon

2)
Check default/existing remote repo and location
#git remote -v
origin    git://git.openstack.org/openstack/horizon.git (fetch)
origin    git://git.openstack.org/openstack/horizon.git (push)

3)
Add new remote repo.
Location of our remote repo where we are saving our horizon changes.
#git remote add mycld https://github.com/myCloud/horizon.git
#git remote -v
mycld    https://github.com/myCloud/horizon.git (fetch)
mycld    https://github.com/myCloud/horizon.git (push)
origin    git://git.openstack.org/openstack/horizon.git (fetch)
origin    git://git.openstack.org/openstack/horizon.git (push)

4)
Grab all branches and tags from the remote 'mycld'.
git fetch doesn’t touch your working tree at all, so gives you a little breathing space to decide what you want to do next.
git pull pulls from a remote branch and merges it.
git fetch only fetches from the remote branch but it does not merge
git pull = git fetch + git merge
#git fetch mycld
#git branch
*master

5)
Checkout the branch my_homepage from the remote 'mycld'.
#git checkout --track mycld/my_homepage
#git branch
* my_homepage
  master

6)
Note, right now I am in the branch 'mycld/my_homepage'.
#git pull

7)
Note, right now I am in the branch 'mycld/my_homepage'.
So the following command will takes commits which are only in the branch 'mycld/my_homepage'
and not in 'origin/master' and store it in temporary location.
Then appy all new commits (which are not in 'mycld/my_homepage') from 'origin/master' to 'mycld/my_homepage'.
Then try to restore all temporary stored commits one by one to 'mycld/my_homepage'.
So after this, In 'mycld/my_homepage' we can again see all our commits on top plus new commits from 'origin/master'.
#git rebase master

8)
Run 'git diff' to see the conflict
#git diff

9)
#Fix all conflicts manually (resolve all conflicts)

10)
Prepare the resoved files for commit
#git add name_of_modified_file(s)

11)
This command will automatically commit all resolved files and move to the next commit.
So we don't need to commit the resoved files manually.
#git rebase --continue

12)
#Repeat the steps from 8 to 11 until all the conflicts get fixed

13)
Optional
#git pull mycld my_homepage

14)
Optional
#Fix conflict manually and #git commit -a

15)
Push the rebased code to remote repo where we are saving our horizon changes.
#git push mycld my_homepage

Wednesday, January 22, 2014

OpenStack How to Configure Horizon to use keystone API v3

1)
Goto
horizon/openstack_dashboard/local/local_settings.py
Then Add/uncomment following lines

a)
OPENSTACK_API_VERSIONS = {
    "identity": 3
}



b)
OPENSTACK_KEYSTONE_URL = "http://%s:5000/v3" % OPENSTACK_HOST

Friday, January 17, 2014

Flow of OpenStack Horizon Create Project action Part2

1)
horizon/horizon/workflows/base.py

a)
class Action(forms.Form):
    """
    An ``Action`` represents an atomic logical interaction you can have with
    the system. This is easier to understand with a conceptual example: in the
    context of a "launch instance" workflow, actions would include "naming
    the instance", "selecting an image", and ultimately "launching the
    instance".

    Because ``Actions`` are always interactive, they always provide form
    controls, and thus inherit from Django's ``Form`` class. However, they
    have some additional intelligence added to them:

    * ``Actions`` are aware of the permissions required to complete them.

    * ``Actions`` have a meta-level concept of "help text" which is meant to be
      displayed in such a way as to give context to the action regardless of
      where the action is presented in a site or workflow.

    * ``Actions`` understand how to handle their inputs and produce outputs,
      much like :class:`~horizon.forms.SelfHandlingForm` does now.

    ``Action`` classes may define the following attributes in a ``Meta``
    class within them:

    .. attribute:: name

        The verbose name for this action. Defaults to the name of the class.

    .. attribute:: slug

        A semi-unique slug for this action. Defaults to the "slugified" name
        of the class.

    .. attribute:: permissions

        A list of permission names which this action requires in order to be
        completed. Defaults to an empty list (``[]``).

    .. attribute:: help_text

        A string of simple help text to be displayed alongside the Action's
        fields.

    .. attribute:: help_text_template

        A path to a template which contains more complex help text to be
        displayed alongside the Action's fields. In conjunction with
        :meth:`~horizon.workflows.Action.get_help_text` method you can
        customize your help text template to display practically anything.
    """

    __metaclass__ = ActionMetaclass

    def __init__(self, request, context, *args, **kwargs):
        if request.method == "POST":
            super(Action, self).__init__(request.POST, initial=context)
        else:
            super(Action, self).__init__(initial=context)

        if not hasattr(self, "handle"):
            raise AttributeError("The action %s must define a handle method."
                                 % self.__class__.__name__)
        self.request = request
        self._populate_choices(request, context)
        self.required_css_class = 'required'

    def __unicode__(self):
        return force_unicode(self.name)

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self.slug)

    def _populate_choices(self, request, context):
        for field_name, bound_field in self.fields.items():
            meth = getattr(self, "populate_%s_choices" % field_name, None)
            if meth is not None and callable(meth):
                bound_field.choices = meth(request, context)

    def get_help_text(self, extra_context=None):
        """ Returns the help text for this step. """
        text = ""
        extra_context = extra_context or {}
        if self.help_text_template:
            tmpl = template.loader.get_template(self.help_text_template)
            context = template.RequestContext(self.request, extra_context)
            text += tmpl.render(context)
        else:
            text += linebreaks(force_unicode(self.help_text))
        return safe(text)

    def add_error(self, message):
        """
        Adds an error to the Action's Step based on API issues.
        """
        self._get_errors()[NON_FIELD_ERRORS] = self.error_class([message])

    def handle(self, request, context):
        """
        Handles any requisite processing for this action. The method should
        return either ``None`` or a dictionary of data to be passed to
        :meth:`~horizon.workflows.Step.contribute`.

        Returns ``None`` by default, effectively making it a no-op.
        """
        return None


b)
class Step(object):
    """
    A step is a wrapper around an action which defines it's context in a
    workflow. It knows about details such as:

    * The workflow's context data (data passed from step to step).

    * The data which must be present in the context to begin this step (the
      step's dependencies).

    * The keys which will be added to the context data upon completion of the
      step.

    * The connections between this step's fields and changes in the context
      data (e.g. if that piece of data changes, what needs to be updated in
      this step).

    A ``Step`` class has the following attributes:

    .. attribute:: action

        The :class:`~horizon.workflows.Action` class which this step wraps.

    .. attribute:: depends_on

        A list of context data keys which this step requires in order to
        begin interaction.

    .. attribute:: contributes

        A list of keys which this step will contribute to the workflow's
        context data. Optional keys should still be listed, even if their
        values may be set to ``None``.

    .. attribute:: connections

        A dictionary which maps context data key names to lists of callbacks.
        The callbacks may be functions, dotted python paths to functions
        which may be imported, or dotted strings beginning with ``"self"``
        to indicate methods on the current ``Step`` instance.

    .. attribute:: before

        Another ``Step`` class. This optional attribute is used to provide
        control over workflow ordering when steps are dynamically added to
        workflows. The workflow mechanism will attempt to place the current
        step before the step specified in the attribute.

    .. attribute:: after

        Another ``Step`` class. This attribute has the same purpose as
        :meth:`~horizon.workflows.Step.before` except that it will instead
        attempt to place the current step after the given step.

    .. attribute:: help_text

        A string of simple help text which will be prepended to the ``Action``
        class' help text if desired.

    .. attribute:: template_name

        A path to a template which will be used to render this step. In
        general the default common template should be used. Default:
        ``"horizon/common/_workflow_step.html"``.

    .. attribute:: has_errors

        A boolean value which indicates whether or not this step has any
        errors on the action within it or in the scope of the workflow. This
        attribute will only accurately reflect this status after validation
        has occurred.

    .. attribute:: slug

        Inherited from the ``Action`` class.

    .. attribute:: name

        Inherited from the ``Action`` class.

    .. attribute:: permissions

        Inherited from the ``Action`` class.
    """
    action_class = None
    depends_on = ()
    contributes = ()
    connections = None
    before = None
    after = None
    help_text = ""
    template_name = "horizon/common/_workflow_step.html"

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self.slug)

    def __unicode__(self):
        return force_unicode(self.name)

    def __init__(self, workflow):
        super(Step, self).__init__()
        self.workflow = workflow

        cls = self.__class__.__name__
        if not (self.action_class and issubclass(self.action_class, Action)):
            raise AttributeError("You must specify an action for %s." % cls)

        self.slug = self.action_class.slug
        self.name = self.action_class.name
        self.permissions = self.action_class.permissions
        self.has_errors = False
        self._handlers = {}

        if self.connections is None:
            # We want a dict, but don't want to declare a mutable type on the
            # class directly.
            self.connections = {}

        # Gather our connection handlers and make sure they exist.
        for key, handlers in self.connections.items():
            self._handlers[key] = []
            # TODO(gabriel): This is a poor substitute for broader handling
            if not isinstance(handlers, (list, tuple)):
                raise TypeError("The connection handlers for %s must be a "
                                "list or tuple." % cls)
            for possible_handler in handlers:
                if callable(possible_handler):
                    # If it's callable we know the function exists and is valid
                    self._handlers[key].append(possible_handler)
                    continue
                elif not isinstance(possible_handler, basestring):
                    return TypeError("Connection handlers must be either "
                                     "callables or strings.")
                bits = possible_handler.split(".")
                if bits[0] == "self":
                    root = self
                    for bit in bits[1:]:
                        try:
                            root = getattr(root, bit)
                        except AttributeError:
                            raise AttributeError("The connection handler %s "
                                                 "could not be found on %s."
                                                 % (possible_handler, cls))
                    handler = root
                elif len(bits) == 1:
                    # Import by name from local module not supported
                    raise ValueError("Importing a local function as a string "
                                     "is not supported for the connection "
                                     "handler %s on %s."
                                     % (possible_handler, cls))
                else:
                    # Try a general import
                    module_name = ".".join(bits[:-1])
                    try:
                        mod = import_module(module_name)
                        handler = getattr(mod, bits[-1])
                    except ImportError:
                        raise ImportError("Could not import %s from the "
                                          "module %s as a connection "
                                             "handler on %s."
                                             % (bits[-1], module_name, cls))
                    except AttributeError:
                        raise AttributeError("Could not import %s from the "
                                             "module %s as a connection "
                                             "handler on %s."
                                             % (bits[-1], module_name, cls))
                self._handlers[key].append(handler)

    @property
    def action(self):
        if not getattr(self, "_action", None):
            try:
                # Hook in the action context customization.
                workflow_context = dict(self.workflow.context)
                context = self.prepare_action_context(self.workflow.request,
                                                      workflow_context)
                self._action = self.action_class(self.workflow.request,
                                                 context)
            except Exception:
                LOG.exception("Problem instantiating action class.")
                raise
        return self._action

    def prepare_action_context(self, request, context):
        """
        Allows for customization of how the workflow context is passed to the
        action; this is the reverse of what "contribute" does to make the
        action outputs sane for the workflow. Changes to the context are not
        saved globally here. They are localized to the action.

        Simply returns the unaltered context by default.
        """
        return context

    def get_id(self):
        """ Returns the ID for this step. Suitable for use in HTML markup. """
        return "%s__%s" % (self.workflow.slug, self.slug)

    def _verify_contributions(self, context):
        for key in self.contributes:
            # Make sure we don't skip steps based on weird behavior of
            # POST query dicts.
            field = self.action.fields.get(key, None)
            if field and field.required and not context.get(key):
                context.pop(key, None)
        failed_to_contribute = set(self.contributes)
        failed_to_contribute -= set(context.keys())
        if failed_to_contribute:
            raise exceptions.WorkflowError("The following expected data was "
                                           "not added to the workflow context "
                                           "by the step %s: %s."
                                           % (self.__class__,
                                              failed_to_contribute))
        return True

    def contribute(self, data, context):
        """
        Adds the data listed in ``contributes`` to the workflow's shared
        context. By default, the context is simply updated with all the data
        returned by the action.

        Note that even if the value of one of the ``contributes`` keys is
        not present (e.g. optional) the key should still be added to the
        context with a value of ``None``.
        """
        if data:
            for key in self.contributes:
                context[key] = data.get(key, None)
        return context

    def render(self):
        """ Renders the step. """
        step_template = template.loader.get_template(self.template_name)
        extra_context = {"form": self.action,
                         "step": self}
        context = template.RequestContext(self.workflow.request, extra_context)
        return step_template.render(context)

    def get_help_text(self):
        """ Returns the help text for this step. """
        text = linebreaks(force_unicode(self.help_text))
        text += self.action.get_help_text()
        return safe(text)

    def add_error(self, message):
        """
        Adds an error to the Step based on API issues.
        """
        self.action.add_error(message)

    def has_required_fields(self):
        """
        Returns True if action contains any required fields
        """
        for key in self.contributes:
            field = self.action.fields.get(key, None)
            if (field and field.required):
                return True
        return False




2)
horizon/openstack_dashboard/dashboards/admin/projects/workflows.py

###Action Classes or Form Classes
a)
class CreateProjectInfoAction(workflows.Action):
    # Hide the domain_id and domain_name by default
    domain_id = forms.CharField(label=_("Domain ID"),
                                required=False,
                                widget=forms.HiddenInput())
    domain_name = forms.CharField(label=_("Domain Name"),
                                  required=False,
                                  widget=forms.HiddenInput())
    name = forms.CharField(label=_("Name"))
    description = forms.CharField(widget=forms.widgets.Textarea(),
                                  label=_("Description"),
                                  required=False)
    enabled = forms.BooleanField(label=_("Enabled"),
                                 required=False,
                                 initial=True)

    def __init__(self, request, *args, **kwargs):
        super(CreateProjectInfoAction, self).__init__(request,
                                                      *args,
                                                      **kwargs)
        # For keystone V3, display the two fields in read-only
        if keystone.VERSIONS.active >= 3:
            readonlyInput = forms.TextInput(attrs={'readonly': 'readonly'})
            self.fields["domain_id"].widget = readonlyInput
            self.fields["domain_name"].widget = readonlyInput

    class Meta:
        name = _("Project Info")
        help_text = _("From here you can create a new "
                      "project to organize users.")


b)
class UpdateProjectMembersAction(workflows.MembershipAction):
    def __init__(self, request, *args, **kwargs):
        super(UpdateProjectMembersAction, self).__init__(request,
                                                         *args,
                                                         **kwargs)
        err_msg = _('Unable to retrieve user list. Please try again later.')
        # Use the domain_id from the project
        domain_id = self.initial.get("domain_id", None)
        project_id = ''
        if 'project_id' in self.initial:
            project_id = self.initial['project_id']

        # Get the default role
        try:
            default_role = api.keystone.get_default_role(self.request)
            # Default role is necessary to add members to a project
            if default_role is None:
                default = getattr(settings,
                                  "OPENSTACK_KEYSTONE_DEFAULT_ROLE", None)
                msg = _('Could not find default role "%s" in Keystone') % \
                        default
                raise exceptions.NotFound(msg)
        except Exception:
            exceptions.handle(self.request,
                              err_msg,
                              redirect=reverse(INDEX_URL))
        default_role_name = self.get_default_role_field_name()
        self.fields[default_role_name] = forms.CharField(required=False)
        self.fields[default_role_name].initial = default_role.id

        # Get list of available users
        all_users = []
        try:
            all_users = api.keystone.user_list(request,
                                               domain=domain_id)
        except Exception:
            exceptions.handle(request, err_msg)
        users_list = [(user.id, user.name) for user in all_users]

        # Get list of roles
        role_list = []
        try:
            role_list = api.keystone.role_list(request)
        except Exception:
            exceptions.handle(request,
                              err_msg,
                              redirect=reverse(INDEX_URL))
        for role in role_list:
            field_name = self.get_member_field_name(role.id)
            label = role.name
            self.fields[field_name] = forms.MultipleChoiceField(required=False,
                                                                label=label)
            self.fields[field_name].choices = users_list
            self.fields[field_name].initial = []

        # Figure out users & roles
        if project_id:
            try:
                project_members = api.keystone.user_list(request,
                    project=project_id)
            except Exception:
                exceptions.handle(request, err_msg)

            for user in project_members:
                try:
                    roles = api.keystone.roles_for_user(self.request,
                                                        user.id,
                                                        project_id)
                except Exception:
                    exceptions.handle(request,
                                      err_msg,
                                      redirect=reverse(INDEX_URL))
                for role in roles:
                    field_name = self.get_member_field_name(role.id)
                    self.fields[field_name].initial.append(user.id)

    class Meta:
        name = _("Project Members")
        slug = PROJECT_USER_MEMBER_SLUG

c)
class UpdateProjectQuotaAction(workflows.Action):
    ifcb_label = _("Injected File Content Bytes")
    metadata_items = forms.IntegerField(min_value=-1,
                                        label=_("Metadata Items"))
    cores = forms.IntegerField(min_value=-1, label=_("VCPUs"))
    instances = forms.IntegerField(min_value=-1, label=_("Instances"))
    injected_files = forms.IntegerField(min_value=-1,
                                        label=_("Injected Files"))
    injected_file_content_bytes = forms.IntegerField(min_value=-1,
                                                     label=ifcb_label)
    volumes = forms.IntegerField(min_value=-1, label=_("Volumes"))
    snapshots = forms.IntegerField(min_value=-1, label=_("Snapshots"))
    gigabytes = forms.IntegerField(min_value=-1, label=_("Gigabytes"))
    ram = forms.IntegerField(min_value=-1, label=_("RAM (MB)"))
    floating_ips = forms.IntegerField(min_value=-1, label=_("Floating IPs"))
    fixed_ips = forms.IntegerField(min_value=-1, label=_("Fixed IPs"))
    security_groups = forms.IntegerField(min_value=-1,
                                         label=_("Security Groups"))
    security_group_rules = forms.IntegerField(min_value=-1,
                                              label=_("Security Group Rules"))

    # Neutron
    security_group = forms.IntegerField(min_value=-1,
                                        label=_("Security Groups"))
    security_group_rule = forms.IntegerField(min_value=-1,
                                             label=_("Security Group Rules"))
    floatingip = forms.IntegerField(min_value=-1, label=_("Floating IPs"))
    network = forms.IntegerField(min_value=-1, label=_("Networks"))
    port = forms.IntegerField(min_value=-1, label=_("Ports"))
    router = forms.IntegerField(min_value=-1, label=_("Routers"))
    subnet = forms.IntegerField(min_value=-1, label=_("Subnets"))

    def __init__(self, request, *args, **kwargs):
        super(UpdateProjectQuotaAction, self).__init__(request,
                                                       *args,
                                                       **kwargs)
        disabled_quotas = quotas.get_disabled_quotas(request)
        for field in disabled_quotas:
            if field in self.fields:
                self.fields[field].required = False
                self.fields[field].widget = forms.HiddenInput()

    class Meta:
        name = _("Quota")
        slug = 'update_quotas'
        help_text = _("From here you can set quotas "
                      "(max limits) for the project.")



###Step Classes or wrapper of Action classes
a)
class CreateProjectInfo(workflows.Step):
    action_class = CreateProjectInfoAction
    contributes = ("domain_id",
                   "domain_name",
                   "project_id",
                   "name",
                   "description",
                   "enabled")

b)
class UpdateProjectMembers(workflows.UpdateMembersStep):
    action_class = UpdateProjectMembersAction
    available_list_title = _("All Users")
    members_list_title = _("Project Members")
    no_available_text = _("No users found.")
    no_members_text = _("No users.")

    def contribute(self, data, context):
        if data:
            try:
                roles = api.keystone.role_list(self.workflow.request)
            except Exception:
                exceptions.handle(self.workflow.request,
                                  _('Unable to retrieve user list.'))

            post = self.workflow.request.POST
            for role in roles:
                field = self.get_member_field_name(role.id)
                context[field] = post.getlist(field)
        return context

c)
class UpdateProjectQuota(workflows.Step):
    action_class = UpdateProjectQuotaAction
    depends_on = ("project_id",)
    contributes = quotas.QUOTA_FIELDS


Flow of OpenStack Horizon Create Project action Part1

1)
horizon/horizon/utils/html.py

from django.forms.util import flatatt  # noqa

class HTMLElement(object):
    """ A generic base class that gracefully handles html-style attributes. """
    def __init__(self):
        self.attrs = getattr(self, "attrs", {})
        self.classes = getattr(self, "classes", [])

    def get_default_classes(self):
        """
        Returns an iterable of default classes which should be combined with
        any other declared classes.
        """
        return []

    def get_default_attrs(self):
        """
        Returns a dict of default attributes which should be combined with
        other declared attributes.
        """
        return {}

    def get_final_attrs(self):
        """
        Returns a dict containing the final attributes of this element
        which will be rendered.
        """
        final_attrs = copy.copy(self.get_default_attrs())
        final_attrs.update(self.attrs)
        # Handle css class concatenation
        default = " ".join(self.get_default_classes())
        defined = self.attrs.get('class', '')
        additional = " ".join(getattr(self, "classes", []))
        non_empty = [test for test in (defined, default, additional) if test]
        final_classes = " ".join(non_empty).strip()
        final_attrs.update({'class': final_classes})
        return final_attrs

    @property
    def attr_string(self):
        """
        Returns a flattened string of HTML attributes based on the
        ``attrs`` dict provided to the class.
        """
        return flatatt(self.get_final_attrs())

    @property
    def class_string(self):
        """
        Returns a list of class name of HTML Element in string
        """
        classes_str = " ".join(self.classes)
        return classes_str


2)
horizon/horizon/workflows/base.py


from horizon.utils import html

class Workflow(html.HTMLElement):
    """
    A Workflow is a collection of Steps. It's interface is very
    straightforward, but it is responsible for handling some very
    important tasks such as:

    * Handling the injection, removal, and ordering of arbitrary steps.

    * Determining if the workflow can be completed by a given user at runtime
      based on all available information.

    * Dispatching connections between steps to ensure that when context data
      changes all the applicable callback functions are executed.

    * Verifying/validating the overall data integrity and subsequently
      triggering the final method to complete the workflow.

    The ``Workflow`` class has the following attributes:

    .. attribute:: name

        The verbose name for this workflow which will be displayed to the user.
        Defaults to the class name.

    .. attribute:: slug

        The unique slug for this workflow. Required.

    .. attribute:: steps

        Read-only access to the final ordered set of step instances for
        this workflow.

    .. attribute:: default_steps

        A list of :class:`~horizon.workflows.Step` classes (Step class is a wrapper for modified Form/Action class) which serve as the
        starting point for this workflow's ordered steps. Defaults to an empty
        list (``[]``).

    .. attribute:: finalize_button_name

        The name which will appear on the submit button for the workflow's
        form. Defaults to ``"Save"``.

    .. attribute:: success_message

        A string which will be displayed to the user upon successful completion
        of the workflow. Defaults to
        ``"{{ workflow.name }} completed successfully."``

    .. attribute:: failure_message

        A string which will be displayed to the user upon failure to complete
        the workflow. Defaults to ``"{{ workflow.name }} did not complete."``

    .. attribute:: depends_on

        A roll-up list of all the ``depends_on`` values compiled from the
        workflow's steps.

    .. attribute:: contributions

        A roll-up list of all the ``contributes`` values compiled from the
        workflow's steps.

    .. attribute:: template_name

        Path to the template which should be used to render this workflow.
        In general the default common template should be used. Default:
        ``"horizon/common/_workflow.html"``.

    .. attribute:: entry_point

        The slug of the step which should initially be active when the
        workflow is rendered. This can be passed in upon initialization of
        the workflow, or set anytime after initialization but before calling
        either ``get_entry_point`` or ``render``.

    .. attribute:: redirect_param_name

        The name of a parameter used for tracking the URL to redirect to upon
        completion of the workflow. Defaults to ``"next"``.

    .. attribute:: object

        The object (if any) which this workflow relates to. In the case of
        a workflow which creates a new resource the object would be the created
        resource after the relevant creation steps have been undertaken. In
        the case of a workflow which updates a resource it would be the
        resource being updated after it has been retrieved.

    """
    __metaclass__ = WorkflowMetaclass
    slug = None
    default_steps = ()
    template_name = "horizon/common/_workflow.html"
    finalize_button_name = _("Save")
    success_message = _("%s completed successfully.")
    failure_message = _("%s did not complete.")
    redirect_param_name = "next"
    multipart = False
    _registerable_class = Step

    def __unicode__(self):
        return self.name

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self.slug)

    def __init__(self, request=None, context_seed=None, entry_point=None,
                 *args, **kwargs):
        super(Workflow, self).__init__(*args, **kwargs)
        if self.slug is None:
            raise AttributeError("The workflow %s must have a slug."
                                 % self.__class__.__name__)
        self.name = getattr(self, "name", self.__class__.__name__)
        self.request = request
        self.depends_on = set([])
        self.contributions = set([])
        self.entry_point = entry_point
        self.object = None

        # Put together our steps in order. Note that we pre-register
        # non-default steps so that we can identify them and subsequently
        # insert them in order correctly.
        self._registry = dict([(step_class, step_class(self)) for step_class
                               in self.__class__._cls_registry
                               if step_class not in self.default_steps])
        self._gather_steps()

        # Determine all the context data we need to end up with.
        for step in self.steps:
            self.depends_on = self.depends_on | set(step.depends_on)
            self.contributions = self.contributions | set(step.contributes)

        # Initialize our context. For ease we can preseed it with a
        # regular dictionary. This should happen after steps have been
        # registered and ordered.
        self.context = WorkflowContext(self)
        context_seed = context_seed or {}
        clean_seed = dict([(key, val)
                           for key, val in context_seed.items()
                           if key in self.contributions | self.depends_on])
        self.context_seed = clean_seed
        self.context.update(clean_seed)

        if request and request.method == "POST":
            for step in self.steps:
                valid = step.action.is_valid()
                # Be sure to use the CLEANED data if the workflow is valid.
                if valid:
                    data = step.action.cleaned_data
                else:
                    data = request.POST
                self.context = step.contribute(data, self.context)

    @property
    def steps(self):
        if getattr(self, "_ordered_steps", None) is None:
            self._gather_steps()
        return self._ordered_steps

    def get_step(self, slug):
        """ Returns the instantiated step matching the given slug. """
        for step in self.steps:
            if step.slug == slug:
                return step

    def _gather_steps(self):
        ordered_step_classes = self._order_steps()
        for default_step in self.default_steps:
            self.register(default_step)
            self._registry[default_step] = default_step(self)
        self._ordered_steps = [self._registry[step_class]
                               for step_class in ordered_step_classes
                               if has_permissions(self.request.user,
                                          self._registry[step_class])]

    def _order_steps(self):
        steps = list(copy.copy(self.default_steps))
        additional = self._registry.keys()
        for step in additional:
            try:
                min_pos = steps.index(step.after)
            except ValueError:
                min_pos = 0
            try:
                max_pos = steps.index(step.before)
            except ValueError:
                max_pos = len(steps)
            if min_pos > max_pos:
                raise exceptions.WorkflowError("The step %(new)s can't be "
                                               "placed between the steps "
                                               "%(after)s and %(before)s; the "
                                               "step %(before)s comes before "
                                               "%(after)s."
                                               % {"new": additional,
                                                  "after": step.after,
                                                  "before": step.before})
            steps.insert(max_pos, step)
        return steps

    def get_entry_point(self):
        """
        Returns the slug of the step which the workflow should begin on.

        This method takes into account both already-available data and errors
        within the steps.
        """
        # If we have a valid specified entry point, use it.
        if self.entry_point:
            if self.get_step(self.entry_point):
                return self.entry_point
        # Otherwise fall back to calculating the appropriate entry point.
        for step in self.steps:
            if step.has_errors:
                return step.slug
            try:
                step._verify_contributions(self.context)
            except exceptions.WorkflowError:
                return step.slug
        # If nothing else, just return the first step.
        return self.steps[0].slug

    def _trigger_handlers(self, key):
        responses = []
        handlers = [(step.slug, f) for step in self.steps
                                   for f in step._handlers.get(key, [])]
        for slug, handler in handlers:
            responses.append((slug, handler(self.request, self.context)))
        return responses

    @classmethod
    def register(cls, step_class):
        """ Registers a :class:`~horizon.workflows.Step` with the workflow. """
        if not inspect.isclass(step_class):
            raise ValueError('Only classes may be registered.')
        elif not issubclass(step_class, cls._registerable_class):
            raise ValueError('Only %s classes or subclasses may be registered.'
                             % cls._registerable_class.__name__)
        if step_class in cls._cls_registry:
            return False
        else:
            cls._cls_registry.add(step_class)
            return True

    @classmethod
    def unregister(cls, step_class):
        """
        Unregisters a :class:`~horizon.workflows.Step` from the workflow.
        """
        try:
            cls._cls_registry.remove(step_class)
        except KeyError:
            raise base.NotRegistered('%s is not registered' % cls)
        return cls._unregister(step_class)

    def validate(self, context):
        """
        Hook for custom context data validation. Should return a boolean
        value or raise :class:`~horizon.exceptions.WorkflowValidationError`.
        """
        return True

    def is_valid(self):
        """
        Verified that all required data is present in the context and
        calls the ``validate`` method to allow for finer-grained checks
        on the context data.
        """
        missing = self.depends_on - set(self.context.keys())
        if missing:
            raise exceptions.WorkflowValidationError(
                "Unable to complete the workflow. The values %s are "
                "required but not present." % ", ".join(missing))

        # Validate each step. Cycle through all of them to catch all errors
        # in one pass before returning.
        steps_valid = True
        for step in self.steps:
            if not step.action.is_valid():
                steps_valid = False
                step.has_errors = True
        if not steps_valid:
            return steps_valid
        return self.validate(self.context)

    def finalize(self):
        """
        Finalizes a workflow by running through all the actions in order
        and calling their ``handle`` methods. Returns ``True`` on full success,
        or ``False`` for a partial success, e.g. there were non-critical
        errors. (If it failed completely the function wouldn't return.)
        """
        partial = False
        for step in self.steps:
            try:
                data = step.action.handle(self.request, self.context)
                if data is True or data is None:
                    continue
                elif data is False:
                    partial = True
                else:
                    self.context = step.contribute(data or {}, self.context)
            except Exception:
                partial = True
                exceptions.handle(self.request)
        if not self.handle(self.request, self.context):
            partial = True
        return not partial

    def handle(self, request, context):
        """
        Handles any final processing for this workflow. Should return a boolean
        value indicating success.
        """
        return True

    def get_success_url(self):
        """
        Returns a URL to redirect the user to upon completion. By default it
        will attempt to parse a ``success_url`` attribute on the workflow,
        which can take the form of a reversible URL pattern name, or a
        standard HTTP URL.
        """
        try:
            return urlresolvers.reverse(self.success_url)
        except urlresolvers.NoReverseMatch:
            return self.success_url

    def format_status_message(self, message):
        """
        Hook to allow customization of the message returned to the user
        upon successful or unsuccessful completion of the workflow.

        By default it simply inserts the workflow's name into the message
        string.
        """
        if "%s" in message:
            return message % self.name
        else:
            return message

    def render(self):
        """ Renders the workflow. """
        workflow_template = template.loader.get_template(self.template_name)
        extra_context = {"workflow": self}
        if self.request.is_ajax():
            extra_context['modal'] = True
        context = template.RequestContext(self.request, extra_context)
        return workflow_template.render(context)

    def get_absolute_url(self):
        """ Returns the canonical URL for this workflow.

        This is used for the POST action attribute on the form element
        wrapping the workflow.

        For convenience it defaults to the value of
        ``request.get_full_path()`` with any query string stripped off,
        e.g. the path at which the workflow was requested.
        """
        return self.request.get_full_path().partition('?')[0]

    def add_error_to_step(self, message, slug):
        """
        Adds an error to the workflow's Step with the
        specifed slug based on API issues. This is useful
        when you wish for API errors to appear as errors on
        the form rather than using the messages framework.
        """
        step = self.get_step(slug)
        if step:
            step.add_error(message)



3)
horizon/horizon/workflows/__init__.py

from horizon.workflows.base import Workflow  # noqa
assert Workflow


4)
horizon/openstack_dashboard/dashboards/admin/projects/workflows.py


from horizon import workflows

class CreateProject(workflows.Workflow):
    slug = "create_project"
    name = _("Create Project")
    finalize_button_name = _("Create Project")
    success_message = _('Created new project "%s".')
    failure_message = _('Unable to create project "%s".')
    success_url = "horizon:admin:projects:index"
    default_steps = (CreateProjectInfo,
                     UpdateProjectMembers,
                     UpdateProjectQuota)

    def __init__(self, request=None, context_seed=None, entry_point=None,
                 *args, **kwargs):
        if PROJECT_GROUP_ENABLED:
            self.default_steps = (CreateProjectInfo,
                                  UpdateProjectMembers,
                                  UpdateProjectGroups,
                                  UpdateProjectQuota)
        super(CreateProject, self).__init__(request=request,
                                            context_seed=context_seed,
                                            entry_point=entry_point,
                                            *args,
                                            **kwargs)

    def format_status_message(self, message):
        return message % self.context.get('name', 'unknown project')

    def handle(self, request, data):
        # create the project
        domain_id = data['domain_id']
        try:
            desc = data['description']
            self.object = api.keystone.tenant_create(request,
                                                     name=data['name'],
                                                     description=desc,
                                                     enabled=data['enabled'],
                                                     domain=domain_id)
        except Exception:
            exceptions.handle(request, ignore=True)
            return False

        project_id = self.object.id

        # update project members
        users_to_add = 0
        try:
            available_roles = api.keystone.role_list(request)
            member_step = self.get_step(PROJECT_USER_MEMBER_SLUG)
            # count how many users are to be added
            for role in available_roles:
                field_name = member_step.get_member_field_name(role.id)
                role_list = data[field_name]
                users_to_add += len(role_list)
            # add new users to project
            for role in available_roles:
                field_name = member_step.get_member_field_name(role.id)
                role_list = data[field_name]
                users_added = 0
                for user in role_list:
                    api.keystone.add_tenant_user_role(request,
                                                      project=project_id,
                                                      user=user,
                                                      role=role.id)
                    users_added += 1
                users_to_add -= users_added
        except Exception:
            if PROJECT_GROUP_ENABLED:
                group_msg = _(", add project groups")
            else:
                group_msg = ""
            exceptions.handle(request, _('Failed to add %(users_to_add)s '
                                         'project members%(group_msg)s and '
                                         'set project quotas.')
                                      % {'users_to_add': users_to_add,
                                         'group_msg': group_msg})

        if PROJECT_GROUP_ENABLED:
            # update project groups
            groups_to_add = 0
            try:
                available_roles = api.keystone.role_list(request)
                member_step = self.get_step(PROJECT_GROUP_MEMBER_SLUG)

                # count how many groups are to be added
                for role in available_roles:
                    field_name = member_step.get_member_field_name(role.id)
                    role_list = data[field_name]
                    groups_to_add += len(role_list)
                # add new groups to project
                for role in available_roles:
                    field_name = member_step.get_member_field_name(role.id)
                    role_list = data[field_name]
                    groups_added = 0
                    for group in role_list:
                        api.keystone.add_group_role(request,
                                                    role=role.id,
                                                    group=group,
                                                    project=project_id)
                        groups_added += 1
                    groups_to_add -= groups_added
            except Exception:
                exceptions.handle(request, _('Failed to add %s project groups '
                                             'and update project quotas.'
                                             % groups_to_add))

        # Update the project quota.
        nova_data = dict(
            [(key, data[key]) for key in quotas.NOVA_QUOTA_FIELDS])
        try:
            nova.tenant_quota_update(request, project_id, **nova_data)

            if base.is_service_enabled(request, 'volume'):
                cinder_data = dict([(key, data[key]) for key in
                                    quotas.CINDER_QUOTA_FIELDS])
                cinder.tenant_quota_update(request,
                                           project_id,
                                           **cinder_data)

            if api.base.is_service_enabled(request, 'network') and \
                    api.neutron.is_quotas_extension_supported(request):
                neutron_data = dict([(key, data[key]) for key in
                                     quotas.NEUTRON_QUOTA_FIELDS])
                api.neutron.tenant_quota_update(request,
                                                project_id,
                                                **neutron_data)
        except Exception:
            exceptions.handle(request, _('Unable to set project quotas.'))
        return True