lib/ec2/host/host_data.rb in ec2-host-0.0.8 vs lib/ec2/host/host_data.rb in ec2-host-0.0.9
- old
+ new
@@ -16,13 +16,13 @@
extend Forwardable
def_delegators :instance,
:instance_id,
:private_ip_address,
:public_ip_address,
- :launch_time,
- :state,
- :monitoring
+ :launch_time
+ def state; instance.state.name; end
+ def monitoring; instance.monitoring.state; end
alias_method :ip, :private_ip_address
alias_method :start_date, :launch_time
def usages; roles; end
@@ -39,20 +39,22 @@
# match with condition or not
#
# @param [Hash] condition search parameters
def match?(condition)
+ return false if !condition[:state] and terminated? # remove terminated host from lists as default
return false unless role_match?(condition)
condition = HashUtil.except(condition,
:role, :role1, :role2, :role3,
:usage, :usage1, :usage2, :usage3
)
condition.each do |key, values|
- if self.send(key).is_a?(Array)
- return false unless self.send(key).find {|v| values.include?(v) }
+ v = get_value(key)
+ if v.is_a?(Array)
+ return false unless v.find {|_| values.include?(_) }
else
- return false unless values.include?(self.send(key))
+ return false unless values.include?(v)
end
end
true
end
@@ -77,15 +79,26 @@
HashUtil.except(self, :instance).merge(
instance_id: instance_id,
private_ip_address: private_ip_address,
public_ip_address: public_ip_address,
launch_time: launch_time,
- state: state.name,
- monitoring: monitoring.state,
+ state: state,
+ monitoring: monitoring,
).to_json
end
# private
+
+ # "instance.instance_id" => self.send("instance").send("instance_id")
+ def get_value(key)
+ v = self
+ key.to_s.split('.').each {|k| v = v.send(k) }
+ v
+ end
+
+ def terminated?
+ state == "terminated"
+ end
def role_match?(condition)
# usage is an alias of role
if role = (condition[:role] || condition[:usage])
role1, role2, role3 = role.first.split(':')