Browse Source

Allow overriding traits in provision_node (similar to capabilities)

Change-Id: If6cfba9773dd2b5896d359d2bd1aa12f4062cfdf
Story: #2003685
Task: #26356
tags/0.7.0
Dmitry Tantsur 10 months ago
parent
commit
c06e2c9baf
2 changed files with 52 additions and 1 deletions
  1. 7
    1
      metalsmith/_provisioner.py
  2. 45
    0
      metalsmith/test/test_provisioner.py

+ 7
- 1
metalsmith/_provisioner.py View File

@@ -196,7 +196,7 @@ class Provisioner(object):
196 196
 
197 197
     def provision_node(self, node, image, nics=None, root_disk_size=None,
198 198
                        config=None, hostname=None, netboot=False,
199
-                       capabilities=None, wait=None):
199
+                       capabilities=None, traits=None, wait=None):
200 200
         """Provision the node with the given image.
201 201
 
202 202
         Example::
@@ -227,6 +227,10 @@ class Provisioner(object):
227 227
             overwrites the capabilities set by :meth:`reserve_node`.
228 228
             Note that the capabilities are not checked against the ones
229 229
             provided by the node - use :meth:`reserve_node` for that.
230
+        :param traits: Requested traits of the node. If present, overwrites
231
+            the traits set by :meth:`reserve_node`. Note that the traits are
232
+            not checked against the ones provided by the node - use
233
+            :meth:`reserve_node` for that.
230 234
         :param wait: How many seconds to wait for the deployment to finish,
231 235
             None to return immediately.
232 236
         :return: :py:class:`metalsmith.Instance` object with the current
@@ -270,6 +274,8 @@ class Provisioner(object):
270 274
                        '/extra/%s' % _ATTACHED_PORTS: attached_ports,
271 275
                        '/instance_info/%s' % _os_api.HOSTNAME_FIELD: hostname}
272 276
             updates.update(image._node_updates(self.connection))
277
+            if traits is not None:
278
+                updates['/instance_info/traits'] = traits
273 279
 
274 280
             LOG.debug('Updating node %(node)s with %(updates)s',
275 281
                       {'node': _utils.log_node(node), 'updates': updates})

+ 45
- 0
metalsmith/test/test_provisioner.py View File

@@ -675,6 +675,51 @@ abcd  image
675 675
         self.assertFalse(self.api.release_node.called)
676 676
         self.assertFalse(self.conn.network.delete_port.called)
677 677
 
678
+    def test_with_traits(self):
679
+        inst = self.pr.provision_node(self.node, 'image',
680
+                                      [{'network': 'network'}],
681
+                                      traits=['1', '2'])
682
+        self.updates['/instance_info/traits'] = ['1', '2']
683
+
684
+        self.assertEqual(inst.uuid, self.node.uuid)
685
+        self.assertEqual(inst.node, self.node)
686
+
687
+        self.conn.network.create_port.assert_called_once_with(
688
+            network_id=self.conn.network.find_network.return_value.id)
689
+        self.api.attach_port_to_node.assert_called_once_with(
690
+            self.node.uuid, self.conn.network.create_port.return_value.id)
691
+        self.api.update_node.assert_called_once_with(self.node, self.updates)
692
+        self.api.validate_node.assert_called_once_with(self.node,
693
+                                                       validate_deploy=True)
694
+        self.api.node_action.assert_called_once_with(self.node, 'active',
695
+                                                     configdrive=mock.ANY)
696
+        self.assertFalse(self.wait_mock.called)
697
+        self.assertFalse(self.api.release_node.called)
698
+        self.assertFalse(self.conn.network.delete_port.called)
699
+
700
+    def test_override_existing_traits(self):
701
+        self.node.traits = ['42']
702
+        inst = self.pr.provision_node(self.node, 'image',
703
+                                      [{'network': 'network'}],
704
+                                      traits=['1', '2'])
705
+        self.updates['/instance_info/traits'] = ['1', '2']
706
+
707
+        self.assertEqual(inst.uuid, self.node.uuid)
708
+        self.assertEqual(inst.node, self.node)
709
+
710
+        self.conn.network.create_port.assert_called_once_with(
711
+            network_id=self.conn.network.find_network.return_value.id)
712
+        self.api.attach_port_to_node.assert_called_once_with(
713
+            self.node.uuid, self.conn.network.create_port.return_value.id)
714
+        self.api.update_node.assert_called_once_with(self.node, self.updates)
715
+        self.api.validate_node.assert_called_once_with(self.node,
716
+                                                       validate_deploy=True)
717
+        self.api.node_action.assert_called_once_with(self.node, 'active',
718
+                                                     configdrive=mock.ANY)
719
+        self.assertFalse(self.wait_mock.called)
720
+        self.assertFalse(self.api.release_node.called)
721
+        self.assertFalse(self.conn.network.delete_port.called)
722
+
678 723
     def test_with_wait(self):
679 724
         self.conn.network.find_port.return_value = mock.Mock(
680 725
             spec=['fixed_ips'],

Loading…
Cancel
Save