Class: SkullIsland::Resources::Service

Inherits:
SkullIsland::Resource show all
Includes:
Helpers::Meta
Defined in:
lib/skull_island/resources/service.rb

Overview

The Service resource class

Instance Attribute Summary

Attributes inherited from SkullIsland::Resource

#api_client, #entity, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Meta

#add_meta, #import_time, #import_time=, #metatags, #postprocess_tags, #preprocess_tags, #project, #project=, #raw_tags, #remove_meta, #supports_meta?

Methods inherited from SkullIsland::Resource

all, cleanup_except, find, from_hash, gen_getter_method, gen_property_methods, gen_setter_method, get, immutable, #initialize, property, relative_uri, #relative_uri, where

Methods included from Helpers::ResourceClass

#determine_getter_names, #determine_setter_names, #human, #i18n_key, #immutable?, #param_key, #properties, #route_key

Methods included from Helpers::Resource

#<=>, #datetime_from_params, #delayed_set, #digest, #digest_properties, #find_by_digest, #fresh?, #host_regex, #id, #id_property, #immutable?, #import_update_or_skip, #lookup, #model_name, #new?, #persisted?, #postprocess_created_at, #postprocess_updated_at, #properties, #prune_for_save, #recursive_erubi, #reload, #required_properties, #save, #save_uri, #supports_meta?, #tainted?, #to_param, #to_s, #update

Methods included from Validations::Resource

#validate_id, #validate_mutability, #validate_required_properties, #validate_tags

Constructor Details

This class inherits a constructor from SkullIsland::Resource

Class Method Details

