vycontrol_vyos_api_lib.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 vycontrol_messages 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. log("api " + " !!!!!!!!!!!!!! START NEW WEB PROCESS", end=False)
  27. API_LIST = {}
  28. API_LIST["get"] = {}
  29. API_LIST["get"]["description"] = 'Show config'
  30. API_LIST["get"]["path"] = 'retrieve'
  31. API_LIST["get"]["op"] = {}
  32. API_LIST["get"]["op"]["showConfig"] = 'path'
  33. API_LIST["post"] = {}
  34. API_LIST["post"]["description"] = 'Configuration mode requests'
  35. API_LIST["post"]["path"] = "configure"
  36. API_LIST["post"]["op"] = {}
  37. API_LIST["post"]["op"]["set"] = 'path'
  38. API_LIST["post"]["op"]["delete"] = 'path'
  39. API_LIST["post"]["op"]["comment"] = 'path'
  40. API_LIST["conf"] = {}
  41. API_LIST["conf"]["description"] = 'Configuration management requests'
  42. API_LIST["conf"]["path"] = 'config-file'
  43. API_LIST["conf"]["op"] = {}
  44. API_LIST["conf"]["op"]["save"] = 'file'
  45. API_LIST["conf"]["op"]["load"] = 'file'
  46. API_LIST["op-generate"] = {}
  47. API_LIST["op-generate"]["description"] = 'Operational mode commands - generate'
  48. API_LIST["op-generate"]["path"] = 'generate'
  49. API_LIST["op-generate"]["op"] = {}
  50. API_LIST["op-generate"]["op"]["generate"] = 'path'
  51. API_LIST["op-show"] = {}
  52. API_LIST["op-show"]["description"] = 'Operational mode commands - show'
  53. API_LIST["op-show"]["path"] = 'show'
  54. API_LIST["op-show"]["op"] = {}
  55. API_LIST["op-show"]["op"]["show"] = 'path'
  56. def get_key(hostname):
  57. # permcheck
  58. instance = Instance.objects.get(hostname=hostname)
  59. return instance.key
  60. def get_api_data(hostname, api, op, cmd):
  61. instance = Instance.objects.get(hostname=hostname)
  62. if instance.https == True:
  63. protocol = "https"
  64. else:
  65. protocol = "http"
  66. if instance.port == None:
  67. instance.port = 443
  68. api_exists = False
  69. if ( api in API_LIST
  70. and 'op' in API_LIST[api]
  71. and op in API_LIST[api]['op']
  72. ):
  73. api_exists = True
  74. api_op = op
  75. api_path = API_LIST[api]['path']
  76. api_subcommand = API_LIST[api]['op'][op]
  77. else:
  78. return False
  79. if api_exists == False:
  80. return False
  81. else:
  82. #log("api_path ", api_path)
  83. #log("protocol ", protocol)
  84. #log("instance.hostname ", instance.hostname)
  85. #log("instance.port ", instance.port)
  86. api_url = protocol + "://" + instance.hostname + ":" + str(instance.port) + "/" + api_path
  87. api_data = {
  88. 'api_url': api_url,
  89. 'api_op': api_op,
  90. 'api_subcommand': api_subcommand
  91. }
  92. log("api call", api_data)
  93. return api_data
  94. def api(hostname, api, op, cmd, description = ""):
  95. api_data = get_api_data(hostname=hostname, api=api, op=op, cmd=cmd)
  96. if api_data == False:
  97. v = vyapi(result = False)
  98. return v
  99. cmd = {
  100. "op": api_data['api_op'],
  101. api_data['api_subcommand'] : cmd
  102. }
  103. post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
  104. log("api " + api_data['api_subcommand'], post)
  105. post = {
  106. 'key': get_key(hostname),
  107. 'data': json.dumps(cmd)
  108. }
  109. try:
  110. resp = requests.post(api_data['api_url'], verify=False, data=post, timeout=10)
  111. except requests.exceptions.ConnectionError:
  112. try:
  113. status_code = resp.status_code
  114. except UnboundLocalError:
  115. status_code = 0
  116. v = vyapi(result = False, reason= {
  117. 'exception' : 'requests.exceptions.ConnectionError',
  118. 'respcode' : status_code
  119. })
  120. log("failed to post url", api_data['api_url'])
  121. return v
  122. try:
  123. respjson = resp.json()
  124. except json.JSONDecodeError:
  125. respjson = {'success': False, 'error': None, 'data': None}
  126. #log("api raw", respjson)
  127. v = vyapi(
  128. result = respjson['success'],
  129. reason = respjson['error'],
  130. data = respjson['data']
  131. )
  132. log("api resp", [v.result, v.reason, v.data])
  133. log_vars = {
  134. 'api_url': api_data['api_url'],
  135. 'api_op': api_data['api_op'],
  136. 'api_cmd': cmd,
  137. 'resp_obj': resp,
  138. 'resp_code': resp.status_code,
  139. 'resp_result': v.result,
  140. 'resp_reason': v.reason,
  141. 'resp_data': v.data
  142. }
  143. log("api " + description, log_vars)
  144. return v