This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Add example errors connector and move simple example connector to a different namespace #437
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
8.7.0.0 | ||
8.6.0.0 |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# | ||
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
# or more contributor license agreements. Licensed under the Elastic License; | ||
# you may not use this file except in compliance with the Elastic License. | ||
# | ||
|
||
# frozen_string_literal: true | ||
|
||
require 'connectors/base/connector' | ||
require 'core/filtering/validation_status' | ||
require 'utility' | ||
|
||
module Connectors | ||
module Example | ||
module Simple | ||
class Connector < Connectors::Base::Connector | ||
def self.service_type | ||
'example' | ||
end | ||
|
||
def self.display_name | ||
'Example Connector' | ||
end | ||
|
||
# Field 'Foo' won't have a default value. Field 'Bar' will have the default value 'Value'. | ||
def self.configurable_fields | ||
{ | ||
'foo' => { | ||
'label' => 'Foo', | ||
'value' => nil | ||
}, | ||
:bar => { | ||
:label => 'Bar', | ||
:value => 'Value' | ||
} | ||
} | ||
end | ||
|
||
def initialize(configuration: {}, job_description: {}) | ||
super | ||
end | ||
|
||
def do_health_check | ||
# Do the health check by trying to access 3rd-party system just to verify that everything is set up properly. | ||
# | ||
# To emulate unhealthy 3rd-party system situation, uncomment the following line: | ||
# raise 'something went wrong' | ||
end | ||
|
||
def self.validate_filtering(filtering = {}) | ||
# TODO: real filtering validation will follow later | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've changed this logic to only be present in the base connector and only override Just as a heads up, when it's merged :-) |
||
errors = [ | ||
{ | ||
:ids => ['missing-implementation'], | ||
:messages => ['Filtering is not implemented yet for the example connector'] | ||
} | ||
] | ||
|
||
return { :state => Core::Filtering::ValidationStatus::INVALID, :errors => errors } if filtering.present? | ||
|
||
{ :state => Core::Filtering::ValidationStatus::VALID, :errors => [] } | ||
end | ||
|
||
def yield_documents | ||
attachments = [ | ||
load_attachment('first_attachment.txt'), | ||
load_attachment('second_attachment.txt'), | ||
load_attachment('third_attachment.txt'), | ||
] | ||
|
||
attachments.each_with_index do |att, index| | ||
data = { id: (index + 1).to_s, name: "example document #{index + 1}", _attachment: File.read(att) } | ||
|
||
# Uncomment one of these two lines to simulate longer running sync jobs | ||
# | ||
# sleep(rand(10..60).seconds) | ||
# sleep(rand(1..10).minutes) | ||
|
||
yield data | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_attachment(path) | ||
attachment_dir = "#{File.dirname(__FILE__)}/attachments" | ||
attachment_path = "#{attachment_dir}/#{path}" | ||
|
||
unless File.exist?(attachment_path) | ||
raise "Attachment at location '#{attachment_path}' doesn't exist. Attachments should be located under #{attachment_dir}" | ||
end | ||
|
||
File.open(attachment_path) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# | ||
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
# or more contributor license agreements. Licensed under the Elastic License; | ||
# you may not use this file except in compliance with the Elastic License. | ||
# | ||
|
||
# frozen_string_literal: true | ||
|
||
require 'connectors/base/connector' | ||
require 'core/filtering/validation_status' | ||
require 'utility' | ||
require 'faker' | ||
|
||
module Connectors | ||
module Example | ||
module WithErrors | ||
class Connector < Connectors::Base::Connector | ||
def self.service_type | ||
'example-with-errors' | ||
end | ||
|
||
def self.display_name | ||
'Example Connector that produces transient errors' | ||
end | ||
|
||
# Field 'Foo' won't have a default value. Field 'Bar' will have the default value 'Value'. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think that comment can be removed, right? |
||
def self.configurable_fields | ||
{ | ||
'chance_to_raise' => { | ||
'label' => 'Chance to raise an error when extracting a document (0..100)', | ||
'value' => 1 | ||
}, | ||
'generated_document_count' => { | ||
'label' => 'Number of documents to generate', | ||
'value' => 10_000 | ||
} | ||
} | ||
end | ||
|
||
def initialize(configuration: {}, job_description: {}) | ||
super | ||
|
||
@chance_to_raise = configuration.dig('chance_to_raise', 'value').to_i | ||
@generated_document_count = configuration.dig('generated_document_count', 'value').to_i | ||
|
||
raise 'Invalid chance to raise: should be between 0 and 100' if @chance_to_raise < 0 || @chance_to_raise > 100 | ||
|
||
Faker::Config.random = Random.new(1337) # we want to have a seed to consistently generate same text over and over | ||
end | ||
|
||
def do_health_check; end | ||
|
||
def self.validate_filtering(_filtering = {}) | ||
{ :state => Core::Filtering::ValidationStatus::VALID, :errors => [] } | ||
end | ||
|
||
def yield_documents | ||
@generated_document_count.times.map do |i| | ||
yield_with_handling_tolerable_errors do | ||
raise 'that is a random exception' if rand(1..100) > (100 - @chance_to_raise) | ||
|
||
document = { 'id' => i, 'name' => Faker::Name.name, 'text' => Faker::Lorem.sentence } | ||
|
||
yield document | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
spec/connectors/example/connector_spec.rb → ...nnectors/example/simple/connector_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👏 That'll be super helpful for the filtering example connector, too