File tree Expand file tree Collapse file tree
elasticgraph-admin_lambda
lib/elastic_graph/admin_lambda
sig/elastic_graph/admin_lambda
integration/elastic_graph/admin_lambda
unit/elastic_graph/admin_lambda Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,7 +20,10 @@ def initialize
2020
2121 def handle_request ( event :, context :)
2222 # @type var event: ::Hash[::String, untyped]
23- rake_output = RakeAdapter . run_rake ( event . fetch ( "argv" ) )
23+ rake_output = RakeAdapter . run_rake (
24+ event . fetch ( "argv" ) ,
25+ hide_delete_changes : event . fetch ( "hide_delete_changes" , false )
26+ )
2427
2528 # Log the output of the rake task. We also want to return it so that when we invoke
2629 # a lambda rake task from the terminal we can print the output there.
Original file line number Diff line number Diff line change @@ -14,17 +14,24 @@ module AdminLambda
1414 # @private
1515 class RakeAdapter
1616 RAKEFILE = File . expand_path ( "../Rakefile" , __FILE__ )
17+ DELETE_CHANGE_PATTERN = /\A - [^:\n ]+: /
1718
18- def self . run_rake ( argv )
19- capture_output do
19+ def self . run_rake ( argv , hide_delete_changes : false )
20+ rake_output = capture_output do
2021 # We need to instantiate a new application on each invocation, because Rake is normally
2122 # designed to run once and exit. It keeps track of tasks that have already run and will
2223 # no-op when you try to run a task a 2nd or 3rd time. Using a new application instance
2324 # each time avoids this issue.
2425 ::Rake . with_application ( Application . new ) do |application |
2526 application . run ( argv )
2627 end
27- end
28+ end || ""
29+
30+ hide_delete_changes ? filter_delete_changes ( rake_output ) : rake_output
31+ end
32+
33+ def self . filter_delete_changes ( rake_output )
34+ rake_output . lines . reject { |line | line . match? ( DELETE_CHANGE_PATTERN ) } . join
2835 end
2936
3037 # Captures stdout/stderr into a string so we can return it from the lambda.
Original file line number Diff line number Diff line change @@ -2,8 +2,10 @@ module ElasticGraph
22 module AdminLambda
33 class RakeAdapter
44 RAKEFILE: ::String
5+ DELETE_CHANGE_PATTERN: ::Regexp
56
6- def self.run_rake : (::Array[::String]) -> ::String?
7+ def self.run_rake : (::Array[::String], ?hide_delete_changes: bool ) -> ::String
8+ def self.filter_delete_changes : (::String) -> ::String
79 def self.capture_output : () { () -> void } -> ::String?
810
911 class Application < ::Rake::Application
Original file line number Diff line number Diff line change @@ -42,6 +42,23 @@ module AdminLambda
4242 end
4343 end
4444
45+ it "can filter delete changes from the rake output" do
46+ allow ( RakeAdapter ) . to receive ( :capture_output ) . and_return ( <<~OUTPUT )
47+ Updated mappings for index `widgets`:
48+ - properties.name: {"type"=>"keyword"}
49+ + properties.description: {"type"=>"text"}
50+ ~ settings.index.refresh_interval: `"1s"` => `"5s"`
51+ - this is a regular line, not a diff
52+ OUTPUT
53+
54+ expect ( RakeAdapter . run_rake ( [ "clusters:configure:dry_run" ] , hide_delete_changes : true ) ) . to eq ( <<~OUTPUT )
55+ Updated mappings for index `widgets`:
56+ + properties.description: {"type"=>"text"}
57+ ~ settings.index.refresh_interval: `"1s"` => `"5s"`
58+ - this is a regular line, not a diff
59+ OUTPUT
60+ end
61+
4562 def rake_tasks_printed_to ( string )
4663 string
4764 . split ( "\n " )
Original file line number Diff line number Diff line change 2222 } . to output ( a_string_including ( "rake clusters:configure:dry_run" ) ) . to_stdout_from_any_process
2323 end
2424 end
25+
26+ it "can hide delete changes from the rake output" do
27+ expect_loading_lambda_to_define_constant (
28+ lambda : "elastic_graph/admin_lambda/lambda_function.rb" ,
29+ const : :HandleAdminRequest
30+ ) do |lambda_function |
31+ allow ( ElasticGraph ::AdminLambda ::RakeAdapter ) . to receive ( :run_rake )
32+ . with ( [ "clusters:configure:dry_run" ] , hide_delete_changes : true )
33+ . and_return ( "+ properties.description: {\" type\" =>\" text\" }\n " )
34+
35+ expect {
36+ response = lambda_function . handle_request (
37+ event : { "argv" => [ "clusters:configure:dry_run" ] , "hide_delete_changes" => true } ,
38+ context : { }
39+ )
40+ expect ( response [ "rake_output" ] ) . to eq ( "+ properties.description: {\" type\" =>\" text\" }\n " )
41+ } . to output ( a_string_including ( "+ properties.description: {\" type\" =>\" text\" }\n " ) ) . to_stdout_from_any_process
42+ end
43+ end
2544end
You can’t perform that action at this time.
0 commit comments