Class: Dendrite::ServiceNode

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Validator
Defined in:
lib/dendrite/service_node.rb

Defined Under Namespace

Classes: DefaultServer, Dependency, Deploy, DomainName, Port, Scale, Telemetry

Constant Summary collapse

VALID_EMAIL_REGEX =
/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
Metadata =
OpenStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validator

#valid?

Constructor Details

#initialize(**args) ⇒ ServiceNode

Returns a new instance of ServiceNode.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/dendrite/service_node.rb', line 146

def initialize(**args)
  @ports = {}
  @default_servers = {}
  @domain_names = {}
  args.each do |k,v|
    case k
    when :ports
      v.each do |name, port|
        @ports[name] = Port.new(name, port)
      end
    when :deploy
      @deploy = Deploy.new(v[:repository], v[:package]) if v != nil
    when :scale
      @scale = Scale.new(v[:max_instance_count], v[:min_instance_count], v[:min_memory], v[:min_cpu]) if v != nil
    when :telemetry
      @telemetry = Telemetry.new(v[:health_url], v[:notification_email]) if v != nil
    when :default_servers
      v.each do |node|
        @default_servers[node[:environment]] ||= []
        @default_servers[node[:environment]] << DefaultServer.new(node[:environment], node[:host], node[:port])
      end
    when :domain_names
      v.each do |dns|
        @domain_names[dns[:environment]] ||= []
        @domain_names[dns[:environment]] << DomainName.new(dns[:environment], dns[:domain_name])
      end
    when :metadata
      @metadata = Metadata.new(v)
    when :users
      @users = v.keys
    else
      instance_variable_set("@#{k}", v)
    end
  end
  @dependencies = {}
end

Instance Attribute Details

#componentObject (readonly)

Returns the value of attribute component.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def component
  @component
end

#default_serversObject (readonly)

Returns the value of attribute default_servers.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def default_servers
  @default_servers
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def dependencies
  @dependencies
end

#deployObject (readonly)

Returns the value of attribute deploy.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def deploy
  @deploy
end

#domain_namesObject (readonly)

Returns the value of attribute domain_names.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def domain_names
  @domain_names
end

#lead_emailObject (readonly)

Returns the value of attribute lead_email.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def lead_email
  @lead_email
end

#metadataObject (readonly)

Returns the value of attribute metadata.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def 
  @metadata
end

#organizationObject (readonly)

Returns the value of attribute organization.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def organization
  @organization
end

#portsObject (readonly)

Returns the value of attribute ports.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def ports
  @ports
end

#scaleObject (readonly)

Returns the value of attribute scale.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def scale
  @scale
end

#team_emailObject (readonly)

Returns the value of attribute team_email.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def team_email
  @team_email
end

#telemetryObject (readonly)

Returns the value of attribute telemetry.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def telemetry
  @telemetry
end

#typeObject (readonly)

Returns the value of attribute type.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def type
  @type
end

#usersObject (readonly)

Returns the value of attribute users.



130
131
132
# File 'lib/dendrite/service_node.rb', line 130

def users
  @users
end

Instance Method Details

#add_dependency(service:, latency:, identifier:, read_only: false) ⇒ Object



199
200
201
# File 'lib/dendrite/service_node.rb', line 199

def add_dependency(service:, latency:, identifier:, read_only: false)
  @dependencies[service.name] = Dependency.new(service, latency, identifier, read_only == true)
end

#loadbalancer_portObject



195
196
197
# File 'lib/dendrite/service_node.rb', line 195

def loadbalancer_port
  ports[:loadbalancer_port].port if ports[:loadbalancer_port]
end

#nameObject



187
188
189
# File 'lib/dendrite/service_node.rb', line 187

def name
  "#{organization}_#{component}_#{@name}" if @name
end

#real_nameObject



183
184
185
# File 'lib/dendrite/service_node.rb', line 183

def real_name
  @name
end

#service_portObject



191
192
193
# File 'lib/dendrite/service_node.rb', line 191

def service_port
  ports[:service_port].port if ports[:service_port]
end

#to_hObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/dendrite/service_node.rb', line 203

def to_h
  data = {
    organization: organization,
    component: component,
    name: real_name,
    complete_name: name,
    lead_email: lead_email,
    team_email: team_email,
    type: type,
    ports: ports.values.collect(&:to_h),
    dependencies: dependencies.keys
  }
  data.merge!({deploy: deploy ? deploy.to_h : {}})
  data.merge!({scale: scale ? scale.to_h : {}})
  data.merge!({telemetry: telemetry ? telemetry.to_h : {}})
  data.merge!({default_servers: default_servers.values.flatten.collect(&:to_h)})
end