brpm_module_brpm 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +38 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +18 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE +21 -0
  7. data/README.md +7 -0
  8. data/Rakefile +24 -0
  9. data/automations/create_release_from_excel.rb +34 -0
  10. data/automations/create_release_from_excel.txt +5 -0
  11. data/automations/create_release_request.rb +100 -0
  12. data/automations/create_release_request.txt +5 -0
  13. data/automations/create_request.rb +73 -0
  14. data/automations/create_request.txt +23 -0
  15. data/automations/promote_request.rb +197 -0
  16. data/automations/promote_request.txt +24 -0
  17. data/automations/select_application_version.rb +22 -0
  18. data/automations/select_application_version.txt +17 -0
  19. data/automations/select_component_version.rb +12 -0
  20. data/automations/select_component_version.txt +5 -0
  21. data/automations/select_request_template.rb +6 -0
  22. data/automations/select_request_template.txt +13 -0
  23. data/automations/wait_for_step_in_other_request.rb +11 -0
  24. data/automations/wait_for_step_in_other_request.txt +13 -0
  25. data/automations/wait_till_promoted_request_is_completed.rb +9 -0
  26. data/automations/wait_till_promoted_request_is_completed.txt +5 -0
  27. data/bin/create_release_request +23 -0
  28. data/brpm_module_brpm.gemspec +39 -0
  29. data/config.yml +11 -0
  30. data/lib/brpm_request.rb +207 -0
  31. data/lib/brpm_rest.rb +312 -0
  32. data/lib/brpm_rest_client.rb +1501 -0
  33. data/resource_automations/select_application.rb +16 -0
  34. data/resource_automations/select_application.txt +5 -0
  35. data/resource_automations/select_environment.rb +14 -0
  36. data/resource_automations/select_environment.txt +5 -0
  37. data/resource_automations/select_request_template.rb +16 -0
  38. data/resource_automations/select_request_template.txt +5 -0
  39. data/resource_automations/select_step_in_request.rb +33 -0
  40. data/resource_automations/select_step_in_request.txt +5 -0
  41. data/tests/create_release_request_spec.rb +66 -0
  42. data/tests/create_request_spec.rb +84 -0
  43. data/tests/select_application_version_spec.rb +47 -0
  44. data/tests/spec_helper.rb +71 -0
  45. metadata +131 -0
