class SkullIsland::Resources::Plugin

The Plugin resource class

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

Public Class Methods

batch_import(data, verbose: false, test: false, project: nil, time: nil) click to toggle source

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

# File lib/skull_island/resources/plugin.rb, line 25
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.name = resource_data['name']
    resource.enabled = resource_data['enabled']
    resource.run_on = resource_data['run_on'] if resource_data['run_on']
    resource.delayed_set(:config, resource_data) if resource_data['config']
    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.delayed_set(:consumer, resource_data)
    resource.delayed_set(:route, resource_data)
    resource.delayed_set(:service, resource_data)
    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
enabled_names(api_client: APIClient.instance) click to toggle source

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

# File lib/skull_island/resources/plugin.rb, line 54
def self.enabled_names(api_client: APIClient.instance)
  api_client.get("#{relative_uri}/enabled")['enabled_plugins']
end
schema(name, api_client: APIClient.instance) click to toggle source
# File lib/skull_island/resources/plugin.rb, line 58
def self.schema(name, api_client: APIClient.instance)
  api_client.get("/schemas/plugins/#{name}")
end

Public Instance Methods

digest_properties() click to toggle source
# File lib/skull_island/resources/plugin.rb, line 62
def digest_properties
  super.reject { |k| %i[run_on].include? k }
end
export(options = {}) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/skull_island/resources/plugin.rb, line 67
def export(options = {})
  hash = {
    'name' => name,
    'enabled' => enabled?,
    'config' => config.deep_sort.compact
  }
  hash['consumer'] = "<%= lookup :consumer, '#{consumer.username}' %>" if consumer
  hash['route'] = "<%= lookup :route, '#{route.name}' %>" if route
  hash['service'] = "<%= lookup :service, '#{service.name}' %>" if service
  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
modified_existing?() click to toggle source

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

# File lib/skull_island/resources/plugin.rb, line 88
def modified_existing?
  return false unless new?

  # Find plugins of the same name
  same_name = self.class.where(:name, name)
  return false if same_name.size.zero?

  same_name_and_consumer = consumer ? same_name.where(:consumer, consumer) : nil
  same_name_and_route = route ? same_name.where(:route, route) : nil
  same_name_and_service = service ? same_name.where(:service, service) : nil

  existing = if same_name_and_consumer && same_name_and_consumer.size == 1
               same_name_and_consumer.first
             elsif same_name_and_route && same_name_and_route.size == 1
               same_name_and_route.first
             elsif same_name_and_service && same_name_and_service.size == 1
               same_name_and_service.first
             end
  if existing
    @entity['id'] = existing.id
    save
  else
    false
  end
end

Private Instance Methods

postprocess_config(value) click to toggle source
# File lib/skull_island/resources/plugin.rb, line 123
def postprocess_config(value)
  value.deep_sort.prune.compact
end
postprocess_consumer(value) click to toggle source
# File lib/skull_island/resources/plugin.rb, line 127
def postprocess_consumer(value)
  case value
  when Hash
    Consumer.new(
      entity: value,
      lazy: true,
      tainted: false,
      api_client: api_client
    )
  when String
    Consumer.new(
      entity: { 'id' => value },
      lazy: true,
      tainted: false,
      api_client: api_client
    )
  else
    value
  end
end
postprocess_route(value) click to toggle source
# File lib/skull_island/resources/plugin.rb, line 159
def postprocess_route(value)
  case value
  when Hash
    Route.new(
      entity: value,
      lazy: true,
      tainted: false,
      api_client: api_client
    )
  when String
    Route.new(
      entity: { 'id' => value },
      lazy: true,
      tainted: false,
      api_client: api_client
    )
  else
    value
  end
end
postprocess_service(value) click to toggle source
# File lib/skull_island/resources/plugin.rb, line 191
def postprocess_service(value)
  case value
  when Hash
    Service.new(
      entity: value,
      lazy: true,
      tainted: false,
      api_client: api_client
    )
  when String
    Service.new(
      entity: { 'id' => value },
      lazy: true,
      tainted: false,
      api_client: api_client
    )
  else
    value
  end
end
preprocess_config(input) click to toggle source

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

# File lib/skull_island/resources/plugin.rb, line 119
def preprocess_config(input)
  input.deep_sort
end
preprocess_consumer(input) click to toggle source
# File lib/skull_island/resources/plugin.rb, line 148
def preprocess_consumer(input)
  case input
  when Hash
    input
  when Consumer
    { 'id' => input.id }
  else
    input
  end
end
preprocess_route(input) click to toggle source
# File lib/skull_island/resources/plugin.rb, line 180
def preprocess_route(input)
  case input
  when Hash
    input
  when Route
    { 'id' => input.id }
  else
    input
  end
end
preprocess_service(input) click to toggle source
# File lib/skull_island/resources/plugin.rb, line 212
def preprocess_service(input)
  case input
  when Hash
    input
  when Service
    { 'id' => input.id }
  else
    input
  end
end
validate_config(value) click to toggle source

Used to validate {#config} on set

# File lib/skull_island/resources/plugin.rb, line 224
def validate_config(value)
  # only Hashes are allowed
  value.is_a?(Hash)
end
validate_consumer(value) click to toggle source

Used to validate {#consumer} on set

# File lib/skull_island/resources/plugin.rb, line 230
def validate_consumer(value)
  # allow either a Consumer object or a Hash of a specific structure
  value.is_a?(Consumer) || value.is_a?(Hash)
end
validate_route(value) click to toggle source

Used to validate {#route} on set

# File lib/skull_island/resources/plugin.rb, line 236
def validate_route(value)
  # allow either a Route object or a Hash of a specific structure
  value.is_a?(Route) || value.is_a?(Hash)
end
validate_run_on(value) click to toggle source

Used to validate {#run_on} on set

# File lib/skull_island/resources/plugin.rb, line 242
def validate_run_on(value)
  # allow either a Route object or a Hash of a specific structure
  %w[first second all].include?(value)
end
validate_service(value) click to toggle source

Used to validate {#service} on set

# File lib/skull_island/resources/plugin.rb, line 248
def validate_service(value)
  # allow either a Service object or a Hash of a specific structure
  value.is_a?(Service) || value.is_a?(Hash)
end