slas_controller.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class SlasController < ApplicationController
  3. before_action :authentication_check
  4. =begin
  5. Format:
  6. JSON
  7. Example:
  8. {
  9. "id":1,
  10. "name":"some sla",
  11. "condition":{"c_a":1,"c_b":2},
  12. "data":{"o_a":1,"o_b":2},
  13. "updated_at":"2012-09-14T17:51:53Z",
  14. "created_at":"2012-09-14T17:51:53Z",
  15. "updated_by_id":2.
  16. "created_by_id":2,
  17. }
  18. =end
  19. =begin
  20. Resource:
  21. GET /api/v1/slas.json
  22. Response:
  23. [
  24. {
  25. "id": 1,
  26. "name": "some_name1",
  27. ...
  28. },
  29. {
  30. "id": 2,
  31. "name": "some_name2",
  32. ...
  33. }
  34. ]
  35. Test:
  36. curl http://localhost/api/v1/slas.json -v -u #{login}:#{password}
  37. =end
  38. def index
  39. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  40. assets = {}
  41. # calendars
  42. calendar_ids = []
  43. Calendar.all.order(:name).each {|calendar|
  44. calendar_ids.push calendar.id
  45. assets = calendar.assets(assets)
  46. }
  47. # slas
  48. sla_ids = []
  49. models = Models.all
  50. Sla.all.order(:name).each {|sla|
  51. sla_ids.push sla.id
  52. assets = sla.assets(assets)
  53. # get assets of condition
  54. sla.condition.each {|item, content|
  55. attribute = item.split(/\./)
  56. next if !attribute[1]
  57. attribute_class = attribute[0].to_classname.constantize
  58. reflection = attribute[1].sub(/_id$/, '')
  59. reflection = reflection.to_sym
  60. next if !models[attribute_class]
  61. next if !models[attribute_class][:reflections]
  62. next if !models[attribute_class][:reflections][reflection]
  63. next if !models[attribute_class][:reflections][reflection].klass
  64. attribute_ref_class = models[attribute_class][:reflections][reflection].klass
  65. if content['value'].class == Array
  66. content['value'].each {|item_id|
  67. attribute_object = attribute_ref_class.find_by(id: item_id)
  68. assets = attribute_object.assets(assets)
  69. }
  70. else
  71. attribute_object = attribute_ref_class.find_by(id: content['value'])
  72. assets = attribute_object.assets(assets)
  73. end
  74. }
  75. }
  76. render json: {
  77. calendar_ids: calendar_ids,
  78. sla_ids: sla_ids,
  79. assets: assets,
  80. }, status: :ok
  81. end
  82. =begin
  83. Resource:
  84. GET /api/v1/slas/#{id}.json
  85. Response:
  86. {
  87. "id": 1,
  88. "name": "name_1",
  89. ...
  90. }
  91. Test:
  92. curl http://localhost/api/v1/slas/#{id}.json -v -u #{login}:#{password}
  93. =end
  94. def show
  95. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  96. model_show_render(Sla, params)
  97. end
  98. =begin
  99. Resource:
  100. POST /api/v1/slas.json
  101. Payload:
  102. {
  103. "name":"some sla",
  104. "condition":{"c_a":1,"c_b":2},
  105. "data":{"o_a":1,"o_b":2},
  106. }
  107. Response:
  108. {
  109. "id": 1,
  110. "name": "some_name",
  111. ...
  112. }
  113. Test:
  114. curl http://localhost/api/v1/slas.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"name": "some_name","active": true, "note": "some note"}'
  115. =end
  116. def create
  117. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  118. model_create_render(Sla, params)
  119. end
  120. =begin
  121. Resource:
  122. PUT /api/v1/slas/{id}.json
  123. Payload:
  124. {
  125. "name":"some sla",
  126. "condition":{"c_a":1,"c_b":2},
  127. "data":{"o_a":1,"o_b":2},
  128. }
  129. Response:
  130. {
  131. "id": 1,
  132. "name": "some_name",
  133. ...
  134. }
  135. Test:
  136. curl http://localhost/api/v1/slas.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X PUT -d '{"name": "some_name","active": true, "note": "some note"}'
  137. =end
  138. def update
  139. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  140. model_update_render(Sla, params)
  141. end
  142. =begin
  143. Resource:
  144. DELETE /api/v1/slas/{id}.json
  145. Response:
  146. {}
  147. Test:
  148. curl http://localhost/api/v1/slas.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X DELETE
  149. =end
  150. def destroy
  151. return if deny_if_not_role(Z_ROLENAME_ADMIN)
  152. model_destory_render(Sla, params)
  153. end
  154. end