class SkullIsland::Resources::UpstreamTarget

The Upstream Target resource class

@see docs.konghq.com/1.1.x/admin-api/#target-object Target API definition

Public Class Methods

all(options = {}) click to toggle source

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

# File lib/skull_island/resources/upstream_target.rb, line 47
def self.all(options = {})
  api_client = options[:api_client] || APIClient.instance

  Upstream.all(api_client: api_client).map(&:targets).reduce(&:merge)
end
batch_import(data, verbose: false, test: false, project: nil, time: nil) click to toggle source

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

# File lib/skull_island/resources/upstream_target.rb, line 23
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 |resource_data, index|
    resource = new
    resource.delayed_set(:target, resource_data)
    resource.delayed_set(:upstream, resource_data)
    resource.weight = resource_data['weight'] if resource_data['weight']
    resource.tags = resource_data['tags'] if resource_data['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
  end

  cleanup_except(project, known_ids) if project

  known_ids
end
get(id, options = {}) click to toggle source
# File lib/skull_island/resources/upstream_target.rb, line 53
def self.get(id, options = {})
  if options[:upstream].is_a?(Upstream)
    options[:upstream].target(id)
  elsif options[:upstream]
    upstream_opts = options.merge(lazy: true)
    Upstream.get(options[:upstream], upstream_opts).target(id)
  end
end

Public Instance Methods

export(options = {}) click to toggle source
# File lib/skull_island/resources/upstream_target.rb, line 83
def export(options = {})
  hash = { 'target' => target, 'weight' => weight }
  hash['upstream'] = "<%= lookup :upstream, '#{upstream.name}' %>" if 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
find_by_digest() click to toggle source

Tests for an existing version of this resource based on its properties rather than its `id`

# File lib/skull_island/resources/upstream_target.rb, line 63
def find_by_digest
  result = self.class.where(:digest, digest) # matching digest means the equivalent resource
  if result.size == 1
    @entity  = result.first.entity
    @lazy    = false
    @tainted = false
    true
  else
    false
  end
end
modified_existing?() click to toggle source
# File lib/skull_island/resources/upstream_target.rb, line 96
def modified_existing?
  false # Upstream Targets can not be PATCHed, so can not be modified
end
relative_uri() click to toggle source
# File lib/skull_island/resources/upstream_target.rb, line 75
def relative_uri
  upstream ? "#{upstream.relative_uri}/targets/#{id}" : nil
end
save_uri() click to toggle source
# File lib/skull_island/resources/upstream_target.rb, line 79
def save_uri
  upstream ? "#{upstream.relative_uri}/targets" : nil
end

Private Instance Methods

postprocess_upstream(value) click to toggle source
# File lib/skull_island/resources/upstream_target.rb, line 121
def postprocess_upstream(value)
  if value.is_a?(Hash)
    Upstream.new(
      entity: value,
      lazy: true,
      tainted: false,
      api_client: api_client
    )
  else
    value
  end
end
preprocess_target(input) click to toggle source
# File lib/skull_island/resources/upstream_target.rb, line 102
def preprocess_target(input)
  if input.is_a?(URI)
    "#{input.host}:#{input.port || 8000}"
  else
    input
  end
end
preprocess_upstream(input) click to toggle source
# File lib/skull_island/resources/upstream_target.rb, line 110
def preprocess_upstream(input)
  case input
  when Hash
    input
  when String
    { 'id' => input }
  else
    { 'id' => input.id }
  end
end
validate_target(value) click to toggle source

Used to validate {#target} on set

# File lib/skull_island/resources/upstream_target.rb, line 135
def validate_target(value)
  # only URIs or specific strings
  value.is_a?(URI) || (
    value.is_a?(String) && value.match?(/[\w\d.-]+:\d{1,5}/)
  )
end
validate_upstream(value) click to toggle source

Used to validate upstream on set

# File lib/skull_island/resources/upstream_target.rb, line 143
def validate_upstream(value)
  # allow either a Upstream object or a Hash
  value.is_a?(Upstream) || value.is_a?(Hash) || value.is_a?(String)
end
validate_weight(value) click to toggle source

Used to validate {#weight} on set

# File lib/skull_island/resources/upstream_target.rb, line 149
def validate_weight(value)
  # only positive Integers (or zero) are allowed
  value.is_a?(Integer) && (0..1000).cover?(value)
end