Browse Source

Set useful port name when hostname is provided

It is useful to see the purpose of a port when viewing a port list or
port details. This change will set the port name to
<hostname>-<net-name> when the hostname is provided to the NICs
constructor.

Change-Id: Ic5789c0c3808afaa6b45408bce51d6c4ca86dbe5
tags/0.15.0^0
Steve Baker 1 month ago
parent
commit
a77cfb2ce6
3 changed files with 91 additions and 28 deletions
  1. 9
    3
      metalsmith/_nics.py
  2. 2
    1
      metalsmith/_provisioner.py
  3. 80
    24
      metalsmith/test/test_provisioner.py

+ 9
- 3
metalsmith/_nics.py View File

@@ -28,7 +28,7 @@ LOG = logging.getLogger(__name__)
28 28
 class NICs(object):
29 29
     """Requested NICs."""
30 30
 
31
-    def __init__(self, connection, node, nics):
31
+    def __init__(self, connection, node, nics, hostname=None):
32 32
         if nics is None:
33 33
             nics = []
34 34
 
@@ -43,6 +43,7 @@ class NICs(object):
43 43
         self._connection = connection
44 44
         self._nics = nics
45 45
         self._validated = None
46
+        self._hostname = hostname
46 47
         self.created_ports = []
47 48
         self.attached_ports = []
48 49
 
@@ -137,6 +138,8 @@ class NICs(object):
137 138
         port_args = {'network_id': network.id}
138 139
         if nic.get('fixed_ip'):
139 140
             port_args['fixed_ips'] = [{'ip_address': nic['fixed_ip']}]
141
+        if self._hostname:
142
+            port_args['name'] = '%s-%s' % (self._hostname, network.name)
140 143
 
141 144
         return port_args
142 145
 
@@ -166,8 +169,11 @@ class NICs(object):
166 169
                 'Cannot find network %(net)s for subnet %(sub)s: %(error)s' %
167 170
                 {'net': subnet.network_id, 'sub': nic['subnet'], 'error': exc})
168 171
 
169
-        return {'network_id': network.id,
170
-                'fixed_ips': [{'subnet_id': subnet.id}]}
172
+        port_args = {'network_id': network.id,
173
+                     'fixed_ips': [{'subnet_id': subnet.id}]}
174
+        if self._hostname:
175
+            port_args['name'] = '%s-%s' % (self._hostname, network.name)
176
+        return port_args
171 177
 
172 178
 
173 179
 def detach_and_delete_ports(connection, node, created_ports, attached_ports):

+ 2
- 1
metalsmith/_provisioner.py View File

@@ -382,7 +382,8 @@ class Provisioner(object):
382 382
                                          {'node': node, 'exc': exc})
383 383
 
384 384
         node, allocation = self._check_node_for_deploy(node, hostname)
385
-        nics = _nics.NICs(self.connection, node, nics)
385
+        nics = _nics.NICs(self.connection, node, nics,
386
+                          hostname=allocation and allocation.name or None)
386 387
 
387 388
         try:
388 389
             root_size_gb = _utils.get_root_disk(root_size_gb, node)

+ 80
- 24
metalsmith/test/test_provisioner.py View File

@@ -518,7 +518,9 @@ class TestProvisionNode(Base):
518 518
         self.assertEqual(inst.node, self.node)
519 519
 
520 520
         self.api.network.create_port.assert_called_once_with(
521
-            network_id=self.api.network.find_network.return_value.id)
521
+            network_id=self.api.network.find_network.return_value.id,
522
+            name='example.com-%s' %
523
+            self.api.network.find_network.return_value.name)
522 524
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
523 525
             self.node, self.api.network.create_port.return_value.id)
524 526
         self.api.baremetal.update_node.assert_called_once_with(
@@ -580,7 +582,9 @@ class TestProvisionNode(Base):
580 582
         self.assertEqual(inst.node, self.node)
581 583
 
582 584
         self.api.network.create_port.assert_called_once_with(
583
-            network_id=self.api.network.find_network.return_value.id)
585
+            network_id=self.api.network.find_network.return_value.id,
586
+            name='example.com-%s' %
587
+            self.api.network.find_network.return_value.name)
584 588
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
585 589
             self.node, self.api.network.create_port.return_value.id)
