Skip to content

Commit

Permalink
Merge pull request #25 from grycap/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
micafer authored May 30, 2018
2 parents ffeab54 + bcb0ecf commit bd46ca7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ IM-client 1.5.2
* Fix python3 support.
* Add ssh function.
* Enable using REST API.

IM-client 1.5.3
* Enable to send TOSCA documents.
* Add getoutputs function.
44 changes: 37 additions & 7 deletions im_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def main(operation, options, args, parser):

if (operation not in ["removeresource", "addresource", "create", "destroy", "getinfo", "list", "stop", "start",
"alter", "getcontmsg", "getvminfo", "reconfigure", "getradl", "getvmcontmsg", "stopvm",
"startvm", "sshvm", "ssh", "getstate", "getversion", "export", "import"]):
"startvm", "sshvm", "ssh", "getstate", "getversion", "export", "import", "getoutputs"]):
parser.error("operation not recognised. Use --help to show all the available operations")

auth_data = None
Expand Down Expand Up @@ -361,17 +361,22 @@ def main(operation, options, args, parser):
return False

# Read the file
_, file_extension = os.path.splitext(args[0])
f = open(args[0])
radl_data = "".join(f.readlines())
f.close()
# check for input parameters @input.[param_name]@
radl_data = get_input_params(radl_data)

radl = radl_parse.parse_radl(radl_data)
radl.check()
if file_extension in [".yaml", ".yml"]:
radl = radl_data
else:
# check for input parameters @input.[param_name]@
radl_data = get_input_params(radl_data)
radl = radl_parse.parse_radl(radl_data)
radl.check()

if options.restapi:
headers = {"Authorization": rest_auth_data}
if file_extension in [".yaml", ".yml"]:
headers["Content-Type"] = "text/yaml"
url = "%s/infrastructures" % options.restapi
if asyncr:
url += "?async=yes"
Expand Down Expand Up @@ -484,7 +489,10 @@ def main(operation, options, args, parser):
url = "%s/infrastructures/%s/state" % (options.restapi, inf_id)
resp = requests.request("GET", url, verify=options.verify, headers=headers)
success = resp.status_code == 200
res = resp.json()['state']
if success:
res = resp.json()['state']
else:
res = resp.text
else:
(success, res) = server.GetInfrastructureState(inf_id, auth_data)
if success:
Expand Down Expand Up @@ -850,6 +858,27 @@ def main(operation, options, args, parser):
print("ERROR getting IM service version: " + inf_id)
return False

elif operation == "getoutputs":
inf_id = get_inf_id(args)

if options.restapi:
headers = {"Authorization": rest_auth_data, "Accept": "application/json"}
url = "%s/infrastructures/%s/outputs" % (options.restapi, inf_id)
resp = requests.request("GET", url, verify=options.verify, headers=headers)
success = resp.status_code == 200
if success:
res = resp.json()['outputs']
else:
res = resp.text
else:
print("ERROR getting the infrastructure outputs: Only available with REST API.")
return False
if success:
print("The infrastructure outputs:\n")
for key, value in res.items():
print("%s = %s" % (key, value))
else:
print("Error getting infrastructure outputs: %s" % res)

def get_parser():
"""
Expand Down Expand Up @@ -912,6 +941,7 @@ def get_parser():
parser.add_operation_help('sshvm', '<inf_id> <vm_id> [show_only]')
parser.add_operation_help('export', '<inf_id> [delete]')
parser.add_operation_help('import', '<json_file>')
parser.add_operation_help('getoutputs', '<inf_id>')
parser.add_operation_help('getversion', '')

return parser
Expand Down
27 changes: 27 additions & 0 deletions test/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def get_response(self, method, url, verify, cert=None, headers={}, data=None):
elif url == "/infrastructures/infid/contmsg":
resp.status_code = 200
resp.text = 'contmsg'
elif url == "/infrastructures/infid/outputs":
resp.status_code = 200
resp.text = '{"outputs": {"output1": "value1", "output2": "value2"}}'
resp.json.return_value = json.loads(resp.text)
elif url == "/infrastructures/infid/state":
resp.status_code = 200
resp.text = '{"state": {"state": "running", "vm_states": {"vm1": "running"}}}'
Expand Down Expand Up @@ -837,6 +841,29 @@ def test_parser(self):
(_, args) = parser.parse_args(["create", "test.radl"])
self.assertEqual(['create', 'test.radl'], args)

@patch('requests.request')
@patch("im_client.ServerProxy")
def test_getoutputs(self, server_proxy, requests):
"""
Test getoutputs operation
"""
options = MagicMock()
options.auth_file = get_abs_path("../../auth.dat")
parser = MagicMock()

oldstdout = sys.stdout
out = StringIO()
sys.stdout = out
options.xmlrpc = None
options.restapi = "https://localhost:8800"
requests.side_effect = self.get_response
main("getoutputs", options, ["infid", "vmid"], parser)
output = out.getvalue().strip()
self.assertIn("The infrastructure outputs:\n", output)
self.assertIn("\noutput2 = value2", output)
self.assertIn("\noutput1 = value1", output)
sys.stdout = oldstdout

@patch("im_client.OptionParser.exit")
def test_parser_help(self, option_parser_exit):
"""
Expand Down

0 comments on commit bd46ca7

Please sign in to comment.