[Feature] libvirt xml: pass rng to vm
[Fix] Doc

Issue: - It is not possible to pass [R]andom [N]umber [G]enerator
         device to libvirt guest xml in order to control entropy.

       - Doc has no information on how to provision vms using salt

Solution: - Pass rng parameters via kwargs from node: pillar
            Attach rng xml object to generated xml.

          - Provide with an example

Prod-Related: PROD-19214
Customer-Found
Change-Id: Iea111f2d927edf46f06bb7ccfad06d37b752fba9
diff --git a/README.rst b/README.rst
index 0b5160f..68e9264 100644
--- a/README.rst
+++ b/README.rst
@@ -340,6 +340,47 @@
 
     salt-call event.send 'salt/key/remove'
 
+Control VM provisioning
+
+.. code-block:: yaml
+
+    virt:
+      disk:
+        three_disks:
+          - system:
+              size: 4096
+              image: ubuntu.qcow
+          - repository_snapshot:
+              size: 8192
+              image: snapshot.qcow
+          - cinder-volume:
+              size: 2048
+
+    salt:
+      control:
+        enabled: true
+        virt_enabled: true
+        size:
+          medium_three_disks:
+            cpu: 2
+            ram: 4
+            disk_profile: three_disks
+        cluster:
+          mycluster:
+            domain: neco.virt.domain.com
+            engine: virt
+            node:
+              ubuntu1:
+                provider: node01.domain.com
+                image: ubuntu.qcow
+                size: medium
+                img_dest: /var/lib/libvirt/ssdimages
+                rng:
+                  backend: /dev/urandom
+                  model: random
+                  rate:
+                    period: '1800'
+                    bytes: '1500'
 
 Jinja options
 -------------
diff --git a/_modules/virtng.py b/_modules/virtng.py
index 304a6bb..e664f68 100644
--- a/_modules/virtng.py
+++ b/_modules/virtng.py
@@ -550,6 +550,7 @@
          start=True,  # pylint: disable=redefined-outer-name
          disk='default',
          saltenv='base',
+         rng={},
          **kwargs):
     '''
     Initialize a new vm
@@ -667,6 +668,28 @@
                                           .format(hypervisor))
 
     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
+    if rng:
+        rng_model = rng.get('model', 'random')
+        rng_backend = rng.get('backend', '/dev/urandom')
+        xml_doc = minidom.parseString(xml)
+        rng_xml = xml_doc.createElement("rng")
+        rng_xml.setAttribute("model", "virtio")
+        backend = xml_doc.createElement("backend")
+        backend.setAttribute("model", rng_model)
+        backend.appendChild(xml_doc.createTextNode(rng_backend))
+        rng_xml.appendChild(backend)
+        if 'rate' in rng:
+            rng_rate_period = rng['rate'].get('period', '2000')
+            rng_rate_bytes = rng['rate'].get('bytes', '1234')
+            rate = xml_doc.createElement("rate")
+            rate.setAttribute("period", rng_rate_period)
+            rate.setAttribute("bytes", rng_rate_bytes)
+            rng_xml.appendChild(rate)
+        xml_doc.getElementsByTagName("domain")[0].getElementsByTagName("devices")[0].appendChild(rng_xml)
+        xml = xml_doc.toxml()
+
     define_xml_str(xml)
 
     if start:
diff --git a/salt/control/virt.sls b/salt/control/virt.sls
index 84d9c12..f546c29 100644
--- a/salt/control/virt.sls
+++ b/salt/control/virt.sls
@@ -41,6 +41,9 @@
   - start: True
   - disk: {{ size.disk_profile }}
   - nic: {{ size.net_profile }}
+  {%- if node.rng is defined %}
+  - rng: {{ node.rng }}
+  {%- endif %}
   - kwargs:
       seed: True
       serial_type: pty
diff --git a/tests/pillar/control_virt_custom.sls b/tests/pillar/control_virt_custom.sls
index 7397494..6ca02ad 100644
--- a/tests/pillar/control_virt_custom.sls
+++ b/tests/pillar/control_virt_custom.sls
@@ -53,3 +53,9 @@
             provider: node03.domain.com
             image: meowbuntu.qcom2
             size: medium_three_disks
+            rng:
+              backend: /dev/urandom
+              model: random
+              rate:
+                period: '1800'
+                bytes: '1500'