diff --git a/blazar_dashboard/content/hosts/tests.py b/blazar_dashboard/content/hosts/tests.py index 5c04af2..c4c8894 100644 --- a/blazar_dashboard/content/hosts/tests.py +++ b/blazar_dashboard/content/hosts/tests.py @@ -109,6 +109,31 @@ class HostsTests(test.BaseAdminViewTests): self.assertMessageCount(success=(len(host_names) + 1)) self.assertRedirectsNoFollow(res, INDEX_URL) + @test.create_stubs({blazar_api.client: ('host_list', 'host_create',), + api.nova: ('host_list',)}) + def test_create_hosts_with_extra_caps(self): + blazar_api.client.host_list(IsA(http.HttpRequest) + ).AndReturn([]) + api.nova.host_list(IsA(http.HttpRequest) + ).AndReturn(self.novahosts.list()) + host_names = [h.host_name for h in self.novahosts.list()] + for host_name in host_names: + blazar_api.client.host_create( + IsA(http.HttpRequest), + name=host_name, + extracap="strong" + ).AndReturn([]) + self.mox.ReplayAll() + form_data = { + 'select_hosts_role_member': host_names, + 'extra_caps': '{"extracap": "strong"}' + } + + res = self.client.post(CREATE_URL, form_data) + self.assertNoFormErrors(res) + self.assertMessageCount(success=(len(host_names) + 1)) + self.assertRedirectsNoFollow(res, INDEX_URL) + @test.create_stubs({blazar_api.client: ('host_list', 'host_delete')}) def test_delete_host(self): hosts = self.hosts.list() diff --git a/blazar_dashboard/content/hosts/workflows.py b/blazar_dashboard/content/hosts/workflows.py index 1f56724..1bf1635 100644 --- a/blazar_dashboard/content/hosts/workflows.py +++ b/blazar_dashboard/content/hosts/workflows.py @@ -67,12 +67,33 @@ class SelectHostsAction(workflows.MembershipAction): class AddExtraCapsAction(workflows.Action): - # TODO(hiro-kobayashi): Implement this class + extra_caps = forms.CharField( + label=_("Extra Capabilities"), + required=False, + help_text=_('Enter extra capabilities of hosts in JSON'), + widget=forms.Textarea( + attrs={'rows': 5}), + max_length=511) + class Meta(object): name = _("Extra Capabilities") - help_text = _("Not supported yet.") slug = "add_extra_caps" + def clean(self): + cleaned_data = super(AddExtraCapsAction, self).clean() + extra_caps = cleaned_data.get('extra_caps') + + if extra_caps: + try: + extra_caps = eval(extra_caps) + cleaned_data['extra_caps'] = extra_caps + except (SyntaxError, NameError): + raise forms.ValidationError( + _('Extra capabilities must written in JSON') + ) + + return cleaned_data + class SelectHostsStep(workflows.UpdateMembersStep): action_class = SelectHostsAction @@ -92,10 +113,14 @@ class SelectHostsStep(workflows.UpdateMembersStep): class AddExtraCapsStep(workflows.Step): - # TODO(hiro-kobayashi): Implement this class action_class = AddExtraCapsAction help_text = _("Add extra capabilities") show_roles = False + contributes = ("extra_caps",) + + def contribute(self, data, context): + context['extra_caps'] = data.get('extra_caps') + return context class CreateHostsWorkflow(workflows.Workflow): @@ -108,7 +133,11 @@ class CreateHostsWorkflow(workflows.Workflow): def handle(self, request, context): try: for name in context['names']: - blazar_api.client.host_create(request, name=name) + if context['extra_caps']: + blazar_api.client.host_create(request, name=name, + **context['extra_caps']) + else: + blazar_api.client.host_create(request, name=name) messages.success(request, _('Host %s was successfully ' 'created.') % name) except Exception: