config.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from flask import Blueprint, g
  4. from flask import request
  5. import jimit as ji
  6. import json
  7. from models import Config
  8. from models import Rules
  9. from models import Utils
  10. __author__ = 'James Iter'
  11. __date__ = '2017/3/21'
  12. __contact__ = 'james.iter.cn@gmail.com'
  13. __copyright__ = '(c) 2017 by James Iter.'
  14. blueprint = Blueprint(
  15. 'api_config',
  16. __name__,
  17. url_prefix='/api/config'
  18. )
  19. @Utils.dumps2response
  20. def r_create():
  21. config = Config()
  22. args_rules = [
  23. Rules.GLUSTERFS_VOLUME.value,
  24. Rules.STORAGE_PATH.value,
  25. Rules.VM_NETWORK.value,
  26. Rules.VM_MANAGE_NETWORK.value,
  27. Rules.START_IP.value,
  28. Rules.END_IP.value,
  29. Rules.START_VNC_PORT.value,
  30. Rules.NETMASK.value,
  31. Rules.GATEWAY.value,
  32. Rules.DNS1.value,
  33. Rules.DNS2.value,
  34. Rules.RSA_PRIVATE.value,
  35. Rules.RSA_PUBLIC.value
  36. ]
  37. config.id = 1
  38. config.glusterfs_volume = request.json.get('glusterfs_volume')
  39. config.storage_path = request.json.get('storage_path')
  40. config.vm_network = request.json.get('vm_network')
  41. config.vm_manage_network = request.json.get('vm_manage_network')
  42. config.start_ip = request.json.get('start_ip')
  43. config.end_ip = request.json.get('end_ip')
  44. config.start_vnc_port = request.json.get('start_vnc_port')
  45. config.netmask = request.json.get('netmask')
  46. config.gateway = request.json.get('gateway')
  47. config.dns1 = request.json.get('dns1')
  48. config.dns2 = request.json.get('dns2')
  49. config.rsa_private = request.json.get('rsa_private')
  50. config.rsa_public = request.json.get('rsa_public')
  51. try:
  52. ji.Check.previewing(args_rules, config.__dict__)
  53. config.check_ip()
  54. config.generate_available_ip2set()
  55. config.generate_available_vnc_port()
  56. config.create()
  57. g.config = None
  58. config.id = 1
  59. config.get()
  60. ret = dict()
  61. ret['state'] = ji.Common.exchange_state(20000)
  62. ret['data'] = config.__dict__
  63. return ret
  64. except ji.PreviewingError, e:
  65. return json.loads(e.message)
  66. @Utils.dumps2response
  67. def r_update():
  68. config = Config()
  69. args_rules = [
  70. ]
  71. if 'glusterfs_volume' in request.json:
  72. args_rules.append(
  73. Rules.GLUSTERFS_VOLUME.value,
  74. )
  75. if 'storage_path' in request.json:
  76. args_rules.append(
  77. Rules.STORAGE_PATH.value,
  78. )
  79. if 'vm_network' in request.json:
  80. args_rules.append(
  81. Rules.VM_NETWORK.value,
  82. )
  83. if 'vm_manage_network' in request.json:
  84. args_rules.append(
  85. Rules.VM_MANAGE_NETWORK.value,
  86. )
  87. if 'start_ip' in request.json:
  88. args_rules.append(
  89. Rules.START_IP.value,
  90. )
  91. if 'end_ip' in request.json:
  92. args_rules.append(
  93. Rules.END_IP.value,
  94. )
  95. if 'start_vnc_port' in request.json:
  96. args_rules.append(
  97. Rules.START_VNC_PORT.value,
  98. )
  99. if 'netmask' in request.json:
  100. args_rules.append(
  101. Rules.NETMASK.value,
  102. )
  103. if 'gateway' in request.json:
  104. args_rules.append(
  105. Rules.GATEWAY.value,
  106. )
  107. if 'dns1' in request.json:
  108. args_rules.append(
  109. Rules.DNS1.value,
  110. )
  111. if 'dns2' in request.json:
  112. args_rules.append(
  113. Rules.DNS2.value,
  114. )
  115. if 'rsa_private' in request.json:
  116. args_rules.append(
  117. Rules.RSA_PRIVATE.value,
  118. )
  119. if 'rsa_public' in request.json:
  120. args_rules.append(
  121. Rules.RSA_PUBLIC.value
  122. )
  123. if args_rules.__len__() < 1:
  124. ret = dict()
  125. ret['state'] = ji.Common.exchange_state(20000)
  126. return ret
  127. try:
  128. config.id = 1
  129. ji.Check.previewing(args_rules, request.json)
  130. config.get()
  131. config.glusterfs_volume = request.json.get('glusterfs_volume', config.glusterfs_volume)
  132. config.storage_path = request.json.get('storage_path', config.storage_path)
  133. config.vm_network = request.json.get('vm_network', config.vm_network)
  134. config.vm_manage_network = request.json.get('vm_manage_network', config.vm_manage_network)
  135. config.start_ip = request.json.get('start_ip', config.start_ip)
  136. config.end_ip = request.json.get('end_ip', config.end_ip)
  137. config.start_vnc_port = request.json.get('start_vnc_port', config.start_vnc_port)
  138. config.netmask = request.json.get('netmask', config.netmask)
  139. config.gateway = request.json.get('gateway', config.gateway)
  140. config.dns1 = request.json.get('dns1', config.dns1)
  141. config.dns2 = request.json.get('dns2', config.dns2)
  142. config.rsa_private = request.json.get('rsa_private', config.rsa_private)
  143. config.rsa_public = request.json.get('rsa_public', config.rsa_public)
  144. config.check_ip()
  145. config.generate_available_ip2set()
  146. config.generate_available_vnc_port()
  147. config.update()
  148. config.get()
  149. ret = dict()
  150. ret['state'] = ji.Common.exchange_state(20000)
  151. ret['data'] = config.__dict__
  152. return ret
  153. except ji.PreviewingError, e:
  154. return json.loads(e.message)
  155. @Utils.dumps2response
  156. def r_get():
  157. config = Config()
  158. try:
  159. config.id = 1
  160. config.get()
  161. ret = dict()
  162. ret['state'] = ji.Common.exchange_state(20000)
  163. ret['data'] = config.__dict__
  164. return ret
  165. except ji.PreviewingError, e:
  166. return json.loads(e.message)