Merge "RingBuilder.add_dev returns device id"

This commit is contained in:
Jenkins 2013-05-29 00:10:22 +00:00 committed by Gerrit Code Review
commit a2db3b6f32
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, 'replication_port': replication_port,
'device': device_name, 'weight': weight, 'device': device_name, 'weight': weight,
'meta': meta} 'meta': meta}
builder.add_dev(dev_params) dev_id = builder.add_dev(dev_params)
new_dev = builder.search_devs(dev_params)[0]
print('Device %s with %s weight got id %s' % 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]) builder.save(argv[1])
exit(EXIT_SUCCESS) exit(EXIT_SUCCESS)

View File

@ -257,6 +257,8 @@ class RingBuilder(object):
make multiple changes for a single rebalance. make multiple changes for a single rebalance.
:param dev: device dict :param dev: device dict
:returns: id of device
""" """
if 'id' not in dev: if 'id' not in dev:
dev['id'] = 0 dev['id'] = 0
@ -274,6 +276,7 @@ class RingBuilder(object):
self._set_parts_wanted() self._set_parts_wanted()
self.devs_changed = True self.devs_changed = True
self.version += 1 self.version += 1
return dev['id']
def set_dev_weight(self, dev_id, weight): def set_dev_weight(self, dev_id, weight):
""" """

View File

@ -124,17 +124,20 @@ class TestRingBuilder(unittest.TestCase):
rb = ring.RingBuilder(8, 3, 1) rb = ring.RingBuilder(8, 3, 1)
dev = {'id': 0, 'region': 0, 'zone': 0, 'weight': 1, dev = {'id': 0, 'region': 0, 'zone': 0, 'weight': 1,
'ip': '127.0.0.1', 'port': 10000} '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.assertRaises(exceptions.DuplicateDeviceError, rb.add_dev, dev)
self.assertEqual(dev_id, 0)
rb = ring.RingBuilder(8, 3, 1) rb = ring.RingBuilder(8, 3, 1)
#test add new dev with no id #test add new dev with no id
rb.add_dev({'zone': 0, 'region': 1, 'weight': 1, dev_id = rb.add_dev({'zone': 0, 'region': 1, 'weight': 1,
'ip': '127.0.0.1', 'port': 6000}) 'ip': '127.0.0.1', 'port': 6000})
self.assertEquals(rb.devs[0]['id'], 0) self.assertEquals(rb.devs[0]['id'], 0)
self.assertEqual(dev_id, 0)
#test add another dev with no id #test add another dev with no id
rb.add_dev({'zone': 3, 'region': 2, 'weight': 1, dev_id = rb.add_dev({'zone': 3, 'region': 2, 'weight': 1,
'ip': '127.0.0.1', 'port': 6000}) 'ip': '127.0.0.1', 'port': 6000})
self.assertEquals(rb.devs[1]['id'], 1) self.assertEquals(rb.devs[1]['id'], 1)
self.assertEqual(dev_id, 1)
def test_set_dev_weight(self): def test_set_dev_weight(self):
rb = ring.RingBuilder(8, 3, 1) rb = ring.RingBuilder(8, 3, 1)