brpm_content 0.1.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +38 -0
  3. data/.travis.yml +14 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE +21 -0
  6. data/README.md +308 -0
  7. data/Rakefile +24 -0
  8. data/TO_BE_MIGRATED.txt +8 -0
  9. data/architecture.png +0 -0
  10. data/brpm_content.gemspec +31 -0
  11. data/customers/demo/integrations/brpm/process_event_handler_event.rb +183 -0
  12. data/customers/demo/integrations/jenkins/spec/api/applications_spec.rb +89 -0
  13. data/customers/demo/integrations/jenkins/spec/api/plans_spec.rb +69 -0
  14. data/customers/demo/integrations/jenkins/spec/api/requests_spec.rb +57 -0
  15. data/customers/demo/integrations/jenkins/spec/spec_helper.rb +33 -0
  16. data/customers/demo/integrations/jira/process_webhook_event.rb +33 -0
  17. data/customers/demo/jira_mappings.rb +29 -0
  18. data/infrastructure/integrations/event_handler.rb +62 -0
  19. data/infrastructure/integrations/webhook_receiver.rb +49 -0
  20. data/infrastructure/shell_scripts/backup_database.sh +19 -0
  21. data/infrastructure/shell_scripts/create_bl_package.sh +34 -0
  22. data/infrastructure/shell_scripts/create_release_request.sh +34 -0
  23. data/infrastructure/shell_scripts/ddns.sh +10 -0
  24. data/infrastructure/shell_scripts/install_brpm.sh +63 -0
  25. data/infrastructure/shell_scripts/install_content_module.sh +66 -0
  26. data/infrastructure/shell_scripts/install_content_repo.sh +60 -0
  27. data/infrastructure/shell_scripts/install_module_from_gem.sh +80 -0
  28. data/infrastructure/shell_scripts/maintenance.sh +4 -0
  29. data/infrastructure/shell_scripts/patch_brpm.sh +90 -0
  30. data/infrastructure/shell_scripts/restore_database.sh +33 -0
  31. data/infrastructure/shell_scripts/run_event_handler.cmd +16 -0
  32. data/infrastructure/shell_scripts/run_event_handler.sh +20 -0
  33. data/infrastructure/shell_scripts/run_webhook_receiver.cmd +14 -0
  34. data/infrastructure/shell_scripts/run_webhook_receiver.sh +15 -0
  35. data/infrastructure/shell_scripts/update_content_repo.sh +21 -0
  36. data/infrastructure/silent_install_options_4.6.txt +93 -0
  37. data/infrastructure/silent_install_options_upgrade_4.6.txt +92 -0
  38. data/infrastructure/smtp_settings.rb +42 -0
  39. data/infrastructure/ssh_script_header_additions.rb +21 -0
  40. data/modules/ansible/automations/execute_role.rb +3 -0
  41. data/modules/ansible/automations/execute_role.txt +12 -0
  42. data/modules/ansible/config.yml +5 -0
  43. data/modules/ansible/lib/ansible.rb +41 -0
  44. data/modules/atrium_orchestrator/automations/execute_workflow.rb +12 -0
  45. data/modules/atrium_orchestrator/automations/execute_workflow.txt +25 -0
  46. data/modules/atrium_orchestrator/automations/reserve_ip_address.rb +22 -0
  47. data/modules/atrium_orchestrator/automations/reserve_ip_address.txt +21 -0
  48. data/modules/atrium_orchestrator/config.yml +7 -0
  49. data/modules/atrium_orchestrator/lib/ao_soap_client.rb +94 -0
  50. data/modules/bladelogic/automations/create_package.rb +29 -0
  51. data/modules/bladelogic/automations/create_package.txt +26 -0
  52. data/modules/bladelogic/automations/deploy_package.rb +137 -0
  53. data/modules/bladelogic/automations/deploy_package.txt +53 -0
  54. data/modules/bladelogic/config.yml +7 -0
  55. data/modules/bladelogic/lib/bl_rest/bsa_rest_client.rb +65 -0
  56. data/modules/bladelogic/lib/bl_soap/blpackage.rb +43 -0
  57. data/modules/bladelogic/lib/bl_soap/bsa_soap_base.rb +118 -0
  58. data/modules/bladelogic/lib/bl_soap/bsa_soap_client.rb +34 -0
  59. data/modules/bladelogic/lib/bl_soap/compliance_job.rb +155 -0
  60. data/modules/bladelogic/lib/bl_soap/deploy_job.rb +48 -0
  61. data/modules/bladelogic/lib/bl_soap/depot_group.rb +47 -0
  62. data/modules/bladelogic/lib/bl_soap/job.rb +376 -0
  63. data/modules/bladelogic/lib/bl_soap/job_group.rb +72 -0
  64. data/modules/bladelogic/lib/bl_soap/job_management.rb +22 -0
  65. data/modules/bladelogic/lib/bl_soap/job_result.rb +12 -0
  66. data/modules/bladelogic/lib/bl_soap/job_run.rb +271 -0
  67. data/modules/bladelogic/lib/bl_soap/login.rb +47 -0
  68. data/modules/bladelogic/lib/bl_soap/nsh_script_job.rb +14 -0
  69. data/modules/bladelogic/lib/bl_soap/patch_catalog.rb +178 -0
  70. data/modules/bladelogic/lib/bl_soap/patch_remediation_job.rb +137 -0
  71. data/modules/bladelogic/lib/bl_soap/patching_job.rb +250 -0
  72. data/modules/bladelogic/lib/bl_soap/server.rb +12 -0
  73. data/modules/bladelogic/lib/bl_soap/template.rb +13 -0
  74. data/modules/bladelogic/lib/bl_soap/utility.rb +99 -0
  75. data/modules/bladelogic/resource_automations/select_job.rb +33 -0
  76. data/modules/bladelogic/resource_automations/select_job.txt +15 -0
  77. data/modules/bladelogic/tests/create_package_spec.rb +28 -0
  78. data/modules/bladelogic/tests/deploy_package_spec.rb +23 -0
  79. data/modules/bladelogic/tests/spec_helper.rb +82 -0
  80. data/modules/brpm/automations/create_release_from_excel.rb +34 -0
  81. data/modules/brpm/automations/create_release_from_excel.txt +5 -0
  82. data/modules/brpm/automations/create_release_request.rb +100 -0
  83. data/modules/brpm/automations/create_release_request.txt +5 -0
  84. data/modules/brpm/automations/create_request.rb +73 -0
  85. data/modules/brpm/automations/create_request.txt +23 -0
  86. data/modules/brpm/automations/promote_request.rb +197 -0
  87. data/modules/brpm/automations/promote_request.txt +24 -0
  88. data/modules/brpm/automations/select_application_version.rb +22 -0
  89. data/modules/brpm/automations/select_application_version.txt +17 -0
  90. data/modules/brpm/automations/select_component_version.rb +12 -0
  91. data/modules/brpm/automations/select_component_version.txt +5 -0
  92. data/modules/brpm/automations/select_request_template.rb +6 -0
  93. data/modules/brpm/automations/select_request_template.txt +13 -0
  94. data/modules/brpm/automations/wait_for_step_in_other_request.rb +11 -0
  95. data/modules/brpm/automations/wait_for_step_in_other_request.txt +13 -0
  96. data/modules/brpm/automations/wait_till_promoted_request_is_completed.rb +9 -0
  97. data/modules/brpm/automations/wait_till_promoted_request_is_completed.txt +5 -0
  98. data/modules/brpm/config.yml +6 -0
  99. data/modules/brpm/lib/brpm_request.rb +207 -0
  100. data/modules/brpm/lib/brpm_rest.rb +312 -0
  101. data/modules/brpm/lib/brpm_rest_client.rb +1501 -0
  102. data/modules/brpm/resource_automations/select_application.rb +16 -0
  103. data/modules/brpm/resource_automations/select_application.txt +5 -0
  104. data/modules/brpm/resource_automations/select_environment.rb +14 -0
  105. data/modules/brpm/resource_automations/select_environment.txt +5 -0
  106. data/modules/brpm/resource_automations/select_request_template.rb +16 -0
  107. data/modules/brpm/resource_automations/select_request_template.txt +5 -0
  108. data/modules/brpm/resource_automations/select_step_in_request.rb +33 -0
  109. data/modules/brpm/resource_automations/select_step_in_request.txt +5 -0
  110. data/modules/brpm/sample_applications/e-finance.json +0 -0
  111. data/modules/brpm/tests/create_release_request_spec.rb +66 -0
  112. data/modules/brpm/tests/create_request_spec.rb +84 -0
  113. data/modules/brpm/tests/select_application_version_spec.rb +47 -0
  114. data/modules/brpm/tests/spec_helper.rb +63 -0
  115. data/modules/docker_brpm/automations/create_brpm_instance_input_params.rb +10 -0
  116. data/modules/docker_brpm/automations/create_brpm_instance_input_params.txt +17 -0
  117. data/modules/docker_brpm/automations/create_brpm_instance_one_shot.rb +60 -0
  118. data/modules/docker_brpm/automations/create_brpm_instance_one_shot.txt +14 -0
  119. data/modules/docker_brpm/automations/create_brpm_instance_show_results.rb +23 -0
  120. data/modules/docker_brpm/automations/create_brpm_instance_show_results.txt +13 -0
  121. data/modules/docker_brpm/automations/create_database.rb +24 -0
  122. data/modules/docker_brpm/automations/create_database.txt +3 -0
  123. data/modules/docker_brpm/automations/create_environment.rb +14 -0
  124. data/modules/docker_brpm/automations/create_environment.txt +3 -0
  125. data/modules/docker_brpm/automations/create_image_for_application.rb +23 -0
  126. data/modules/docker_brpm/automations/create_image_for_application.txt +6 -0
  127. data/modules/docker_brpm/automations/create_image_for_brpm_application_input_params.rb +7 -0
  128. data/modules/docker_brpm/automations/create_image_for_brpm_application_input_params.txt +8 -0
  129. data/modules/docker_brpm/automations/create_image_for_brpm_database_creator_input_params.rb +7 -0
  130. data/modules/docker_brpm/automations/create_image_for_brpm_database_creator_input_params.txt +8 -0
  131. data/modules/docker_brpm/automations/create_image_for_database_creator.rb +21 -0
  132. data/modules/docker_brpm/automations/create_image_for_database_creator.txt +6 -0
  133. data/modules/docker_brpm/automations/create_installed_component_for_application.rb +11 -0
  134. data/modules/docker_brpm/automations/create_installed_component_for_application.txt +3 -0
  135. data/modules/docker_brpm/automations/create_installed_component_for_database.rb +7 -0
  136. data/modules/docker_brpm/automations/create_installed_component_for_database.txt +3 -0
  137. data/modules/docker_brpm/automations/create_installed_component_for_database_creator.rb +8 -0
  138. data/modules/docker_brpm/automations/create_installed_component_for_database_creator.txt +3 -0
  139. data/modules/docker_brpm/automations/create_installed_component_for_utilities.rb +7 -0
  140. data/modules/docker_brpm/automations/create_installed_component_for_utilities.txt +3 -0
  141. data/modules/docker_brpm/automations/create_version_tags_for_component_in_all_envs.rb +13 -0
  142. data/modules/docker_brpm/automations/create_version_tags_for_component_in_all_envs.txt +6 -0
  143. data/modules/docker_brpm/automations/delete_brpm_instance.rb +38 -0
  144. data/modules/docker_brpm/automations/delete_brpm_instance.txt +8 -0
  145. data/modules/docker_brpm/automations/execute_test_run_in_jenkins.rb +43 -0
  146. data/modules/docker_brpm/automations/execute_test_run_in_jenkins.txt +13 -0
  147. data/modules/docker_brpm/automations/execute_test_run_in_jenkins_show_results.rb +5 -0
  148. data/modules/docker_brpm/automations/execute_test_run_in_jenkins_show_results.txt +9 -0
  149. data/modules/docker_brpm/automations/launch_create_image_for_brpm_application_request.rb +27 -0
  150. data/modules/docker_brpm/automations/launch_create_image_for_brpm_application_request.txt +3 -0
  151. data/modules/docker_brpm/automations/launch_test_run_request.rb +30 -0
  152. data/modules/docker_brpm/automations/launch_test_run_request.txt +14 -0
  153. data/modules/docker_brpm/automations/release_brpm_input_params.rb +7 -0
  154. data/modules/docker_brpm/automations/release_brpm_input_params.txt +8 -0
  155. data/modules/docker_brpm/automations/run_application_container.rb +27 -0
  156. data/modules/docker_brpm/automations/run_application_container.txt +3 -0
  157. data/modules/docker_brpm/automations/run_database_container.rb +14 -0
  158. data/modules/docker_brpm/automations/run_database_container.txt +3 -0
  159. data/modules/docker_brpm/automations/stop_application_container.rb +11 -0
  160. data/modules/docker_brpm/automations/stop_application_container.txt +3 -0
  161. data/modules/docker_brpm/automations/stop_database_container.rb +11 -0
  162. data/modules/docker_brpm/automations/stop_database_container.txt +3 -0
  163. data/modules/docker_brpm/automations/test_rest_api.rb +18 -0
  164. data/modules/docker_brpm/automations/test_rest_api.txt +3 -0
  165. data/modules/docker_brpm/automations/upgrade_database.rb +19 -0
  166. data/modules/docker_brpm/automations/upgrade_database.txt +8 -0
  167. data/modules/docker_brpm/config.yml +8 -0
  168. data/modules/docker_brpm/lib/docker.rb +39 -0
  169. data/modules/docker_brpm/lib/git.rb +16 -0
  170. data/modules/docker_brpm/sample_applications/dockerizedbrpm.json +0 -0
  171. data/modules/email/automations/send_notification.rb +48 -0
  172. data/modules/email/automations/send_notification.txt +27 -0
  173. data/modules/email/config.yml +6 -0
  174. data/modules/framework/automations/direct_execute.rb +10 -0
  175. data/modules/framework/automations/direct_execute.txt +16 -0
  176. data/modules/framework/automations/install_module.rb +13 -0
  177. data/modules/framework/automations/install_module.txt +16 -0
  178. data/modules/framework/bin/brpm_install +106 -0
  179. data/modules/framework/bin/event_handler +63 -0
  180. data/modules/framework/bin/webhook_receiver +49 -0
  181. data/modules/framework/brpm_auto.rb +307 -0
  182. data/modules/framework/brpm_framework.rb +88 -0
  183. data/modules/framework/brpm_script_executor.rb +146 -0
  184. data/modules/framework/config.yml +7 -0
  185. data/modules/framework/config/customer_include.rb +26 -0
  186. data/modules/framework/config/server.yml +3 -0
  187. data/modules/framework/customer_include_default.rb +25 -0
  188. data/modules/framework/lib/logging/brpm_logger.rb +39 -0
  189. data/modules/framework/lib/logging/logger_base.rb +36 -0
  190. data/modules/framework/lib/logging/simple_logger.rb +27 -0
  191. data/modules/framework/lib/params/all_params.rb +80 -0
  192. data/modules/framework/lib/params/integration_settings.rb +27 -0
  193. data/modules/framework/lib/params/params.rb +172 -0
  194. data/modules/framework/lib/params/params_base.rb +81 -0
  195. data/modules/framework/lib/params/request_params.rb +38 -0
  196. data/modules/framework/lib/resource_framework.rb +211 -0
  197. data/modules/framework/lib/rest_api.rb +155 -0
  198. data/modules/framework/lib/semaphore.rb +79 -0
  199. data/modules/framework/lib/utilities.rb +317 -0
  200. data/modules/framework/lib/version_control/git.rb +192 -0
  201. data/modules/framework/lib/version_control/svn.rb +221 -0
  202. data/modules/framework/log.html +39 -0
  203. data/modules/framework/tests/all_params_spec.rb +116 -0
  204. data/modules/framework/tests/brpm_auto_spec.rb +84 -0
  205. data/modules/framework/tests/customer_include/config/customer_include.rb +10 -0
  206. data/modules/framework/tests/customer_include/config/server.yml +3 -0
  207. data/modules/framework/tests/customer_include_spec.rb +29 -0
  208. data/modules/framework/tests/local_jirb.rb +15 -0
  209. data/modules/framework/tests/params_spec.rb +172 -0
  210. data/modules/framework/tests/request_params_spec.rb +86 -0
  211. data/modules/framework/tests/server_yaml_spec.rb +19 -0
  212. data/modules/framework/tests/spec_helper.rb +42 -0
  213. data/modules/framework/transport/dispatch_baa.rb +355 -0
  214. data/modules/framework/transport/dispatch_base.rb +345 -0
  215. data/modules/framework/transport/dispatch_nsh.rb +248 -0
  216. data/modules/framework/transport/dispatch_ssh.rb +154 -0
  217. data/modules/framework/transport/transport_baa.rb +1095 -0
  218. data/modules/framework/transport/transport_nsh.rb +359 -0
  219. data/modules/framework/transport/transport_ssh.rb +220 -0
  220. data/modules/framework/write_to.rb +1 -0
  221. data/modules/jenkins/automations/create_release.rb +4 -0
  222. data/modules/jenkins/automations/create_release.txt +23 -0
  223. data/modules/jenkins/automations/f2_downloadJenkinsArtifact.rb +126 -0
  224. data/modules/jenkins/config.yml +7 -0
  225. data/modules/jenkins/lib/jenkins.rb +300 -0
  226. data/modules/jenkins/resource_automations/f2_rsc_jenkinsJobs.rb +58 -0
  227. data/modules/jira/automations/add_comment.rb +4 -0
  228. data/modules/jira/automations/add_comment.txt +23 -0
  229. data/modules/jira/automations/create_release.rb +4 -0
  230. data/modules/jira/automations/create_release.txt +23 -0
  231. data/modules/jira/automations/delete_release.rb +4 -0
  232. data/modules/jira/automations/delete_release.txt +23 -0
  233. data/modules/jira/automations/transition_issue.rb +4 -0
  234. data/modules/jira/automations/transition_issue.txt +23 -0
  235. data/modules/jira/automations/transition_issues_for_request.rb +29 -0
  236. data/modules/jira/automations/transition_issues_for_request.txt +20 -0
  237. data/modules/jira/automations/transition_issues_for_run.rb +26 -0
  238. data/modules/jira/automations/transition_issues_for_run.txt +20 -0
  239. data/modules/jira/automations/update_release.rb +4 -0
  240. data/modules/jira/automations/update_release.txt +26 -0
  241. data/modules/jira/config.yml +7 -0
  242. data/modules/jira/lib/jira_rest_client.rb +185 -0
  243. data/modules/jira/tests/create_read_update_delete_release_spec.rb +39 -0
  244. data/modules/jira/tests/spec_helper.rb +39 -0
  245. data/modules/jira/tests/transition_issue_spec.rb +32 -0
  246. data/modules/patch_process_management/automations/execute_job/script.rb +105 -0
  247. data/modules/patch_process_management/automations/execute_job/wrapper.txt +39 -0
  248. data/modules/patch_process_management/automations/patch_analysis/script.rb +107 -0
  249. data/modules/patch_process_management/automations/patch_analysis/wrapper.txt +33 -0
  250. data/modules/patch_process_management/automations/patch_readiness/script.rb +89 -0
  251. data/modules/patch_process_management/automations/patch_readiness/wrapper.txt +33 -0
  252. data/modules/patch_process_management/automations/patch_remediation/script.rb +217 -0
  253. data/modules/patch_process_management/automations/patch_remediation/wrapper.txt +40 -0
  254. data/modules/patch_process_management/config.yml +10 -0
  255. data/modules/teamcity/automations/trigger_build.rb +4 -0
  256. data/modules/teamcity/automations/trigger_build.txt +14 -0
  257. data/modules/teamcity/config.yml +7 -0
  258. data/modules/teamcity/lib/teamcity_rest_client.rb +17 -0
  259. data/modules/teamcity/tests/spec_helper.rb +34 -0
  260. data/modules/teamcity/tests/trigger_build_spec.rb +27 -0
  261. data/modules/windows/automations/deploy_directory_over_windows_shares/script.rb +87 -0
  262. data/modules/windows/automations/deploy_directory_over_windows_shares/wrapper.txt +29 -0
  263. data/modules/windows/automations/deploy_file_over_windows_shares/script.rb +31 -0
  264. data/modules/windows/automations/deploy_file_over_windows_shares/wrapper.txt +24 -0
  265. data/modules/windows/config.yml +6 -0
  266. data/modules/windows/lib/samba.rb +29 -0
  267. metadata +397 -0
