Merge "Add option to overrided minion id without scrict linux formula dependency"
diff --git a/README.rst b/README.rst
index 3660f24..d3bdc0f 100644
--- a/README.rst
+++ b/README.rst
@@ -404,6 +404,11 @@
         - name: nic04
           bridge: br-public
           model: virtio
+        - name: nic05
+          bridge: br-prv
+          model: virtio
+          virtualport:
+            type: openvswitch
 
 
     salt:
diff --git a/_modules/virtng.py b/_modules/virtng.py
index f9f93b9..ce09508 100644
--- a/_modules/virtng.py
+++ b/_modules/virtng.py
@@ -430,67 +430,11 @@
 
 def _nic_profile(profile_name, hypervisor, **kwargs):
 
-    default = [{'eth0': {}}]
-    vmware_overlay = {'type': 'bridge', 'source': 'DEFAULT', 'model': 'e1000'}
-    kvm_overlay = {'type': 'bridge', 'source': 'br0', 'model': 'virtio'}
-    overlays = {
-            'kvm': kvm_overlay,
-            'qemu': kvm_overlay,
-            'esxi': vmware_overlay,
-            'vmware': vmware_overlay,
-            }
-
-    # support old location
-    config_data = __salt__['config.option']('virt.nic', {}).get(
-        profile_name, None
-    )
-
-    if config_data is None:
-        config_data = __salt__['config.get']('virt:nic', {}).get(
-            profile_name, default
-        )
-
-    interfaces = []
-
     def append_dict_profile_to_interface_list(profile_dict):
         for interface_name, attributes in profile_dict.items():
             attributes['name'] = interface_name
             interfaces.append(attributes)
 
-    # old style dicts (top-level dicts)
-    #
-    # virt:
-    #    nic:
-    #        eth0:
-    #            bridge: br0
-    #        eth1:
-    #            network: test_net
-    if isinstance(config_data, dict):
-        append_dict_profile_to_interface_list(config_data)
-
-    # new style lists (may contain dicts)
-    #
-    # virt:
-    #   nic:
-    #     - eth0:
-    #         bridge: br0
-    #     - eth1:
-    #         network: test_net
-    #
-    # virt:
-    #   nic:
-    #     - name: eth0
-    #       bridge: br0
-    #     - name: eth1
-    #       network: test_net
-    elif isinstance(config_data, list):
-        for interface in config_data:
-            if isinstance(interface, dict):
-                if len(interface) == 1:
-                    append_dict_profile_to_interface_list(interface)
-                else:
-                    interfaces.append(interface)
-
     def _normalize_net_types(attributes):
         '''
         Guess which style of definition:
@@ -514,6 +458,7 @@
 
         attributes['type'] = attributes.get('type', None)
         attributes['source'] = attributes.get('source', None)
+        attributes['virtualport'] = attributes.get('virtualport', None)
 
     def _apply_default_overlay(attributes):
         for key, value in overlays[hypervisor].items():
@@ -532,6 +477,40 @@
         else:
             attributes['mac'] = salt.utils.gen_mac()
 
+
+    default = [{'eth0': {}}]
+    vmware_overlay = {'type': 'bridge', 'source': 'DEFAULT', 'model': 'e1000'}
+    kvm_overlay = {'type': 'bridge', 'source': 'br0', 'model': 'virtio'}
+    overlays = {
+            'kvm': kvm_overlay,
+            'qemu': kvm_overlay,
+            'esxi': vmware_overlay,
+            'vmware': vmware_overlay,
+            }
+
+    # support old location
+    config_data = __salt__['config.option']('virt.nic', {}).get(
+        profile_name, None
+    )
+
+    if config_data is None:
+        config_data = __salt__['config.get']('virt:nic', {}).get(
+            profile_name, default
+        )
+
+    interfaces = []
+
+    if isinstance(config_data, dict):
+        append_dict_profile_to_interface_list(config_data)
+
+    elif isinstance(config_data, list):
+        for interface in config_data:
+            if isinstance(interface, dict):
+                if len(interface) == 1:
+                    append_dict_profile_to_interface_list(interface)
+                else:
+                    interfaces.append(interface)
+
     for interface in interfaces:
         _normalize_net_types(interface)
         _assign_mac(interface)
@@ -671,6 +650,18 @@
     xml = _gen_xml(name, cpu, mem, diskp, nicp, hypervisor, **kwargs)
 
     # TODO: Remove this code and refactor module, when salt-common would have updated libvirt_domain.jinja template
+    for _nic in nicp:
+        if _nic['virtualport']:
+            xml_doc = minidom.parseString(xml)
+            interfaces = xml_doc.getElementsByTagName("domain")[0].getElementsByTagName("devices")[0].getElementsByTagName("interface")
+            for interface in interfaces:
+                if interface.getElementsByTagName('mac')[0].getAttribute('address').lower() == _nic['mac'].lower():
+                    vport_xml = xml_doc.createElement("virtualport")
+                    vport_xml.setAttribute("type", _nic['virtualport']['type'])
+                    interface.appendChild(vport_xml)
+            xml = xml_doc.toxml()
+
+    # TODO: Remove this code and refactor module, when salt-common would have updated libvirt_domain.jinja template
     if rng:
         rng_model = rng.get('model', 'random')
         rng_backend = rng.get('backend', '/dev/urandom')
diff --git a/salt/control/virt.sls b/salt/control/virt.sls
index d508252..a2e56ff 100644
--- a/salt/control/virt.sls
+++ b/salt/control/virt.sls
@@ -20,7 +20,7 @@
 
 {%- if cluster.engine == "virt" %}
 
-libvirt_service:
+salt_libvirt_service:
   service.running:
   - name: {{ control.virt_service }}
   - enable: true
@@ -72,7 +72,7 @@
       {%- endif %}
   - unless: virsh list --all --name| grep -E "^{{ node_name }}.{{ cluster.domain }}$"
   - require:
-    - libvirt_service
+    - salt_libvirt_service
 
 #salt_control_seed_{{ cluster_name }}_{{ node_name }}:
 #  module.run:
diff --git a/tests/pillar/control_virt_custom.sls b/tests/pillar/control_virt_custom.sls
index 6ca02ad..71cf37f 100644
--- a/tests/pillar/control_virt_custom.sls
+++ b/tests/pillar/control_virt_custom.sls
@@ -9,6 +9,25 @@
           image: snapshot.qcow
       - cinder-volume:
           size: 2048
+  nic:
+    control:
+    - name: nic01
+      bridge: br-pxe
+      model: virtio
+    - name: nic02
+      bridge: br-cp
+      model: virtio
+    - name: nic03
+      bridge: br-store-front
+      model: virtio
+    - name: nic04
+      bridge: br-public
+      model: virtio
+    - name: nic05
+      bridge: br-prv
+      model: virtio
+      virtualport:
+        type: openvswitch
 salt:
   minion:
     enabled: true