RingBuilder.add_dev returns device id

When added a new devices into builder the add_dev function returns it
unique id.

blueprint argparse-in-swift-ring-builder

Change-Id: I57080bb625e812f6cea71199df907a44b332b552
This commit is contained in:
Ilya Kharin 2013-05-16 18:44:41 +04:00
parent 43bf568f48
commit 3957dbc5d4
3 changed files with 13 additions and 8 deletions

View File

@ -335,10 +335,9 @@ swift-ring-builder <builder_file> 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)

View File

@ -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):
"""

View File

@ -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)