Browse Source

Support create member with multiple NICs

Added Networking Tab to member create work flow.
Deault provider PTG will be shown as selected
NIC, additionally user can select other PTGs
to to add multiple NICs

Change-Id: I3ceadc0f2ca9de6d9be2c4349eba6623a9aa4cd4
Closes-Bug: #1491540
tags/2015.2.0b1
ank 3 years ago
parent
commit
276a0fd1ab

+ 48
- 0
gbpui/panels/policytargets/templates/policytargets/_update_groups.html View File

@@ -0,0 +1,48 @@
1
+{% load i18n %}
2
+
3
+<noscript><h3>{{ step }}</h3></noscript>
4
+<table class="table-fixed" id="networkListSortContainer">
5
+  <tbody>
6
+    <tr>
7
+      <td class="actions">
8
+        <label id="selected_network_label">{% trans "Selected groups" %}</label>
9
+        <ul id="selected_network" class="networklist">
10
+        </ul>
11
+        <label>{% trans "Available groups" %}</label>
12
+        <ul id="available_network" class="networklist">
13
+        </ul>
14
+      </td>
15
+      <td class="help_text">
16
+          <p>{% blocktrans %}Choose group from Available groups to Selected 
17
+              groups by push button or drag and drop, you may change group order 
18
+              by drag and drop as well. {% endblocktrans %}</p>
19
+      </td>
20
+    </tr>
21
+  </tbody>
22
+</table>
23
+
24
+<table class="table-fixed" id="networkListIdContainer">
25
+  <tbody>
26
+    <tr>
27
+      <td class="actions">
28
+          <div id="networkListId">
29
+            {% include "horizon/common/_form_fields.html" %}
30
+          </div>
31
+      </td>
32
+      <td class="help_text">
33
+          {{ step.get_help_text }}
34
+      </td>
35
+    </tr>
36
+  </tbody>
37
+</table>
38
+
39
+
40
+<script>
41
+  if (typeof $ !== 'undefined') {
42
+    horizon.instances.workflow_init($(".workflow"));
43
+  } else {
44
+    addHorizonLoadEvent(function() {
45
+      horizon.instances.workflow_init($(".workflow"));
46
+    });
47
+  }
48
+</script>

+ 64
- 6
gbpui/panels/policytargets/workflows.py View File

@@ -13,6 +13,7 @@
13 13
 import logging
14 14
 
15 15
 from django.core.urlresolvers import reverse
16
+from django import shortcuts
16 17
 from django.utils import html
17 18
 from django.utils.text import normalize_newlines  # noqa
18 19
 from django.utils.translation import ugettext_lazy as _
@@ -389,6 +390,60 @@ class SetAccessControls(workflows.Step):
389 390
         return context
390 391
 
391 392
 
393
+class SetGroupAction(workflows.Action):
394
+    # To reuse horizon instance launch code related to Networking,
395
+    # form filed must be 'network' only
396
+    network = forms.MultipleChoiceField(label=_("Groups"),
397
+                                        widget=forms.CheckboxSelectMultiple(),
398
+                                        error_messages={
399
+                                            'required': _(
400
+                                                "At least one group must"
401
+                                                " be specified.")},
402
+                                        help_text=_("Launch member instance in"
403
+                                                    " these groups"))
404
+
405
+    widget = forms.HiddenInput()
406
+
407
+    def __init__(self, request, *args, **kwargs):
408
+        super(SetGroupAction, self).__init__(request, *args, **kwargs)
409
+        policy_targetid = self.request.path.split("/")[-2]
410
+        self.fields['network'].initial = [policy_targetid]
411
+
412
+    class Meta(object):
413
+        name = _("Groups")
414
+        help_text = _("Select groups for launching the member instance in.")
415
+
416
+    def populate_network_choices(self, request, context):
417
+        try:
418
+            pt_list = []
419
+            pts = client.policy_target_list(request,
420
+                tenant_id=request.user.tenant_id)
421
+            for pt in pts:
422
+                pt.set_id_as_name_if_empty()
423
+                pt_list.append((pt.id, pt.name))
424
+            return sorted(pt_list, key=lambda obj: obj[1])
425
+        except Exception:
426
+            msg = _("Failed to retrieve groups")
427
+            LOG.error(msg)
428
+            exceptions.handle(request, msg, redirect=shortcuts.redirect)
429
+
430
+
431
+class SetGroup(workflows.Step):
432
+    action_class = SetGroupAction
433
+
434
+    template_name = "project/policytargets/_update_groups.html"
435
+    contributes = ("group_id",)
436
+
437
+    def contribute(self, data, context):
438
+        if data:
439
+            groups = self.workflow.request.POST.getlist("network")
440
+            groups = [n for n in groups if n != '']
441
+            if groups:
442
+                context['group_id'] = groups
443
+
444
+        return context
445
+
446
+
392 447
 class LaunchInstance(workflows.Workflow):
