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.



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

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.



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

def aliases
  @aliases
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#protocolsObject (readonly)

Returns the value of attribute protocols.



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

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



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

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

#puppet_typeObject



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

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