Skip to content

Commit

Permalink
Enable getinfo to filter by system name
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Jul 11, 2024
1 parent bd4785e commit feb5f4e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
4 changes: 3 additions & 1 deletion im_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,10 @@ def get_parser():
help="Force the deletion of the infrastructure")
parser.add_option("-q", "--quiet", action="store_true", default=False, dest="quiet",
help="Work in quiet mode")
parser.add_option("-n", "--name", action="store_true", default=False, dest="name",
parser.add_option("-n", "--name", action="store_true", default=None, dest="name",
help="Use infrastructure name instead of ID")
parser.add_option("-s", "--system_name", action="store_true", default=None, dest="system_name",
help="Filter VMs by system name")
parser.add_operation_help('list', '')
parser.add_operation_help('create', '<radl_file> [async_flag]')
parser.add_operation_help('destroy', '<inf_id>')
Expand Down
34 changes: 24 additions & 10 deletions imclient/imclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,33 +718,47 @@ def _getvmcontmsg(self):
vm_id = self._get_vm_id()
return self.getvminfo(inf_id, vm_id, "contmsg")

def getvminfo(self, inf_id, vm_id, prop=None):
def getvminfo(self, inf_id, vm_id, prop=None, system_name=None):
"""
Get VM info.
Arguments:
- inf_id(string): Infrastructure ID.
- vm_id(string): VM ID.
- prop(string): Optional RADL property to get.
- system_name(string): Optional system name to filter the VMs.
Returns: A tuple with the operation success (boolean) and the value of the prop in case of success
or the error message otherwise.
"""
if self.options.restapi:
headers = {"Authorization": self.rest_auth_data}
url = "%s/infrastructures/%s/vms/%s" % (self.options.restapi, inf_id, vm_id)
if prop:
if prop and not system_name:
url += "/" + prop
resp = requests.request("GET", url, verify=self.options.verify, headers=headers)
success = resp.status_code == 200
info = resp.text
if system_name and success:
radl_info = radl_parse.parse_radl(resp.text)
if radl_info.systems[0].name == system_name:
info = radl_info.systems[0].getValue(prop)
else:
info = ""
else:
info = resp.text
else:
if prop:
if prop and not system_name:
if prop == "contmsg":
(success, info) = self.server.GetVMContMsg(inf_id, vm_id, self.auth_data)
else:
(success, info) = self.server.GetVMProperty(inf_id, vm_id, prop, self.auth_data)
else:
(success, info) = self.server.GetVMInfo(inf_id, vm_id, self.auth_data)
if success and system_name:
radl_info = radl_parse.parse_radl(info)
if radl_info.systems[0].name == system_name:
info = radl_info.systems[0].getValue(prop)
else:
info = ""

return success, info

Expand All @@ -758,20 +772,20 @@ def _getvminfo(self):

return self.getvminfo(inf_id, vm_id, prop)

def _get_vms_info_generator(self, inf_id, vm_ids, propiedad):
def _get_vms_info_generator(self, inf_id, vm_ids, prop, system_name):
"""Helper function to return a generator."""
for vm_id in vm_ids:
self.args = [inf_id, vm_id, propiedad]
success, radl = self._getvminfo()
success, radl = self.getvminfo(inf_id, vm_id, prop, system_name)
yield vm_id, success, radl

def getinfo(self, inf_id, prop=None):
def getinfo(self, inf_id, prop=None, system_name=None):
"""
Get infrastructure info.
Arguments:
- inf_id(string): Infrastructure ID.
- prop(string): Optional RADL property to get.
- system_name(string): Optional system name to filter the VMs.
Returns: A tuple with the operation success (boolean) and the value of the prop in case of success
or the error message otherwise.
"""
Expand All @@ -791,7 +805,7 @@ def getinfo(self, inf_id, prop=None):
(success, vm_ids) = self.server.GetInfrastructureInfo(inf_id, self.auth_data)

if success:
return True, self._get_vms_info_generator(inf_id, vm_ids, prop)
return True, self._get_vms_info_generator(inf_id, vm_ids, prop, system_name)
else:
return False, "ERROR getting the information about the infrastructure: " + str(vm_ids)

Expand All @@ -801,7 +815,7 @@ def _getinfo(self):
if len(self.args) >= 2:
prop = self.args[1]

return self.getinfo(inf_id, prop)
return self.getinfo(inf_id, prop, self.options.system_name)

def destroy(self, inf_id, asyncr=False):
"""
Expand Down
54 changes: 52 additions & 2 deletions test/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,26 @@ def get_response(method, url, verify, cert=None, headers=None, data=None):
resp.text = "radltest"
elif url == "/infrastructures/infid":
resp.status_code = 200
resp.text = ('{ "uri-list": [ { "uri" : "http://localhost/infid/vms/vm1" }]}')
resp.text = ('{ "uri-list": [{ "uri" : "http://localhost/infid/vms/vm1" }]}')
resp.json.return_value = json.loads(resp.text)
elif url == "/infrastructures/infid2":
resp.status_code = 200
resp.text = ('{ "uri-list": [{ "uri" : "http://localhost/infid2/vms/vm1" }, ' +
'{ "uri" : "http://localhost/infid2/vms/vm2" }]}')
resp.json.return_value = json.loads(resp.text)
elif url == "/infrastructures/infid/vms/vm1":
resp.status_code = 200
resp.text = "radltest"
elif url == "/infrastructures/infid2/vms/vm1":
resp.status_code = 200
resp.text = "system node (cpu.count = 1)"
elif url == "/infrastructures/infid2/vms/vm2":
resp.status_code = 200
resp.text = "system node2 (cpu.count = 2)"
elif url == "/infrastructures/infid/radl":
resp.status_code = 200
resp.json.return_value = {"radl": "radltest"}
elif url == "/infrastructures/infid/vms/vmid/contmsg":
elif url in ["/infrastructures/infid/vms/vmid/contmsg", "/infrastructures/infid/vms/vm1/contmsg"]:
resp.status_code = 200
resp.text = 'getvmcontmsg'
elif url == "/version":
Expand Down Expand Up @@ -554,6 +565,7 @@ def test_getinfo(self, server_proxy, requests):
options.restapi = None
options.quiet = False
options.name = False
options.system_name = None
parser = MagicMock()

out = StringIO()
Expand All @@ -575,6 +587,44 @@ def test_getinfo(self, server_proxy, requests):
self.assertIn("Info about VM with ID: vm1\nradltest", output)
sys.stdout = oldstdout

out = StringIO()
sys.stdout = out
options.xmlrpc = None
options.restapi = "https://localhost:8800"
requests.side_effect = self.get_response
res = main("getinfo", options, ["infid", "contmsg"], parser)
self.assertEquals(res, True)
output = out.getvalue().strip()
self.assertIn("Info about VM with ID: vm1\ngetvmcontmsg", output)
sys.stdout = oldstdout

out = StringIO()
oldstdout = sys.stdout
sys.stdout = out
options.xmlrpc = "http://localhost:8899"
options.restapi = None
options.system_name = "node"
str_radl1 = "system node (cpu.count = 1)"
str_radl2 = "system node2 (cpu.count = 2)"
proxy.GetInfrastructureInfo.return_value = (True, ["vm1", "vm2"])
proxy.GetVMInfo.side_effect = [(True, str_radl1), (True, str_radl2)]
res = main("getinfo", options, ["infid", "cpu.count"], parser)
self.assertEquals(res, True)
output = out.getvalue().strip()
self.assertIn("Info about VM with ID: vm1\n1", output)

out = StringIO()
sys.stdout = out
options.xmlrpc = None
options.restapi = "https://localhost:8800"
options.system_name = "node"
requests.side_effect = self.get_response
res = main("getinfo", options, ["infid2", "cpu.count"], parser)
self.assertEquals(res, True)
output = out.getvalue().strip()
self.assertIn("Info about VM with ID: vm1\n1", output)
sys.stdout = oldstdout

@patch('requests.request')
@patch("imclient.imclient.ServerProxy")
def test_destroy(self, server_proxy, requests):
Expand Down

0 comments on commit feb5f4e

Please sign in to comment.