Merge "Add extra capabilities for create host"

This commit is contained in:
Zuul 2017-11-02 09:38:28 +00:00 committed by Gerrit Code Review
commit a2cba83d7a
2 changed files with 58 additions and 4 deletions

View File

@ -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()

View File

@ -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: