lib/arborist/node/service.rb in arborist-0.0.1.pre20160606141735 vs lib/arborist/node/service.rb in arborist-0.0.1.pre20160829140603
- old
+ new
@@ -5,15 +5,17 @@
require 'ipaddr'
require 'socket'
require 'arborist/node'
require 'arborist/mixins'
+require 'arborist/exceptions'
# A node type for Arborist trees that represent services running on hosts.
class Arborist::Node::Service < Arborist::Node
- include Arborist::HashUtilities
+ include Arborist::HashUtilities,
+ Arborist::NetworkUtilities
# The default transport layer protocol to use for services that don't specify
# one
DEFAULT_PROTOCOL = 'tcp'
@@ -27,10 +29,11 @@
def initialize( identifier, host, attributes={}, &block )
raise Arborist::NodeError, "no host given" unless host.is_a?( Arborist::Node::Host )
qualified_identifier = "%s-%s" % [ host.identifier, identifier ]
@host = host
+ @addresses = nil
@app_protocol = nil
@protocol = nil
@port = nil
attributes[ :app_protocol ] ||= identifier
@@ -82,12 +85,28 @@
return @protocol unless new_proto
@protocol = new_proto
end
+ ### Set an IP address of the service. This must be one of the addresses of its
+ ### containing host.
+ def address( new_address )
+ self.log.debug "Adding address %p to %p" % [ new_address, self ]
+ normalized_addresses = normalize_address( new_address )
+
+ unless normalized_addresses.all? {|addr| @host.addresses.include?(addr) }
+ raise Arborist::ConfigError, "%s is not one of %s's addresses" %
+ [ new_address, @host.identifier ]
+ end
+
+ @addresses ||= []
+ @addresses += normalized_addresses
+ end
+
+
### Delegate the service's address to its host.
def addresses
- return @host.addresses
+ return @addresses || @host.addresses
end
### Returns +true+ if the node matches the specified +key+ and +val+ criteria.
def match_criteria?( key, val )