diff --git a/bin/swift-ring-builder b/bin/swift-ring-builder index 161373bbba..334f512660 100755 --- a/bin/swift-ring-builder +++ b/bin/swift-ring-builder @@ -335,10 +335,9 @@ swift-ring-builder add 'replication_port': replication_port, 'device': device_name, 'weight': weight, 'meta': meta} - builder.add_dev(dev_params) - new_dev = builder.search_devs(dev_params)[0] + dev_id = builder.add_dev(dev_params) print('Device %s with %s weight got id %s' % - (format_device(new_dev), weight, new_dev['id'])) + (format_device(dev_params), weight, dev_id)) builder.save(argv[1]) exit(EXIT_SUCCESS) diff --git a/swift/common/ring/builder.py b/swift/common/ring/builder.py index 961b7d8252..ba36f6ddbe 100644 --- a/swift/common/ring/builder.py +++ b/swift/common/ring/builder.py @@ -257,6 +257,8 @@ class RingBuilder(object): make multiple changes for a single rebalance. :param dev: device dict + + :returns: id of device """ if 'id' not in dev: dev['id'] = 0 @@ -274,6 +276,7 @@ class RingBuilder(object): self._set_parts_wanted() self.devs_changed = True self.version += 1 + return dev['id'] def set_dev_weight(self, dev_id, weight): """ diff --git a/test/unit/common/ring/test_builder.py b/test/unit/common/ring/test_builder.py index c2ac395e81..b2ab2d96ed 100644 --- a/test/unit/common/ring/test_builder.py +++ b/test/unit/common/ring/test_builder.py @@ -124,17 +124,20 @@ class TestRingBuilder(unittest.TestCase): rb = ring.RingBuilder(8, 3, 1) dev = {'id': 0, 'region': 0, 'zone': 0, 'weight': 1, 'ip': '127.0.0.1', 'port': 10000} - rb.add_dev(dev) + dev_id = rb.add_dev(dev) self.assertRaises(exceptions.DuplicateDeviceError, rb.add_dev, dev) + self.assertEqual(dev_id, 0) rb = ring.RingBuilder(8, 3, 1) #test add new dev with no id - rb.add_dev({'zone': 0, 'region': 1, 'weight': 1, - 'ip': '127.0.0.1', 'port': 6000}) + dev_id = rb.add_dev({'zone': 0, 'region': 1, 'weight': 1, + 'ip': '127.0.0.1', 'port': 6000}) self.assertEquals(rb.devs[0]['id'], 0) + self.assertEqual(dev_id, 0) #test add another dev with no id - rb.add_dev({'zone': 3, 'region': 2, 'weight': 1, - 'ip': '127.0.0.1', 'port': 6000}) + dev_id = rb.add_dev({'zone': 3, 'region': 2, 'weight': 1, + 'ip': '127.0.0.1', 'port': 6000}) self.assertEquals(rb.devs[1]['id'], 1) + self.assertEqual(dev_id, 1) def test_set_dev_weight(self): rb = ring.RingBuilder(8, 3, 1)