dhcp_api.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2017-2020 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. from flask import Flask
  27. from flask import Response, jsonify
  28. import requests
  29. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  30. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  31. import json
  32. import CLEUCreds
  33. import re
  34. from gevent.pywsgi import WSGIServer
  35. from webargs.flaskparser import use_kwargs
  36. from webargs import fields
  37. from cleu.config import Config as C
  38. PORT = 8084
  39. PAGE_LIMIT = 13
  40. app = Flask("DHCP Abstraction API")
  41. CNR_HEADERS = {"Accept": "application/json", "Content-Type": "application/json", "Authorization": CLEUCreds.JCLARKE_BASIC}
  42. def get_items_pages(*args, **kwargs):
  43. global PAGE_LIMIT
  44. more_pages = True
  45. result = []
  46. response = None
  47. cnt = 0
  48. while more_pages and cnt < PAGE_LIMIT:
  49. try:
  50. response = requests.request(*args, **kwargs)
  51. response.raise_for_status()
  52. result += response.json()
  53. if "Link" in response.headers:
  54. links = requests.utils.parse_header_links(response.headers["Link"])
  55. found_next = False
  56. for link in links:
  57. if link["rel"] == "next":
  58. args = (args[0], link["url"])
  59. kwargs.pop("params", None)
  60. found_next = True
  61. break
  62. if found_next:
  63. cnt += 1
  64. continue
  65. more_pages = False
  66. else:
  67. more_pages = False
  68. except Exception as e:
  69. return (result, response)
  70. return (result, response)
  71. @app.route("/api/v1/subnetLookup")
  72. @use_kwargs({"subnet": fields.Str()}, locations=("query",))
  73. def get_leases_for_subnet(**kwargs):
  74. if not "subnet" in kwargs:
  75. return jsonify({"msg": "subnet parameter is required"}), 400
  76. url = C.DHCP_BASE + "Scope"
  77. response = None
  78. subnet = re.sub(r"/\d+$", "", kwargs["subnet"])
  79. octets = subnet.split(".")
  80. for i, octet in enumerate(octets):
  81. if i < 2:
  82. continue
  83. if int(octet) == 0:
  84. octets[i] = ".*"
  85. subnet_re = "\\.".join(octets)
  86. try:
  87. (scopes, response) = get_items_pages("GET", url, params={"subnet": subnet_re}, headers=CNR_HEADERS, verify=False)
  88. response.raise_for_status()
  89. except Exception as e:
  90. status_code = 500
  91. if response:
  92. status_code = response.status_code
  93. return (
  94. jsonify({"msg": "Error getting scope for subnet {}: {}".format(kwargs["subnet"], getattr(e, "message", repr(e)))}),
  95. status_code,
  96. )
  97. if len(scopes) == 0:
  98. return jsonify({"msg": "Error getting scope for subnet {}".format(kwargs["subnet"])}), 400
  99. names = []
  100. for scope in scopes:
  101. names.append(scope["name"])
  102. url = C.DHCP_BASE + "Lease"
  103. result = []
  104. try:
  105. (result, response) = get_items_pages(
  106. "GET", url, params={"state": "leased", "address": subnet_re}, headers=CNR_HEADERS, verify=False
  107. )
  108. response.raise_for_status()
  109. except Exception as e:
  110. status_code = 500
  111. if response:
  112. status_code = response.status_code
  113. return (
  114. jsonify({"msg": "Error getting leases for subnet {}: {}".format(kwargs["subnet"], getattr(e, "message", repr(e)))}),
  115. staus_code,
  116. )
  117. return jsonify(result), 200
  118. if __name__ == "__main__":
  119. # app.run(host='10.100.253.13', port=8081, threaded=True)
  120. ssl_context = {"certfile": "cleu_cert.pem", "keyfile": "cleu_privkey.pem"}
  121. http_server = WSGIServer((C.WSGI_SERVER, PORT), app, **ssl_context)
  122. http_server.serve_forever()