|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Smithy |
| 4 | + module Client |
| 5 | + module Waiters |
| 6 | + # Abstract poller class which polls a single API operation and inspects |
| 7 | + # the output and/or error for states matching one of its acceptors. |
| 8 | + class Poller |
| 9 | + def initialize(options = {}) |
| 10 | + @operation_name = options[:operation_name] |
| 11 | + @acceptors = options[:acceptors] |
| 12 | + end |
| 13 | + |
| 14 | + def call(client, params) |
| 15 | + @input = params |
| 16 | + # TODO: make build_input public and update this line |
| 17 | + input = client.send(:build_input, @operation_name, params) |
| 18 | + input.handlers.remove(Plugins::RaiseResponseErrors::Handler) |
| 19 | + output = input.send_request |
| 20 | + status = evaluate_acceptors(output) |
| 21 | + [output, status.to_sym] |
| 22 | + end |
| 23 | + |
| 24 | + private |
| 25 | + |
| 26 | + def evaluate_acceptors(output) |
| 27 | + @acceptors.each do |acceptor| |
| 28 | + return acceptor['state'] if acceptor_matches?(acceptor['matcher'], output) |
| 29 | + end |
| 30 | + output.error.nil? ? 'retry' : 'error' |
| 31 | + end |
| 32 | + |
| 33 | + def acceptor_matches?(matcher, output) |
| 34 | + matcher_type = matcher.keys.first |
| 35 | + send("matches_#{matcher_type}?", matcher[matcher_type], output) |
| 36 | + end |
| 37 | + |
| 38 | + def matches_output?(path_matcher, output) |
| 39 | + return false if output.data.nil? |
| 40 | + |
| 41 | + actual = JMESPath.search(path_matcher['path'], output.data) |
| 42 | + equal?(actual, path_matcher['expected'], path_matcher['comparator']) |
| 43 | + end |
| 44 | + |
| 45 | + # rubocop:disable Naming/MethodName |
| 46 | + def matches_inputOutput?(path_matcher, output) |
| 47 | + return false unless !output.data.nil? && @input |
| 48 | + |
| 49 | + data = { |
| 50 | + input: @input, |
| 51 | + output: output.data |
| 52 | + } |
| 53 | + actual = JMESPath.search(path_matcher['path'], data) |
| 54 | + equal?(actual, path_matcher['expected'], path_matcher['comparator']) |
| 55 | + end |
| 56 | + |
| 57 | + def matches_success?(path_matcher, output) |
| 58 | + path_matcher == true ? !output.data.nil? : !output.error.nil? |
| 59 | + end |
| 60 | + |
| 61 | + def matches_errorType?(path_matcher, output) |
| 62 | + return false if output.error.nil? |
| 63 | + |
| 64 | + output.error.class.to_s.end_with?("Errors::#{path_matcher}") |
| 65 | + end |
| 66 | + |
| 67 | + def equal?(actual, expected, comparator) |
| 68 | + send("#{comparator}?", actual, expected) |
| 69 | + end |
| 70 | + |
| 71 | + def stringEquals?(actual, expected) |
| 72 | + actual == expected |
| 73 | + end |
| 74 | + |
| 75 | + def booleanEquals?(actual, expected) |
| 76 | + actual.to_s == expected |
| 77 | + end |
| 78 | + |
| 79 | + def allStringEquals?(actual, expected) |
| 80 | + return false if actual.nil? || actual.empty? |
| 81 | + |
| 82 | + actual.all? { |value| value == expected } |
| 83 | + end |
| 84 | + |
| 85 | + def anyStringEquals?(actual, expected) |
| 86 | + return false if actual.nil? || actual.empty? |
| 87 | + |
| 88 | + actual.any? { |value| value == expected } |
| 89 | + end |
| 90 | + # rubocop:enable Naming/MethodName |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | +end |
0 commit comments