586 590
         self.api.baremetal.update_node.assert_called_once_with(
@@ -604,7 +608,9 @@ class TestProvisionNode(Base):
604 608
         config.generate.assert_called_once_with(self.node,
605 609
                                                 self.allocation.name)
606 610
         self.api.network.create_port.assert_called_once_with(
607
-            network_id=self.api.network.find_network.return_value.id)
611
+            network_id=self.api.network.find_network.return_value.id,
612
+            name='example.com-%s' %
613
+            self.api.network.find_network.return_value.name)
608 614
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
609 615
             self.node, self.api.network.create_port.return_value.id)
610 616
         self.api.baremetal.update_node.assert_called_once_with(
@@ -641,7 +647,9 @@ class TestProvisionNode(Base):
641 647
         self.api.baremetal.update_allocation.assert_called_once_with(
642 648
             self.allocation, name=hostname)
643 649
         self.api.network.create_port.assert_called_once_with(
644
-            network_id=self.api.network.find_network.return_value.id)
650
+            network_id=self.api.network.find_network.return_value.id,
651
+            name='control-0.example.com-%s' %
652
+            self.api.network.find_network.return_value.name)
645 653
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
646 654
             self.node, self.api.network.create_port.return_value.id)
647 655
         self.api.baremetal.update_node.assert_called_once_with(
@@ -667,7 +675,9 @@ class TestProvisionNode(Base):
667 675
 
668 676
         self.assertFalse(self.api.baremetal.update_allocation.called)
669 677
         self.api.network.create_port.assert_called_once_with(
670
-            network_id=self.api.network.find_network.return_value.id)
678
+            network_id=self.api.network.find_network.return_value.id,
679
+            name='control-0.example.com-%s' %
680
+            self.api.network.find_network.return_value.name)
671 681
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
672 682
             self.node, self.api.network.create_port.return_value.id)
673 683
         self.api.baremetal.update_node.assert_called_once_with(
@@ -695,7 +705,9 @@ class TestProvisionNode(Base):
695 705
 
696 706
         self.assertFalse(self.api.baremetal.update_allocation.called)
697 707
         self.api.network.create_port.assert_called_once_with(
698
-            network_id=self.api.network.find_network.return_value.id)
708
+            network_id=self.api.network.find_network.return_value.id,
709
+            name='control-0.example.com-%s' %
710
+            self.api.network.find_network.return_value.name)
699 711
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
700 712
             self.node, self.api.network.create_port.return_value.id)
701 713
         self.api.baremetal.update_node.assert_called_once_with(
@@ -750,7 +762,9 @@ class TestProvisionNode(Base):
750 762
         self.api.baremetal.update_allocation.assert_called_once_with(
751 763
             self.allocation, name=self.node.name)
752 764
         self.api.network.create_port.assert_called_once_with(
753
-            network_id=self.api.network.find_network.return_value.id)
765
+            network_id=self.api.network.find_network.return_value.id,
766
+            name='control-0-%s' %
767
+            self.api.network.find_network.return_value.name)
754 768
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
755 769
             self.node, self.api.network.create_port.return_value.id)
756 770
         self.api.baremetal.update_node.assert_called_once_with(
@@ -781,7 +795,9 @@ class TestProvisionNode(Base):
781 795
         self.assertIs(inst.allocation, self.allocation)
782 796
 
783 797
         self.api.network.create_port.assert_called_once_with(
784
-            network_id=self.api.network.find_network.return_value.id)
798
+            network_id=self.api.network.find_network.return_value.id,
799
+            name='000-%s' %
800
+            self.api.network.find_network.return_value.name)
785 801
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
786 802
             self.node, self.api.network.create_port.return_value.id)
787 803
         self.api.baremetal.update_node.assert_called_once_with(
@@ -813,7 +829,11 @@ class TestProvisionNode(Base):
813 829
             mock.call(self.node.id)
814 830
         ])
815 831
         self.api.network.create_port.assert_called_once_with(
816
-            network_id=self.api.network.find_network.return_value.id)
832
+            network_id=self.api.network.find_network.return_value.id,
833
+            name='%s-%s' % (
834
+                self.api.baremetal.wait_for_allocation.return_value.name,
835
+                self.api.network.find_network.return_value.name
836
+            ))
817 837
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
818 838
             self.node, self.api.network.create_port.return_value.id)
819 839
         self.api.baremetal.update_node.assert_called_once_with(
@@ -846,7 +866,11 @@ class TestProvisionNode(Base):
846 866
             mock.call(self.node.id)
847 867
         ])
848 868
         self.api.network.create_port.assert_called_once_with(
849
-            network_id=self.api.network.find_network.return_value.id)
869
+            network_id=self.api.network.find_network.return_value.id,
870
+            name='%s-%s' % (
871
+                self.api.baremetal.wait_for_allocation.return_value.name,
872
+                self.api.network.find_network.return_value.name
873
+            ))
850 874
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
851 875
             self.node, self.api.network.create_port.return_value.id)
