vayacondios-server 0.2.11 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -1
- data/.travis.yml +2 -0
- data/Gemfile +15 -9
- data/LICENSE.md +2 -6
- data/Procfile +1 -1
- data/README.md +656 -111
- data/Rakefile +89 -6
- data/bin/vcd +10 -0
- data/bin/vcd-server +8 -0
- data/config/database.yml +6 -0
- data/config/spec.example.yml +18 -0
- data/config/vayacondios.example.yml +15 -0
- data/config/vcd-server.rb +37 -0
- data/examples/configuration.rb +56 -0
- data/examples/event_stream.rb +19 -0
- data/examples/simple.rb +61 -0
- data/features/event.feature +319 -0
- data/features/events.feature +208 -0
- data/features/stash.feature +840 -0
- data/features/stashes.feature +492 -0
- data/features/step_definitions/stash_steps.rb +113 -0
- data/features/stream.feature +30 -0
- data/features/support/em.rb +14 -0
- data/features/support/env.rb +13 -0
- data/lib/vayacondios/configuration.rb +63 -0
- data/lib/vayacondios/server/api.rb +126 -0
- data/lib/vayacondios/server/api_options.rb +56 -0
- data/lib/vayacondios/server/configuration.rb +23 -0
- data/lib/vayacondios/server/driver.rb +71 -0
- data/lib/vayacondios/server/drivers/mongo.rb +126 -0
- data/lib/vayacondios/server/handlers/document_handler.rb +81 -0
- data/lib/vayacondios/server/handlers/event_handler.rb +31 -26
- data/lib/vayacondios/server/handlers/events_handler.rb +31 -0
- data/lib/vayacondios/server/handlers/stash_handler.rb +69 -0
- data/lib/vayacondios/server/handlers/stashes_handler.rb +49 -0
- data/lib/vayacondios/server/handlers/stream_handler.rb +39 -0
- data/lib/vayacondios/server/models/document.rb +87 -0
- data/lib/vayacondios/server/models/event.rb +198 -0
- data/lib/vayacondios/server/models/stash.rb +100 -0
- data/lib/vayacondios/server.rb +35 -0
- data/lib/vayacondios-server.rb +19 -13
- data/lib/vayacondios.rb +22 -0
- data/pom.xml +124 -4
- data/spec/configuration_spec.rb +41 -0
- data/spec/server/api_options_spec.rb +32 -0
- data/spec/server/api_spec.rb +279 -0
- data/spec/server/configuration_spec.rb +27 -0
- data/spec/server/drivers/mongo_spec.rb +107 -0
- data/spec/server/handlers/event_handler_spec.rb +62 -0
- data/spec/server/handlers/events_handler_spec.rb +51 -0
- data/spec/server/handlers/stash_handler_spec.rb +68 -0
- data/spec/server/handlers/stashes_handler_spec.rb +50 -0
- data/spec/server/handlers/stream_handler_spec.rb +5 -0
- data/spec/server/models/document_spec.rb +9 -0
- data/spec/server/models/event_spec.rb +185 -0
- data/spec/server/models/stash_spec.rb +95 -0
- data/spec/spec_helper.rb +23 -3
- data/spec/support/database_helper.rb +42 -0
- data/spec/support/log_helper.rb +19 -0
- data/spec/support/shared_context_for_events.rb +22 -0
- data/spec/support/shared_context_for_stashes.rb +24 -0
- data/spec/support/shared_examples_for_handlers.rb +32 -0
- data/src/main/java/com/infochimps/vayacondios/BaseClient.java +342 -0
- data/src/main/java/com/infochimps/vayacondios/HTTPClient.java +426 -0
- data/src/main/java/com/infochimps/vayacondios/VayacondiosClient.java +487 -65
- data/src/main/java/com/infochimps/vayacondios/test/IntegrationTest.java +3 -0
- data/src/test/java/com/infochimps/vayacondios/BaseClientTest.java +50 -0
- data/src/test/java/com/infochimps/vayacondios/HTTPClientIT.java +267 -0
- data/vayacondios-server.gemspec +9 -9
- metadata +127 -122
- checksums.yaml +0 -15
- data/.rspec +0 -2
- data/.yardopts +0 -10
- data/Guardfile +0 -41
- data/app/http_shim.rb +0 -71
- data/bin/vcd.sh +0 -27
- data/config/http_shim.rb +0 -43
- data/config/vayacondios.example.yaml +0 -7
- data/config/vayacondios.yaml +0 -7
- data/examples/java/ItemSetTest.java +0 -76
- data/lib/tasks/publish.rake +0 -23
- data/lib/tasks/spec.rake +0 -11
- data/lib/tasks/yard.rake +0 -2
- data/lib/vayacondios/client/config.rb +0 -7
- data/lib/vayacondios/client/configliere.rb +0 -38
- data/lib/vayacondios/client/cube_client.rb +0 -39
- data/lib/vayacondios/client/http_client.rb +0 -49
- data/lib/vayacondios/client/itemset.rb +0 -130
- data/lib/vayacondios/client/legacy_switch.rb +0 -43
- data/lib/vayacondios/client/notifier.rb +0 -123
- data/lib/vayacondios/client/zabbix_client.rb +0 -148
- data/lib/vayacondios/legacy_switch.rb +0 -43
- data/lib/vayacondios/server/errors/bad_request.rb +0 -6
- data/lib/vayacondios/server/errors/not_found.rb +0 -6
- data/lib/vayacondios/server/handlers/config_handler.rb +0 -32
- data/lib/vayacondios/server/handlers/itemset_handler.rb +0 -60
- data/lib/vayacondios/server/legacy_switch.rb +0 -43
- data/lib/vayacondios/server/model/config_document.rb +0 -89
- data/lib/vayacondios/server/model/document.rb +0 -25
- data/lib/vayacondios/server/model/event_document.rb +0 -94
- data/lib/vayacondios/server/model/itemset_document.rb +0 -126
- data/lib/vayacondios/server/rack/extract_methods.rb +0 -35
- data/lib/vayacondios/server/rack/jsonize.rb +0 -43
- data/lib/vayacondios/server/rack/params.rb +0 -50
- data/lib/vayacondios/server/rack/path.rb +0 -23
- data/lib/vayacondios/server/rack/path_validation.rb +0 -22
- data/lib/vayacondios/version.rb +0 -3
- data/lib/vayacondios-client.rb +0 -22
- data/scripts/hadoop_monitor/configurable.rb +0 -66
- data/scripts/hadoop_monitor/hadoop_attempt_scraper.rb +0 -45
- data/scripts/hadoop_monitor/hadoop_client.rb +0 -273
- data/scripts/hadoop_monitor/hadoop_monitor.rb +0 -101
- data/scripts/hadoop_monitor/hadoopable.rb +0 -65
- data/scripts/hadoop_monitor/machine_monitor.rb +0 -115
- data/scripts/s3_cataloger/buckets +0 -33
- data/scripts/s3_cataloger/foreach_bucket +0 -88
- data/scripts/s3_cataloger/parse_ls.py +0 -391
- data/spec/client/itemset_legacy_spec.rb +0 -55
- data/spec/client/itemset_spec.rb +0 -60
- data/spec/client/notifier_spec.rb +0 -120
- data/spec/server/config_spec.rb +0 -113
- data/spec/server/event_spec.rb +0 -103
- data/spec/server/itemset_legacy_spec.rb +0 -320
- data/spec/server/itemset_spec.rb +0 -317
- data/spec/server/rack/extract_methods_spec.rb +0 -60
- data/spec/server/rack/path_spec.rb +0 -36
- data/spec/server/rack/path_validation_spec.rb +0 -22
- data/spec/server/server_spec.rb +0 -20
- data/spec/support/mongo_cleaner.rb +0 -32
- data/src/main/java/ItemSetTest.java +0 -76
- data/src/main/java/com/infochimps/util/CurrentClass.java +0 -26
- data/src/main/java/com/infochimps/util/DebugUtil.java +0 -38
- data/src/main/java/com/infochimps/util/HttpHelper.java +0 -181
- data/src/main/java/com/infochimps/vayacondios/ItemSets.java +0 -373
- data/src/main/java/com/infochimps/vayacondios/LinkToVCD.java +0 -18
- data/src/main/java/com/infochimps/vayacondios/MemoryVCDShim.java +0 -84
- data/src/main/java/com/infochimps/vayacondios/Organization.java +0 -62
- data/src/main/java/com/infochimps/vayacondios/PathBuilder.java +0 -13
- data/src/main/java/com/infochimps/vayacondios/StandardVCDLink.java +0 -218
- data/src/main/java/com/infochimps/vayacondios/VCDIntegrationTest.java +0 -108
- data/src/test/java/com/infochimps/vayacondios/TestVayacondiosInMemory.java +0 -78
- data/vayacondios-client.gemspec +0 -25
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'gorillib/logger/log'
|
2
|
-
|
3
|
-
class Vayacondios
|
4
|
-
# Are we operating on JSON Hashes (Vayacondios) or on JSON arrays
|
5
|
-
# (Vayacondios Legacy)? These classes determine which to use.
|
6
|
-
|
7
|
-
class StandardContentsHandler
|
8
|
-
def wrap_contents(contents) {contents: contents} end
|
9
|
-
def extract_contents(document) document['contents'] end
|
10
|
-
def extract_response(document) document['contents'] end
|
11
|
-
def proper_request(document) true end
|
12
|
-
end
|
13
|
-
|
14
|
-
class LegacyContentsHandler
|
15
|
-
def wrap_contents(contents) contents end
|
16
|
-
def extract_contents(document) document end
|
17
|
-
def extract_response(document) document end
|
18
|
-
def proper_request(document) true end
|
19
|
-
end
|
20
|
-
|
21
|
-
@@legacy_switch = nil
|
22
|
-
|
23
|
-
def self.legacy_switch
|
24
|
-
if @@legacy_switch.nil?
|
25
|
-
legacy_mode_on = Settings[:vayacondios][:legacy]
|
26
|
-
@@legacy_switch = get_legacy_switch(legacy_mode_on)
|
27
|
-
Log.info("using #{legacy_mode_on ? 'legacy' : 'standard'} mode")
|
28
|
-
end
|
29
|
-
@@legacy_switch
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.force_legacy_mode on
|
33
|
-
Log.info("forcing #{on ? 'legacy' : 'standard'} mode")
|
34
|
-
@@legacy_switch = get_legacy_switch on
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def self.get_legacy_switch on
|
40
|
-
(on ? LegacyContentsHandler : StandardContentsHandler).new
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# Vayacondios::ConfigHandler
|
2
|
-
#
|
3
|
-
# This handler will accept requests to update config for an organization. All
|
4
|
-
# updates will merge with an existing document.
|
5
|
-
|
6
|
-
class Vayacondios
|
7
|
-
|
8
|
-
class ConfigHandler
|
9
|
-
|
10
|
-
def initialize(mongodb)
|
11
|
-
@mongo = mongodb
|
12
|
-
end
|
13
|
-
|
14
|
-
def update(document, options={})
|
15
|
-
raise Vayacondios::Error::BadRequest.new unless options[:topic] && options[:id]
|
16
|
-
raise Vayacondios::Error::BadRequest.new if /\W/ =~ options[:id]
|
17
|
-
|
18
|
-
existing_document = ConfigDocument.find(@mongo, options)
|
19
|
-
if existing_document
|
20
|
-
existing_document.update(document)
|
21
|
-
else
|
22
|
-
existing_document = ConfigDocument.create(@mongo, document, options)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.find(mongodb, options)
|
27
|
-
existing_document = ConfigDocument.find(mongodb, options)
|
28
|
-
raise Vayacondios::Error::NotFound.new unless existing_document
|
29
|
-
existing_document
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
# Vayacondios::ItemsetHandler
|
2
|
-
#
|
3
|
-
# This handler will accept requests to handle arrays for an organization.
|
4
|
-
# These arrays can only contain numbers and strings.
|
5
|
-
# GET requests are idempotent
|
6
|
-
# POST requests are forbidden
|
7
|
-
# PUT requests will clobber an existing array
|
8
|
-
# DELETE requests require an array of objects to remove
|
9
|
-
|
10
|
-
class Vayacondios
|
11
|
-
|
12
|
-
class ItemsetHandler
|
13
|
-
|
14
|
-
def initialize(mongodb)
|
15
|
-
@mongo = mongodb
|
16
|
-
end
|
17
|
-
|
18
|
-
def update(document, options={})
|
19
|
-
validate_options options
|
20
|
-
|
21
|
-
existing_document = ItemsetDocument.find(@mongo, options)
|
22
|
-
if existing_document
|
23
|
-
existing_document.update(document)
|
24
|
-
else
|
25
|
-
existing_document = ItemsetDocument.create(@mongo, document, options)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def patch(document, options={})
|
30
|
-
validate_options options
|
31
|
-
|
32
|
-
existing_document = ItemsetDocument.find(@mongo, options)
|
33
|
-
if existing_document
|
34
|
-
existing_document.patch(document)
|
35
|
-
else
|
36
|
-
existing_document = ItemsetDocument.create(@mongo, document, options)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def destroy(document, options={})
|
41
|
-
validate_options options
|
42
|
-
|
43
|
-
existing_document = ItemsetDocument.find(@mongo, options)
|
44
|
-
existing_document.destroy(document)
|
45
|
-
end
|
46
|
-
|
47
|
-
def self.find(mongodb, options)
|
48
|
-
existing_document = ItemsetDocument.find(mongodb, options)
|
49
|
-
raise Error::NotFound.new unless existing_document
|
50
|
-
existing_document
|
51
|
-
end
|
52
|
-
|
53
|
-
protected
|
54
|
-
|
55
|
-
def validate_options(options)
|
56
|
-
raise Error::BadRequest.new unless options[:topic] && options[:id]
|
57
|
-
raise Error::BadRequest.new if /\W/ =~ options[:id]
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'gorillib/logger/log'
|
2
|
-
|
3
|
-
class Vayacondios
|
4
|
-
# Are we operating on JSON Hashes (Vayacondios) or on JSON arrays
|
5
|
-
# (Vayacondios Legacy)? These classes determine which to use.
|
6
|
-
|
7
|
-
class StandardContentsHandler
|
8
|
-
def wrap_contents(contents) {contents: contents} end
|
9
|
-
def extract_contents(document) document['contents'] end
|
10
|
-
def extract_response(document) document['contents'] end
|
11
|
-
def proper_request(document) true end
|
12
|
-
end
|
13
|
-
|
14
|
-
class LegacyContentsHandler
|
15
|
-
def wrap_contents(contents) contents end
|
16
|
-
def extract_contents(document) document end
|
17
|
-
def extract_response(document) document end
|
18
|
-
def proper_request(document) true end
|
19
|
-
end
|
20
|
-
|
21
|
-
@@legacy_switch = nil
|
22
|
-
|
23
|
-
def self.legacy_switch
|
24
|
-
if @@legacy_switch.nil?
|
25
|
-
legacy_mode_on = Settings[:vayacondios][:legacy]
|
26
|
-
@@legacy_switch = get_legacy_switch(legacy_mode_on)
|
27
|
-
Log.info("using #{legacy_mode_on ? 'legacy' : 'standard'} mode")
|
28
|
-
end
|
29
|
-
@@legacy_switch
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.force_legacy_mode on
|
33
|
-
Log.info("forcing #{on ? 'legacy' : 'standard'} mode")
|
34
|
-
@@legacy_switch = get_legacy_switch on
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def self.get_legacy_switch on
|
40
|
-
(on ? LegacyContentsHandler : StandardContentsHandler).new
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
@@ -1,89 +0,0 @@
|
|
1
|
-
require 'vayacondios/server/model/document'
|
2
|
-
|
3
|
-
# The configuration model
|
4
|
-
#
|
5
|
-
# Configuration documents are key-value pairs, represented in JSON. A document
|
6
|
-
# consists of a primary key called the topic (_id in mongodb). It belongs to a
|
7
|
-
# collection named "#{organization_name}.config"
|
8
|
-
#
|
9
|
-
# Note: mongodb is passed in beacuse Goliath makes Thread lookups will not
|
10
|
-
# work while Goliath is in a streaming context.
|
11
|
-
|
12
|
-
class Vayacondios::ConfigDocument < Vayacondios::Document
|
13
|
-
attr_reader :organization, :topic, :body, :id
|
14
|
-
|
15
|
-
def initialize(mongodb, options = {})
|
16
|
-
super options
|
17
|
-
@mongo = mongodb
|
18
|
-
options = sanitize_options(options)
|
19
|
-
@id = options[:id]
|
20
|
-
|
21
|
-
@body = nil
|
22
|
-
@field = options[:field] ||= options[:id]
|
23
|
-
@mongo = mongodb
|
24
|
-
|
25
|
-
collection_name = [organization.to_s, 'config'].join('.')
|
26
|
-
@collection = @mongo.collection(collection_name)
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.create(mongodb, document, options={})
|
30
|
-
self.new(mongodb, options).update(document)
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.find(mongodb, options={})
|
34
|
-
self.new(mongodb, options).find
|
35
|
-
end
|
36
|
-
|
37
|
-
def find
|
38
|
-
result = @collection.find_one({_id: @topic})
|
39
|
-
|
40
|
-
if result.present?
|
41
|
-
result.delete("_id")
|
42
|
-
@body = result
|
43
|
-
# @body = @field.split('.').inject(result){|acc, attr| acc = acc[attr]} if @field.present?
|
44
|
-
self
|
45
|
-
else
|
46
|
-
nil
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def update(document)
|
51
|
-
raise Vayacondios::Error::BadRequest.new if !document.is_a?(Hash)
|
52
|
-
|
53
|
-
# Merge ourselves
|
54
|
-
document = body.deep_merge(document) if body
|
55
|
-
|
56
|
-
fields = document
|
57
|
-
# fields = {@field => document} if @field.present?
|
58
|
-
|
59
|
-
@body = document
|
60
|
-
@collection.update({:_id => @topic}, {'$set' => fields}, {upsert: true})
|
61
|
-
|
62
|
-
self
|
63
|
-
end
|
64
|
-
|
65
|
-
def destroy(document)
|
66
|
-
super
|
67
|
-
end
|
68
|
-
|
69
|
-
protected
|
70
|
-
|
71
|
-
def sanitize_options(options)
|
72
|
-
options = options.symbolize_keys
|
73
|
-
|
74
|
-
topic = options[:topic]
|
75
|
-
|
76
|
-
if (topic.is_a?(Hash) && topic["$oid"].present?)
|
77
|
-
topic = BSON::ObjectId(topic["$oid"])
|
78
|
-
elsif topic.is_a?(String)
|
79
|
-
topic = topic.gsub(/\W/,'')
|
80
|
-
if topic.to_s.match(/^[a-f0-9]{24}$/)
|
81
|
-
topic = BSON::ObjectId(topic)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
field = options[:field].gsub(/\W/, '') if options[:field].present?
|
86
|
-
|
87
|
-
options.merge(topic: topic, field: field)
|
88
|
-
end
|
89
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
class Vayacondios::Document < Hash
|
2
|
-
attr_accessor :organization, :topic
|
3
|
-
|
4
|
-
def initialize(options = {})
|
5
|
-
options = options.symbolize_keys
|
6
|
-
@organization = options[:organization]
|
7
|
-
@topic = options[:topic]
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.create
|
11
|
-
raise NotImplementedError.new("#{self.name} must be overriden by a subclass.")
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.update
|
15
|
-
raise NotImplementedError.new("#{self.name} must be overriden by a subclass.")
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.find
|
19
|
-
raise NotImplementedError.new("#{self.name} must be overriden by a subclass.")
|
20
|
-
end
|
21
|
-
|
22
|
-
def destroy
|
23
|
-
raise NotImplementedError.new("#{self.name} must be overriden by a subclass.")
|
24
|
-
end
|
25
|
-
end
|
@@ -1,94 +0,0 @@
|
|
1
|
-
require 'vayacondios/server/model/document'
|
2
|
-
|
3
|
-
# The event model
|
4
|
-
#
|
5
|
-
# Event documents are key-value pairs, represented in JSON. A document
|
6
|
-
# consists of a primary key called the topic (_id in mongodb). It belongs to a
|
7
|
-
# collection named "#{organization_name}.#{topic}.events"
|
8
|
-
#
|
9
|
-
# Note: mongodb is passed in beacuse Goliath makes Thread lookups will not
|
10
|
-
# work while Goliath is in a streaming context.
|
11
|
-
|
12
|
-
class Vayacondios::EventDocument < Vayacondios::Document
|
13
|
-
attr_reader :organization, :topic, :body
|
14
|
-
|
15
|
-
def initialize(mongodb, options = {})
|
16
|
-
super options
|
17
|
-
@mongo = mongodb
|
18
|
-
options = sanitize_options(options)
|
19
|
-
|
20
|
-
@body = nil
|
21
|
-
@id = format_id(options[:id])
|
22
|
-
@mongo = mongodb
|
23
|
-
|
24
|
-
collection_name = [organization, topic, 'events'].join('.')
|
25
|
-
@collection = @mongo.collection(collection_name)
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.create(mongodb, document, options={})
|
29
|
-
self.new(mongodb, options).update(document)
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.find(mongodb, options={})
|
33
|
-
self.new(mongodb, options).find
|
34
|
-
end
|
35
|
-
|
36
|
-
def find
|
37
|
-
result = @collection.find_one({_id: @id})
|
38
|
-
if result.present?
|
39
|
-
result.delete("_id")
|
40
|
-
result['_timestamp'] = result.delete("t")
|
41
|
-
result.merge! result.delete("d") if result["d"].present?
|
42
|
-
@body = result
|
43
|
-
self
|
44
|
-
else
|
45
|
-
nil
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def update(document)
|
50
|
-
document = to_mongo(document)
|
51
|
-
|
52
|
-
@body = document[:d]
|
53
|
-
if @id
|
54
|
-
@collection.update({:_id => @id}, document, {upsert: true})
|
55
|
-
else
|
56
|
-
@collection.insert(document)
|
57
|
-
end
|
58
|
-
|
59
|
-
self
|
60
|
-
end
|
61
|
-
|
62
|
-
def destroy(document)
|
63
|
-
super
|
64
|
-
end
|
65
|
-
|
66
|
-
protected
|
67
|
-
|
68
|
-
def sanitize_options(options)
|
69
|
-
options = options.symbolize_keys
|
70
|
-
|
71
|
-
topic = options[:topic].gsub(/\W+/, '_')
|
72
|
-
id = format_id options[:id]
|
73
|
-
|
74
|
-
options.merge!(topic: topic, id: id)
|
75
|
-
end
|
76
|
-
|
77
|
-
def format_id(id)
|
78
|
-
if (id.is_a?(Hash) && id["$oid"].present?)
|
79
|
-
id = BSON::ObjectId(id["$oid"])
|
80
|
-
else
|
81
|
-
id = id.to_s.gsub(/\W/,'')
|
82
|
-
id = BSON::ObjectId(id) if id.match(/^[a-f0-9]{24}$/)
|
83
|
-
end
|
84
|
-
id
|
85
|
-
end
|
86
|
-
|
87
|
-
def to_mongo(document)
|
88
|
-
{}.tap do |result|
|
89
|
-
result[:d] = document.dup
|
90
|
-
result[:_id] = @id if @id
|
91
|
-
result[:t] = document.delete(:_timestamp) || Time.now
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
@@ -1,126 +0,0 @@
|
|
1
|
-
require 'vayacondios/server/model/document'
|
2
|
-
require 'vayacondios/server/legacy_switch'
|
3
|
-
|
4
|
-
# The configuration model
|
5
|
-
#
|
6
|
-
# Configuration documents are key-value pairs, represented in JSON. A document
|
7
|
-
# consists of a primary key called the topic (_id in mongodb). It belongs to a
|
8
|
-
# collection named "#{organization_name}.config"
|
9
|
-
#
|
10
|
-
# Note: mongodb is passed in beacuse Goliath makes Thread lookups will not
|
11
|
-
# work while Goliath is in a streaming context.
|
12
|
-
|
13
|
-
class Vayacondios::ItemsetDocument < Vayacondios::Document
|
14
|
-
attr_reader :organization, :topic, :id
|
15
|
-
|
16
|
-
def initialize(mongodb, options = {})
|
17
|
-
super options
|
18
|
-
@mongo = mongodb
|
19
|
-
options = sanitize_options(options)
|
20
|
-
@id = options[:id]
|
21
|
-
|
22
|
-
@body = nil
|
23
|
-
@mongo = mongodb
|
24
|
-
|
25
|
-
@handler = Vayacondios.legacy_switch
|
26
|
-
|
27
|
-
collection_name = [organization.to_s, topic, 'itemset'].join('.')
|
28
|
-
@collection = @mongo.collection(collection_name)
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.create(mongodb, document, options={})
|
32
|
-
self.new(mongodb, options).update(document)
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.find(mongodb, options={})
|
36
|
-
self.new(mongodb, options).find
|
37
|
-
end
|
38
|
-
|
39
|
-
def body
|
40
|
-
@handler.wrap_contents(@body)
|
41
|
-
end
|
42
|
-
|
43
|
-
def find
|
44
|
-
result = @collection.find_one({_id: @id})
|
45
|
-
|
46
|
-
if result
|
47
|
-
result.delete("_id")
|
48
|
-
@body = result["d"]
|
49
|
-
self
|
50
|
-
else
|
51
|
-
nil
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def update(document)
|
56
|
-
raise Vayacondios::Error::BadRequest.new unless @handler.proper_request(document)
|
57
|
-
|
58
|
-
@body = extract_contents(document)
|
59
|
-
|
60
|
-
|
61
|
-
@collection.update({:_id => @id}, {:_id => @id, 'd' => @body }, {upsert: true})
|
62
|
-
|
63
|
-
self
|
64
|
-
end
|
65
|
-
|
66
|
-
def patch(document)
|
67
|
-
raise Vayacondios::Error::BadRequest.new unless @handler.proper_request(document)
|
68
|
-
|
69
|
-
# Merge ourselves
|
70
|
-
if @body
|
71
|
-
@body = @body + extract_contents(document)
|
72
|
-
else
|
73
|
-
@body = extract_contents(document)
|
74
|
-
end
|
75
|
-
|
76
|
-
@collection.update({:_id => @id}, {
|
77
|
-
'$addToSet' => {
|
78
|
-
'd' => {
|
79
|
-
'$each'=> extract_contents(document)
|
80
|
-
}
|
81
|
-
}
|
82
|
-
}, {upsert: true})
|
83
|
-
|
84
|
-
self
|
85
|
-
end
|
86
|
-
|
87
|
-
def destroy(document)
|
88
|
-
raise Vayacondios::Error::BadRequest.new unless @handler.proper_request(document)
|
89
|
-
|
90
|
-
@body -= extract_contents(document)
|
91
|
-
|
92
|
-
@collection.update({:_id => @id}, {
|
93
|
-
'$pullAll' => {
|
94
|
-
'd' => extract_contents(document)
|
95
|
-
}
|
96
|
-
})
|
97
|
-
|
98
|
-
self
|
99
|
-
end
|
100
|
-
|
101
|
-
protected
|
102
|
-
|
103
|
-
def extract_contents(document)
|
104
|
-
puts "doc = #{document}. contents = #{result = @handler.extract_contents(document)}"
|
105
|
-
result
|
106
|
-
end
|
107
|
-
|
108
|
-
def sanitize_options(options)
|
109
|
-
options = options.symbolize_keys
|
110
|
-
|
111
|
-
topic = options[:topic]
|
112
|
-
|
113
|
-
if (topic.is_a?(Hash) && topic["$oid"].present?)
|
114
|
-
topic = BSON::ObjectId(topic["$oid"])
|
115
|
-
elsif topic.is_a?(String)
|
116
|
-
topic = topic.gsub(/\W/,'')
|
117
|
-
if topic.to_s.match(/^[a-f0-9]{24}$/)
|
118
|
-
topic = BSON::ObjectId(topic)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
field = options[:field].gsub(/\W/, '') if options[:field].present?
|
123
|
-
|
124
|
-
options.merge(topic: topic, field: field)
|
125
|
-
end
|
126
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
class Vayacondios
|
2
|
-
module Rack
|
3
|
-
class ExtractMethods
|
4
|
-
include Goliath::Rack::AsyncMiddleware
|
5
|
-
|
6
|
-
def call(env)
|
7
|
-
method_name = extract_method(env)
|
8
|
-
super env.merge(vayacondios_method: method_name)
|
9
|
-
end
|
10
|
-
|
11
|
-
def extract_method env
|
12
|
-
return unless env['REQUEST_METHOD']
|
13
|
-
case env['REQUEST_METHOD'].upcase
|
14
|
-
when 'PUT' then
|
15
|
-
if env.has_key? 'HTTP_X_METHOD'
|
16
|
-
if env['HTTP_X_METHOD'].upcase == 'PATCH'
|
17
|
-
:patch
|
18
|
-
elsif env['HTTP_X_METHOD'].upcase == 'DELETE'
|
19
|
-
:delete
|
20
|
-
else
|
21
|
-
:update
|
22
|
-
end
|
23
|
-
else
|
24
|
-
:update
|
25
|
-
end
|
26
|
-
when 'GET' then :show
|
27
|
-
when 'POST' then :create
|
28
|
-
when 'PATCH' then :patch
|
29
|
-
when 'DELETE' then :delete
|
30
|
-
else nil
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
class Vayacondios
|
2
|
-
module Rack
|
3
|
-
|
4
|
-
# Sets the `Content-Type` of the request and response to
|
5
|
-
# `application/json`.
|
6
|
-
class JSONize
|
7
|
-
include Goliath::Rack::AsyncMiddleware
|
8
|
-
|
9
|
-
# Sets the `Content-Type` header to `application/json` so
|
10
|
-
# downstream parameter parsing will work correctly.
|
11
|
-
#
|
12
|
-
# @param [Hash] env the request environment
|
13
|
-
def call(env)
|
14
|
-
env.logger.debug("Started #{env[Goliath::Request::REQUEST_METHOD]} \"#{env[Goliath::Request::REQUEST_PATH]}\" for #{env[Goliath::Request::REMOTE_ADDR]} as JSON")
|
15
|
-
env['CONTENT_TYPE'] = 'application/json'
|
16
|
-
super(env)
|
17
|
-
end
|
18
|
-
|
19
|
-
# Sets the `Content-Type` header to `application/json` and
|
20
|
-
# serializes the response body to a JSON string.
|
21
|
-
#
|
22
|
-
# Will *not* do this if the request was sent to `/status` which
|
23
|
-
# is expected to just return the string `OK`.
|
24
|
-
#
|
25
|
-
# As per Infomart, the client expects an empty string when there is no body to
|
26
|
-
# return. This does not conform to proper JSON spec and will be an improper response
|
27
|
-
# for most other users.
|
28
|
-
#
|
29
|
-
# @param [Hash] env the request environment
|
30
|
-
# @param [Integer] status the HTTP status code of the response
|
31
|
-
# @param [Hash] headers the HTTP headers of the response
|
32
|
-
# @param [Object] body the upstream response body
|
33
|
-
# @return [Array] the response
|
34
|
-
def post_process(env, status, headers, body)
|
35
|
-
return [status, headers, body] if env["REQUEST_PATH"] == '/status'
|
36
|
-
headers['Content-Type'] = 'application/json'
|
37
|
-
body = body.nil? ? [''] : [MultiJson.encode(body)]
|
38
|
-
[status, headers, body]
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
class Vayacondios
|
2
|
-
module Rack
|
3
|
-
|
4
|
-
# Parses parameters in the request.
|
5
|
-
#
|
6
|
-
# Will parse parameters from the query string as well as from the
|
7
|
-
# request body.
|
8
|
-
#
|
9
|
-
# Rack gets unhappy when the parameters are anything other than a
|
10
|
-
# Hash but we want clients to be able to send non-Hash-like
|
11
|
-
# request bodies. To support this case, the request's params are
|
12
|
-
# stored into the `_document` key of the params, to be fetched
|
13
|
-
# later by Vayacondios::HttpServer#document.
|
14
|
-
class Params < Goliath::Rack::Params
|
15
|
-
|
16
|
-
# Parses the parameters of the request.
|
17
|
-
#
|
18
|
-
# @param [Hash] env the request environment
|
19
|
-
def retrieve_params(env)
|
20
|
-
params = {}
|
21
|
-
params.merge!(::Rack::Utils.parse_nested_query(env['QUERY_STRING']))
|
22
|
-
|
23
|
-
if env['rack.input']
|
24
|
-
post_params = ::Rack::Utils::Multipart.parse_multipart(env)
|
25
|
-
unless post_params
|
26
|
-
body = env['rack.input'].read
|
27
|
-
return params if body.empty?
|
28
|
-
env['rack.input'].rewind
|
29
|
-
|
30
|
-
post_params = case(env['CONTENT_TYPE'])
|
31
|
-
when Goliath::Rack::URL_ENCODED then
|
32
|
-
::Rack::Utils.parse_nested_query(body)
|
33
|
-
when Goliath::Rack::JSON_ENCODED then
|
34
|
-
MultiJson.decode(body)
|
35
|
-
else
|
36
|
-
{}
|
37
|
-
end
|
38
|
-
if post_params.is_a?(Hash)
|
39
|
-
params.merge!(post_params)
|
40
|
-
else
|
41
|
-
params['_document'] = post_params
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
params
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
class Vayacondios
|
2
|
-
module Rack
|
3
|
-
class Path
|
4
|
-
include Goliath::Rack::AsyncMiddleware
|
5
|
-
|
6
|
-
def call(env)
|
7
|
-
path_params = parse_path(env[Goliath::Request::REQUEST_PATH])
|
8
|
-
super env.merge(vayacondios_path: path_params)
|
9
|
-
end
|
10
|
-
|
11
|
-
def parse_path(path)
|
12
|
-
path_regex = /^\/v1\/(?<organization>[a-z]\w+)\/(?<type>config|event|itemset)(\/(?<topic>\w+)(\/(?<id>(\w+\/?)+))?)?(\/|\.(?<format>json))?$/i
|
13
|
-
if (match = path_regex.match(path))
|
14
|
-
{}.tap do |segments|
|
15
|
-
match.names.each do |segment|
|
16
|
-
segments[segment.to_sym] = match[segment]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|