def test_device(self): # Create app c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) # Create cluster cluster = c.cluster_create() self.assertEqual(True, cluster['id'] != '') # Create node node_req = {} node_req['cluster'] = cluster['id'] node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } node = c.node_add(node_req) self.assertEqual(True, node['id'] != '') # Create a device request device_req = {} device_req['name'] = "sda" device_req['weight'] = 100 device_req['node'] = node['id'] device = c.device_add(device_req) self.assertEqual(True, device) # Get node information info = c.node_info(node['id']) self.assertEqual(True, len(info['devices']) == 1) self.assertEqual(True, len(info['devices'][0]['bricks']) == 0) self.assertEqual(True, info['devices'][0]['name'] == device_req['name']) self.assertEqual(True, info['devices'][0]['weight'] == device_req['weight']) self.assertEqual(True, info['devices'][0]['id'] != '') # Get info from an unknown id with self.assertRaises(requests.exceptions.HTTPError): c.device_info("badid") # Get device information device_info = c.device_info(info['devices'][0]['id']) self.assertEqual(True, device_info == info['devices'][0]) # Try to delete node, and will not until we delete the device with self.assertRaises(requests.exceptions.HTTPError): c.node_delete(node['id']) # Delete unknown device with self.assertRaises(requests.exceptions.HTTPError): c.node_delete("badid") # Delete device device_delete = c.device_delete(device_info['id']) self.assertEqual(True, device_delete) # Delete node node_delete = c.node_delete(node['id']) self.assertEqual(True, node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertEqual(True, cluster_delete)
def test_volume(self): # Create cluster c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) self.assertEqual(True, c != '') cluster = c.cluster_create() self.assertEqual(True, cluster['id'] != '') # Create node request packet for i in range(4): node_req = {} node_req['cluster'] = cluster['id'] node_req['hostnames'] = { "manage": ["node%s-manage.gluster.lab.com" % (i)], "storage": ["node%s-storage.gluster.lab.com" % (i)] } node_req['zone'] = i + 1 # Create node node = c.node_add(node_req) self.assertEqual(True, node['id'] != '') # Create and add devices for i in range(1, 20): device_req = {} device_req['name'] = "sda%s" % (i) device_req['weight'] = 100 device_req['node'] = node['id'] device = c.device_add(device_req) self.assertEqual(True, device) # Get list of volumes list = c.volume_list() self.assertEqual(True, len(list['volumes']) == 0) # Create a volume volume_req = {} volume_req['size'] = 10 volume = c.volume_create(volume_req) self.assertEqual(True, volume['id'] != "") self.assertEqual(True, volume['size'] == volume_req['size']) # Get list of volumes list = c.volume_list() self.assertEqual(True, len(list['volumes']) == 1) self.assertEqual(True, list['volumes'][0] == volume['id']) # Get info on incorrect id with self.assertRaises(requests.exceptions.HTTPError): c.volume_info("badid") # Get info info = c.volume_info(volume['id']) self.assertEqual(True, info == volume) # Expand volume with a bad id volume_ex_params = {} volume_ex_params['expand_size'] = 10 with self.assertRaises(requests.exceptions.HTTPError): c.volume_expand("badid", volume_ex_params) # Expand volume volumeInfo = c.volume_expand(volume['id'], volume_ex_params) self.assertEqual(True, volumeInfo['size'] == 20) # Delete bad id with self.assertRaises(requests.exceptions.HTTPError): c.volume_delete("badid") # Delete volume volume_delete = c.volume_delete(volume['id']) self.assertEqual(True, volume_delete) clusterInfo = c.cluster_info(cluster['id']) for node_id in clusterInfo['nodes']: #Get node information nodeInfo = c.node_info(node_id) # Delete all devices for device in nodeInfo['devices']: device_delete = c.device_delete(device['id']) self.assertEqual(True, device_delete) #Delete node node_delete = c.node_delete(node_id) self.assertEqual(True, node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertEqual(True, cluster_delete)
def test_device(self): # Create app c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) # Create cluster cluster = c.cluster_create() self.assertEqual(True, cluster['id'] != '') # Create node node_req = {} node_req['cluster'] = cluster['id'] node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } node = c.node_add(node_req) self.assertEqual(True, node['id'] != '') # Create a device request device_req = {} device_req['name'] = "sda" device_req['node'] = node['id'] device = c.device_add(device_req) self.assertEqual(True, device) # Get node information info = c.node_info(node['id']) self.assertEqual(True, len(info['devices']) == 1) self.assertEqual(True, len(info['devices'][0]['bricks']) == 0) self.assertEqual( True, info['devices'][0]['name'] == device_req['name']) self.assertEqual(True, info['devices'][0]['id'] != '') # Get info from an unknown id with self.assertRaises(requests.exceptions.HTTPError): c.device_info("badid") # Get device information device_id = info['devices'][0]['id'] device_info = c.device_info(device_id) self.assertEqual(True, device_info == info['devices'][0]) # Set offline state = {} state['state'] = 'offline' self.assertEqual(True, c.device_state(device_id, state)) # Get device info info = c.device_info(device_id) self.assertEqual(info['state'], 'offline') state['state'] = 'online' self.assertEqual(True, c.device_state(device_id, state)) info = c.device_info(device_id) self.assertEqual(info['state'], 'online') # Try to delete node, and will not until we delete the device with self.assertRaises(requests.exceptions.HTTPError): c.node_delete(node['id']) # Delete unknown device with self.assertRaises(requests.exceptions.HTTPError): c.node_delete("badid") # Delete device device_delete = c.device_delete(device_info['id']) self.assertEqual(True, device_delete) # Delete node node_delete = c.node_delete(node['id']) self.assertEqual(True, node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertEqual(True, cluster_delete)
def test_volume(self): # Create cluster c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) self.assertEqual(True, c != '') cluster = c.cluster_create() self.assertEqual(True, cluster['id'] != '') # Create node request packet print "Creating Cluster" for i in range(3): node_req = {} node_req['cluster'] = cluster['id'] node_req['hostnames'] = { "manage": ["node%s-manage.gluster.lab.com" % (i)], "storage": ["node%s-storage.gluster.lab.com" % (i)]} node_req['zone'] = i + 1 # Create node node = c.node_add(node_req) self.assertEqual(True, node['id'] != '') # Create and add devices for i in range(1, 4): device_req = {} device_req['name'] = "sda%s" % (i) device_req['node'] = node['id'] device = c.device_add(device_req) self.assertEqual(True, device) # Get list of volumes list = c.volume_list() self.assertEqual(True, len(list['volumes']) == 0) # Create a volume print "Creating a volume" volume_req = {} volume_req['size'] = 10 volume = c.volume_create(volume_req) self.assertEqual(True, volume['id'] != "") self.assertEqual(True, volume['size'] == volume_req['size']) # Get list of volumes list = c.volume_list() self.assertEqual(True, len(list['volumes']) == 1) self.assertEqual(True, list['volumes'][0] == volume['id']) # Get info on incorrect id with self.assertRaises(requests.exceptions.HTTPError): c.volume_info("badid") # Get info info = c.volume_info(volume['id']) self.assertEqual(True, info == volume) # Expand volume with a bad id volume_ex_params = {} volume_ex_params['expand_size'] = 10 with self.assertRaises(requests.exceptions.HTTPError): c.volume_expand("badid", volume_ex_params) # Expand volume print "Expanding volume" volumeInfo = c.volume_expand(volume['id'], volume_ex_params) self.assertEqual(True, volumeInfo['size'] == 20) # Delete bad id with self.assertRaises(requests.exceptions.HTTPError): c.volume_delete("badid") # Delete volume print "Deleting volume" volume_delete = c.volume_delete(volume['id']) self.assertEqual(True, volume_delete) print "Deleting Cluster" clusterInfo = c.cluster_info(cluster['id']) for node_id in clusterInfo['nodes']: # Get node information nodeInfo = c.node_info(node_id) # Delete all devices for device in nodeInfo['devices']: device_delete = c.device_delete(device['id']) self.assertEqual(True, device_delete) # Delete node node_delete = c.node_delete(node_id) self.assertEqual(True, node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertEqual(True, cluster_delete)
def test_volume(self): # Create cluster c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY, poll_delay=TEST_POLL_DELAY) self.assertEqual(True, c != '') cluster_req = {} cluster_req['block'] = True cluster_req['file'] = True cluster = c.cluster_create(cluster_req) self.assertNotEqual(cluster['id'], '') # Create node request packet print ("Creating Cluster") for i in range(3): node_req = {} node_req['cluster'] = cluster['id'] node_req['hostnames'] = { "manage": ["node%s-manage.gluster.lab.com" % (i)], "storage": ["node%s-storage.gluster.lab.com" % (i)]} node_req['zone'] = i + 1 # Create node node = c.node_add(node_req) self.assertNotEqual(node['id'], '') # Create and add devices for i in range(1, 4): device_req = {} device_req['name'] = "/dev/sda%s" % (i) device_req['node'] = node['id'] device = c.device_add(device_req) self.assertTrue(device) # Get list of volumes list = c.volume_list() self.assertEqual(len(list['volumes']), 0) # Create a volume print ("Creating a volume") volume_req = {} volume_req['size'] = 10 volume = c.volume_create(volume_req) self.assertNotEqual(volume['id'], "") self.assertEqual(volume['size'], volume_req['size']) # Get list of volumes list = c.volume_list() self.assertEqual(len(list['volumes']), 1) self.assertEqual(list['volumes'][0], volume['id']) # Get info on incorrect id with self.assertRaises(requests.exceptions.HTTPError): c.volume_info("badid") # Get info info = c.volume_info(volume['id']) self.assertEqual(info, volume) # Expand volume with a bad id volume_ex_params = {} volume_ex_params['expand_size'] = 10 with self.assertRaises(requests.exceptions.HTTPError): c.volume_expand("badid", volume_ex_params) # Expand volume print ("Expanding volume") volumeInfo = c.volume_expand(volume['id'], volume_ex_params) self.assertEqual(volumeInfo['size'], 20) # Delete bad id with self.assertRaises(requests.exceptions.HTTPError): c.volume_delete("badid") # Delete volume print ("Deleting volume") volume_delete = c.volume_delete(volume['id']) self.assertTrue(volume_delete) print ("Deleting Cluster") clusterInfo = c.cluster_info(cluster['id']) for node_id in clusterInfo['nodes']: # Get node information nodeInfo = c.node_info(node_id) # Delete all devices for device in nodeInfo['devices']: devid = device['id'] self.assertTrue(c.device_state(devid, {'state': 'offline'})) self.assertTrue(c.device_state(devid, {'state': 'failed'})) device_delete = c.device_delete(devid) self.assertTrue(device_delete) # Delete node node_delete = c.node_delete(node_id) self.assertTrue(node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertTrue(cluster_delete)
def test_device_tags(self): # Create app c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY, poll_delay=TEST_POLL_DELAY) # Create cluster cluster_req = {} cluster_req['block'] = True cluster_req['file'] = True cluster = c.cluster_create(cluster_req) self.assertNotEqual(cluster['id'], '') # Create node node_req = {} node_req['cluster'] = cluster['id'] node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } node = c.node_add(node_req) self.assertNotEqual(node['id'], '') # Create a device (with tags) device_req = {} device_req['name'] = "/dev/sda" device_req['node'] = node['id'] device_req["tags"] = { "foo": "bar", "speed": "ultra", } device = c.device_add(device_req) self.assertTrue(device) # get information info = c.node_info(node['id']) self.assertEqual(len(info['devices']), 1) device_id = info['devices'][0]['id'] # check tags on device device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "foo": "bar", "speed": "ultra", }) # add some new tags r = c.device_set_tags(device_id, dict( change_type=heketi.TAGS_UPDATE, tags={"robot": "calculon"})) self.assertTrue(r) device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "foo": "bar", "speed": "ultra", "robot": "calculon", }) # reset tags to empty r = c.device_set_tags(device_id, dict( change_type=heketi.TAGS_SET, tags={})) self.assertTrue(r) device_info = c.device_info(device_id) self.assertFalse(device_info.get('tags')) # add some new tags back r = c.device_set_tags(device_id, dict( change_type=heketi.TAGS_UPDATE, tags={"robot": "calculon", "fish": "blinky"})) self.assertTrue(r) device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "robot": "calculon", "fish": "blinky", }) # delete a particular tag r = c.device_set_tags(device_id, dict( change_type=heketi.TAGS_DELETE, tags={"robot": ""})) self.assertTrue(r) device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "fish": "blinky", }) # invalid change_type raises error with self.assertRaises(requests.exceptions.HTTPError): c.device_set_tags(device_id, dict( change_type="hermes", tags={"robot": "flexo"})) # invalid tag name raises error with self.assertRaises(requests.exceptions.HTTPError): c.device_set_tags(device_id, dict( change_type=heketi.TAGS_UPDATE, tags={"": "ok"})) # check nothing changed device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "fish": "blinky", }) # delete device self.assertTrue(c.device_state(device_id, {'state': 'offline'})) self.assertTrue(c.device_state(device_id, {'state': 'failed'})) self.assertTrue(c.device_delete(device_id)) # Delete node node_delete = c.node_delete(node['id']) self.assertTrue(node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertTrue(cluster_delete)
def test_volume(self): # Create cluster c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) self.assertEqual(True, c != '') cluster_req = {} cluster_req['block'] = True cluster_req['file'] = True cluster = c.cluster_create(cluster_req) self.assertNotEqual(cluster['id'], '') # Create node request packet print("Creating Cluster") for i in range(3): node_req = {} node_req['cluster'] = cluster['id'] node_req['hostnames'] = { "manage": ["node%s-manage.gluster.lab.com" % (i)], "storage": ["node%s-storage.gluster.lab.com" % (i)] } node_req['zone'] = i + 1 # Create node node = c.node_add(node_req) self.assertNotEqual(node['id'], '') # Create and add devices for i in range(1, 4): device_req = {} device_req['name'] = "/dev/sda%s" % (i) device_req['node'] = node['id'] device = c.device_add(device_req) self.assertTrue(device) # Get list of volumes list = c.volume_list() self.assertEqual(len(list['volumes']), 0) # Create a volume print("Creating a volume") volume_req = {} volume_req['size'] = 10 volume = c.volume_create(volume_req) self.assertNotEqual(volume['id'], "") self.assertEqual(volume['size'], volume_req['size']) # Get list of volumes list = c.volume_list() self.assertEqual(len(list['volumes']), 1) self.assertEqual(list['volumes'][0], volume['id']) # Get info on incorrect id with self.assertRaises(requests.exceptions.HTTPError): c.volume_info("badid") # Get info info = c.volume_info(volume['id']) self.assertEqual(info, volume) # Expand volume with a bad id volume_ex_params = {} volume_ex_params['expand_size'] = 10 with self.assertRaises(requests.exceptions.HTTPError): c.volume_expand("badid", volume_ex_params) # Expand volume print("Expanding volume") volumeInfo = c.volume_expand(volume['id'], volume_ex_params) self.assertEqual(volumeInfo['size'], 20) # Delete bad id with self.assertRaises(requests.exceptions.HTTPError): c.volume_delete("badid") # Delete volume print("Deleting volume") volume_delete = c.volume_delete(volume['id']) self.assertTrue(volume_delete) print("Deleting Cluster") clusterInfo = c.cluster_info(cluster['id']) for node_id in clusterInfo['nodes']: # Get node information nodeInfo = c.node_info(node_id) # Delete all devices for device in nodeInfo['devices']: devid = device['id'] self.assertTrue(c.device_state(devid, {'state': 'offline'})) self.assertTrue(c.device_state(devid, {'state': 'failed'})) device_delete = c.device_delete(devid) self.assertTrue(device_delete) # Delete node node_delete = c.node_delete(node_id) self.assertTrue(node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertTrue(cluster_delete)
def test_device(self): # Create app c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY, poll_delay=TEST_POLL_DELAY) # Create cluster cluster_req = {} cluster_req['block'] = True cluster_req['file'] = True cluster = c.cluster_create(cluster_req) self.assertNotEqual(cluster['id'], '') # Create node node_req = {} node_req['cluster'] = cluster['id'] node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } node = c.node_add(node_req) self.assertNotEqual(node['id'], '') # Create a device request device_req = {} device_req['name'] = "/dev/sda" device_req['node'] = node['id'] device = c.device_add(device_req) self.assertTrue(device) # Get node information info = c.node_info(node['id']) self.assertEqual(len(info['devices']), 1) self.assertEqual(len(info['devices'][0]['bricks']), 0) self.assertEqual(info['devices'][0]['name'], device_req['name']) self.assertNotEqual(info['devices'][0]['id'], '') # Get info from an unknown id with self.assertRaises(requests.exceptions.HTTPError): c.device_info("badid") # Get device information device_id = info['devices'][0]['id'] device_info = c.device_info(device_id) self.assertEqual(device_info, info['devices'][0]) # Set offline state = {} state['state'] = 'offline' self.assertTrue(c.device_state(device_id, state)) # Get device info info = c.device_info(device_id) self.assertEqual(info['state'], 'offline') state['state'] = 'online' self.assertTrue(c.device_state(device_id, state)) info = c.device_info(device_id) self.assertEqual(info['state'], 'online') # Resync device device_resync = c.device_resync(device_id) self.assertTrue(device_resync) # Try to delete node, and will not until we delete the device with self.assertRaises(requests.exceptions.HTTPError): c.node_delete(node['id']) # Delete unknown device with self.assertRaises(requests.exceptions.HTTPError): c.node_delete("badid") # Set device to offline state = {} state['state'] = 'offline' self.assertTrue(c.device_state(device_id, state)) # Set device to failed state = {} state['state'] = 'failed' self.assertTrue(c.device_state(device_id, state)) # Delete device device_delete = c.device_delete(device_info['id']) self.assertTrue(device_delete) # Delete node node_delete = c.node_delete(node['id']) self.assertTrue(node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertTrue(cluster_delete)
def test_device(self): # Create app c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) # Create cluster cluster_req = {} cluster_req['block'] = True cluster_req['file'] = True cluster = c.cluster_create(cluster_req) self.assertNotEqual(cluster['id'], '') # Create node node_req = {} node_req['cluster'] = cluster['id'] node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } node = c.node_add(node_req) self.assertNotEqual(node['id'], '') # Create a device request device_req = {} device_req['name'] = "/dev/sda" device_req['node'] = node['id'] device = c.device_add(device_req) self.assertTrue(device) # Get node information info = c.node_info(node['id']) self.assertEqual(len(info['devices']), 1) self.assertEqual(len(info['devices'][0]['bricks']), 0) self.assertEqual(info['devices'][0]['name'], device_req['name']) self.assertNotEqual(info['devices'][0]['id'], '') # Get info from an unknown id with self.assertRaises(requests.exceptions.HTTPError): c.device_info("badid") # Get device information device_id = info['devices'][0]['id'] device_info = c.device_info(device_id) self.assertEqual(device_info, info['devices'][0]) # Set offline state = {} state['state'] = 'offline' self.assertTrue(c.device_state(device_id, state)) # Get device info info = c.device_info(device_id) self.assertEqual(info['state'], 'offline') state['state'] = 'online' self.assertTrue(c.device_state(device_id, state)) info = c.device_info(device_id) self.assertEqual(info['state'], 'online') # Resync device device_resync = c.device_resync(device_id) self.assertTrue(device_resync) # Try to delete node, and will not until we delete the device with self.assertRaises(requests.exceptions.HTTPError): c.node_delete(node['id']) # Delete unknown device with self.assertRaises(requests.exceptions.HTTPError): c.node_delete("badid") # Set device to offline state = {} state['state'] = 'offline' self.assertTrue(c.device_state(device_id, state)) # Set device to failed state = {} state['state'] = 'failed' self.assertTrue(c.device_state(device_id, state)) # Delete device device_delete = c.device_delete(device_info['id']) self.assertTrue(device_delete) # Delete node node_delete = c.node_delete(node['id']) self.assertTrue(node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertTrue(cluster_delete)
class Heketi(HeketiClient): def __init__(self, module): self.module = module self.action = self._get_params('action') self.init_heketi() def hkt_action(self): msg = { 'load': self.load_topology, 'addnode': self.heketi_add_node, 'adddevice': self.heketi_add_device, 'createvolume': self.heketi_create_volume }[self.action]() return msg def _get_params(self, opt, reqd=True): value = self.module.params[opt] if (value is None or not value.strip(" ")) and reqd: msg = "Please provide %s option in the playbook!" % opt self.module.fail_json(rc=1, msg=msg) value = value.strip() if value else value return value def load_topology(self): filename = self._get_params('filename', False) if filename: op = 'heketi-cli' cmd = ' load -json=%s' % filename rc, out, err = self.run_command(op, cmd) self.get_output(rc, out, err) else: cluster_id = self.heketi_cluster_create() node_id = self.heketi_add_node(cluster_id) dev = self.heketi_add_device(node_id) result = {"cluster_id": cluster_id, "node_id": node_id, "device_create": dev} return result def init_heketi(self, server=None): user = self._get_params('user') key = self._get_params('userkey') server = self._get_params('server') self.heketi = HeketiClient(server, user, key) def heketi_add_node(self, cluster_id=None): if not cluster_id: cluster_id = self._get_params('cluster') hostname = {} zone = self._get_params('zone', False) hostname["manage"] = [self._get_params('managehost', False)] hostname["storage"] = [self._get_params('storagehost', False)] if not (zone and hostname["manage"] and hostname["storage"]): return False node_dict = dict(zone=int(zone),hostnames=hostname,cluster=cluster_id) ret = self.heketi.node_add(node_dict) return ret['id'] def heketi_add_device(self, node_id=None): if not node_id: node_id = self._get_params('node', False) if not node_id: return '' try: devices = self._get_params('devices') except: return False devices = literal_eval(devices) dev_dict = {} dev_dict["node"] = node_id for dev in devices: dev_dict["name"] = dev ret = self.heketi.device_add(dev_dict) return True def heketi_cluster_create(self): cid = self._get_params('cluster', False) if cid: return cid ret = self.heketi.cluster_create() return ret['id'] def heketi_create_volume(self): vol = {} vol["size"] = int(self._get_params('size')) vname = self._get_params('name', False) if vname: vol['name'] = vname snapshot = self._get_params('snapshot', False) if snapshot and snapshot == 'true': vol['snapshot'], h = {}, {} h['enable'] = "true" f = self._get_params('snapshot_factor', False) if f: h['factor'] = int(f) vol['snapshot'] = h durability = self._get_params('durability', False) if durability and durability.lower() != 'none': vol['durability'], h = {}, {} if durability.lower() == 'replicate': h['type'] = 'replicate' c = self._get_params('replica_count', False) if c: h['replicate'] = {'replica': int(c)} if durability.lower() == 'disperse': h['type'] = 'disperse' d = self._get_params('disperse_data', False) r = self._get_params('redundancy', False) h['disperse'] = {'data': int(d), 'redundancy': int(r)} h = dict((k, v) for k, v in h.iteritems() if v) vol['durability'] = h clusters = self._get_params('clusters', False) if clusters: vol['clusters'] = clusters ret = self.heketi.volume_create(vol) return ret def run_command(self, op, options): cmd = self.module.get_bin_path(op, True) + options return self.module.run_command(cmd) def get_output(self, output): self.module.exit_json(msg=output, changed=1)
from heketi import HeketiClient import sys import json Device_Add = open("deviceadd.out", 'w') client= HeketiClient('http://heketi-storage-project.cloudapps.mystorage.com','admin', 'My Secret') c = client.cluster_list() a = c.get('clusters') for i in a: client.cluster_info(i) b = client.cluster_info(i) f = b.get('nodes') node_info = json.dumps(f, indent=4, sort_keys=True) Device_Add.write('Heketi Cluster Node information is \n\n') Device_Add.write(node_info + "\n\n" ) for i in f: client.device_add(device_options={"node": i, "name": "/dev/sde"}) Device_Add.write('Device Add Successful for Node {}\n'.format(i))
def test_device_tags(self): # Create app c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY, poll_delay=TEST_POLL_DELAY) # Create cluster cluster_req = {} cluster_req['block'] = True cluster_req['file'] = True cluster = c.cluster_create(cluster_req) self.assertNotEqual(cluster['id'], '') # Create node node_req = {} node_req['cluster'] = cluster['id'] node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } node = c.node_add(node_req) self.assertNotEqual(node['id'], '') # Create a device (with tags) device_req = {} device_req['name'] = "/dev/sda" device_req['node'] = node['id'] device_req["tags"] = { "foo": "bar", "speed": "ultra", } device = c.device_add(device_req) self.assertTrue(device) # get information info = c.node_info(node['id']) self.assertEqual(len(info['devices']), 1) device_id = info['devices'][0]['id'] # check tags on device device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "foo": "bar", "speed": "ultra", }) # add some new tags r = c.device_set_tags( device_id, dict(change_type=heketi.TAGS_UPDATE, tags={"robot": "calculon"})) self.assertTrue(r) device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "foo": "bar", "speed": "ultra", "robot": "calculon", }) # reset tags to empty r = c.device_set_tags(device_id, dict(change_type=heketi.TAGS_SET, tags={})) self.assertTrue(r) device_info = c.device_info(device_id) self.assertFalse(device_info.get('tags')) # add some new tags back r = c.device_set_tags( device_id, dict(change_type=heketi.TAGS_UPDATE, tags={ "robot": "calculon", "fish": "blinky" })) self.assertTrue(r) device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "robot": "calculon", "fish": "blinky", }) # delete a particular tag r = c.device_set_tags( device_id, dict(change_type=heketi.TAGS_DELETE, tags={"robot": ""})) self.assertTrue(r) device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "fish": "blinky", }) # invalid change_type raises error with self.assertRaises(requests.exceptions.HTTPError): c.device_set_tags( device_id, dict(change_type="hermes", tags={"robot": "flexo"})) # invalid tag name raises error with self.assertRaises(requests.exceptions.HTTPError): c.device_set_tags( device_id, dict(change_type=heketi.TAGS_UPDATE, tags={"": "ok"})) # check nothing changed device_info = c.device_info(device_id) self.assertEqual(device_info['tags'], { "fish": "blinky", }) # delete device self.assertTrue(c.device_state(device_id, {'state': 'offline'})) self.assertTrue(c.device_state(device_id, {'state': 'failed'})) self.assertTrue(c.device_delete(device_id)) # Delete node node_delete = c.node_delete(node['id']) self.assertTrue(node_delete) # Delete cluster cluster_delete = c.cluster_delete(cluster['id']) self.assertTrue(cluster_delete)
class Heketi(HeketiClient): def __init__(self, module): self.module = module self.action = self._get_params('action') self.init_heketi() def hkt_action(self): msg = { 'load': self.load_topology, 'addnode': self.heketi_add_node, 'adddevice': self.heketi_add_device, 'createvolume': self.heketi_create_volume }[self.action]() return msg def _get_params(self, opt, reqd=True): value = self.module.params[opt] if (value is None or not value.strip(" ")) and reqd: msg = "Please provide %s option in the playbook!" % opt self.module.fail_json(rc=1, msg=msg) value = value.strip() if value else value return value def load_topology(self): filename = self._get_params('filename', False) if filename: op = 'heketi-cli' cmd = ' load -json=%s' % filename rc, out, err = self.run_command(op, cmd) self.get_output(rc, out, err) else: cluster_id = self.heketi_cluster_create() node_id = self.heketi_add_node(cluster_id) dev = self.heketi_add_device(node_id) result = { "cluster_id": cluster_id, "node_id": node_id, "device_create": dev } return result def init_heketi(self, server=None): user = self._get_params('user') key = self._get_params('userkey') server = self._get_params('server') self.heketi = HeketiClient(server, user, key) def heketi_add_node(self, cluster_id=None): if not cluster_id: cluster_id = self._get_params('cluster') hostname = {} zone = self._get_params('zone', False) hostname["manage"] = [self._get_params('managehost', False)] hostname["storage"] = [self._get_params('storagehost', False)] if not (zone and hostname["manage"] and hostname["storage"]): return False node_dict = dict(zone=int(zone), hostnames=hostname, cluster=cluster_id) ret = self.heketi.node_add(node_dict) return ret['id'] def heketi_add_device(self, node_id=None): if not node_id: node_id = self._get_params('node', False) if not node_id: return '' try: devices = self._get_params('devices') except: return False devices = literal_eval(devices) dev_dict = {} dev_dict["node"] = node_id for dev in devices: dev_dict["name"] = dev ret = self.heketi.device_add(dev_dict) return True def heketi_cluster_create(self): cid = self._get_params('cluster', False) if cid: return cid ret = self.heketi.cluster_create() return ret['id'] def heketi_create_volume(self): vol = {} vol["size"] = int(self._get_params('size')) vname = self._get_params('name', False) if vname: vol['name'] = vname snapshot = self._get_params('snapshot', False) if snapshot and snapshot == 'true': vol['snapshot'], h = {}, {} h['enable'] = "true" f = self._get_params('snapshot_factor', False) if f: h['factor'] = int(f) vol['snapshot'] = h durability = self._get_params('durability', False) if durability and durability.lower() != 'none': vol['durability'], h = {}, {} if durability.lower() == 'replicate': h['type'] = 'replicate' c = self._get_params('replica_count', False) if c: h['replicate'] = {'replica': int(c)} if durability.lower() == 'disperse': h['type'] = 'disperse' d = self._get_params('disperse_data', False) r = self._get_params('redundancy', False) h['disperse'] = {'data': int(d), 'redundancy': int(r)} h = dict((k, v) for k, v in h.iteritems() if v) vol['durability'] = h clusters = self._get_params('clusters', False) if clusters: vol['clusters'] = clusters ret = self.heketi.volume_create(vol) return ret def run_command(self, op, options): cmd = self.module.get_bin_path(op, True) + options return self.module.run_command(cmd) def get_output(self, output): self.module.exit_json(msg=output, changed=1)