852 876
         self.api.baremetal.update_node.assert_called_once_with(
@@ -908,6 +932,8 @@ class TestProvisionNode(Base):
908 932
 
909 933
         self.api.network.create_port.assert_called_once_with(
910 934
             network_id=self.api.network.find_network.return_value.id,
935
+            name='example.com-%s' %
936
+            self.api.network.find_network.return_value.name,
911 937
             fixed_ips=[{'ip_address': '10.0.0.2'}])
912 938
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
913 939
             self.node, self.api.network.create_port.return_value.id)
@@ -929,6 +955,8 @@ class TestProvisionNode(Base):
929 955
 
930 956
         self.api.network.create_port.assert_called_once_with(
931 957
             network_id=self.api.network.get_network.return_value.id,
958
+            name='example.com-%s' %
959
+            self.api.network.get_network.return_value.name,
932 960
             fixed_ips=[{'subnet_id':
933 961
                         self.api.network.find_subnet.return_value.id}])
934 962
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
@@ -954,7 +982,9 @@ class TestProvisionNode(Base):
954 982
         self.pr.provision_node(self.node, 'image', [{'network': 'network'}])
955 983
 
956 984
         self.api.network.create_port.assert_called_once_with(
957
-            network_id=self.api.network.find_network.return_value.id)
985
+            network_id=self.api.network.find_network.return_value.id,
986
+            name='example.com-%s' %
987
+            self.api.network.find_network.return_value.name)
958 988
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
959 989
             self.node, self.api.network.create_port.return_value.id)
960 990
         self.api.baremetal.update_node.assert_called_once_with(
@@ -982,7 +1012,9 @@ class TestProvisionNode(Base):
982 1012
 
983 1013
         self.assertFalse(self.api.image.find_image.called)
984 1014
         self.api.network.create_port.assert_called_once_with(
985
-            network_id=self.api.network.find_network.return_value.id)
1015
+            network_id=self.api.network.find_network.return_value.id,
1016
+            name='example.com-%s' %
1017
+            self.api.network.find_network.return_value.name)
986 1018
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
987 1019
             self.node, self.api.network.create_port.return_value.id)
988 1020
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1017,7 +1049,9 @@ abcd  image
1017 1049
         self.assertFalse(self.api.image.find_image.called)
1018 1050
         mock_get.assert_called_once_with('https://host/checksums')
1019 1051
         self.api.network.create_port.assert_called_once_with(
1020
-            network_id=self.api.network.find_network.return_value.id)
1052
+            network_id=self.api.network.find_network.return_value.id,
1053
+            name='example.com-%s' %
1054
+            self.api.network.find_network.return_value.name)
1021 1055
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1022 1056
             self.node, self.api.network.create_port.return_value.id)
1023 1057
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1048,7 +1082,9 @@ abcd  image
1048 1082
 
1049 1083
         self.assertFalse(self.api.image.find_image.called)
1050 1084
         self.api.network.create_port.assert_called_once_with(
1051
-            network_id=self.api.network.find_network.return_value.id)
1085
+            network_id=self.api.network.find_network.return_value.id,
1086
+            name='example.com-%s' %
1087
+            self.api.network.find_network.return_value.name)
1052 1088
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1053 1089
             self.node, self.api.network.create_port.return_value.id)
1054 1090
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1076,7 +1112,9 @@ abcd  image
1076 1112
 
1077 1113
         self.assertFalse(self.api.image.find_image.called)
1078 1114
         self.api.network.create_port.assert_called_once_with(
1079
-            network_id=self.api.network.find_network.return_value.id)
1115
+            network_id=self.api.network.find_network.return_value.id,
1116
+            name='example.com-%s' %
1117
+            self.api.network.find_network.return_value.name)
1080 1118
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1081 1119
             self.node, self.api.network.create_port.return_value.id)
1082 1120
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1107,7 +1145,9 @@ abcd  image
1107 1145
 
1108 1146
         self.assertFalse(self.api.image.find_image.called)
1109 1147
         self.api.network.create_port.assert_called_once_with(
1110
-            network_id=self.api.network.find_network.return_value.id)
1148
+            network_id=self.api.network.find_network.return_value.id,
1149
+            name='example.com-%s' %
1150
+            self.api.network.find_network.return_value.name)
1111 1151
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1112 1152
             self.node, self.api.network.create_port.return_value.id)
