Job creation for multiple clients

Implements: blueprint freezer-api-web-ui

Change-Id: I6d2b29b18909cef627646b437454d6075b725df5
This commit is contained in:
Memo Garcia 2015-07-06 11:17:50 +01:00
parent e5ec8452a8
commit 8bd2a03710
3 changed files with 50 additions and 100 deletions

View File

@ -74,12 +74,10 @@ class Backup(Dict2Object):
return self.backup_id
class Client(Dict2Object):
nested_dict = 'client'
@property
def id(self):
return self.client_id
class Client(object):
def __init__(self, client, hostname):
self.client = client
self.hostname = hostname
class ActionJob(object):
@ -136,8 +134,9 @@ def _freezerclient(request):
def job_create(request, context):
"""Create a new job file """
job = create_dict_action(**context)
client_id = job.pop('client_id', None)
job['description'] = job.pop('description', None)
job['client_id'] = job.pop('client_id', None)
schedule = {
'end_datetime': job.pop('end_datetime', None),
'interval': job.pop('interval', None),
@ -145,6 +144,7 @@ def job_create(request, context):
}
job['job_schedule'] = schedule
job['job_actions'] = []
job['client_id'] = client_id
return _freezerclient(request).jobs.create(job)
@ -273,7 +273,8 @@ def action_delete(request, ids):
def client_list(request):
clients = _freezerclient(request).registration.list()
clients = [Client(client) for client in clients]
clients = [Client(c['uuid'], c['client']['hostname'])
for c in clients]
return clients

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
from django.utils.translation import ugettext_lazy as _
import datetime
from horizon import exceptions
@ -19,111 +20,56 @@ from horizon import workflows
import horizon_web_ui.freezer_ui.api.api as freezer_api
class ClientsConfigurationAction(workflows.Action):
client_id = forms.ChoiceField(
help_text=_("Set the client for this job"),
required=True)
def populate_client_id_choices(self, request, context):
clients = []
try:
clients = freezer_api.client_list(request)
except Exception:
exceptions.handle(request, _('Error getting client list'))
client_id = [(c.client_id, c.hostname) for c in clients]
client_id.insert(0, ('', _('Select A Client')))
return client_id
class Meta(object):
name = _("Clients")
slug = "clients"
LOG = logging.getLogger(__name__)
class ClientsConfiguration(workflows.Step):
action_class = ClientsConfigurationAction
contributes = ('client_id',)
class ActionsConfigurationAction(workflows.MembershipAction):
class ClientsConfigurationAction(workflows.MembershipAction):
def __init__(self, request, *args, **kwargs):
super(ActionsConfigurationAction, self).__init__(request,
super(ClientsConfigurationAction, self).__init__(request,
*args,
**kwargs)
err_msg = _('Unable to retrieve client list.')
original_name = args[0].get('original_name', None)
err_msg = _('Unable to retrieve list of actions.')
default_role_name = self.get_default_role_field_name()
self.fields[default_role_name] = forms.CharField(required=False)
self.fields[default_role_name].initial = 'member'
if original_name:
default_role_field_name = self.get_default_role_field_name()
self.fields[default_role_field_name] = \
forms.CharField(required=False,
widget=forms.HiddenInput())
self.fields[default_role_field_name].initial = 'member'
actions = self.get_member_field_name('member')
self.fields[actions] = \
forms.MultipleChoiceField(required=False,
widget=forms.HiddenInput())
else:
default_role_field_name = self.get_default_role_field_name()
self.fields[default_role_field_name] = \
forms.CharField(required=False)
self.fields[default_role_field_name].initial = 'member'
actions = self.get_member_field_name('member')
self.fields[actions] = forms.MultipleChoiceField(required=False)
all_actions = []
all_clients = []
try:
all_actions = freezer_api.action_list(request)
all_clients = freezer_api.client_list(request)
except Exception:
exceptions.handle(request, err_msg)
client_list = [(c.client, c.hostname)
for c in all_clients]
all_actions = [(a.action_id, a.freezer_action['backup_name'])
for a in all_actions]
field_name = self.get_member_field_name('member')
if not original_name:
self.fields[field_name] = forms.MultipleChoiceField(required=False)
self.fields[field_name].choices = client_list
self.fields[actions].choices = all_actions
initial_actions = []
if request.method == 'POST':
return
try:
if original_name:
configured_actions = \
freezer_api.actions_in_job(request, original_name)
initial_actions = [a.action_id for a in configured_actions]
except Exception:
exceptions.handle(request, err_msg)
self.fields[actions].initial = initial_actions
class Meta(object):
name = _("Actions")
slug = "selected_actions"
help_text_template = "freezer_ui/jobs" \
"/_actions.html"
class Meta:
name = _("Clients")
slug = 'selected_clients'
class ActionsConfiguration(workflows.UpdateMembersStep):
action_class = ActionsConfigurationAction
help_text = _(
"Select the clients that will be backed up using this configuration.")
available_list_title = _("All Actions")
members_list_title = _("Selected Actions")
no_available_text = _("No actions found.")
no_members_text = _("No actions selected.")
class ClientsConfiguration(workflows.UpdateMembersStep):
action_class = ClientsConfigurationAction
help_text = _("From here you can add and remove clients to "
"this job from the list of available clients")
available_list_title = _("All Clients")
members_list_title = _("Available clients")
no_available_text = _("No clients found.")
no_members_text = _("No clients selected.")
show_roles = False
contributes = ("actions",)
contributes = ("clients",)
def contribute(self, data, context):
request = self.workflow.request
if data:
member_field_name = self.get_member_field_name('member')
context['actions'] = data.get(member_field_name, [])
field_name = self.get_member_field_name('member')
context["clients"] = request.POST.getlist(field_name)
return context
@ -185,7 +131,7 @@ class SchedulingConfiguration(workflows.Step):
action_class = SchedulingConfigurationAction
contributes = ('start_datetime',
'interval',
'end_datetime')
'end_datetime',)
class InfoConfigurationAction(workflows.Action):
@ -206,15 +152,15 @@ class InfoConfigurationAction(workflows.Action):
class InfoConfiguration(workflows.Step):
action_class = InfoConfigurationAction
contributes = ('description',
'original_name')
'original_name',)
class ConfigureJob(workflows.Workflow):
slug = "job"
name = _("Job Configuration")
finalize_button_name = _("Save")
success_message = _('Job file saved correctly.')
failure_message = _('Unable to save job file.')
success_message = _('Job created correctly.')
failure_message = _('Unable to created job.')
success_url = "horizon:freezer_ui:jobs:index"
default_steps = (InfoConfiguration,
ClientsConfiguration,
@ -223,9 +169,12 @@ class ConfigureJob(workflows.Workflow):
def handle(self, request, context):
try:
if context['original_name'] == '':
return freezer_api.job_create(request, context)
for client in context['clients']:
context['client_id'] = client
freezer_api.job_create(request, context)
else:
return freezer_api.job_edit(request, context)
return True
except Exception:
exceptions.handle(request)
return False

View File

@ -12,14 +12,14 @@
def create_dict_action(**kwargs):
"""
Create a dict only with values that exists so we avoid send keys with
"""Create a dict only with values that exists so we avoid send keys with
None values
"""
return {k: v for k, v in kwargs.items() if v}
class SessionJob(object):
"""Create a session object """
def __init__(self, job_id, session_id, client_id, status):
self.job_id = job_id
self.session_id = session_id