393 448
     slug = "create_member"
394 449
     name = _("Create Member")
@@ -398,6 +453,7 @@ class LaunchInstance(workflows.Workflow):
398 453
     default_steps = (workflows_create_instance.SelectProjectUser,
399 454
                      workflows_create_instance.SetInstanceDetails,
400 455
                      SetAccessControls,
456
+                     SetGroup,
401 457
                      workflows_create_instance.PostCreationStep,
402 458
                      workflows_create_instance.SetAdvanced)
403 459
 
@@ -417,7 +473,6 @@ class LaunchInstance(workflows.Workflow):
417 473
     @sensitive_variables('context')
418 474
     def handle(self, request, context):
419 475
         custom_script = context.get('script_data', '')
420
-
421 476
         dev_mapping_1 = None
422 477
         dev_mapping_2 = None
423 478
 
@@ -474,7 +529,6 @@ class LaunchInstance(workflows.Workflow):
474 529
             ]
475 530
         avail_zone = context.get('availability_zone', None)
476 531
         try:
477
-            policy_target_id = self.request.path.split("/")[-2]
478 532
             instance_count = int(context['count'])
479 533
             count = 1
480 534
             while count <= instance_count:
@@ -482,9 +536,12 @@ class LaunchInstance(workflows.Workflow):
482 536
                     instance_name = context['name']
483 537
                 else:
484 538
                     instance_name = context['name'] + str(count)
485
-                ep = client.pt_create(
486
-                    request, policy_target_group_id=policy_target_id,
487
-                    name=instance_name[:41] + "_gbpui")
539
+                nics = []
540
+                for ptg_id in context['group_id']:
541
+                    ep = client.pt_create(
542
+                        request, policy_target_group_id=ptg_id,
543
+                        name=instance_name[:41] + "_gbpui")
544
+                    nics.append({'port-id': ep.port_id})
488 545
                 api.nova.server_create(request,
489 546
                                    instance_name,
490 547
                                    image_id,
@@ -494,7 +551,7 @@ class LaunchInstance(workflows.Workflow):
494 551
                                    security_groups=None,
495 552
                                    block_device_mapping=dev_mapping_1,
496 553
                                    block_device_mapping_v2=dev_mapping_2,
497
-                                   nics=[{'port-id': ep.port_id}],
554
+                                   nics=nics,
498 555
                                    availability_zone=avail_zone,
499 556
                                    instance_count=1,
500 557
                                    admin_pass=context['admin_pass'],
@@ -507,6 +564,7 @@ class LaunchInstance(workflows.Workflow):
507 564
             msg = error % {'count': count, 'name': instance_name}
508 565
             LOG.error(msg)
509 566
             u = "horizon:project:policytargets:policy_targetdetails"
567
+            policy_target_id = self.request.path.split("/")[-2]
510 568
             redirect = reverse(u, kwargs={'policy_target_id':
511 569
                 policy_target_id})
512 570
             exceptions.handle(request, msg, redirect=redirect)

Loading…
Cancel
Save