Class: Aide::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/aide/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ Service

Returns a new instance of Service.



5
6
7
# File 'lib/aide/service.rb', line 5

def initialize(name:)
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/aide/service.rb', line 3

def name
  @name
end

Instance Method Details

#addressObject



11
12
13
# File 'lib/aide/service.rb', line 11

def address
  send(Aide.config.service_address_method)
end

#auth(filtered: false) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aide/service.rb', line 71

def auth(filtered: false)
  template = config[:auth]

  if template.nil? || self.user.nil? || self.password.nil?
    return
  end

  template = template.dup
  template.gsub!(/{{\.user}}/, self.user.to_s)

  password = nil
  if filtered
    password = 'PASSWORD'
  else
    password = self.password.to_s
  end

  template.gsub!(/{{\.password}}/, password)

  template
end

#databaseObject



167
168
169
170
171
172
173
174
175
# File 'lib/aide/service.rb', line 167

def database
  return if database_key.nil?

  @database ||= begin
                  database = Diplomat::Kv.get(database_key, {}, :return)
                  database = nil if database == ""
                  database
                end
end

#database_keyObject



163
164
165
# File 'lib/aide/service.rb', line 163

def database_key
  config[:database_key]
end

#display_url(filtered: false) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/aide/service.rb', line 63

def display_url(filtered: false)
  if !config[:multi_url].nil?
    multi_url(filtered: filtered)
  else
    url(filtered: filtered)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/aide/service.rb', line 177

def empty?
  service.to_h.empty?
end

#multi_url(filtered: false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/aide/service.rb', line 42

def multi_url(filtered: false)
  return if empty?

  template = config[:multi_url]
  return if template.nil?

  template = template.dup
  template.gsub!(/{{\.protocol}}/, self.protocol.to_s)
  template.gsub!(/{{\.auth}}/, self.auth(filtered: filtered).to_s)
  template.gsub!(/{{\.database}}/, self.database.to_s)
  template.gsub!(/{{\.nodes}}/, self.nodes.to_s)

  template
end

#multi_url!Object



57
58
59
60
61
# File 'lib/aide/service.rb', line 57

def multi_url!
  self.multi_url.tap do |orig_url|
    raise MissingService.new(name) if orig_url.nil?
  end
end

#node(node_service = service) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/aide/service.rb', line 104

def node(node_service=service)
  template = config[:node]
  return if template.nil?

  template = template.dup
  template.gsub!(/{{.address}}/, node_service.send(Aide.config.service_address_method).to_s)
  template.gsub!(/{{.port}}/, node_service.ServicePort.to_s)

  template
end

#node!Object



115
116
117
118
119
# File 'lib/aide/service.rb', line 115

def node!
  self.node.tap do |orig_node|
    raise MissingService.new(name) if orig_node.nil?
  end
end

#nodesObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/aide/service.rb', line 93

def nodes
  return if empty?

  template = config[:node]
  return if template.nil?

  services.map do |service|
    self.node(service)
  end.join(',')
end

#passwordObject



139
140
141
142
143
144
145
146
147
# File 'lib/aide/service.rb', line 139

def password
  return if password_key.nil?

  @password ||= begin
                  password = Diplomat::Kv.get(password_key, {}, :return)
                  password = nil if password == ""
                  password
                end
end

#password_keyObject



135
136
137
# File 'lib/aide/service.rb', line 135

def password_key
  config[:password_key]
end

#protocolObject



153
154
155
156
157
158
159
160
161
# File 'lib/aide/service.rb', line 153

def protocol
  return if protocol_key.nil?

  @protocol ||= begin
                  protocol = Diplomat::Kv.get(protocol_key, {}, :return)
                  protocol = nil if protocol == ""
                  protocol
                end
end

#protocol_keyObject



149
150
151
# File 'lib/aide/service.rb', line 149

def protocol_key
  config[:protocol_key]
end

#url(filtered: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aide/service.rb', line 17

def url(filtered: false)
  return if empty?

  template = config[:url]
  return if template.nil?

  template = template.dup
  template.gsub!(/{{\.protocol}}/, self.protocol.to_s)
  template.gsub!(/{{\.address}}/, self.address.to_s)
  template.gsub!(/{{\.Address}}/, self.Address.to_s)
  template.gsub!(/{{\.ServiceAddress}}/, self.ServiceAddress.to_s)
  template.gsub!(/{{\.ServicePort}}/, self.ServicePort.to_s)
  template.gsub!(/{{\.port}}/, self.port.to_s)
  template.gsub!(/{{\.auth}}/, self.auth(filtered: filtered).to_s)
  template.gsub!(/{{\.database}}/, self.database.to_s)

  template
end

#url!Object



36
37
38
39
40
# File 'lib/aide/service.rb', line 36

def url!
  self.url.tap do |orig_url|
    raise MissingService.new(name) if orig_url.nil?
  end
end

#userObject



125
126
127
128
129
130
131
132
133
# File 'lib/aide/service.rb', line 125

def user
  return if user_key.nil?

  @user ||= begin
                  user = Diplomat::Kv.get(user_key, {}, :return)
                  user = nil if user == ""
                  user
                end
end

#user_keyObject



121
122
123
# File 'lib/aide/service.rb', line 121

def user_key
  config[:user_key]
end