def test_cluster(self): c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) cluster = c.cluster_create() self.assertEqual(True, cluster['id'] != "") self.assertEqual(True, len(cluster['nodes']) == 0) self.assertEqual(True, len(cluster['volumes']) == 0) # Request bad id with self.assertRaises(requests.exceptions.HTTPError): c.cluster_info("bad") # Get info about the cluster info = c.cluster_info(cluster['id']) self.assertEqual(True, info == cluster) # Get a list of clusters list = c.cluster_list() self.assertEqual(True, len(list['clusters']) == 1) self.assertEqual(True, list['clusters'][0] == cluster['id']) # Delete non-existent cluster with self.assertRaises(requests.exceptions.HTTPError): c.cluster_delete("badid") # Delete current cluster self.assertEqual(True, c.cluster_delete(info['id']))
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)
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)
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_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)
# -*- coding:utf-8-*- import unittest import json import os import logging from requests.exceptions import ConnectionError from heketi import HeketiClient from cloudfly_heketi.admin_base import site from cloudfly_heketi.packaged.logger import logger from cloudfly_heketi.conf import conf from cloudfly_heketi.packaged.env_file import OSEnv c = HeketiClient(conf.HEKETI_SERVER, 'admin', conf.HEKETI_ADMIN_KEY) cluster_req = {} # c = HeketiClient(env_file.heketi_server,env_file.heketi_user,env_file.heketi_user_key) # class Test_Heketi(unittest.TestCase,OSEnv): # host = OSEnv().heketi_url() # user = OSEnv().heketi_user() # key = OSEnv().heketi_user_key() # logger('设置Heketi连接方式') # c = HeketiClient(host,user,key) # cluster_req = {} def create_cluster(self): try: cluster_req['file'] = True
def test_node_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_req["tags"] = { "foo": "bar", "speed": "ultra", } node = c.node_add(node_req) self.assertNotEqual(node['id'], '') node_id = node['id'] nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "foo": "bar", "speed": "ultra", }) # add some new tags r = c.node_set_tags(node_id, dict( change_type=heketi.TAGS_UPDATE, tags={"robot": "bender"})) self.assertTrue(r) nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "foo": "bar", "speed": "ultra", "robot": "bender", }) # reset tags to empty r = c.node_set_tags(node_id, dict( change_type=heketi.TAGS_SET, tags={})) self.assertTrue(r) nodeInfo = c.node_info(node_id) self.assertFalse(nodeInfo.get('tags')) # add some new tags back r = c.node_set_tags(node_id, dict( change_type=heketi.TAGS_UPDATE, tags={"robot": "bender", "fish": "bulb"})) self.assertTrue(r) nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "robot": "bender", "fish": "bulb", }) # delete a particular tag r = c.node_set_tags(node_id, dict( change_type=heketi.TAGS_DELETE, tags={"robot": ""})) self.assertTrue(r) nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "fish": "bulb", }) # invalid change_type raises error with self.assertRaises(requests.exceptions.HTTPError): c.node_set_tags(node_id, dict( change_type="zoidberg", tags={"robot": "flexo"})) # invalid tag name raises error with self.assertRaises(requests.exceptions.HTTPError): c.node_set_tags(node_id, dict( change_type=heketi.TAGS_UPDATE, tags={"$! W ~~~": "ok"})) # check nothing changed nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "fish": "bulb", }) # 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)
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_node_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_req["tags"] = { "foo": "bar", "speed": "ultra", } node = c.node_add(node_req) self.assertNotEqual(node['id'], '') node_id = node['id'] nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "foo": "bar", "speed": "ultra", }) # add some new tags r = c.node_set_tags( node_id, dict(change_type=heketi.TAGS_UPDATE, tags={"robot": "bender"})) self.assertTrue(r) nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "foo": "bar", "speed": "ultra", "robot": "bender", }) # reset tags to empty r = c.node_set_tags(node_id, dict(change_type=heketi.TAGS_SET, tags={})) self.assertTrue(r) nodeInfo = c.node_info(node_id) self.assertFalse(nodeInfo.get('tags')) # add some new tags back r = c.node_set_tags( node_id, dict(change_type=heketi.TAGS_UPDATE, tags={ "robot": "bender", "fish": "bulb" })) self.assertTrue(r) nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "robot": "bender", "fish": "bulb", }) # delete a particular tag r = c.node_set_tags( node_id, dict(change_type=heketi.TAGS_DELETE, tags={"robot": ""})) self.assertTrue(r) nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "fish": "bulb", }) # invalid change_type raises error with self.assertRaises(requests.exceptions.HTTPError): c.node_set_tags( node_id, dict(change_type="zoidberg", tags={"robot": "flexo"})) # invalid tag name raises error with self.assertRaises(requests.exceptions.HTTPError): c.node_set_tags( node_id, dict(change_type=heketi.TAGS_UPDATE, tags={"$! W ~~~": "ok"})) # check nothing changed nodeInfo = c.node_info(node_id) self.assertEqual(nodeInfo['tags'], { "fish": "bulb", }) # 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)
from heketi import HeketiClient import sys import json xw = open("nodes.out", 'w') node_output = open("node_output", "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') g = f[1] cni = client.node_info(g) node_info = json.dumps(cni, indent=4, sort_keys=True) xw.write(node_info) xw.close() json_file = open ("nodes.out") json_data = json.load(json_file) global name,state,deviceId for device in json_data['devices']: name = device['name'] state = device['state'] deviceId = device['id']
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 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)
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)
def test_cluster(self): c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) cluster_req = {} cluster_req['block'] = True cluster_req['file'] = True cluster = c.cluster_create(cluster_req) self.assertNotEqual(cluster['id'], "") self.assertEqual(len(cluster['nodes']), 0) self.assertEqual(len(cluster['volumes']), 0) self.assertTrue(cluster['block']) self.assertTrue(cluster['file']) # Request bad id with self.assertRaises(requests.exceptions.HTTPError): c.cluster_info("bad") # Get info about the cluster info = c.cluster_info(cluster['id']) self.assertEqual(info, cluster) # change cluster flags cluster_setflags_req = {} cluster_setflags_req['block'] = False cluster_setflags_req['file'] = True ok = c.cluster_setflags(cluster['id'], cluster_setflags_req) self.assertTrue(ok) # verify the cluster flags have changed info = c.cluster_info(cluster['id']) self.assertEqual(info['id'], cluster['id']) self.assertFalse(info['block']) self.assertTrue(info['file']) # Get a list of clusters list = c.cluster_list() self.assertEqual(1, len(list['clusters'])) self.assertEqual(list['clusters'][0], cluster['id']) # Delete non-existent cluster with self.assertRaises(requests.exceptions.HTTPError): c.cluster_delete("badid") # Delete current cluster self.assertTrue(c.cluster_delete(info['id']))
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_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)
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_node(self): node_req = {} c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) self.assertEqual(True, c != '') # Create cluster cluster = c.cluster_create() self.assertEqual(True, cluster['id'] != "") self.assertEqual(True, len(cluster['nodes']) == 0) self.assertEqual(True, len(cluster['volumes']) == 0) # Add node to unknown cluster node_req['cluster'] = "bad_id" node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } with self.assertRaises(requests.exceptions.HTTPError): c.node_add(node_req) # Create node request packet node_req['cluster'] = cluster['id'] node = c.node_add(node_req) self.assertEqual(True, node['zone'] == node_req['zone']) self.assertEqual(True, node['id'] != "") self.assertEqual(True, node_req['hostnames'] == node['hostnames']) self.assertEqual(True, len(node['devices']) == 0) # Info on invalid id with self.assertRaises(requests.exceptions.HTTPError): c.node_info("badid") # Get node info info = c.node_info(node['id']) self.assertEqual(True, info == node) self.assertEqual(info['state'], 'online') # Set offline state = {} state['state'] = 'offline' self.assertEqual(True, c.node_state(node['id'], state)) # Get node info info = c.node_info(node['id']) self.assertEqual(info['state'], 'offline') state['state'] = 'online' self.assertEqual(True, c.node_state(node['id'], state)) info = c.node_info(node['id']) self.assertEqual(info['state'], 'online') # Delete invalid node with self.assertRaises(requests.exceptions.HTTPError): c.node_delete("badid") # Can't delete cluster with a node with self.assertRaises(requests.exceptions.HTTPError): c.cluster_delete(cluster['id']) # Delete node del_node = c.node_delete(node['id']) self.assertEqual(True, del_node) # Delete cluster del_cluster = c.cluster_delete(cluster['id']) self.assertEqual(True, del_cluster)
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_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_node(self): node_req = {} c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) self.assertNotEqual(c, '') # Create cluster cluster_req = {} cluster_req['block'] = True cluster_req['file'] = True cluster = c.cluster_create(cluster_req) self.assertNotEqual(cluster['id'], "") self.assertEqual(len(cluster['nodes']), 0) self.assertEqual(len(cluster['volumes']), 0) # Add node to unknown cluster node_req['cluster'] = "bad_id" node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } with self.assertRaises(requests.exceptions.HTTPError): c.node_add(node_req) # Create node request packet node_req['cluster'] = cluster['id'] node = c.node_add(node_req) self.assertEqual(node['zone'], node_req['zone']) self.assertNotEqual(node['id'], "") self.assertEqual(node_req['hostnames'], node['hostnames']) self.assertEqual(len(node['devices']), 0) # Info on invalid id with self.assertRaises(requests.exceptions.HTTPError): c.node_info("badid") # Get node info info = c.node_info(node['id']) self.assertEqual(info, node) self.assertEqual(info['state'], 'online') # Set offline state = {} state['state'] = 'offline' self.assertTrue(c.node_state(node['id'], state)) # Get node info info = c.node_info(node['id']) self.assertEqual(info['state'], 'offline') state['state'] = 'online' self.assertTrue(c.node_state(node['id'], state)) info = c.node_info(node['id']) self.assertEqual(info['state'], 'online') # Delete invalid node with self.assertRaises(requests.exceptions.HTTPError): c.node_delete("badid") # Can't delete cluster with a node with self.assertRaises(requests.exceptions.HTTPError): c.cluster_delete(cluster['id']) # Delete node del_node = c.node_delete(node['id']) self.assertTrue(del_node) # Delete cluster del_cluster = c.cluster_delete(cluster['id']) self.assertTrue(del_cluster)
def test_node(self): node_req = {} c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY) self.assertEqual(True, c != '') # Create cluster cluster = c.cluster_create() self.assertEqual(True, cluster['id'] != "") self.assertEqual(True, len(cluster['nodes']) == 0) self.assertEqual(True, len(cluster['volumes']) == 0) # Add node to unknown cluster node_req['cluster'] = "bad_id" node_req['zone'] = 10 node_req['hostnames'] = { "manage": ["node1-manage.gluster.lab.com"], "storage": ["node1-storage.gluster.lab.com"] } with self.assertRaises(requests.exceptions.HTTPError): c.node_add(node_req) # Create node request packet node_req['cluster'] = cluster['id'] node = c.node_add(node_req) self.assertEqual(True, node['zone'] == node_req['zone']) self.assertEqual(True, node['id'] != "") self.assertEqual(True, node_req['hostnames'] == node['hostnames']) self.assertEqual(True, len(node['devices']) == 0) # Info on invalid id with self.assertRaises(requests.exceptions.HTTPError): c.node_info("badid") # Get node info info = c.node_info(node['id']) self.assertEqual(True, info == node) # Delete invalid node with self.assertRaises(requests.exceptions.HTTPError): c.node_delete("badid") # Can't delete cluster with a node with self.assertRaises(requests.exceptions.HTTPError): c.cluster_delete(cluster['id']) # Delete node del_node = c.node_delete(node['id']) self.assertEqual(True, del_node) # Delete cluster del_cluster = c.cluster_delete(cluster['id']) self.assertEqual(True, del_cluster)
from heketi import HeketiClient import sys import json xw = open("test.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) xw.write('Heketi Cluster information for Id {} Begins \n'.format(i)) b = client.cluster_info(i) cluster_info = json.dumps(b, indent=4, sort_keys=True) xw.write(cluster_info + "\n\n") f = b.get('nodes') g = b.get('volumes') xw.write('\n\n\n\n') xw.write('Node Information for the Cluster Begins \n\n') for i in f: client.node_info(i) xw.write('Node Info for Id {} is \n'.format(i)) cni = client.node_info(i) node_info = json.dumps(cni, indent=4, sort_keys=True) xw.write(node_info + "\n\n")