@@ -0,0 +1,1501 @@
1
+ class BrpmRestClient
2
+ def initialize(brpm_url = BrpmAuto.params.brpm_url, brpm_api_token= BrpmAuto.params.brpm_api_token)
3
+ @brpm_url = brpm_url
4
+ @brpm_api_token = brpm_api_token
5
+ end
6
+
7
+ # Performs a get on the passed model
8
+ #
9
+ # ==== Attributes
10
+ #
11
+ # * +model_name+ - rpm model [requests, plans, steps, version_tags, etc]
12
+ # * +model_id+ - id of a specific item in the model (optional)
13
+ # * +options+ - hash of options includes
14
+ # +filters+ - string of the filter text: filters[BrpmAuto.login]=bbyrd
15
+ # includes all the Rest.rest_call options
16
+ #
17
+ # ==== Returns
18
+ #
19
+ # * hash of http response
20
+ def get(model_name, model_id = nil, options = {})
21
+ url = get_brpm_url(model_name, model_id) if get_option(options, "filters") == ""
22
+ url = get_brpm_url(model_name, nil, options["filters"]) if get_option(options, "filters") != ""
23
+ result = Rest.get(url, options)
24
+
25
+ result = brpm_get "v1/#{model_name}#{model_id == nil ? "" : "/#{model_id}" }"
26
+ end
27
+
28
+ # Performs a put on the passed model
29
+ # use this to update a single record
30
+ # ==== Attributes
31
+ #
32
+ # * +model_name+ - rpm model [requests, plans, steps, version_tags, etc]
33
+ # * +model_id+ - id of a specific item in the model (optional)
34
+ # * +data+ - hash of the put data
35
+ # * +options+ - hash of options includes
36
+ # includes all the Rest.rest_call options
37
+ #
38
+ # ==== Returns
39
+ #
40
+ # * hash of http response
41
+ def update(model_name, model_id, data, options = {})
42
+ url = get_brpm_url(model_name, model_id)
43
+ options["data"] = data
44
+ result = Rest.put(url, options)
45
+ result
46
+ end
47
+
48
+ # Performs a post on the passed model
49
+ # use this to create a new record
50
+ # ==== Attributes
51
+ #
52
+ # * +model_name+ - rpm model [requests, plans, steps, version_tags, etc]
53
+ # * +data+ - hash of the put data
54
+ # * +options+ - hash of options includes
55
+ # includes all the Rest.rest_call options
56
+ #
57
+ # ==== Returns
58
+ #
59
+ # * hash of http response
60
+ def create(model_name, data, options = {})
61
+ options["data"] = data
62
+ url = get_brpm_url(model_name)
63
+ result = Rest.post(url, options)
64
+ result
65
+ end
66
+
67
+ def get_user_by_id(user_id)
68
+ result = brpm_get "v1/users/#{user_id}"
69
+
70
+ if result["status"] == "success"
71
+ result_hash = result["response"]
72
+ else
73
+ raise "Error searching for user #{user_id}: #{result["error_message"]}"
74
+ end
75
+
76
+ result_hash
77
+ end
78
+
79
+ def get_user_by_name(first_name, last_name)
80
+ result = brpm_get "v1/users?filters[first_name]=#{first_name}&filters[last_name]=#{last_name}"
81
+
82
+ if result["status"] == "success"
83
+ result_hash = result["response"].first
84
+ else
85
+ if result["code"] == 404
86
+ result_hash=nil
87
+ else
88
+ raise "Could not find user #{first_name} #{last_name}: #{result["error_message"]}"
89
+ end
90
+ end
91
+
92
+ result_hash
93
+ end
94
+
95
+ def get_group_by_name(name)
96
+ result = brpm_get "v1/groups?filters[name]=#{name}"
97
+
98
+ if result["status"] == "success"
99
+ result_hash = result["response"].first
100
+ else
101
+ if result["code"] == 404
102
+ result_hash=nil
103
+ else
104
+ raise "Could not find group #{name}: #{result["error_message"]}"
105
+ end
106
+ end
107
+
108
+ result_hash
109
+ end
110
+
111
+ def get_property_by_name(property_name)
112
+ result = brpm_get "v1/properties?filters[name]=#{property_name}"
113
+
114
+ if result["status"] == "success"
115
+ result_hash = result["response"].first
116
+ else
117
+ if result["code"] == 404
118
+ result_hash=nil
119
+ else
120
+ raise "Could not find property #{property_name}: #{result["error_message"]}"
121
+ end
122
+ end
123
+
124
+ result_hash
125
+ end
126
+
127
+ def create_server(server_name, dns = nil, environment_ids = nil, ip_address = nil, os_platform = nil, property_ids = nil)
128
+ server={}
129
+ server["name"] = server_name
130
+ server["environment_ids"] = environment_ids unless environment_ids.nil?
131
+ server["ip_address"] = ip_address unless ip_address.nil?
132
+ server["os_platform"] = os_platform unless os_platform.nil?
133
+ server["property_ids"] = property_ids unless property_ids.nil?
134
+
135
+ result = brpm_post "v1/servers", { :server => server }
136
+
137
+ if result["status"] == "success"
138
+ result_hash = result["response"]
139
+ else
140
+ if Rest.already_exists_error(result)
141
+ BrpmAuto.log "This server already exists."
142
+ result_hash = get_server_by_name(server_name)
143
+ existing_environment_ids = result_hash["environments"].map { |env| env["id"] }
144
+
145
+ environment_ids ||= []
146
+
147
+ (environment_ids - existing_environment_ids).each do |environment_id|
148
+ link_environment_to_server(environment_id, server_name)
149
+ end
150
+ else
151
+ raise "Could not create server: #{result["error_message"]}"
152
+ end
153
+ end
154
+
155
+ result_hash
156
+ end
157
+
158
+ def get_server_by_name(server_name)
159
+ result = brpm_get "v1/servers?filters[name]=#{server_name}"
160
+
161
+ if result["status"] == "success"
162
+ result_hash = result["response"].first
163
+ else
164
+ if result["code"] == 404
165
+ result_hash=nil
166
+ else
167
+ raise "Could not find server #{server_name}: #{result["error_message"]}"
168
+ end
169
+ end
170
+
171
+ result_hash
172
+ end
173
+
174
+ def get_request_templates_by_app(app_name)
175
+ app = get_app_by_name(app_name)
176
+
177
+ result = brpm_get "v1/request_templates?filters[app_id]=#{app["id"]}"
178
+
179
+ if result["status"] == "success"
180
+ result_hash = result["response"]
181
+ else
182
+ if result["code"] == 404
183
+ result_hash={}
184
+ else
185
+ raise "Could not find request templates for app #{app_name}: #{result["error_message"]}"
186
+ end
187
+ end
188
+
189
+ result_hash
190
+ end
191
+
192
+ def get_apps
193
+ result = brpm_get "v1/apps"
194
+
195
+ if result["status"] == "success"
196
+ result_hash = result["response"]
197
+ else
198
+ if result["code"] == 404
199
+ result_hash = {}
200
+ else
201
+ raise "Error getting apps: #{result["error_message"]}"
202
+ end
203
+ end
204
+
205
+ result_hash
206
+ end
207
+
208
+ def get_app_by_id(app_id)
209
+ result = brpm_get "v1/apps/#{app_id}"
210
+
211
+ if result["status"] == "success"
212
+ result_hash = result["response"]
213
+ else
214
+ raise "Error searching for application #{app_id}: #{result["error_message"]}"
215
+ end
216
+
217
+ result_hash
218
+ end
219
+
220
+ def get_app_by_name(app_name)
221
+ result = brpm_get "v1/apps?filters[name]=#{app_name}"
222
+
223
+ if result["status"] == "success"
224
+ result_hash = result["response"].first
225
+ else
226
+ if result["code"] == 404
227
+ result_hash=nil
228
+ else
229
+ raise "Could not find application #{app_name}: #{result["error_message"]}"
230
+ end
231
+ end
232
+
233
+ result_hash
234
+ end
235
+
236
+ def get_component_by_id(component_id)
237
+ result = brpm_get "v1/components/#{component_id}"
238
+
239
+ if result["status"] == "success"
240
+ result_hash = result["response"]
241
+ else
242
+ raise "Error searching for component #{component_id}: #{result["error_message"]}"
243
+ end
244
+
245
+ result_hash
246
+ end
247
+
248
+ def get_environments_of_application(app_name)
249
+ app = get_app_by_name(app_name)
250
+ app["environments"]
251
+ end
252
+
253
+ def get_environment_by_id(environment_id)
254
+ result = brpm_get "v1/environments/#{environment_id}"
255
+
256
+ if result["status"] == "success"
257
+ result_hash = result["response"]
258
+ else
259
+ raise "Error searching for environment #{environment_id}: #{result["error_message"]}"
260
+ end
261
+
262
+ result_hash
263
+ end
264
+
265
+ def get_environment_by_name(environment_name)
266
+ result = brpm_get "v1/environments?filters[name]=#{environment_name}"
267
+
268
+ if result["status"] == "success"
269
+ result_hash = result["response"].first
270
+ else
271
+ if result["code"] == 404
272
+ result_hash=nil
273
+ else
274
+ raise "Error searching for environment #{environment_name}: #{result["error_message"]}"
275
+ end
276
+ end
277
+
278
+ result_hash
279
+ end
280
+
281
+ def create_environment(environment_name)
282
+ environment={}
283
+ environment["name"]=environment_name
284
+
285
+ result = brpm_post "v1/environments", { :environment => environment }
286
+
287
+ if result["status"] == "success"
288
+ result_hash = result["response"]
289
+ else
290
+ if Rest.already_exists_error(result)
291
+ BrpmAuto.log "This environment already exists. Continuing ..."
292
+ result_hash = get_environment_by_name(environment_name)
293
+ else
294
+ raise "Could not create environment: #{result["error_message"]}"
295
+ end
296
+ end
297
+
298
+ result_hash
299
+ end
300
+
301
+ def rename_environment(environment_id, environment_name)
302
+ environment={}
303
+ environment["name"]="#{Time.now.strftime("%Y%m%d%H%M%S")} #{environment_name}"
304
+
305
+ result = brpm_put "v1/environments/#{environment_id}", { :environment => environment }
306
+
307
+ unless result["status"] == "success"
308
+ raise "Could not rename environment: #{result["error_message"]}"
309
+ end
310
+ end
311
+
312
+ def delete_environment(environment_name)
313
+ environment = get_environment_by_name(environment_name)
314
+
315
+ if environment.nil?
316
+ BrpmAuto.log "This environment doesn't exist. Continuing ..."
317
+ return
318
+ end
319
+
320
+ rename_environment(environment["id"], environment_name)
321
+
322
+ result = brpm_delete "v1/environments/#{environment["id"]}"
323
+
324
+ unless result["status"] == "success"
325
+ raise "Could not delete environment: #{result["error_message"]}"
326
+ end
327
+ end
328
+
329
+ def link_environment_to_server(environment_id, server_name)
330
+ server = get_server_by_name(server_name)
331
+
332
+ server_to_update={}
333
+ server_to_update["environment_ids"] = server["environments"].map{|f| f["id"]}
334
+
335
+ if server_to_update["environment_ids"].include?(environment_id)
336
+ BrpmAuto.log "This server is already linked to the application. Continuing..."
337
+ return
338
+ end
339
+
340
+ server_to_update["environment_ids"].push(environment_id)
341
+
342
+ result = brpm_put "v1/servers/#{server["id"]}", { :server => server_to_update }
343
+
344
+ unless result["status"] == "success"
345
+ raise "Could not link environment to server: #{result["error_message"]}"
346
+ end
347
+ end
348
+
349
+ def unlink_environment_from_server(environment_name, server_name)
350
+ server = get_server_by_name(server_name)
351
+
352
+ if server.nil?
353
+ BrpmAuto.log "This server doesn't exist. Continuing ..."
354
+ return
355
+ end
356
+
357
+ environment = get_environment_by_name(environment_name)
358
+
359
+ if environment.nil?
360
+ BrpmAuto.log "This environment doesn't exist. Continuing ..."
361
+ return
362
+ end
363
+
364
+ server_to_update={}
365
+ server_to_update["environment_ids"] = server["environments"].map{|f| f["id"]}
366
+
367
+ unless server_to_update["environment_ids"].include?(environment["id"])
368
+ BrpmAuto.log "This environment is not linked to the server. Continuing ..."
369
+ return
370
+ end
371
+
372
+ server_to_update["environment_ids"].delete(environment["id"])
373
+
374
+ result = brpm_put "v1/servers/#{server["id"]}", { :server => server_to_update}
375
+
376
+ unless result["status"] == "success"
377
+ raise "Could not unlink environment from server: #{result["error_message"]}"
378
+ end
379
+ end
380
+
381
+ def link_environment_to_app(environment_id, app_name)
382
+ app = get_app_by_name(app_name)
383
+
384
+ app_to_update={}
385
+ app_to_update["environment_ids"] = app["environments"].map{|f| f["id"]}
386
+
387
+ if app_to_update["environment_ids"].include?(environment_id)
388
+ BrpmAuto.log "This environment is already linked to the application. Continuing..."
389
+ return
390
+ end
391
+
392
+ app_to_update["environment_ids"].push(environment_id)
393
+
394
+ result = brpm_put "v1/apps/#{app["id"]}", { :app => app_to_update}
395
+
396
+ unless result["status"] == "success"
397
+ raise "Could not link environment to app: #{result["error_message"]}"
398
+ end
399
+ end
400
+
401
+ def unlink_environment_from_app(environment_name, app_name)
402
+ app = get_app_by_name(app_name)
403
+
404
+ if app.nil?
405
+ BrpmAuto.log "This application doesn't exist. Continuing ..."
406
+ return
407
+ end
408
+
409
+ environment = get_environment_by_name(environment_name)
410
+
411
+ if environment.nil?
412
+ BrpmAuto.log "This environment doesn't exist. Continuing ..."
413
+ return
414
+ end
415
+
416
+ app_to_update={}
417
+ app_to_update["environment_ids"] = app["environments"].map{|f| f["id"]}
418
+
419
+ unless app_to_update["environment_ids"].include?(environment["id"])
420
+ BrpmAuto.log "This environment is not linked to the app. Continuing ..."
421
+ return
422
+ end
423
+
424
+ app_to_update["environment_ids"].delete(environment["id"])
425
+
426
+ result = brpm_put "v1/apps/#{app["id"]}", { :app => app_to_update}
427
+
428
+ unless result["status"] == "success"
429
+ raise "Could not unlink environment from app: #{result["error_message"]}"
430
+ end
431
+ end
432
+
433
+ def get_installed_component_by_id(component_id)
434
+ result = brpm_get "v1/installed_components/#{component_id}"
435
+
436
+ if result["status"] == "success"
437
+ result_hash = result["response"]
438
+ else
439
+ raise "Error searching for installed component #{component_id}: #{result["error_message"]}"
440
+ end
441
+
442
+ result_hash
443
+ end
444
+
445
+ def get_installed_component(component_name, environment_name)
446
+ result = brpm_get "v1/installed_components?filters[component_name]=#{component_name}&filters[environment_name]=#{environment_name}"
447
+
448
+ if result["status"] == "success"
449
+ result_hash = result["response"].first
450
+ else
451
+ if result["code"] == 404
452
+ result_hash=nil
453
+ else
454
+ raise "Error searching for installed component #{component_name} / #{environment_name} #{result["error_message"]}"
455
+ end
456
+ end
457
+
458
+ result_hash
459
+ end
460
+
461
+ def get_installed_components_by(filter)
462
+ filter_string = "?"
463
+ filter.each do |key, value|
464
+ filter_string += "filters[#{key}]=#{value}&"
465
+ end
466
+ filter_string = filter_string[0..-1]
467
+
468
+ result = brpm_get "v1/installed_components#{filter_string}"
469
+
470
+ if result["status"] == "success"
471
+ result_hash = result["response"]
472
+ else
473
+ if result["code"] == 404
474
+ result_hash = {}
475
+ else
476
+ raise "Error searching for installed_components by #{filter_string}: #{result["error_message"]}"
477
+ end
478
+ end
479
+
480
+ result_hash
481
+ end
482
+
483
+ def create_installed_component(component_name, component_version, environment_name, app_name, server_name)
484
+ installed_comp = {}
485
+ installed_comp["app_name"] = app_name
486
+ installed_comp["component_name"] = component_name
487
+ installed_comp["environment_name"] = environment_name
488
+ installed_comp["version"]= component_version
489
+ installed_comp["server_names"] = [ server_name ]
490
+
491
+ result = brpm_post "v1/installed_components", { :installed_component => installed_comp }
492
+
493
+ if result["status"] == "success"
494
+ result_hash = result["response"]
495
+ else
496
+ if Rest.already_exists_error(result)
497
+ BrpmAuto.log "This installed component already exists."
498
+ result_hash = get_installed_component(component_name, environment_name)
499
+ result_hash = get_installed_component_by_id(result_hash["id"])
500
+
501
+ servers = result_hash["servers"].map { |server| server["name"] }
502
+
503
+ BrpmAuto.log "Verifying if the existing installed component is already linked to the server ..."
504
+ if servers.include?(server_name)
505
+ BrpmAuto.log "The existing installed component is already linked to the server. Continuing ..."
506
+ else
507
+ BrpmAuto.log "The existing installed component is not yet linked to the server, doing it now ..."
508
+ servers.push server_name
509
+ set_servers_of_installed_component(result_hash["id"], servers)
510
+ end
511
+ else
512
+ raise "Could not create installed component: #{result["error_message"]}"
513
+ end
514
+ end
515
+
516
+ BrpmAuto.log "Copying the version tags from environment [default] to environment #{environment_name} ..."
517
+ copy_version_tags_of_app_and_comp_from_env_to_env(app_name, component_name, "[default]", environment_name)
518
+
519
+ result_hash
520
+ end
521
+
522
+ def set_property_of_installed_component(installed_component_id, property, value)
523
+ installed_comp= {}
524
+ installed_comp["properties_with_values"] = {}
525
+ installed_comp["properties_with_values"][property] = value
526
+
527
+ result = brpm_put "v1/installed_components/#{installed_component_id}", { :installed_component => installed_comp }
528
+
529
+ unless result["status"] == "success"
530
+ raise "Could not set properties on installed component: #{result["error_message"]}"
531
+ end
532
+ end
533
+
534
+ def get_server_group_from_step_id(step_id)
535
+ step = get_step_by_id(step_id)
536
+
537
+ installed_component = get_installed_component_by_id(step["installed_component"]["id"])
538
+
539
+ return nil unless installed_component["server_group"]
540
+
541
+ installed_component["server_group"]["name"]
542
+ end
543
+
544
+ def set_servers_of_installed_component(installed_component_id, server_names)
545
+ installed_comp= {}
546
+ installed_comp["server_names"] = server_names
547
+
548
+ result = brpm_put "v1/installed_components/#{installed_component_id}", { :installed_component => installed_comp }
549
+
550
+ unless result["status"] == "success"
551
+ raise "Could not set servers on installed component: #{result["error_message"]}"
552
+ end
553
+ end
554
+
555
+ def set_property_of_server(server_id, property_id, value)
556
+ property = {}
557
+ property["property_values_with_holders"] = {}
558
+ property["property_values_with_holders"]["value_holder_type"] = "Server"
559
+ property["property_values_with_holders"]["value_holder_id"] = server_id
560
+ property["property_values_with_holders"]["value"] = value
561
+
562
+ result = brpm_put "v1/properties/#{property_id}", { :property => property }
563
+
564
+ unless result["status"] == "success"
565
+ raise "Could not set property on server: #{result["error_message"]}"
566
+ end
567
+ end
568
+
569
+ def create_version_tag(app_name, component_name, environment, version_tag_name)
570
+ version_tag={}
571
+ version_tag["name"] = version_tag_name
572
+ version_tag["find_application"] = app_name
573
+ version_tag["find_component"] = component_name
574
+ version_tag["find_environment"] = environment
575
+ version_tag["active"] = true
576
+
577
+ result = brpm_post "v1/version_tags", { :version_tag => version_tag }
578
+
579
+ if result["status"] == "success"
580
+ result_hash = result["response"]
581
+ else
582
+ if Rest.already_exists_error(result)
583
+ BrpmAuto.log "This version tag already exists. Continuing ..."
584
+ result_hash = get_version_tag(app_name, component_name, environment, version_tag_name)
585
+ else
586
+ raise "Could not create version tag: #{result["error_message"]}"
587
+ end
588
+ end
589
+
590
+ result_hash
591
+ end
592
+
593
+ def delete_version_tag(version_tag_id)
594
+ result = brpm_delete "v1/version_tags/#{version_tag_id}"
595
+
596
+ unless result["status"] == "success"
597
+ raise "Could not delete version tag: #{result["error_message"]}"
598
+ end
599
+ end
600
+
601
+ def get_version_tag(app_name, component_name, environment_name, version_tag_name)
602
+ result = brpm_get "v1/version_tags?filters[app_name]=#{app_name}&filters[component_name]=#{component_name}&filters[environment_name]=#{environment_name}&filters[name]=#{version_tag_name}"
603
+
604
+ if result["status"] == "success"
605
+ result_hash = result["response"].first
606
+ else
607
+ if result["code"] == 404
608
+ result_hash=nil
609
+ else
610
+ raise "Could not find application #{app_name}: #{result["error_message"]}"
611
+ end
612
+ end
613
+
614
+ result_hash
615
+ end
616
+
617
+ def get_version_tags_by_app_and_comp_and_env(app_name, component_name, environment_name)
618
+ result = brpm_get "v1/version_tags?filters[app_name]=#{app_name}&filters[component_name]=#{component_name}&filters[environment_name]=#{environment_name}"
619
+
620
+ if result["status"] == "success"
621
+ result_hash = result["response"]
622
+ else
623
+ if result["code"] == 404
624
+ result_hash = {}
625
+ else
626
+ raise "Could not find version tags for app #{app_name}, component #{component_name}, environment #{environment_name}: #{result["error_message"]}"
627
+ end
628
+ end
629
+
630
+ result_hash
631
+ end
632
+
633
+ def get_version_tags_by(filter)
634
+ filter_string = "?"
635
+ filter.each do |key, value|
636
+ filter_string += "filters[#{key}]=#{value}&"
637
+ end
638
+ filter_string = filter_string[0..-1]
639
+
640
+ result = brpm_get "v1/version_tags#{filter_string}"
641
+
642
+ if result["status"] == "success"
643
+ result_hash = result["response"]
644
+ else
645
+ if result["code"] == 404
646
+ result_hash = {}
647
+ else
648
+ raise "Error searching for version_tags by #{filter_string}: #{result["error_message"]}"
649
+ end
650
+ end
651
+
652
+ result_hash
653
+ end
654
+
655
+ def copy_version_tags_of_app_and_comp_from_env_to_env(app_name, component_name, source_env, target_env)
656
+ source_version_tags = get_version_tags_by_app_and_comp_and_env(app_name, component_name, source_env)
657
+
658
+ source_version_tags.each do |source_version_tag|
659
+ create_version_tag(app_name, component_name, target_env, source_version_tag["name"])
660
+ end
661
+ end
662
+
663
+ def get_plan_template_by_name(plan_template_name)
664
+ result = brpm_get "v1/plan_templates?filters[name]=#{plan_template_name}"
665
+
666
+ if result["status"] == "success"
667
+ result_hash = result["response"].first
668
+ else
669
+ if result["code"] == 404
670
+ result_hash=nil
671
+ else
672
+ raise "Error searching for plan template #{plan_template_name}: #{result["error_message"]}"
673
+ end
674
+ end
675
+
676
+ result_hash
677
+ end
678
+
679
+ def create_plan(template_name, plan_name, date = nil)
680
+ plan = {}
681
+ plan["plan_template_name"] = template_name
682
+ plan["name"] = plan_name
683
+ plan["release_date"] = date.utc if date
684
+
685
+ result = brpm_post "v1/plans", { :plan => plan }
686
+
687
+ if result["status"] == "success"
688
+ result_hash = result["response"]
689
+ else
690
+ if Rest.already_exists_error(result)
691
+ BrpmAuto.log "This plan already exists. Continuing ..."
692
+ result_hash = get_plan_by_name(plan_name)
693
+ else
694
+ raise "Could not create plan: #{result["error_message"]}"
695
+ end
696
+ end
697
+
698
+ result_hash
699
+ end
700
+
701
+ def delete_plan(plan_id)
702
+ result = brpm_delete "v1/plans/#{plan_id}"
703
+
704
+ unless result["status"] == "success"
705
+ raise "Could not delete plan: #{result["error_message"]}"
706
+ end
707
+ end
708
+
709
+ def plan_plan(plan_id)
710
+ plan = {}
711
+ plan["aasm_event"] = "plan_it"
712
+
713
+ result = brpm_put "v1/plans/#{plan_id}", { :plan => plan}
714
+
715
+ unless result["status"] == "success"
716
+ raise "Could not plan plan: #{result["error_message"]}"
717
+ end
718
+ end
719
+
720
+ def start_plan(plan_id)
721
+ plan = {}
722
+ plan["aasm_event"] = "start"
723
+
724
+ result = brpm_put "v1/plans/#{plan_id}", { :plan => plan}
725
+
726
+ unless result["status"] == "success"
727
+ raise "Could not start plan: #{result["error_message"]}"
728
+ end
729
+ end
730
+
731
+ def cancel_plan(plan_id)
732
+ plan = {}
733
+ plan["aasm_event"] = "cancel"
734
+
735
+ result = brpm_put "v1/plans/#{plan_id}", { :plan => plan}
736
+
737
+ unless result["status"] == "success"
738
+ raise "Could not cancel plan: #{result["error_message"]}"
739
+ end
740
+ end
741
+
742
+ def get_plan_by_id(plan_id)
743
+ result = brpm_get "v1/plans/#{plan_id}"
744
+
745
+ if result["status"] == "success"
746
+ result_hash = result["response"]
747
+ else
748
+ raise "Error searching for plan #{plan_id}: #{result["error_message"]}"
749
+ end
750
+
751
+ result_hash
752
+ end
753
+
754
+ def get_plan_by_name(plan_name)
755
+ result = brpm_get "v1/plans?filters[name]=#{plan_name}"
756
+
757
+ if result["status"] == "success"
758
+ result_hash = result["response"].first
759
+ else
760
+ if result["code"] == 404
761
+ result_hash=nil
762
+ else
763
+ raise "Error searching for plan #{plan_name}: #{result["error_message"]}"
764
+ end
765
+ end
766
+
767
+ result_hash
768
+ end
769
+
770
+ def get_plans_by(filter)
771
+ filter_string = "?"
772
+ filter.each do |key, value|
773
+ filter_string += "filters[#{key}]=#{value}&"
774
+ end
775
+ filter_string = filter_string[0..-1]
776
+
777
+ result = brpm_get "v1/plans#{filter_string}"
778
+
779
+ if result["status"] == "success"
780
+ result_hash = result["response"]
781
+ else
782
+ if result["code"] == 404
783
+ result_hash = {}
784
+ else
785
+ raise "Error searching for plans by #{filter_string}: #{result["error_message"]}"
786
+ end
787
+ end
788
+
789
+ result_hash
790
+ end
791
+
792
+ def get_plan_stage_id(plan_id, stage_name)
793
+ plan = get_plan_by_id(plan_id)
794
+
795
+ plan["plan_template"]["stages"].each do |stage|
796
+ return stage["id"].to_i if stage["name"] == stage_name
797
+ end
798
+
799
+ raise "Could not find a stage named #{stage_name} for plan #{plan_id}"
800
+ end
801
+
802
+ def get_plan_stage_by_id(plan_stage_id)
803
+ result = brpm_get "v1/plan_stages/#{plan_stage_id}"
804
+
805
+ if result["status"] == "success"
806
+ result_hash = result["response"]
807
+ else
808
+ raise "Error searching for plan stage #{plan_stage_id}: #{result["error_message"]}"
809
+ end
810
+
811
+ result_hash
812
+ end
813
+
814
+ def get_route_by_id(route_id)
815
+ result = brpm_get "v1/routes/#{route_id}"
816
+
817
+ if result["status"] == "success"
818
+ result_hash = result["response"]
819
+ else
820
+ raise "Error searching for route #{route_id}: #{result["error_message"]}"
821
+ end
822
+
823
+ result_hash
824
+ end
825
+
826
+ def get_constraints
827
+ result = brpm_get "v1/constraints"
828
+
829
+ if result["status"] == "success"
830
+ result_hash = result["response"]
831
+ else
832
+ if result["code"] == 404
833
+ result_hash = {}
834
+ else
835
+ raise "Error searching for constraints: #{result["error_message"]}"
836
+ end
837
+ end
838
+
839
+ result_hash
840
+ end
841
+
842
+ def get_plan_route_by_plan_and_app(plan_id, app_id)
843
+ result = brpm_get "v1/plan_routes?filters[plan_id]=#{plan_id}&filters[app_id]=#{app_id}"
844
+
845
+ if result["status"] == "success"
846
+ result_hash = result["response"].first
847
+ else
848
+ if result["code"] == 404
849
+ result_hash=nil
850
+ else
851
+ raise "Error searching for plan route for plan id #{plan_id} and app id #{app_id}: #{result["error_message"]}"
852
+ end
853
+ end
854
+
855
+ result_hash
856
+ end
857
+
858
+ def get_route_gate_by_id(route_gate_id)
859
+ result = brpm_get "v1/route_gates/#{route_gate_id}"
860
+
861
+ if result["status"] == "success"
862
+ result_hash = result["response"]
863
+ else
864
+ raise "Error searching for route gate #{route_gate_id}: #{result["error_message"]}"
865
+ end
866
+
867
+ result_hash
868
+ end
869
+
870
+ def get_first_environment_of_route_for_plan_and_app(plan_id, app_id)
871
+ BrpmAuto.log "Getting the plan route..."
872
+ plan_route = get_plan_route_by_plan_and_app(plan_id, app_id)
873
+
874
+ unless plan_route
875
+ BrpmAuto.log "No plan route found for plan id #{plan_id} and app id #{app_id}."
876
+ return nil
877
+ end
878
+
879
+ BrpmAuto.log "Getting the route..."
880
+ route = get_route_by_id(plan_route["route"]["id"])
881
+
882
+ route_gate = route["route_gates"].find { |route_gate| route_gate["position"] == 1 }
883
+
884
+ return route_gate["environment"]["name"]
885
+ end
886
+
887
+ def create_request(template_name, name, environment, execute_now, data = {})
888
+ request = {}
889
+ request["template_name"] = template_name
890
+ request["name"] = name
891
+ request["environment"] = environment
892
+ request["execute_now"] = execute_now
893
+ request["data"] = data
894
+
895
+ result = brpm_post "v1/requests", { :request => request }
896
+
897
+ unless result["status"] == "success"
898
+ raise "Could not create the request: #{result["error_message"]}"
899
+ end
900
+
901
+ result["response"]
902
+ end
903
+
904
+ def create_request_from_hash(request)
905
+ result = brpm_post "v1/requests", { :request => request }
906
+
907
+ unless result["status"] == "success"
908
+ raise "Could not create the request: #{result["error_message"]}"
909
+ end
910
+
911
+ result["response"]
912
+ end
913
+
914
+ def create_request_for_plan(plan_id, stage_name, name, requestor_id, app_name, env_name, execute_now, data = nil)
915
+ plan_stage_id = get_plan_stage_id(plan_id, stage_name)
916
+
917
+ request = {}
918
+ request["plan_member_attributes"] = { "plan_id" => plan_id, "plan_stage_id" => plan_stage_id }
919
+ request["name"] = name
920
+ request["requestor_id"] = requestor_id
921
+ request["deployment_coordinator_id"] = requestor_id
922
+ request["app_ids"] = [ get_app_by_name(app_name)["id"] ]
923
+ request["environment"] = env_name
924
+ request["execute_now"] = execute_now
925
+ request["data"] = data
926
+
927
+ result = brpm_post "v1/requests", { :request => request }
928
+
929
+ unless result["status"] == "success"
930
+ raise "Could not create the request: #{result["error_message"]}"
931
+ end
932
+
933
+ result["response"]
934
+ end
935
+
936
+ def create_request_for_plan_from_template(plan_id, stage_name, template_name, name, env_name, execute_now, data = {})
937
+ plan_stage_id = get_plan_stage_id(plan_id, stage_name)
938
+
939
+ request = {}
940
+ request["plan_member_attributes"] = { "plan_id" => plan_id, "plan_stage_id" => plan_stage_id }
941
+ request["template_name"] = template_name
942
+ request["name"] = name
943
+ request["environment"] = env_name
944
+ request["execute_now"] = execute_now
945
+ request["data"] = data
946
+
947
+ result = brpm_post "v1/requests", { :request => request }
948
+
949
+ unless result["status"] == "success"
950
+ raise "Could not create the request: #{result["error_message"]}"
951
+ end
952
+
953
+ result["response"]
954
+ end
955
+
956
+ def delete_request(request_id)
957
+ result = brpm_delete "v1/requests/#{request_id}"
958
+
959
+ unless result["status"] == "success"
960
+ raise "Could not delete request: #{result["error_message"]}"
961
+ end
962
+ end
963
+
964
+ def move_request_to_plan_and_stage(request_id, plan_id, stage_name)
965
+ plan_stage_id = get_plan_stage_id(plan_id, stage_name)
966
+
967
+ request = {}
968
+ request["plan_member_attributes"] = { "plan_id" => plan_id, "plan_stage_id" => plan_stage_id }
969
+
970
+ result = brpm_put "v1/requests/#{request_id}", { :request => request }
971
+
972
+ unless result["status"] == "success"
973
+ raise "Could not create the request: #{result["error_message"]}"
974
+ end
975
+
976
+ result["response"]
977
+ end
978
+
979
+ def plan_request(request_id)
980
+ request = {}
981
+ request["aasm_event"] = "plan_it"
982
+
983
+ result = brpm_put "v1/requests/#{request_id}", { :request => request}
984
+
985
+ unless result["status"] == "success"
986
+ raise "Could not plan request: #{result["error_message"]}"
987
+ end
988
+ end
989
+
990
+ def start_request(request_id)
991
+ request = {}
992
+ request["aasm_event"] = "start"
993
+
994
+ result = brpm_put "v1/requests/#{request_id}", { :request => request}
995
+
996
+ unless result["status"] == "success"
997
+ raise "Could not start request: #{result["error_message"]}"
998
+ end
999
+ end
1000
+
1001
+ def get_request_by_id(request_id)
1002
+ result = brpm_get "v1/requests/#{request_id}"
1003
+
1004
+ if result["status"] == "success"
1005
+ result_hash = result["response"]
1006
+ else
1007
+ raise "Error searching for request #{request_id}: #{result["error_message"]}"
1008
+ end
1009
+
1010
+ result_hash
1011
+ end
1012
+
1013
+ def get_requests_by_name(request_name)
1014
+ result = brpm_get "v1/requests?filters[name]=#{request_name}"
1015
+
1016
+ if result["status"] == "success"
1017
+ result_hash = result["response"]
1018
+ else
1019
+ if result["code"] == 404
1020
+ result_hash=nil
1021
+ else
1022
+ raise "Error searching for request #{request_name}: #{result["error_message"]}"
1023
+ end
1024
+ end
1025
+
1026
+ result_hash
1027
+ end
1028
+
1029
+ def get_requests_by(filter)
1030
+ filter_string = "?"
1031
+ filter.each do |key, value|
1032
+ filter_string += "filters[#{key}]=#{value}&"
1033
+ end
1034
+ filter_string = filter_string[0..-1]
1035
+
1036
+ result = brpm_get "v1/requests#{filter_string}"
1037
+
1038
+ if result["status"] == "success"
1039
+ result_hash = result["response"]
1040
+ else
1041
+ if result["code"] == 404
1042
+ result_hash = {}
1043
+ else
1044
+ raise "Error searching for request by #{filter_string}: #{result["error_message"]}"
1045
+ end
1046
+ end
1047
+
1048
+ result_hash
1049
+ end
1050
+
1051
+ def get_requests_by_name_and_plan_and_stage(request_name, plan_name, stage_name)
1052
+ result = brpm_get "v1/requests?filters[name]=#{request_name}"
1053
+
1054
+ if result["status"] == "success"
1055
+ result_hash = result["response"].select { |request| request["plan_member"]["plan"]["name"] == plan_name and request["plan_member"]["stage"]["name"] == stage_name }
1056
+ else
1057
+ if result["code"] == 404
1058
+ result_hash = {}
1059
+ else
1060
+ raise "Error searching for request #{request_name}: #{result["error_message"]}"
1061
+ end
1062
+ end
1063
+
1064
+ result_hash
1065
+ end
1066
+
1067
+ def get_requests_by_plan_id_and_stage_name(plan_id, stage_name)
1068
+ result = brpm_get "v1/plans/#{plan_id}"
1069
+
1070
+ if result["status"] == "success"
1071
+ members_in_plan = result["response"]["members"]
1072
+ members_in_stage = members_in_plan.select { |member| member["stage"]["name"] == stage_name and member.has_key?("request") }
1073
+ result_hash = members_in_stage.map { |member| { "id" => member["request"]["number"].to_i - 1000, "number" => member["request"]["number"], "name" => member["request"]["name"] } }
1074
+ else
1075
+ if result["code"] == 404
1076
+ result_hash = {}
1077
+ else
1078
+ raise "Error searching for requests by plan id #{plan_id} and stage name #{stage_name}: #{result["error_message"]}"
1079
+ end
1080
+ end
1081
+
1082
+ result_hash
1083
+ end
1084
+
1085
+ def get_requests_by_plan_id_and_stage_name_and_app_name(plan_id, stage_name, app_name)
1086
+ requests_by_plan_and_stage = get_requests_by_plan_id_and_stage_name(plan_id, stage_name)
1087
+ request_ids_1 = requests_by_plan_and_stage.map { |request| request["id"] }
1088
+
1089
+ requests_by_app = get_requests_by({ "app_id" => get_app_by_name(app_name)["id"] })
1090
+ request_ids_2 = requests_by_app.map { |request| request["id"] }
1091
+
1092
+ request_ids_1 & request_ids_2
1093
+ end
1094
+
1095
+ def update_request_from_hash(request)
1096
+ result = brpm_put "v1/requests/#{request["id"]}", { :request => request }
1097
+
1098
+ unless result["status"] == "success"
1099
+ raise "Could not update the request: #{result["error_message"]}"
1100
+ end
1101
+
1102
+ result["response"]
1103
+ end
1104
+
1105
+ def monitor_request(request_id, options = {})
1106
+ target_status = "complete"
1107
+
1108
+ max_time = 15*60 # seconds
1109
+ max_time = 60 * options[:max_time].to_i if options.has_key?(:max_time) and !options[:max_time].nil?
1110
+
1111
+ monitor_step_name = (options.has_key?(:monitor_step_name) and !options[:monitor_step_name].nil?) ? options[:monitor_step_name] : "none"
1112
+ monitor_step_id = (options.has_key?(:monitor_step_id) and !options[:monitor_step_id].nil?) ? options[:monitor_step_id] : "none"
1113
+
1114
+ checking_interval = 15 #seconds
1115
+ checking_interval = options[:checking_interval].to_i if options.has_key?(:checking_interval) and !options[:checking_interval].nil?
1116
+
1117
+ req_status = "none"
1118
+ start_time = Time.now
1119
+ elapsed = 0
1120
+ BrpmAuto.log "Starting the monitoring loop for request #{request_id} with an interval of #{checking_interval} seconds and a maximum time of #{max_time} seconds ..."
1121
+ until (elapsed > max_time || req_status == target_status)
1122
+ request = get_request_by_id(request_id)
1123
+
1124
+ if monitor_step_name == "none" and monitor_step_id == "none"
1125
+ req_status = request["aasm_state"]
1126
+ else
1127
+ found = false
1128
+ request["steps"].each do |step|
1129
+ if (monitor_step_name != "none" and step["name"] == monitor_step_name) or
1130
+ (monitor_step_id != "none" and step["id"] == monitor_step_id)
1131
+ req_status = step["aasm_state"]
1132
+ found = true
1133
+ break
1134
+ end
1135
+ end
1136
+ unless found
1137
+ raise "Step #{monitor_step_name} not found in request #{request_id}"
1138
+ end
1139
+ end
1140
+
1141
+ if req_status == target_status
1142
+ BrpmAuto.log "Found request in #{target_status} status! Returning."
1143
+ break
1144
+ end
1145
+
1146
+ if req_status == "problem"
1147
+ raise "Found request #{request_id} in problem state."
1148
+ end
1149
+
1150
+ BrpmAuto.log "\tWaiting(#{elapsed.floor.to_s}) - Current status: #{req_status}"
1151
+ sleep(checking_interval)
1152
+ elapsed = Time.now - start_time
1153
+ end
1154
+
1155
+ if elapsed > max_time
1156
+ raise "Maximum time: #{max_time}(secs) reached. Status is: #{req_status}, looking for: #{target_status}"
1157
+ end
1158
+ end
1159
+
1160
+ def create_step_from_hash(step)
1161
+ result = brpm_post "v1/steps", { :step => step }
1162
+
1163
+ unless result["status"] == "success"
1164
+ raise "Could not create the step: #{result["error_message"]}"
1165
+ end
1166
+
1167
+ result["response"]
1168
+ end
1169
+
1170
+ def create_step(request_id, name, owner_type, owner_id)
1171
+ step = {}
1172
+ step["request_id"] = request_id
1173
+ step["name"] = name
1174
+ step["owner_type"] = owner_type
1175
+ step["owner_id"] = owner_id
1176
+
1177
+ result = brpm_post "v1/steps", { :step => step }
1178
+
1179
+ unless result["status"] == "success"
1180
+ raise "Could not create the step: #{result["error_message"]}"
1181
+ end
1182
+
1183
+ result["response"]
1184
+ end
1185
+
1186
+ def get_step_by_id(step_id)
1187
+ result = brpm_get "v1/steps/#{step_id}"
1188
+
1189
+ if result["status"] == "success"
1190
+ result_hash = result["response"]
1191
+ else
1192
+ raise "Error searching for step #{step_id}: #{result["error_message"]}"
1193
+ end
1194
+
1195
+ result_hash
1196
+ end
1197
+
1198
+ def update_step_from_hash(step)
1199
+ result = brpm_put "v1/steps/#{step["id"]}", { :step => step }
1200
+
1201
+ unless result["status"] == "success"
1202
+ raise "Could not update the step: #{result["error_message"]}"
1203
+ end
1204
+
1205
+ result["response"]
1206
+ end
1207
+
1208
+ def get_steps_by(filter)
1209
+ filter_string = "?"
1210
+ filter.each do |key, value|
1211
+ filter_string += "filters[#{key}]=#{value}&"
1212
+ end
1213
+ filter_string = filter_string[0..-1]
1214
+
1215
+ result = brpm_get "v1/steps#{filter_string}"
1216
+
1217
+ if result["status"] == "success"
1218
+ result_hash = result["response"]
1219
+ else
1220
+ if result["code"] == 404
1221
+ result_hash = {}
1222
+ else
1223
+ raise "Error searching for steps by #{filter_string}: #{result["error_message"]}"
1224
+ end
1225
+ end
1226
+
1227
+ result_hash
1228
+ end
1229
+
1230
+ def get_run_by_id(run_id)
1231
+ result = brpm_get "v1/runs/#{run_id}"
1232
+
1233
+ if result["status"] == "success"
1234
+ result_hash = result["response"]
1235
+ else
1236
+ raise "Error searching for run #{run_id}: #{result["error_message"]}"
1237
+ end
1238
+
1239
+ result_hash
1240
+ end
1241
+
1242
+ def set_version_tag_of_steps_for_component(request, component_name, version_tag_name)
1243
+ steps_for_component = request["steps"].select{ |step| step["component_name"] == component_name}
1244
+
1245
+ return if steps_for_component.count == 0
1246
+
1247
+ version_tag = get_version_tag(request["apps"].first["name"], component_name, request["environment"]["name"], version_tag_name)
1248
+
1249
+ if version_tag.nil?
1250
+ raise "No version tag found for app #{request["apps"].first["name"]}, component #{component_name}, environment #{request["environment"]["name"]}, version tag name #{version_tag_name}"
1251
+ end
1252
+
1253
+ steps_for_component.each do |step|
1254
+ step_data = {}
1255
+ step_data["version_tag_id"] = version_tag["id"]
1256
+ step_data["component_version"] = version_tag_name
1257
+
1258
+ result = brpm_put "v1/steps/#{step["id"]}", { :step => step_data}
1259
+
1260
+ unless result["status"] == "success"
1261
+ raise "Could not set the version tag of the step: #{result["error_message"]}"
1262
+ end
1263
+ end
1264
+ end
1265
+
1266
+ def set_version_of_steps_for_component(request, component_name, version)
1267
+ steps_for_component = request["steps"].select{ |step| step["component_name"] == component_name}
1268
+ steps_for_component.each do |step|
1269
+ step_data = {}
1270
+ step_data["component_version"] = version
1271
+
1272
+ result = brpm_put "v1/steps/#{step["id"]}", { :step => step_data}
1273
+
1274
+ unless result["status"] == "success"
1275
+ raise "Could not set the version of the step: #{result["error_message"]}"
1276
+ end
1277
+ end
1278
+ end
1279
+
1280
+ def get_ticket_by_foreign_id(ticket_foreign_id)
1281
+ result = brpm_get "v1/tickets?filters[foreign_id]=#{ticket_foreign_id}"
1282
+
1283
+ if result["status"] == "success"
1284
+ result_hash = result["response"].first
1285
+ else
1286
+ if result["code"] == 404
1287
+ result_hash = nil
1288
+ else
1289
+ raise "Could not find ticket #{ticket_foreign_id}: #{result["error_message"]}"
1290
+ end
1291
+ end
1292
+
1293
+ result_hash
1294
+ end
1295
+
1296
+ def get_tickets_by_request_id(request_id)
1297
+ result = brpm_get "v1/tickets?filters[request_id]=#{request_id}"
1298
+
1299
+ if result["status"] == "success"
1300
+ result_hash = result["response"]
1301
+ else
1302
+ if result["code"] == 404
1303
+ result_hash = {}
1304
+ else
1305
+ raise "Error searching for tickets by rquest id #{request_id}: #{result["error_message"]}"
1306
+ end
1307
+ end
1308
+
1309
+ result_hash
1310
+ end
1311
+
1312
+ def get_tickets_by_run_id_and_request_state(run_id, state)
1313
+ BrpmAuto.log "Getting the requests that are part of run #{run_id} and have state #{state}..."
1314
+ run = get_run_by_id(run_id)
1315
+ request_ids = run["plan_members"].select { |member| member.has_key?("request") and member["request"]["aasm_state"] == state }.map { |member| member["request"]["id"] }
1316
+ BrpmAuto.log "Found #{request_ids.count} requests."
1317
+
1318
+ BrpmAuto.log "Getting the tickets that are linked to each of the found requests..."
1319
+ tickets = []
1320
+ request_ids.each do |request_id|
1321
+ tickets_for_request = get_tickets_by_request_id(request_id)
1322
+ tickets = tickets.merge(tickets_for_request)
1323
+ end
1324
+ BrpmAuto.log "Found #{tickets.count} tickets in total."
1325
+
1326
+ tickets
1327
+ end
1328
+
1329
+ def get_tickets_by(filter)
1330
+ filter_string = "?"
1331
+ filter.each do |key, value|
1332
+ filter_string += "filters[#{key}]=#{value}&"
1333
+ end
1334
+ filter_string = filter_string[0..-1]
1335
+
1336
+ result = brpm_get "v1/tickets#{filter_string}"
1337
+
1338
+ if result["status"] == "success"
1339
+ result_hash = result["response"]
1340
+ else
1341
+ if result["code"] == 404
1342
+ result_hash = {}
1343
+ else
1344
+ raise "Error searching for tickets by #{filter_string}: #{result["error_message"]}"
1345
+ end
1346
+ end
1347
+
1348
+ result_hash
1349
+ end
1350
+
1351
+ def create_ticket_from_hash(ticket)
1352
+ result = brpm_post "v1/tickets", { :ticket => ticket }
1353
+
1354
+ unless result["status"] == "success"
1355
+ raise "Could not create the ticket: #{result["error_message"]}"
1356
+ end
1357
+
1358
+ result["response"]
1359
+ end
1360
+
1361
+ def update_ticket_from_hash(ticket)
1362
+ result = brpm_put "v1/tickets/#{ticket["id"]}", { :ticket => ticket }
1363
+
1364
+ unless result["status"] == "success"
1365
+ raise "Could not update the ticket: #{result["error_message"]}"
1366
+ end
1367
+
1368
+ result["response"]
1369
+ end
1370
+
1371
+ def create_or_update_ticket(ticket)
1372
+ BrpmAuto.log "Checking if the corresponding ticket already exists ..."
1373
+ existing_ticket = get_ticket_by_foreign_id ticket["foreign_id"]
1374
+
1375
+ if existing_ticket.nil?
1376
+ BrpmAuto.log "Ticket doesn't exist yet."
1377
+ ticket_already_exists=false
1378
+ else
1379
+ BrpmAuto.log "Ticket already exists."
1380
+ ticket_already_exists=true
1381
+
1382
+ ticket["id"] = existing_ticket["id"].to_s
1383
+ ticket["extended_attributes_attributes"] = sync_attributes(existing_ticket["extended_attributes"], ticket["extended_attributes_attributes"])
1384
+ end
1385
+
1386
+ now = Time.now.to_s
1387
+ sync_attribute "first received", now, ticket["extended_attributes_attributes"] unless ticket_already_exists
1388
+ sync_attribute "last updated", now, ticket["extended_attributes_attributes"]
1389
+
1390
+ # Create/update the ticket
1391
+ data = {}
1392
+ data["ticket"] = ticket
1393
+
1394
+ if ticket_already_exists
1395
+ BrpmAuto.log "Updating the ticket ..."
1396
+ update_ticket_from_hash(ticket)
1397
+ BrpmAuto.log "Ticket is updated."
1398
+ else
1399
+ BrpmAuto.log "Creating the ticket ..."
1400
+ create_ticket_from_hash(ticket)
1401
+ BrpmAuto.log "Ticket is created."
1402
+ end
1403
+ end
1404
+
1405
+ def sync_attributes(existing_attributes, updated_attributes)
1406
+ existing_attributes ||= []
1407
+ updated_attributes ||= []
1408
+
1409
+ updated_attributes.each do |updated_attribute|
1410
+ existing_attribute = existing_attributes.find { |existing_attribute| existing_attribute["name"] == updated_attribute["name"] }
1411
+ if existing_attribute.nil?
1412
+ existing_attributes.push(updated_attribute)
1413
+ else
1414
+ existing_attribute["value_text"] = updated_attribute["value_text"]
1415
+ end
1416
+ end
1417
+ existing_attributes
1418
+ end
1419
+
1420
+ def get_attribute_value name, attributes
1421
+ attribute = attributes.find { |attribute| attribute["name"] == name }
1422
+
1423
+ return attribute["value_text"] unless attribute.nil?
1424
+ end
1425
+
1426
+ def sync_attribute name, value, attributes
1427
+ attribute = attributes.find { |attribute| attribute["name"] == name }
1428
+
1429
+ if attribute.nil?
1430
+ attribute = {}
1431
+ attribute["name"] = name
1432
+ attributes.push(attribute)
1433
+ end
1434
+
1435
+ attribute["value_text"] = value
1436
+ end
1437
+
1438
+ def get_work_task_by_name(name)
1439
+ result = brpm_get "v1/work_tasks?filters[name]=#{name}"
1440
+
1441
+ if result["status"] == "success"
1442
+ result_hash = result["response"].first
1443
+ else
1444
+ if result["code"] == 404
1445
+ result_hash = nil
1446
+ else
1447
+ raise "Could not find work_task #{name}: #{result["error_message"]}"
1448
+ end
1449
+ end
1450
+
1451
+ result_hash
1452
+ end
1453
+
1454
+ # Sends an email based on step recipients
1455
+ #
1456
+ # ==== Attributes
1457
+ #
1458
+ # * +subject+ - text of email subject
1459
+ # * +body+ - text of email body
1460
+ #
1461
+ # ==== Returns
1462
+ #
1463
+ # * empty string
1464
+ def notify(body, subject = "Mail from automation", recipients = nil, step_id = BrpmAuto.params.step_id)
1465
+ data = { "filters" => { "notify" => { "body" => body, "subject" => subject } } }
1466
+ data["filters"]["notify"]["recipients"] = recipients unless recipients.nil?
1467
+
1468
+ result = brpm_get "v1/steps/#{step_id}/notify", { :data => data } # a REST GET with a request body???
1469
+ end
1470
+
1471
+ private
1472
+
1473
+ def add_token(path)
1474
+ path + (path.include?("?") ? "&" : "?") + "token=#{@brpm_api_token}"
1475
+ end
1476
+
1477
+ def get_brpm_url(model_name, id = nil, filters = nil)
1478
+ url = "#{@brpm_url}/v1/#{model_name}#{id == nil ? "" : "/#{id}" }"
1479
+ url += "?#{filters}" if filters
1480
+
1481
+ add_token(url)
1482
+ end
1483
+
1484
+ def brpm_get(path, options = {})
1485
+ Rest.get("#{@brpm_url}/#{add_token(path)}", options)
1486
+ end
1487
+
1488
+ def brpm_post(path, data, options = {})
1489
+ Rest.post("#{@brpm_url}/#{add_token(path)}", data, options)
1490
+ end
1491
+
1492
+ def brpm_put(path, data, options = {})
1493
+ Rest.put("#{@brpm_url}/#{add_token(path)}", data, options)
1494
+ end
1495
+
1496
+ def brpm_delete(path, options = {})
1497
+ Rest.delete("#{@brpm_url}/#{add_token(path)}", options)
1498
+ end
1499
+ end
1500
+
1501
+