Class: Cyclid::API::Plugins::Base

Inherits:
Object
  • Object
show all
Defined in:
app/cyclid/plugins.rb

Overview

Base class for Plugins

Direct Known Subclasses

Action, Api, Builder, Dispatcher, Provisioner, Source, Transport

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'app/cyclid/plugins.rb', line 30

def name
  @name
end

Class Method Details

.config?Boolean

Does this plugin support configuration data?

Returns:

  • (Boolean)


44
45
46
# File 'app/cyclid/plugins.rb', line 44

def config?
  false
end

.config_schemaObject

Get the schema for the configuration data that the plugin stores



121
122
123
# File 'app/cyclid/plugins.rb', line 121

def config_schema
  {}
end

.default_configObject

Provide the default configuration state that should be used when creating a new config



116
117
118
# File 'app/cyclid/plugins.rb', line 116

def default_config
  {}
end

.get_config(org) ⇒ Object

Get the configuration for the given org



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/cyclid/plugins.rb', line 49

def get_config(org)
  # If the organization was passed by name, convert it into an Organization object
  org = Organization.find_by(name: org) if org.is_a? String
  raise 'organization does not exist' if org.nil?

  # XXX Plugins of different types can have the same name; we need to
  # add a 'type' field and also find by the type.
  config = org.plugin_configs.find_by(plugin: @name)
  if config.nil?
    # No config currently exists; create a new default config
    config = PluginConfig.new(plugin: @name,
                              version: '1.0.0',
                              config: Oj.dump(default_config.stringify_keys))
    config.save!

    org.plugin_configs << config
  end

  # Convert the model to a hash, add the config schema, and convert the JSON config
  # blob back into a hash
  config_hash = config.serializable_hash
  config_hash['schema'] = config_schema
  config_hash['config'] = Oj.load(config.config)

  return config_hash
rescue StandardError => ex
  Cyclid.logger.error "couldn't get/create plugin config for #{@name}: #{ex}"
  raise
end

.human_nameObject

Returns the ‘human’ name for the plugin type



33
34
35
# File 'app/cyclid/plugins.rb', line 33

def human_name
  'base'
end

.register_plugin(name) ⇒ Object

Add the (derived) plugin to the plugin registry



38
39
40
41
# File 'app/cyclid/plugins.rb', line 38

def register_plugin(name)
  @name = name
  Cyclid.plugins.register(self)
end

.set_config(new_config, org) ⇒ Object

Set the configuration for the given org



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/cyclid/plugins.rb', line 80

def set_config(new_config, org)
  new_config.stringify_keys!

  config = org.plugin_configs.find_by(plugin: @name)
  if config.nil?
    # No config currently exists; create a new default config
    config = PluginConfig.new(plugin: @name,
                              version: '1.0.0',
                              config: Oj.dump(default_config.stringify_keys))
    config.save!

    org.plugin_configs << config
  end

  # Let the plugin validate & merge the changes into the config hash
  config_hash = config.serializable_hash
  current_config = config_hash['config']
  Cyclid.logger.debug "current_config=#{current_config}"
  merged_config = update_config(Oj.load(current_config), new_config)

  raise 'plugin rejected the configuration' if merged_config == false

  Cyclid.logger.debug "merged_config=#{merged_config}"

  # Update the stored configuration
  config.config = Oj.dump(merged_config.stringify_keys)
  config.save!
end

.update_config(_current, _new) ⇒ Object

Validite the given configuration items and merge them into the correct configuration, returning an updated complete configuration that can be stored.



111
112
113
# File 'app/cyclid/plugins.rb', line 111

def update_config(_current, _new)
  return false
end