From a0677700f4f85534ea8a729cf9ca3faa0dfe98dc Mon Sep 17 00:00:00 2001 From: micafer Date: Fri, 4 May 2018 12:52:07 +0200 Subject: [PATCH 1/2] Improve code and docs --- README.md | 9 +++++++-- im_client.py | 48 ++++++++++++++++++++++++------------------------ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index ddbed14..59597ae 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ configuration of all the user required applications providing the user with a fully functional infrastructure. ```sh -usage: client.py [-u|--xmlrpc-url ] [-a|--auth_file ] operation op_parameters +Usage: im_client.py [-u|--xmlrpc-url ] [-r|--restapi-url ] [-v|--verify-ssl] [-a|--auth_file ] operation op_parameters ``` 1 INSTALLATION @@ -196,7 +196,7 @@ An example of the auth file: The :program:`im_client` is called like this: ``` - $ im_client.py [-u|--xmlrpc-url url] [-r|--rest-url url] [-a|--auth_file filename] operation op_parameters + $ im_client.py [-u|--xmlrpc-url ] [-r|--restapi-url ] [-v|--verify-ssl] [-a|--auth_file ] operation op_parameters ``` * option: -u|--xmlrpc-url url @@ -208,6 +208,11 @@ The :program:`im_client` is called like this: URL to the REST API on the IM service. This option or the ` -u` one must be specified. + +.. option:: -v|--verify-ssl + + Verify the certificates of the SSL connection. + The default value is `False`, * option: -a|--auth_file filename diff --git a/im_client.py b/im_client.py index 2712131..afeb8a3 100755 --- a/im_client.py +++ b/im_client.py @@ -292,7 +292,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/vms/%s" % (options.restapi.rstrip("/"), inf_id, vm_list) - resp = requests.request("DELETE", url, verify=False, headers=headers) + resp = requests.request("DELETE", url, verify=options.verify, headers=headers) success = resp.status_code == 200 if success: vms_id = vm_list @@ -330,7 +330,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data, "Accept": "application/json"} url = "%s/infrastructures/%s" % (options.restapi, inf_id) - resp = requests.request("POST", url, verify=False, headers=headers, data=str(radl)) + resp = requests.request("POST", url, verify=options.verify, headers=headers, data=str(radl)) success = resp.status_code == 200 restres = resp.text if success: @@ -370,7 +370,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures" % options.restapi - resp = requests.request("POST", url, verify=False, headers=headers, data=str(radl)) + resp = requests.request("POST", url, verify=options.verify, headers=headers, data=str(radl)) success = resp.status_code == 200 inf_id = resp.text if success: @@ -404,7 +404,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/vms/%s" % (options.restapi, inf_id, vm_id) - resp = requests.request("PUT", url, verify=False, headers=headers, data=str(radl)) + resp = requests.request("PUT", url, verify=options.verify, headers=headers, data=str(radl)) success = resp.status_code == 200 res = resp.text else: @@ -439,7 +439,7 @@ def main(operation, options, args, parser): url = "%s/infrastructures/%s/reconfigure" % (options.restapi, inf_id) if len(args) >= 3: url += "?vm_list=" + args[2] - resp = requests.request("PUT", url, verify=False, headers=headers, data=str(radl)) + resp = requests.request("PUT", url, verify=options.verify, headers=headers, data=str(radl)) success = resp.status_code == 200 res = resp.text else: @@ -457,7 +457,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/contmsg" % (options.restapi, inf_id) - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 cont_out = resp.text else: @@ -477,7 +477,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data, "Accept": "application/json"} url = "%s/infrastructures/%s/state" % (options.restapi, inf_id) - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 res = resp.json()['state'] else: @@ -508,7 +508,7 @@ def main(operation, options, args, parser): url = "%s/infrastructures/%s/vms/%s" % (options.restapi, inf_id, vm_id) if propiedad: url += "/" + propiedad - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 info = resp.text else: @@ -531,7 +531,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data, "Accept": "application/json"} url = "%s/infrastructures/%s" % (options.restapi, inf_id) - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 restres = resp.text if success: @@ -552,7 +552,7 @@ def main(operation, options, args, parser): url = "%s/infrastructures/%s/vms/%s" % (options.restapi, inf_id, vm_id) if propiedad: url += "/" + propiedad - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 info = resp.text else: @@ -574,7 +574,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s" % (options.restapi, inf_id) - resp = requests.request("DELETE", url, verify=False, headers=headers) + resp = requests.request("DELETE", url, verify=options.verify, headers=headers) success = resp.status_code == 200 inf_id = resp.text else: @@ -590,7 +590,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data, "Accept": "application/json"} url = "%s/infrastructures" % options.restapi - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 if success: res = [] @@ -615,7 +615,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/start" % (options.restapi, inf_id) - resp = requests.request("PUT", url, verify=False, headers=headers) + resp = requests.request("PUT", url, verify=options.verify, headers=headers) success = resp.status_code == 200 inf_id = resp.text else: @@ -632,7 +632,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/stop" % (options.restapi, inf_id) - resp = requests.request("PUT", url, verify=False, headers=headers) + resp = requests.request("PUT", url, verify=options.verify, headers=headers) success = resp.status_code == 200 inf_id = resp.text else: @@ -649,7 +649,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/radl" % (options.restapi, inf_id) - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 radl = resp.text else: @@ -672,7 +672,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/vms/%s/contmsg" % (options.restapi, inf_id, vm_id) - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 info = resp.text else: @@ -695,7 +695,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/vms/%s/start" % (options.restapi, inf_id, vm_id) - resp = requests.request("PUT", url, verify=False, headers=headers) + resp = requests.request("PUT", url, verify=options.verify, headers=headers) success = resp.status_code == 200 info = resp.text else: @@ -718,7 +718,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/vms/%s/stop" % (options.restapi, inf_id, vm_id) - resp = requests.request("PUT", url, verify=False, headers=headers) + resp = requests.request("PUT", url, verify=options.verify, headers=headers) success = resp.status_code == 200 info = resp.text else: @@ -757,7 +757,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures/%s/vms/%s" % (options.restapi, inf_id, vm_id) - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 info = resp.text else: @@ -777,7 +777,7 @@ def main(operation, options, args, parser): elif operation == "getversion": if options.restapi: url = "%s/version" % options.restapi - resp = requests.request("GET", url, verify=False) + resp = requests.request("GET", url, verify=options.verify) success = resp.status_code == 200 version = resp.text else: @@ -800,7 +800,7 @@ def main(operation, options, args, parser): url = "%s/infrastructures/%s/data" % (options.restapi, inf_id) if delete: url += "?delete=yes" - resp = requests.request("GET", url, verify=False, headers=headers) + resp = requests.request("GET", url, verify=options.verify, headers=headers) success = resp.status_code == 200 if success: data = resp.json()["data"] @@ -831,7 +831,7 @@ def main(operation, options, args, parser): if options.restapi: headers = {"Authorization": rest_auth_data} url = "%s/infrastructures" % options.restapi - resp = requests.request("PUT", url, verify=False, headers=headers, data=data) + resp = requests.request("PUT", url, verify=options.verify, headers=headers, data=data) success = resp.status_code == 200 inf_id = resp.text if success: @@ -877,7 +877,7 @@ def get_parser(): under certain conditions; please read the license at \n\ http://www.gnu.org/licenses/gpl-3.0.txt for details." - parser = PosOptionParser(usage="%prog [-u|--xmlrpc-url ] [-v|--verify-ssl] [-a|--auth_file ] " + parser = PosOptionParser(usage="%prog [-u|--xmlrpc-url ] [-r|--restapi-url ] [-v|--verify-ssl] [-a|--auth_file ] " "operation op_parameters" + NOTICE, version="%prog " + __version__) parser.add_option("-a", "--auth_file", dest="auth_file", nargs=1, default=default_auth_file, help="Authentication" " data file", type="string") @@ -885,7 +885,7 @@ def get_parser(): "InfrastructureManager XML-RCP daemon", type="string") parser.add_option("-r", "--rest-url", dest="restapi", nargs=1, default=default_restapi, help="URL address of the " "InfrastructureManager REST API", type="string") - parser.add_option("-v", "--verify-ssl", action="store_true", dest="verify", help="Verify the certificate of the " + parser.add_option("-v", "--verify-ssl", action="store_true", default=False, dest="verify", help="Verify the certificate of the " "InfrastructureManager XML-RCP server") parser.add_operation_help('list', '') parser.add_operation_help('create', '') From 8fade84a68209d77d72806fbd52b018a175a8b14 Mon Sep 17 00:00:00 2001 From: micafer Date: Fri, 4 May 2018 13:07:25 +0200 Subject: [PATCH 2/2] Fix test --- test/unit/test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/test_client.py b/test/unit/test_client.py index ead1996..6f91835 100755 --- a/test/unit/test_client.py +++ b/test/unit/test_client.py @@ -851,7 +851,7 @@ def test_parser_help(self, option_parser_exit): parser.parse_args(["--help"]) output = out.getvalue().strip() self.assertEqual(output[:16], "Usage: nosetests") - self.assertIn("[-u|--xmlrpc-url ] [-v|--verify-ssl] [-a|--auth_file ] operation op_parameters", + self.assertIn("[-u|--xmlrpc-url ] [-r|--restapi-url ] [-v|--verify-ssl] [-a|--auth_file ] operation op_parameters", output) sys.stdout = oldstdout