Class: SkullIsland::Resources::Upstream

Inherits:
SkullIsland::Resource show all
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 inherited from SkullIsland::Resource

all, 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, #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) ⇒ Object

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



25
26
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
# File 'lib/skull_island/resources/upstream.rb', line 25

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

  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.import_update_or_skip(index: index, verbose: verbose, test: test)
    puts '[INFO] Processing UpstreamTarget entries...' if verbose

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

Instance Method Details

#add_target!(details) ⇒ Object

Convenience method to add upstream targets



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

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



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

property :created_at, read_only: true, postprocess: true

#export(options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/skull_island/resources/upstream.rb', line 103

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 if tags
  [*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



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

property :hash_fallback, validate: true

#hash_fallback_headerObject

The hash_fallback_header property



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

property :hash_fallback_header, validate: true

#hash_onObject

The hash_on property



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

property :hash_on, validate: true

The hash_on_cookie property



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

property :hash_on_cookie, validate: true

The hash_on_cookie_path property



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

property :hash_on_cookie_path, validate: true

#hash_on_headerObject

The hash_on_header property



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

property :hash_on_header, validate: true

#healthObject

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



58
59
60
61
62
63
64
65
66
# File 'lib/skull_island/resources/upstream.rb', line 58

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



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

property :healthchecks, validate: true

#modified_existing?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/skull_island/resources/upstream.rb', line 126

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



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

property :name, required: true, validate: true

#slotsObject

The slots property



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

property :slots, validate: true

#tagsObject

The tags property



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

property :tags, validate: true

#target(target_id) ⇒ Object



80
81
82
# File 'lib/skull_island/resources/upstream.rb', line 80

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

#targetsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/skull_island/resources/upstream.rb', line 84

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