Method: Imperium::Service#create_index

Defined in:
lib/imperium/service.rb

#create_indexInteger (readonly)

Returns:

  • (Integer)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/imperium/service.rb', line 32

class Service < APIObject
  self.attribute_map = {
    'ID' => :id,
    'Name' => :name,
    'Tags' => :tags,
    'Address' => :address,
    'Port' => :port,
    'Check' => :check,
    'Checks' => :checks,
    'EnableTagOverride' => :enable_tag_override,
    'CreateIndex' => :create_index,
    'ModifyIndex' => :modify_index
  }

  def initialize(*args)
    # So we can << onto these w/o having to nil check everywhere first
    @tags ||= []
    @checks ||= []
    super
  end

  def add_check(val)
    @checks <<  maybe_convert_service_check(val) unless val.nil?
  end
  alias check= add_check


  def checks=(val)
    @checks = (val || []).map { |obj| maybe_convert_service_check(obj) }
  end

  def tags=(val)
    @tags = (val.nil? ? [] : val)
  end

  # Generate a hash containing the data necessary for registering this service.
  #
  # If both Check and Checks are present in the object they're coalesced into
  # a single Checks key.
  #
  # @return [Hash<String => String,Integer,Hash<String => String>,Array<Hash<String => String>>]
  def registration_data
    to_h.tap do |h|
      h.delete('CreateIndex')
      h.delete('ModifyIndex')
      h.delete('Checks') if checks.empty?
    end
  end

  private

  def maybe_convert_service_check(attrs_or_check)
    attrs_or_check.is_a?(Hash) ? ServiceCheck.new(attrs_or_check) : attrs_or_check
  end
end