Class: SkullIsland::Resources::Upstream

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

Overview

The Upstream 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, #destroy, #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, #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



27
28
29
30
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
# File 'lib/skull_island/resources/upstream.rb', line 27

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.slots = rdata['slots'] if rdata['slots']
    resource.hash_on = rdata['hash_on']
    resource.hash_fallback = rdata['hash_fallback'] if rdata['hash_fallback']
    resource.hash_on_header = rdata['hash_on_header'] if rdata['hash_on_header']
    if rdata['hash_fallback_header']
      resource.hash_fallback_header = rdata['hash_fallback_header']
    end
    resource.hash_on_cookie = rdata['hash_on_cookie'] if rdata['hash_on_cookie']
    if rdata['hash_on_cookie_path']
      resource.hash_on_cookie_path = rdata['hash_on_cookie_path']
    end
    resource.healthchecks = rdata['healthchecks'] if rdata['healthchecks']
    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
    puts '[INFO] Processing UpstreamTarget entries...' if verbose

    UpstreamTarget.batch_import(
      (rdata['targets'] || []).map { |t| t.merge('upstream' => { 'id' => resource.id }) },
      verbose: verbose,
      test: test,
      project: project,
      time: time
    )
  end

  cleanup_except(project, known_ids) if project
end

Instance Method Details

#add_target!(details) ⇒ Object

Convenience method to add upstream targets



80
81
82
83
84
85
86
87
88
89
# File 'lib/skull_island/resources/upstream.rb', line 80

def add_target!(details)
  r = if details.is_a?(UpstreamTarget)
        details
      else
        UpstreamTarget.from_hash(details, api_client: api_client)
      end

  r.upstream = self
  r.save
end

#created_atObject

The created_at property



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

property :created_at, read_only: true, postprocess: true

#export(options = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/skull_island/resources/upstream.rb', line 114

def export(options = {})
  hash = {
    'name' => name,
    'slots' => slots,
    'hash_on' => hash_on,
    'hash_fallback' => hash_fallback,
    'hash_on_header' => hash_on_header,
    'hash_fallback_header' => hash_fallback_header,
    'hash_on_cookie' => hash_on_cookie,
    'hash_on_cookie_path' => hash_on_cookie_path,
    'healthchecks' => healthchecks
  }
  hash['targets'] = targets.collect { |target| target.export(exclude: 'upstream') }
  hash['tags'] = tags unless tags.empty?
  [*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

#hash_fallbackObject

The hash_fallback property



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

property :hash_fallback, validate: true

#hash_fallback_headerObject

The hash_fallback_header property



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

property :hash_fallback_header, validate: true

#hash_onObject

The hash_on property



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

property :hash_on, validate: true

The hash_on_cookie property



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

property :hash_on_cookie, validate: true

The hash_on_cookie_path property



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

property :hash_on_cookie_path, validate: true

#hash_on_headerObject

The hash_on_header property



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

property :hash_on_header, validate: true

#healthObject

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



69
70
71
72
73
74
75
76
77
# File 'lib/skull_island/resources/upstream.rb', line 69

def health
  if new?
    # No health status for new Upstreams
    nil
  else
    health_json = api_client.get("#{relative_uri}/health")
    health_json['data']
  end
end

#healthchecksObject

The healthchecks property



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

property :healthchecks, validate: true

#modified_existing?Boolean

Returns:

  • (Boolean)


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

def modified_existing?
  return false unless new?

  # Find routes 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/upstream.rb', line 12

property :name, required: true, validate: true

#slotsObject

The slots property



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

property :slots, validate: true

#tagsObject

The tags property



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

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

#target(target_id) ⇒ Object



91
92
93
# File 'lib/skull_island/resources/upstream.rb', line 91

def target(target_id)
  targets.where(id: target_id).first
end

#targetsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/skull_island/resources/upstream.rb', line 95

def targets
  target_list_data = api_client.get("#{relative_uri}/targets")
  root = 'data' # root for API JSON response data
  # TODO: do something with lazy requests...

  ResourceCollection.new(
    target_list_data[root].map do |record|
      UpstreamTarget.new(
        entity: record,
        lazy: false,
        tainted: false,
        api_client: api_client
      )
    end,
    type: UpstreamTarget,
    api_client: api_client
  )
end