How can i obtain the resources based on the project and user selected in Horizon?

asked 2015-08-27 01:52:40 -0600

sanjana gravatar image

updated 2015-08-27 01:55:18 -0600

# I have a workflow with three fields
    class CreateAction(workflows.Action):

        project = forms.ChoiceField(label=_("Project"),
                                    required=True)
        user = forms.ChoiceField(label=_("User"),
                                 required=True)
        resource = forms.ChoiceField(label=_("Resource"),
                                     required=True)

    #Here the choices for resource must be based on the project and the user values.
    #How can i get the values of project and user from the workflow?

    def __init__(self, request, *args, **kwargs):
            super(CreateAlarmInfoAction, self).__init__(request, *args, **kwargs)
            self.fields['project'].choices = (
                self.get_project_list_choices(request))

            self.fields['user'].choices = (
                self.get_user_list_choices(request))

            self.fields['resource'].choices = (
                self.get_resource_list_choices(request))
    #In this method, to get a list of resources i need to pass the previously selected project and user id
        def get_resource_list_choices(self, request):
            resource_choices = [('', _("Select a resource"))]
            list_of_resources = self._get_resources(request)
            for obj in list_of_resources:
                if obj.name is not None and obj.name not in resource_choices:
                    resource_choices.append((obj.id, obj.name))
            return resource_choices
    #I also have to pass the project id and user id in the ceilometer api
        def _get_resources(self, request):
            resources = []
            try:
                resources = api.ceilometer.resource_list(request)

            except Exception:
                msg = _('Resource list cannot be retrieved.')
                exceptions.handle(request, msg)
            return resources


   # I am fetching the user list as follows

     def get_user_list_choices(self, request):
            user_choices = [('', _("Select a user"))]
            list_of_users = self._get_users(request)
            for obj in list_of_users:
                user_choices.append((obj.id, obj.username))
            return user_choices

        def _get_users(self, request):
            users = []
            try:
                users = api.keystone.user_list(request)
            except Exception:
                msg = _('User list cannot be retrieved.')
                exceptions.handle(request, msg)
            return users
  #  similarly the project list is also fetched
edit retag flag offensive close merge delete