.batch_import(data, verbose: false, test: false, project: nil, time: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/AbcSize



31
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
# File 'lib/skull_island/resources/service.rb', line 31

def self.batch_import(data, verbose: false, test: false, project: nil, time: nil)
  raise(Exceptions::InvalidArguments) unless data.is_a?(Array)

  known_ids = []

  data.each_with_index do |rdata, index|
    resource = new
    resource.name = rdata['name']
    resource.retries = rdata['retries'] if rdata['retries']
    resource.protocol = rdata['protocol']
    resource.delayed_set(:host, rdata)
    resource.delayed_set(:port, rdata)
    resource.path = rdata['path'] if rdata['path']
    resource.connect_timeout = rdata['connect_timeout'] if rdata['connect_timeout']
    resource.write_timeout = rdata['write_timeout'] if rdata['write_timeout']
    resource.read_timeout = rdata['read_timeout'] if rdata['read_timeout']
    resource.tls_verify = rdata['tls_verify'] if rdata['tls_verify']
    resource.delayed_set(:client_certificate, rdata) if rdata['client_certificate']
    resource.delayed_set(:ca_certificates, rdata) if rdata['ca_certificates']
    resource.tags = rdata['tags'] if rdata['tags']
    resource.project = project if project
    resource.import_time = (time || Time.now.utc.to_i) if project
    resource.import_update_or_skip(index: index, verbose: verbose, test: test)
    known_ids << resource.id

    previous_routes = resource.routes.dup

    added_routes = Route.batch_import(
      (rdata['routes'] || []).map { |r| r.merge('service' => { 'id' => resource.id }) },
      verbose: verbose,
      test: test,
      project: project,
      time: time,
      cleanup: false
    )

    Route.cleanup_except(project, added_routes, previous_routes)
  end

  cleanup_except(project, known_ids) if project

  known_ids
end

Instance Method Details

#add_route!(details) ⇒ Object

Convenience method to add routes



79
80
81
82
83
84
# File 'lib/skull_island/resources/service.rb', line 79

def add_route!(details)
  r = details.is_a?(Route) ? details : Route.from_hash(details, api_client: api_client)

  r.service = self
  r.save
end

#ca_certificatesObject

The ca_certificates property



22
# File 'lib/skull_island/resources/service.rb', line 22

property :ca_certificates,    validate: true, preprocess: true, postprocess: true

#client_certificateObject

The client_certificate property



23
# File 'lib/skull_island/resources/service.rb', line 23

property :client_certificate, validate: true, preprocess: true, postprocess: true

#connect_timeoutObject

The connect_timeout property



19
# File 'lib/skull_island/resources/service.rb', line 19

property :connect_timeout,    validate: true

#created_atObject

The created_at property



24
# File 'lib/skull_island/resources/service.rb', line 24

property :created_at, read_only: true, postprocess: true

#destroyObject



86
87
88
89
# File 'lib/skull_island/resources/service.rb', line 86

def destroy
  routes.each(&:destroy)
  super
end

#export(options = {}) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/AbcSize



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/skull_island/resources/service.rb', line 104

def export(options = {})
  hash = {
    'name' => name,
    'retries' => retries,
    'protocol' => protocol,
    'host' => host,
    'port' => port,
    'path' => path,
    'connect_timeout' => connect_timeout,
    'write_timeout' => write_timeout,
    'read_timeout' => read_timeout
  }
  hash['routes'] = routes.collect { |route| route.export(exclude: 'service') }
  hash['tags'] = tags unless tags.empty?
  if client_certificate&.name
    hash['client_certificate'] = "<%= lookup :certificate, '#{client_certificate.name}' %>"
  elsif client_certificate
    hash['client_certificate'] = { 'id' => client_certificate.id }
  end
  if ca_certificates && !ca_certificates.empty?
    hash['ca_certificates'] = export_ca_certificates
  end
  hash['tls_verify'] = tls_verify if [true, false].include?(tls_verify)
  [*options[:exclude]].each do |exclude|
    hash.delete(exclude.to_s)
  end
  [*options[:include]].each do |inc|
    hash[inc.to_s] = send(inc.to_sym)
  end
  hash.reject { |_, value| value.nil? }
end

#hostObject

The host property



15
# File 'lib/skull_island/resources/service.rb', line 15

property :host,               validate: true, required: true

#modified_existing?Boolean

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity rubocop:enable Metrics/AbcSize

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/skull_island/resources/service.rb', line 139

def modified_existing?
  return false unless new?

  # Find services of the same name
  same_name = self.class.where(:name, name)

  existing = same_name.size == 1 ? same_name.first : nil

  if existing
    @entity['id'] = existing.id
    save
  else
    false
  end
end

#nameObject

The name property



12
# File 'lib/skull_island/resources/service.rb', line 12

property :name

#pathObject

The path property



18
# File 'lib/skull_island/resources/service.rb', line 18

property :path

#pluginsObject

Provides a collection of related Plugin instances



97
98
99
# File 'lib/skull_island/resources/service.rb', line 97

def plugins
  Plugin.where(:service, self, api_client: api_client)
end

#portObject

The port property



16
# File 'lib/skull_island/resources/service.rb', line 16

property :port,               validate: true, required: true

#protocolObject

The protocol property



14
# File 'lib/skull_island/resources/service.rb', line 14

property :protocol,           validate: true, required: true

#read_timeoutObject

The read_timeout property



21
# File 'lib/skull_island/resources/service.rb', line 21

property :read_timeout,       validate: true

#retriesObject

The retries property



13
# File 'lib/skull_island/resources/service.rb', line 13

property :retries,            validate: true

#routesObject

Provides a collection of related Route instances



92
93
94
# File 'lib/skull_island/resources/service.rb', line 92

def routes
  Route.where(:service, self, api_client: api_client)
end

#tagsObject

The tags property



26
# File 'lib/skull_island/resources/service.rb', line 26

property :tags, validate: true, preprocess: true, postprocess: true

#tls_verifyObject

The tls_verify property



17
# File 'lib/skull_island/resources/service.rb', line 17

property :tls_verify,         type: :boolean

#updated_atObject

The updated_at property



25
# File 'lib/skull_island/resources/service.rb', line 25

property :updated_at, read_only: true, postprocess: true

#urlObject



162
163
164
165
166
167
168
# File 'lib/skull_island/resources/service.rb', line 162

def url
  u = URI('')
  u.scheme = protocol
  u.host = host
  u.port = port unless [80, 443].include? port
  u.to_s
end

#url=(uri_or_string) ⇒ Object



155
156
157
158
159
160
# File 'lib/skull_island/resources/service.rb', line 155

def url=(uri_or_string)
  uri_data = URI(uri_or_string)
  self.protocol = uri_data.scheme
  self.host = uri_data.host
  self.port = uri_data.port
end

#write_timeoutObject

The write_timeout property



20
# File 'lib/skull_island/resources/service.rb', line 20

property :write_timeout,      validate: true