Class: Service

Inherits:
Object show all
Defined in:
modules/netbase/lib/puppet/functions/netbase/services/merge.rb

Overview

create a simple object to mimic the puppet type Netbase::Service

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data, strict) ⇒ Service

Returns a new instance of Service.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 6

def initialize(name, data, strict)
  @name = name
  @port = data['port']
  @protocols = data['protocols']
  @portend = data.fetch('portend', nil)
  @aliases = data.fetch('aliases', []).to_set
  @description = data.fetch('description', nil)
  # This allow us to either compare via proto and port or just port
  # it's not great having this in the object class but it allows us to
  # abuse set comparisons
  @strict = strict
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



4
5
6
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 4

def aliases
  @aliases
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 4

def name
  @name
end

#portObject (readonly)

Returns the value of attribute port.



4
5
6
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 4

def port
  @port
end

#protocolsObject (readonly)

Returns the value of attribute protocols.



4
5
6
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 4

def protocols
  @protocols
end

Instance Method Details

#==(other) ⇒ Object

override ==, eq? and hash to allow use to do set comparisons

is not strictly required for set comparisons but make sense

to also include this



22
23
24
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 22

def ==(other)
  hash == other.hash
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 26

def eql?(other)
  hash == other.hash
end

#hashObject



30
31
32
33
34
35
36
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 30

def hash
  if @strict
    "#{@port}_#{@protocols.join}".hash
  else
    @port.hash
  end
end

#puppet_typeObject



38
39
40
41
42
43
44
45
46
47
# File 'modules/netbase/lib/puppet/functions/netbase/services/merge.rb', line 38

def puppet_type
  result = {
    'protocols' => @protocols,
    'port' => @port,
  }
  result['portend'] = @portend unless @portend.nil?
  result['description'] = @description unless @description.nil?
  result['aliases'] = @aliases.to_a unless @aliases.empty?
  result
end