Class: KSConnect::API::Plugin
- Inherits:
-
Object
- Object
- KSConnect::API::Plugin
show all
- Includes:
- Logs
- Defined in:
- lib/ksconnect/api/plugin.rb,
lib/ksconnect/api/plugin/data.rb,
lib/ksconnect/api/plugin/config.rb,
lib/ksconnect/api/plugin/domain.rb
Defined Under Namespace
Classes: Config, Data, Domain
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Logs
included
Constructor Details
#initialize(name, main = false) ⇒ Plugin
Returns a new instance of Plugin.
13
14
15
16
17
18
19
20
|
# File 'lib/ksconnect/api/plugin.rb', line 13
def initialize(name, main = false)
@name = name
@main_plugin = main
@domains = {}
load_domains
subscribe_to_events
end
|
Instance Attribute Details
#config ⇒ Object
22
23
24
|
# File 'lib/ksconnect/api/plugin.rb', line 22
def config
@config ||= Config.new
end
|
#domains ⇒ Object
Returns the value of attribute domains.
9
10
11
|
# File 'lib/ksconnect/api/plugin.rb', line 9
def domains
@domains
end
|
#name ⇒ Object
Returns the value of attribute name.
10
11
12
|
# File 'lib/ksconnect/api/plugin.rb', line 10
def name
@name
end
|
Instance Method Details
26
27
28
|
# File 'lib/ksconnect/api/plugin.rb', line 26
def configure
yield(config)
end
|
#domains_key ⇒ Object
109
110
111
112
|
# File 'lib/ksconnect/api/plugin.rb', line 109
def domains_key
@domain_list_uuid ||= redis.hget("#{@name}:data", "domain_names")
"kloudsec_data:#{@domain_list_uuid}"
end
|
#load_domains ⇒ Object
This method loads the domain list from Redis, adding or removing domains as appropriate. Note that it does not update the ip address of existing domains.
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/ksconnect/api/plugin.rb', line 32
def load_domains
domain_to_ip = redis.hgetall(domains_key)
new_domains = domain_to_ip.keys - @domains.values.map(&:name)
new_domains.each { |domain_name| @domains[domain_name] = Domain.new(domain_name, domain_to_ip[domain_name], @name) }
@domains.select! { |k, _| domain_to_ip.keys.include?(k) }
end
|
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/ksconnect/api/plugin.rb', line 81
def perform_request(request)
request.stringify_keys!
domain_name = request['domain_name']
logger.warn "Invalid push request with no domain: #{request}" and return unless domain_name
request_type = request['request_type']
case request_type
when 'initialize'
@domains[domain_name] = Domain.new(domain_name, get_ip_for(domain_name), @name)
config.on_initialize.call(request)
when 'update'
if @domains[domain_name]
@domains[domain_name].update_ip_address
else
@domains[domain_name] = Domain.new(domain_name, get_ip_for(domain_name), @name)
end
config.on_update.call(request)
when 'teardown'
config.on_teardown.call(request)
@domains.delete(domain_name)
else
raise "Invalid request type"
end
redis.publish("core:push", { domain_name: domain_name, plugin_name: @name, request_type: request_type }.to_json) if should_repush?
end
|
#subscribe_to_events ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/ksconnect/api/plugin.rb', line 44
def subscribe_to_events
KSConnect.channel("#{@name}:push") do |message|
begin
msg = JSON.parse(message)
rescue Exception => e
logger.error e
logger.error "Error parsing message as JSON: #{message}"
next
end
if %w(initialize update teardown).include? msg['request_type']
perform_request(msg)
else
begin
result = config.on_push.call(msg)
update_action(msg["action_uuid"], :done, result) if msg["action_uuid"]
rescue => e
update_action(msg["action_uuid"], :failed, "") if msg["action_uuid"]
raise e
end
end
end
end
|
#update_action(action_id, state, body = "") ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/ksconnect/api/plugin.rb', line 68
def update_action(action_id, state, body="")
raise "Updating action with empty action_id" if action_id.nil? or action_id.empty?
redis.multi do |redis_transaction|
redis_transaction.mapped_hmset "kloudsec_data:#{action_id}", {
plugin_name: @name,
uuid: action_id,
state: state,
response: body,
}
redis_transaction.expire "kloudsec_data:#{action_id}", 300
end
end
|