vapilib.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import requests
  2. import json
  3. import pprint
  4. import sys
  5. import logging
  6. #logger = logging.getLogger(__name__)
  7. from config.models import Instance
  8. from django.contrib.auth.models import Group
  9. from django.contrib.auth.models import User
  10. import perms
  11. from vmsg import log
  12. class vyapi:
  13. error = None
  14. success = None
  15. result = None
  16. data = None
  17. reason = None
  18. def __init__(self, result, data = None, reason=None):
  19. if result == True:
  20. self.success = True
  21. else:
  22. self.error = True
  23. self.result = result
  24. self.data = data
  25. self.reason = reason
  26. API_LIST = {}
  27. API_LIST["get"] = {}
  28. API_LIST["get"]["description"] = 'Show config'
  29. API_LIST["get"]["path"] = 'retrieve'
  30. API_LIST["get"]["op"] = {}
  31. API_LIST["get"]["op"]["showConfig"] = 'path'
  32. API_LIST["post"] = {}
  33. API_LIST["post"]["description"] = 'Configuration mode requests'
  34. API_LIST["post"]["path"] = "configure"
  35. API_LIST["post"]["op"] = {}
  36. API_LIST["post"]["op"]["set"] = 'path'
  37. API_LIST["post"]["op"]["delete"] = 'path'
  38. API_LIST["post"]["op"]["comment"] = 'path'
  39. API_LIST["conf"] = {}
  40. API_LIST["conf"]["description"] = 'Configuration management requests'
  41. API_LIST["conf"]["path"] = 'config-file'
  42. API_LIST["conf"]["op"] = {}
  43. API_LIST["conf"]["op"]["save"] = 'file'
  44. API_LIST["conf"]["op"]["load"] = 'file'
  45. API_LIST["op-generate"] = {}
  46. API_LIST["op-generate"]["description"] = 'Operational mode commands - generate'
  47. API_LIST["op-generate"]["path"] = 'generate'
  48. API_LIST["op-generate"]["op"] = {}
  49. API_LIST["op-generate"]["op"]["generate"] = 'path'
  50. API_LIST["op-show"] = {}
  51. API_LIST["op-show"]["description"] = 'Operational mode commands - show'
  52. API_LIST["op-show"]["path"] = 'show'
  53. API_LIST["op-show"]["op"] = {}
  54. API_LIST["op-show"]["op"]["show"] = 'path'
  55. def get_key(hostname):
  56. # permcheck
  57. instance = Instance.objects.get(hostname=hostname)
  58. return instance.key
  59. def get_api_data(hostname, api, op, cmd):
  60. instance = Instance.objects.get(hostname=hostname)
  61. if instance.https == True:
  62. protocol = "https"
  63. else:
  64. protocol = "http"
  65. if instance.port == None:
  66. instance.port = 443
  67. api_exists = False
  68. if ( api in API_LIST
  69. and 'op' in API_LIST[api]
  70. and op in API_LIST[api]['op']
  71. ):
  72. api_exists = True
  73. api_op = op
  74. api_path = API_LIST[api]['path']
  75. api_subcommand = API_LIST[api]['op'][op]
  76. else:
  77. return False
  78. if api_exists == False:
  79. return False
  80. else:
  81. api_url = protocol + "://" + instance.hostname + ":" + str(instance.port) + "/" + api_path
  82. api_data = {
  83. 'api_url': api_url,
  84. 'api_op': api_op,
  85. 'api_subcommand': api_subcommand
  86. }
  87. log("api call", api_data)
  88. return api_data
  89. def api(hostname, api, op, cmd, description = ""):
  90. api_data = get_api_data(hostname=hostname, api=api, op=op, cmd=cmd)
  91. if api_data == False:
  92. v = vyapi(result = False)
  93. return v
  94. cmd = {
  95. "op": api_data['api_op'],
  96. api_data['api_subcommand'] : cmd
  97. }
  98. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  99. log("api " + api_data['api_subcommand'], post)
  100. post = {
  101. 'key': get_key(hostname),
  102. 'data': json.dumps(cmd)
  103. }
  104. try:
  105. resp = requests.post(api_data['api_url'], verify=False, data=post, timeout=10)
  106. except requests.exceptions.ConnectionError:
  107. try:
  108. status_code = resp.status_code
  109. except UnboundLocalError:
  110. status_code = 0
  111. v = vyapi(result = False, reason= {
  112. 'exception' : 'requests.exceptions.ConnectionError',
  113. 'respcode' : status_code
  114. })
  115. log("failed to post url", api_data['api_url'])
  116. return v
  117. try:
  118. respjson = resp.json()
  119. except json.JSONDecodeError:
  120. respjson = {'success': False, 'error': None, 'data': None}
  121. v = vyapi(
  122. result = respjson['success'],
  123. reason = respjson['error'],
  124. data = respjson['data']
  125. )
  126. log("api resp", [v.result, v.reason, v.data])
  127. log_vars = {
  128. 'api_url': api_data['api_url'],
  129. 'api_op': api_data['api_op'],
  130. 'api_cmd': cmd,
  131. 'resp_obj': resp,
  132. 'resp_code': resp.status_code,
  133. 'resp_result': v.result,
  134. 'resp_reason': v.reason,
  135. 'resp_data': v.data
  136. }
  137. log("api " + description, log_vars)
  138. return v