@@ -0,0 +1,12 @@
1
+ if BrpmAuto.request_params["auto_created"]
2
+ BrpmAuto.log "The request was created in an automated way, not overriding the request params from the manual input step."
3
+ else
4
+ params = BrpmAuto.params
5
+ component_versions = BrpmAuto.request_params["component_versions"] || {}
6
+
7
+ BrpmAuto.log "Adding component version '#{params["component"]}' '#{params["component_version"]}' to the request_params..."
8
+ component_versions[params["component"]] = params["component_version"]
9
+
10
+ BrpmAuto.request_params["component_versions"] = component_versions
11
+ end
12
+
@@ -0,0 +1,5 @@
1
+ params["direct_execute"] = "true"
2
+
3
+ require "#{ENV["BRPM_CONTENT_HOME"] || "#{ENV["BRPM_HOME"]}/modules"}/gems/brpm_content-#{ENV["BRPM_CONTENT_VERSION"] || "latest"}/modules/framework/brpm_script_executor.rb"
4
+
5
+ BrpmScriptExecutor.execute_automation_script_from_gem("brpm_module_brpm", "select_component_version", params)
@@ -0,0 +1,6 @@
1
+ params = BrpmAuto.params
2
+ request_params = BrpmAuto.request_params
3
+
4
+ BrpmAuto.log "Adding request template '#{params["request_template_id"]}' to the request_params..."
5
+ request_params["request_template_id"] = params["request_template_id"]
6
+
@@ -0,0 +1,13 @@
1
+ ###
2
+ # request_template_id:
3
+ # name: Request template
4
+ # position: A1:C1
5
+ # type: in-external-single-select
6
+ # external_resource: select_request_template
7
+ # required: yes
8
+ ###
9
+ params["direct_execute"] = "true"
10
+
11
+ require "#{ENV["BRPM_CONTENT_HOME"] || "#{ENV["BRPM_HOME"]}/modules"}/gems/brpm_content-#{ENV["BRPM_CONTENT_VERSION"] || "latest"}/modules/framework/brpm_script_executor.rb"
12
+
13
+ BrpmScriptExecutor.execute_automation_script_from_gem("brpm_module_brpm", "select_request_template", params)
@@ -0,0 +1,11 @@
1
+ brpm_rest_client = BrpmRestClient.new
2
+ params = BrpmAuto.params
3
+
4
+ req_step = params["other_request_step"].split("|")
5
+ request_id = req_step[0].to_i
6
+ step_id = req_step[1].to_i
7
+
8
+ BrpmAuto.log "Monitoring step #{step_id} of request #{request_id} ..."
9
+ brpm_rest_client.monitor_request(request_id, { :monitor_step_id => step_id })
10
+
11
+
@@ -0,0 +1,13 @@
1
+ ###
2
+ # other_request_step:
3
+ # name: other_request_step
4
+ # position: A1:F10
5
+ # type: in-external-single-select
6
+ # external_resource: select_step_in_request
7
+ # required: yes
8
+ ###
9
+ params["direct_execute"] = "true"
10
+
11
+ require "#{ENV["BRPM_CONTENT_HOME"] || "#{ENV["BRPM_HOME"]}/modules"}/gems/brpm_content-#{ENV["BRPM_CONTENT_VERSION"] || "latest"}/modules/framework/brpm_script_executor.rb"
12
+
13
+ BrpmScriptExecutor.execute_automation_script_from_gem("brpm_module_brpm", "wait_for_step_in_other_request", params)
@@ -0,0 +1,9 @@
1
+ brpm_rest_client = BrpmRestClient.new
2
+ request_params = BrpmAuto.request_params
3
+
4
+ BrpmAuto.log "Getting the request ..."
5
+ request = brpm_rest_client.get_request_by_id(request_params["promoted_request_id"])
6
+
7
+ BrpmAuto.log "Waiting until the request has finished ..."
8
+ brpm_rest_client.monitor_request(request["id"], { :max_time => 60 * 24})
9
+
@@ -0,0 +1,5 @@
1
+ params["direct_execute"] = "true"
2
+
3
+ require "#{ENV["BRPM_CONTENT_HOME"] || "#{ENV["BRPM_HOME"]}/modules"}/gems/brpm_content-#{ENV["BRPM_CONTENT_VERSION"] || "latest"}/modules/framework/brpm_script_executor.rb"
4
+
5
+ BrpmScriptExecutor.execute_automation_script_from_gem("brpm_module_brpm", "wait_till_promoted_request_is_completed", params)
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ require "brpm_script_executor"
3
+
4
+ params = {}
5
+ params["application_name"] = ENV["APPLICATION_NAME"]
6
+ params["application_version"] = ENV["APPLICATION_VERSION"]
7
+
8
+ params["ef_net_version"] = ENV["EF_NET_VERSION"]
9
+ params["ef_java_version"] = ENV["EF_JAVA_VERSION"]
10
+
11
+ params["release_request_template_name"] = ENV["RELEASE_REQUEST_TEMPLATE_NAME"]
12
+ params["release_plan_template_name"] = ENV["RELEASE_PLAN_TEMPLATE_NAME"]
13
+ params["release_plan_name"] = ENV["RELEASE_PLAN_NAME"]
14
+
15
+ params["brpm_url"] = "http://#{ENV["BRPM_HOST"]}:#{ENV["BRPM_PORT"]}/brpm"
16
+ params["brpm_api_token"] = ENV["BRPM_TOKEN"]
17
+
18
+ params["log_file"] = ENV["LOG_FILE"]
19
+ params["also_log_to_console"] = "true"
20
+
21
+ BrpmScriptExecutor.execute_automation_script_from_gem("brpm_module_brpm", "create_release_request", params)
22
+
23
+
@@ -0,0 +1,39 @@
1
+ require "yaml"
2
+
3
+ config = YAML.load_file(File.join(File.dirname(__FILE__), "config.yml"))
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = File.basename(File.expand_path(File.dirname(__FILE__)))
7
+ spec.version = config["version"]
8
+ spec.platform = Gem::Platform::RUBY
9
+ spec.license = "GNU General Public License v2.0"
10
+ spec.authors = [config["author"]]
11
+ spec.email = config["email"]
12
+ spec.homepage = config["homepage"]
13
+ spec.summary = config["summary"]
14
+ spec.description = config["description"]
15
+
16
+ spec.add_runtime_dependency "brpm_content", ">=0.1.11"
17
+
18
+ if config["dependencies"]
19
+ config["dependencies"].each do |dependency|
20
+ if dependency.is_a?(Hash)
21
+ modul = dependency.keys[0]
22
+ options = dependency.values[0]
23
+ else
24
+ modul = dependency
25
+ options = {}
26
+ end
27
+ spec.add_runtime_dependency modul, options["version"] unless ["brpm", "bladelogic", "jira"].include?(modul)
28
+ end
29
+ end
30
+
31
+ spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "rspec"
33
+
34
+ spec.files = `git ls-files`.split("\n")
35
+ spec.require_path = 'lib'
36
+
37
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
38
+ spec.bindir = "bin"
39
+ end
data/config.yml ADDED
@@ -0,0 +1,11 @@
1
+ dependencies:
2
+
3
+ integration_servers:
4
+
5
+ version: 0.1.10
6
+
7
+ author: Niek Bartholomeus
8
+ email: niek.bartholomeus@gmail.com
9
+ homepage: https://github.com/BMC-RLM/brpm_module_brpm
10
+ summary: BRPM automation scripts and libraries to run on top of the BRPM Content framework
11
+ description: BRPM automation scripts and libraries to run on top of the BRPM Content framework. See https://github.com/BMC-RLM/brpm_content for more information about the BRPM Content framework
@@ -0,0 +1,207 @@
1
+ # Class for interacting with requests
2
+ class BrpmRequest < BrpmRest
3
+
4
+ # Initializes an instance of the class
5
+ #
6
+ # ==== Attributes
7
+ #
8
+ # * +id+ - id of the request to work with
9
+ # * +base_url+ - url of brpm server
10
+ # * +options+ - hash of options (see Rest.rest_call for description)
11
+ #
12
+ def initialize(id, base_url, options = {}, compat_options = {})
13
+ if options.has_key?("SS_output_dir")
14
+ BrpmAuto.log "Load for this class has changed, no longer necessary to send params as 2nd argument"
15
+ options = compat_options
16
+ end
17
+ @id = id
18
+ response = BrpmAuto.get("requests", @id)
19
+ @request = response["data"]
20
+ end
21
+
22
+ # Gets a list of requests based on a filter
23
+ #
24
+ # ==== Attributes
25
+ #
26
+ # * +filter_param+ - filter for requests there are extensive filter options
27
+ # ex: filters["planned_end_date"]>2013-04-22
28
+ #
29
+ # ==== Returns
30
+ #
31
+ # * array of request hashs
32
+ def get_list(filter_param)
33
+ response = Rest.rest_call(rest_url("requests", @id, filter_param), "get")
34
+ @request = response["data"]
35
+ end
36
+
37
+ # Returns the steps for the request
38
+ #
39
+ # ==== Returns
40
+ #
41
+ # * hash of steps from request
42
+ def steps
43
+ steps = @request["steps"]
44
+ end
45
+
46
+ # Updates the aasm state of the request
47
+ #
48
+ # ==== Attributes
49
+ #
50
+ # * +aasm_event+ - event name [plan, start, problem, resolve]
51
+ #
52
+ # ==== Returns
53
+ #
54
+ # * hash of uppdated request
55
+ def update_state(aasm_event)
56
+ request_info = {"request" => {"aasm_event" => aasm_event }}
57
+ result = update("requests", @id, request_info)
58
+ end
59
+
60
+ # Provides a host status for the passed targets
61
+ #
62
+ # ==== Returns
63
+ #
64
+ # * hash of request
65
+ def request
66
+ @request
67
+ end
68
+
69
+ # Gets the app associated with the request
70
+ #
71
+ # ==== Returns
72
+ #
73
+ # * hash of app information
74
+ def app
75
+ @request["apps"].first
76
+ end
77
+
78
+ # Gets the installed_components associated with request application
79
+ #
80
+ # ==== Returns
81
+ #
82
+ # * hash of installed_components
83
+ def installed_components
84
+ return @installed_components if defined?(@installed_components)
85
+ res = get("installed_components", nil, {"filters" => "filters[app_name]=#{url_encode(app["name"])}"})
86
+ @installed_components = res["data"]
87
+ end
88
+
89
+ # Gets the components associated with request application
90
+ #
91
+ # ==== Returns
92
+ #
93
+ # * hash of components
94
+ def app_components
95
+ installed_components unless defined?(@installed_components)
96
+ @installed_components.map{|l| l["application_component"]["component"]}.uniq
97
+ end
98
+
99
+ # Gets the components associated with request application
100
+ #
101
+ # ==== Returns
102
+ #
103
+ # * hash of components
104
+ def app_environments
105
+ installed_components unless defined?(@installed_components)
106
+ @installed_components.map{|l| l["application_environment"]["environment"]}.uniq
107
+ end
108
+
109
+ # Gets the owner of the request
110
+ #
111
+ # ==== Returns
112
+ #
113
+ # * username of request owner
114
+ def owner
115
+ request["owner"]
116
+ end
117
+
118
+ # Gets the requestor of the request
119
+ #
120
+ # ==== Returns
121
+ #
122
+ # * username of requestor
123
+ def requestor
124
+ request["requestor"]
125
+ end
126
+
127
+ # Gets the plan of the request
128
+ #
129
+ # ==== Returns
130
+ #
131
+ # * hash of plan or nil if not part of a plan
132
+ def plan
133
+ return nil if request["plan_member"].nil?
134
+ plan_id = request["plan_member"]["plan"]["id"]
135
+ res = get("plans", plan_id)
136
+ end
137
+
138
+ # Gets the stage of the plan the request is in
139
+ #
140
+ # ==== Returns
141
+ #
142
+ # * hash of stage
143
+ def stage
144
+ return nil if request["plan_member"].nil?
145
+ request["plan_member"]["stage"]
146
+ end
147
+
148
+ # Gets the routes available for the app/plan
149
+ #
150
+ # ==== Returns
151
+ #
152
+ # * array of hashes of plan routes
153
+ def plan_routes
154
+ return nil if request["plan_member"].nil?
155
+ plan["plan_routes"]
156
+ end
157
+
158
+ # Gets the routes available for the app
159
+ #
160
+ # ==== Returns
161
+ #
162
+ # * array of hashes of routes
163
+ def app_routes
164
+ res = get("apps", app["id"])
165
+ res["data"]["routes"]
166
+ end
167
+
168
+ # Gets the environments available for the route
169
+ #
170
+ # ==== Attributes
171
+ #
172
+ # * +route_id+ - id of the route
173
+ #
174
+ # ==== Returns
175
+ #
176
+ # * array of environments for the route
177
+ def route_environments(route_id)
178
+ # Returns environment list for a particular route
179
+ envs = {}
180
+ res = get("routes", route_id)
181
+ res["data"]["route_gates"].each_with_index do |gate,idx|
182
+ envs[gate["environment"]["name"]] = {"id" => gate["environment"]["id"], "position" => idx.to_s }
183
+ end
184
+ envs
185
+ end
186
+
187
+ # Gets the plan stages available for the plan
188
+ #
189
+ # ==== Returns
190
+ #
191
+ # * array of hashes of plan stages
192
+ def plan_stages
193
+ plan["plan_stages"]
194
+ end
195
+
196
+ # Gets the groups available
197
+ #
198
+ # ==== Returns
199
+ #
200
+ # * array of hashes of groups
201
+ def groups
202
+ result = get("groups")
203
+ end
204
+
205
+
206
+ end
207
+
data/lib/brpm_rest.rb ADDED
@@ -0,0 +1,312 @@
1
+
2
+ # Base class for rest interactions
3
+ # optimized for rest calls to BRPM
4
+ class BrpmRest
5
+
6
+ # Initialize an instance of the brpmrest class
7
+ #
8
+ # ==== Attributes
9
+ #
10
+ # * +base_url+ - base url for rest calls
11
+ # * +options+ - hash of options, includes:
12
+ # token: a rest token for brpm
13
+ # output_file: file for BrpmAuto.log results (usually @p.SS_output_file)
14
+ #
15
+ def initialize(base_url, options = {}, compat_options = {})
16
+ self.extend Utilities
17
+ if options.has_key?("SS_output_dir")
18
+ BrpmAuto.log "Load for this class has changed, no longer necessary to send params as 2nd argument"
19
+ options = compat_options
20
+ end
21
+ @base_url = base_url
22
+ token = defined?(Token) ? Token : ""
23
+ @token = get_option(options, "token", token)
24
+ end
25
+
26
+ # Performs a get on the passed model
27
+ #
28
+ # ==== Attributes
29
+ #
30
+ # * +model_name+ - rpm model [requests, plans, steps, version_tags, etc]
31
+ # * +model_id+ - id of a specific item in the model (optional)
32
+ # * +options+ - hash of options includes
33
+ # +filters+ - string of the filter text: filters[BrpmAuto.login]=bbyrd
34
+ # includes all the Rest.rest_call options
35
+ #
36
+ # ==== Returns
37
+ #
38
+ # * hash of http response
39
+ def get(model_name, model_id = nil, options = {})
40
+ url = rest_url(model_name, model_id) if get_option(options, "filters") == ""
41
+ url = rest_url(model_name, nil, options["filters"]) if get_option(options, "filters") != ""
42
+ result = Rest.rest_call(url, "get", options)
43
+ end
44
+
45
+ # Performs a put on the passed model
46
+ # use this to update a single record
47
+ # ==== Attributes
48
+ #
49
+ # * +model_name+ - rpm model [requests, plans, steps, version_tags, etc]
50
+ # * +model_id+ - id of a specific item in the model (optional)
51
+ # * +data+ - hash of the put data
52
+ # * +options+ - hash of options includes
53
+ # includes all the Rest.rest_call options
54
+ #
55
+ # ==== Returns
56
+ #
57
+ # * hash of http response
58
+ def update(model_name, model_id, data, options = {})
59
+ url = rest_url(model_name, model_id)
60
+ options["data"] = data
61
+ result = Rest.rest_call(url, "put", options)
62
+ result
63
+ end
64
+
65
+ # Performs a post on the passed model
66
+ # use this to create a new record
67
+ # ==== Attributes
68
+ #
69
+ # * +model_name+ - rpm model [requests, plans, steps, version_tags, etc]
70
+ # * +data+ - hash of the put data
71
+ # * +options+ - hash of options includes
72
+ # includes all the Rest.rest_call options
73
+ #
74
+ # ==== Returns
75
+ #
76
+ # * hash of http response
77
+ def create(model_name, data, options = {})
78
+ options["data"] = data
79
+ url = rest_url(model_name)
80
+ result = Rest.rest_call(url, "post", options)
81
+ result
82
+ end
83
+
84
+ # Sets the token for brpm rest calls
85
+ #
86
+ # ==== Attributes
87
+ #
88
+ # * +token+ - rest token
89
+ #
90
+ def set_token(token)
91
+ @token = token
92
+ end
93
+
94
+ # Takes an array of version tag info and creates the version tags
95
+ #
96
+ # ==== Attributes
97
+ #
98
+ # * +tag_info+ - an array of hashes
99
+ # ex: [{ "application" => "app1", "component" => "database", "name" => "1.2.1", "artifact_url" => "file:///home/brady/stuff"}]
100
+ # * +options+ - a hash of options passed to the rest call
101
+ #
102
+ # ==== Returns
103
+ #
104
+ # * a hash of the command output
105
+ def create_version_tags(tag_info, options = {})
106
+ # Meant to be called after importing a spreadsheet of versions
107
+ results = {"status" => "ERROR", "message" => "", "data" => []}
108
+ message = "Processing tags: #{tag_info.size.to_s} to do\n"
109
+ version_tag = { "name" => "", "artifact_url" => "", "find_application" => "", "find_component" => "", "active" => true}
110
+ tag_info.each do |v_tag|
111
+ if v_tag.has_key?("name")
112
+ version_tag["find_application"] = v_tag["application"]
113
+ version_tag["find_component"] = v_tag["component"]
114
+ version_tag["name"] = v_tag["name"]
115
+ version_tag["artifact_url"] = v_tag["artifact_url"]
116
+ message += "adding #{v_tag["name"]} to #{v_tag["component"]}"
117
+ result = create("version_tags", {"version_tag" => version_tag}, options)
118
+ message += ", Status: #{result["status"]}\n"
119
+ results["data"] << result["data"]
120
+ results["status"] = result["status"]
121
+ else
122
+ message += "bad record: #{v_tag.inspect}\n"
123
+ end
124
+ end
125
+ results["message"] = message
126
+ results
127
+ end
128
+
129
+ # Queries RPM for a version by name
130
+ #
131
+ # ==== Attributes
132
+ #
133
+ # * +name+ - a version name
134
+ #
135
+ # ==== Returns
136
+ #
137
+ # * an array of matching version objects or "ERROR" if not found
138
+ #
139
+ def version_tag_query(name)
140
+ result = "ERROR"
141
+ result = get("version_tags",nil,{"filters" => "filters[name]=#{url_encode(name)}", "suppress_errors" => true})
142
+ if result["status"] == "success"
143
+ BrpmAuto.log "Tag Exists?: #{@base_url}\nResult: #{result["data"].inspect}"
144
+ result = result["data"]
145
+ else
146
+ BrpmAuto.log "No version tags found"
147
+ result = []
148
+ end
149
+ result
150
+ end
151
+
152
+ # Takes a version name and assigns it to the steps in a request
153
+ # === skips steps where the version does not exist
154
+ # ==== Attributes
155
+ #
156
+ # * +version+ - name of a version
157
+ # * +steps+ - an array of steps (returned from rest call to requests)
158
+ # * +options+ - hash of options passed to rest object e.g. {"verbose" => "yes"}
159
+ #
160
+ # ==== Returns
161
+ #
162
+ # * hash {"status" => success or ERROR, "rest_result" => [] array of rest responses
163
+ #
164
+ def assign_version_to_steps(version, steps, options = {})
165
+ result = {"status" => "ERROR - failed to update steps", "rest_result" => []}
166
+ components = steps.map{|l| l["component_name"]}.uniq
167
+ version_tags = version_tag_query(version)
168
+ return "ERROR no version tags for #{version}" if version_tags.is_a?(String) && version_tags.start_with?("ERROR")
169
+ components.reject{|l| l.nil? }.each do |component|
170
+ comp_steps = steps_with_matching_component(steps, component)
171
+ BrpmAuto.log "Comp: #{component}, steps: #{comp_steps.size == 0 ? "no steps" : comp_steps.map{|l| l["name"] }.join(",") }"
172
+ version_tag_id = "0"
173
+ version_tags.each{|k| version_tag_id = k["id"] if k["installed_component_id"] == comp_steps[0]["installed_component_id"] }
174
+ if version_tag_id == "0"
175
+ BrpmAuto.log "No version_tag for component: #{component}"
176
+ else
177
+ BrpmAuto.log "Tag exists for component"
178
+ comp_steps.each do |step|
179
+ step_data = {"version_tag_id" => version_tag_id, "component_version" => version}
180
+ rest_result = update("steps", step["id"], step_data, options)
181
+ if rest_result["status"] == "success"
182
+ BrpmAuto.log "Updating step: #{step["id"]}\nResult: #{rest_result["data"].inspect}"
183
+ result["status"] = "success"
184
+ end
185
+ result["rest_result"] << rest_result.inspect
186
+ end
187
+ end
188
+ end
189
+ result
190
+ end
191
+
192
+ # Takes an array of step objects and a component and returns the steps that match
193
+ #
194
+ # ==== Attributes
195
+ #
196
+ # * +steps+ - an array of steps (returned from rest call to requests)
197
+ # * +comp+ - a component name
198
+ #
199
+ # ==== Returns
200
+ #
201
+ # * array of the steps that match
202
+ def steps_with_matching_component(steps, comp)
203
+ result = []
204
+ steps.each do |step|
205
+ result << step if !step["installed_component_id"].nil? && comp == step["component_name"]
206
+ end
207
+ result
208
+ end
209
+
210
+ # Takes a request_id and monitors status until a condition is met
211
+ #
212
+ # ==== Attributes
213
+ #
214
+ # * +request_id+ - id of the calling request
215
+ # * +target_state+ - state to watch request or step for (default = complete)
216
+ # * +options+ - a hash of options, includes:
217
+ # +monitor_step_name+ - this monitor whether a specific step has reached the target state
218
+ # +max_time+ - maximum time in minutes to wait (default = 15)
219
+ # +interval+ - interval in seconds between checks (default = 15)
220
+ # +verbose+ - passed to rest call for verbose output (true/false)
221
+ #
222
+ # ==== Returns
223
+ #
224
+ # * array of the steps that match
225
+ def monitor_request(request_id, target_state = "complete", options = {})
226
+ states = ["created","planned","started","problem","hold","complete"]
227
+ max_time = get_option(options, "max_time", 15).to_i
228
+ max_time = max_time * 60 # seconds = 15 minutes
229
+ monitor_step_name = get_option(options, "monitor_step_name", "none")
230
+ seed_interval = get_option(options, "interval", 15) #seconds
231
+ verbose = get_option(options, "verbose")
232
+ verbose = "no" unless verbose == "yes"
233
+ raise "Command_Failed: bad request_id" if !(request_id.to_i > 0)
234
+ raise "Command_Failed: state not allowed, choose from [#{states.join(",")}]" if !states.include?(target_state)
235
+ BrpmAuto.message_box("Montoring Request: #{request_id}","sep")
236
+ req_status = "none"
237
+ start_time = Time.now
238
+ elapsed = 0
239
+ until (elapsed > max_time || req_status == target_state)
240
+ rest_result = get("requests", request_id, {"verbose" => verbose})
241
+ raise "Command_Failed: Request not found" if rest_result["status"] == "ERROR"
242
+ if monitor_step_name == "none"
243
+ req_status = rest_result["data"]["aasm_state"]
244
+ else
245
+ found = false
246
+ i_pos = rest_result["data"]["steps"].map{|l| l["name"]}.index(monitor_step_name)
247
+ raise "Command_Failed: Step name [#{monitor_step_name}] not found" if i_pos.nil?
248
+ req_status = rest_result["data"]["steps"][i_pos]["aasm_state"]
249
+ end
250
+ if req_status == target_state
251
+ break
252
+ else
253
+ BrpmAuto.log "\tWaiting(#{elapsed.floor.to_s}) - Current status: #{req_status}"
254
+ sleep(checking_interval)
255
+ elapsed = Time.now - start_time
256
+ end
257
+ end
258
+ if req_status == target_state
259
+ req_status = "Success test, looking for #{target_state}: Success!"
260
+ else
261
+ if elapsed > max_time
262
+ req_status = "Command_Failed: Max time: #{max_time}(secs) reached. Status is: #{req_status}, looking for: #{target_state}"
263
+ else
264
+ req_status = "REST call generated bad data, Status is: #{req_status}, looking for: #{target_state}"
265
+ end
266
+ end
267
+ req_status
268
+ end
269
+
270
+ def checking_interval(interval, total_time)
271
+ if (total_time/interval).to_i > 50
272
+ interval = interval * 30
273
+ elsif (total_time/interval).to_i > 20
274
+ interval = interval * 10
275
+ elsif (total_time/interval).to_i > 15
276
+ interval = interval * 6
277
+ elsif (total_time/interval).to_i > 10
278
+ interval = interval * 4
279
+ elsif (total_time/interval).to_i > 5
280
+ interval = interval * 2
281
+ end
282
+ interval
283
+ end
284
+
285
+
286
+ # Sends an email based on step recipients
287
+ #
288
+ # ==== Attributes
289
+ #
290
+ # * +subject+ - text of email subject
291
+ # * +body+ - text of email body
292
+ #
293
+ # ==== Returns
294
+ #
295
+ # * empty string
296
+ def notify(step_id, body, subject = "Mail from automation")
297
+ url = "#{@base_url}/v1/steps/#{step_id}/notify?token=#{@token}"
298
+ data = {"filters"=>{"notify"=>{"body"=> body, "subject"=> subject}}}
299
+ result = Rest.rest_call(url, "get", {"data" => data})
300
+ end
301
+
302
+ private
303
+
304
+ def rest_url(model_name, id = nil, filters = nil)
305
+ url = "#{@base_url}/v1/#{model_name}#{id == nil ? "" : "/#{id}" }"
306
+ url += "?#{filters}&token=#{@token}" if filters
307
+ url += "?token=#{@token}" unless filters
308
+ url
309
+ end
310
+
311
+ end
312
+