Skip to content

Commit

Permalink
Merge pull request #94 from grycap/devel
Browse files Browse the repository at this point in the history
improve code
  • Loading branch information
micafer authored Jan 18, 2023
2 parents e7d2546 + 4853adc commit 0de79b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
18 changes: 8 additions & 10 deletions im_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,15 @@ def get_inf_id(self):
inf_id = int(self.args[0])

if self.options.name:
success, infras = self.list_infras(True)
success, infras = self.list_infras(flt=".*description\s*.*\s*(\s*name\s*=\s*'%s'.*).*" % inf_id)
if not success:
raise Exception("Error getting infrastructure list.")
res = [infid for infid, inf_name in infras.items() if inf_name == inf_id]
if len(res) == 0:
if len(infras) == 0:
raise Exception("Infrastructure Name not found")
elif len(res) >= 1:
if len(res) > 1:
elif len(infras) >= 1:
if len(infras) > 1:
print("WARNING!: more that one infrastructure with the same name. First one returned.")
return res[0]
return infras[0]
else:
return inf_id
else:
Expand Down Expand Up @@ -645,9 +644,8 @@ def destroy(self):

return success, res

def list_infras(self, show_name=False):
flt = None
if len(self.args) >= 1:
def list_infras(self, show_name=False, flt=None):
if flt is None and len(self.args) >= 1:
flt = self.args[0]

if self.options.restapi:
Expand Down Expand Up @@ -1098,7 +1096,7 @@ def main(operation, options, args, parser):
return success

elif operation == "list":
success, res = imclient.list_infras(options.name)
success, res = imclient.list_infras(show_name=options.name)
if success:
if res:
if options.quiet:
Expand Down
6 changes: 3 additions & 3 deletions test/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,16 +600,16 @@ def test_destroy(self, server_proxy, requests):

# Test using name instead of the ID
options.name = True
proxy.GetInfrastructureList.return_value = (True, ["inf1", "inf2"])
proxy.GetInfrastructureRADL.side_effect = [(True, "description desc (name = 'some name')\nsystem s1 ()"),
(True, "description desc (name = 'some name2')\nsystem s2 ()")]
proxy.GetInfrastructureList.return_value = (True, ["inf1"])
out = StringIO()
oldstdout = sys.stdout
sys.stdout = out
res = main("destroy", options, ["some name"], parser)
self.assertEquals(res, True)
output = out.getvalue().strip()
self.assertIn("Infrastructure successfully destroyed", output)
self.assertEqual(proxy.GetInfrastructureList.call_args_list[0][0][1],
".*description\s*.*\s*(\s*name\s*=\s*'some name'.*).*")
self.assertEqual(proxy.DestroyInfrastructure.call_args_list[1][0][0], 'inf1')
options.name = False

Expand Down

0 comments on commit 0de79b4

Please sign in to comment.