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

.authorObject

Return the plugin author



49
50
51
# File 'app/cyclid/plugins.rb', line 49

def author
  @author || 'Unknown'
end

.config?Boolean

Does this plugin support configuration data?

Returns:

  • (Boolean)


74
75
76
# File 'app/cyclid/plugins.rb', line 74

def config?
  false
end

.config_schemaObject

Get the schema for the configuration data that the plugin stores



151
152
153
# File 'app/cyclid/plugins.rb', line 151

def config_schema
  {}
end

.default_configObject

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



146
147
148
# File 'app/cyclid/plugins.rb', line 146

def default_config
  {}
end

.get_config(org) ⇒ Object

Get the configuration for the given org



79
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 79

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

.homepageObject

Return a URL to the plugin homepage



59
60
61
# File 'app/cyclid/plugins.rb', line 59

def homepage
  @homepage || 'https://cyclid.io'
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

.licenseObject

Return the plugin license



54
55
56
# File 'app/cyclid/plugins.rb', line 54

def license
  @license || 'Unknown'
end

.metadataObject

Return plugin metadata



64
65
66
67
68
69
70
71
# File 'app/cyclid/plugins.rb', line 64

def 
  { name: @name,
    type: human_name,
    version: version,
    author: author,
    license: license,
    homepage: homepage }
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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/cyclid/plugins.rb', line 110

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.



141
142
143
# File 'app/cyclid/plugins.rb', line 141

def update_config(_current, _new)
  return false
end

.versionObject

Return the plugin version



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

def version
  @version || '1.0.0'
end