Method: Cisco::Client::GRPC#req

Defined in:
lib/cisco_node_utils/client/grpc/client.rb

#req(stub, type, args) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cisco_node_utils/client/grpc/client.rb', line 149

def req(stub, type, args)
  if cache_enable? && @cache_hash[type] && @cache_hash[type][args.cli]
    return @cache_hash[type][args.cli]
  end

  debug "Sending '#{type}' request:"
  if args.is_a?(ShowCmdArgs) || args.is_a?(CliConfigArgs)
    debug "  with cli: '#{args.cli}'"
  end
  output = Cisco::Client.silence_warnings do
    response = stub.send(type, args,
                         timeout:  @timeout,
                         username: @username,
                         password: @password)
    # gRPC server may split the response into multiples
    response = response.is_a?(Enumerator) ? response.to_a : [response]
    debug "Got responses: #{response.map(&:class).join(', ')}"
    # Check for errors first
    handle_errors(args, response.select { |r| !r.errors.empty? })

    # If we got here, no errors occurred
    handle_response(args, response)
  end

  @cache_hash[type][args.cli] = output if cache_enable? && !output.empty?
  return output
rescue ::GRPC::BadStatus => e
  warn "gRPC error '#{e.code}' during '#{type}' request: "
  if args.is_a?(ShowCmdArgs) || args.is_a?(CliConfigArgs)
    warn "  with cli: '#{args.cli}'"
  end
  warn "  '#{e.details}'"
  case e.code
  when ::GRPC::Core::StatusCodes::UNAVAILABLE
    raise Cisco::ConnectionRefused, "Connection refused: #{e.details}"
  when ::GRPC::Core::StatusCodes::UNAUTHENTICATED
    raise Cisco::AuthenticationFailed, e.details
  else
    raise Cisco::ClientError, e.details
  end
end