Class: SkullIsland::Resources::Plugin

Inherits:
SkullIsland::Resource show all
Defined in:
lib/skull_island/resources/plugin.rb

Overview

The Plugin 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, #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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/skull_island/resources/plugin.rb', line 20

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

  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.config = resource_data['config'].deep_sort if resource_data['config']
    resource.tags = resource_data['tags'] if resource_data['tags']
    resource.delayed_set(:consumer, resource_data, 'consumer')
    resource.delayed_set(:route, resource_data, 'route')
    resource.delayed_set(:service, resource_data, 'service')
    resource.import_update_or_skip(index: index, verbose: verbose, test: test)
  end
end

.enabled_names(api_client: APIClient.instance) ⇒ Object



37
38
39
# File 'lib/skull_island/resources/plugin.rb', line 37

def self.enabled_names(api_client: APIClient.instance)
  api_client.get("#{relative_uri}/enabled")['enabled_plugins']
end

.schema(name, api_client: APIClient.instance) ⇒ Object



41
42
43
# File 'lib/skull_island/resources/plugin.rb', line 41

def self.schema(name, api_client: APIClient.instance)
  api_client.get("#{relative_uri}/schema/#{name}")
end

Instance Method Details

#configObject

The config property



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

property :config, validate: true, preprocess: true, postprocess: true

#consumerObject

The consumer property



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

property :consumer, validate: true, preprocess: true, postprocess: true

#created_atObject

The created_at property



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

property :created_at, read_only: true, postprocess: true

#digest_propertiesObject



45
46
47
# File 'lib/skull_island/resources/plugin.rb', line 45

def digest_properties
  super.reject { |k| i[run_on].include? k }
end

#enabledObject

The enabled property



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

property :enabled, type: :boolean

#export(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/skull_island/resources/plugin.rb', line 49

def export(options = {})
  hash = {
    'name' => name,
    'enabled' => enabled?,
    'config' => config.deep_sort
  }
  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 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

#modified_existing?Boolean

rubocop:disable Metrics/PerceivedComplexity

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/skull_island/resources/plugin.rb', line 69

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 = same_name.where(:consumer, consumer)
  same_name_and_route = same_name.where(:route, route)
  same_name_and_service = same_name.where(:service, service)
  existing = if same_name_and_consumer.size == 1
               same_name_and_consumer.first
             elsif same_name_and_route.size == 1
               same_name_and_route.first
             elsif same_name_and_service.size == 1
               same_name_and_service.first
             end
  if existing
    @entity['id'] = existing.id
    save
  else
    false
  end
end

#nameObject

The name property



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

property :name

#routeObject

The route property



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

property :route, validate: true, preprocess: true, postprocess: true

#run_onObject

The run_on property



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

property :run_on, validate: true

#serviceObject

The service property



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

property :service, validate: true, preprocess: true, postprocess: true

#tagsObject

The tags property



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

property :tags, validate: true