1113 1153
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1126,7 +1166,9 @@ abcd  image
1126 1166
                                root_size_gb=50)
1127 1167
 
1128 1168
         self.api.network.create_port.assert_called_once_with(
1129
-            network_id=self.api.network.find_network.return_value.id)
1169
+            network_id=self.api.network.find_network.return_value.id,
1170
+            name='example.com-%s' %
1171
+            self.api.network.find_network.return_value.name)
1130 1172
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1131 1173
             self.node, self.api.network.create_port.return_value.id)
1132 1174
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1145,7 +1187,9 @@ abcd  image
1145 1187
                                swap_size_mb=4096)
1146 1188
 
1147 1189
         self.api.network.create_port.assert_called_once_with(
1148
-            network_id=self.api.network.find_network.return_value.id)
1190
+            network_id=self.api.network.find_network.return_value.id,
1191
+            name='example.com-%s' %
1192
+            self.api.network.find_network.return_value.name)
1149 1193
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1150 1194
             self.node, self.api.network.create_port.return_value.id)
1151 1195
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1168,7 +1212,9 @@ abcd  image
1168 1212
         self.assertEqual(inst.node, self.node)
1169 1213
 
1170 1214
         self.api.network.create_port.assert_called_once_with(
1171
-            network_id=self.api.network.find_network.return_value.id)
1215
+            network_id=self.api.network.find_network.return_value.id,
1216
+            name='example.com-%s' %
1217
+            self.api.network.find_network.return_value.name)
1172 1218
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1173 1219
             self.node, self.api.network.create_port.return_value.id)
1174 1220
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1191,7 +1237,9 @@ abcd  image
1191 1237
         self.assertEqual(inst.node, self.node)
1192 1238
 
1193 1239
         self.api.network.create_port.assert_called_once_with(
1194
-            network_id=self.api.network.find_network.return_value.id)
1240
+            network_id=self.api.network.find_network.return_value.id,
1241
+            name='example.com-%s' %
1242
+            self.api.network.find_network.return_value.name)
1195 1243
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1196 1244
             self.node, self.api.network.create_port.return_value.id)
1197 1245
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1216,7 +1264,9 @@ abcd  image
1216 1264
         self.assertEqual(inst.node, self.node)
1217 1265
 
1218 1266
         self.api.network.create_port.assert_called_once_with(
1219
-            network_id=self.api.network.find_network.return_value.id)
1267
+            network_id=self.api.network.find_network.return_value.id,
1268
+            name='example.com-%s' %
1269
+            self.api.network.find_network.return_value.name)
1220 1270
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1221 1271
             self.node, self.api.network.create_port.return_value.id)
1222 1272
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1238,7 +1288,9 @@ abcd  image
1238 1288
         self.assertEqual(inst.node, self.node)
1239 1289
 
1240 1290
         self.api.network.create_port.assert_called_once_with(
1241
-            network_id=self.api.network.find_network.return_value.id)
1291
+            network_id=self.api.network.find_network.return_value.id,
1292
+            name='example.com-%s' %
1293
+            self.api.network.find_network.return_value.name)
1242 1294
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1243 1295
             self.node, self.api.network.create_port.return_value.id)
1244 1296
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1261,7 +1313,9 @@ abcd  image
1261 1313
         self.assertEqual(inst.node, self.node)
1262 1314
 
1263 1315
         self.api.network.create_port.assert_called_once_with(
1264
-            network_id=self.api.network.find_network.return_value.id)
1316
+            network_id=self.api.network.find_network.return_value.id,
1317
+            name='example.com-%s' %
1318
+            self.api.network.find_network.return_value.name)
1265 1319
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1266 1320
             self.node, self.api.network.create_port.return_value.id)
1267 1321
         self.api.baremetal.update_node.assert_called_once_with(
@@ -1282,7 +1336,9 @@ abcd  image
1282 1336
                                wait=3600)
1283 1337
 
1284 1338
         self.api.network.create_port.assert_called_once_with(
1285
-            network_id=self.api.network.find_network.return_value.id)
1339
+            network_id=self.api.network.find_network.return_value.id,
1340
+            name='example.com-%s' %
1341
+            self.api.network.find_network.return_value.name)
1286 1342
         self.api.baremetal.attach_vif_to_node.assert_called_once_with(
1287 1343
             self.node, self.api.network.create_port.return_value.id)
1288 1344
         self.api.baremetal.update_node.assert_called_once_with(

Loading…
Cancel
Save