Module: Cyclid::API::Organizations::Configs

Defined in:
app/cyclid/controllers/organizations/config.rb

Overview

API endpoints for Organization specific configuration

Organizations collapse

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra callback



70
71
72
73
74
75
76
77
78
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
108
109
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
138
139
140
141
142
143
144
145
# File 'app/cyclid/controllers/organizations/config.rb', line 70

def self.registered(app)
  include Errors::HTTPErrors
  include Constants::JobStatus

  # Return a list of plugins which have configs
  app.get do
    authorized_for!(params[:name], Operations::READ)

    configs = []
    Cyclid.plugins.all.each do |plugin|
      configs << { type: plugin.human_name, name: plugin.name } \
        if plugin.config?
    end

    return configs.to_json
  end

  # Get the current configuration for the given plugin.
  app.get '/:type/:plugin' do
    authorized_for!(params[:name], Operations::READ)

    org = Organization.find_by(name: params[:name])
    halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
      if org.nil?

    Cyclid.logger.debug "type=#{params[:type]} plugin=#{params[:plugin]}"

    # Find the plugin
    plugin = Cyclid.plugins.find(params[:plugin], params[:type])
    halt_with_json_response(404, INVALID_PLUGIN, 'plugin does not exist') \
      if plugin.nil?

    # Ask the plugin for the current config for this organization. This
    # will include the config schema under the 'schema' attribute.
    begin
      config = plugin.get_config(org)

      halt_with_json_response(404, INVALID_PLUGIN_CONFIG, 'failed to get plugin config') \
        if config.nil?
    rescue StandardError => ex
      halt_with_json_response(404, \
                              INVALID_PLUGIN_CONFIG, \
                              "failed to get plugin config: #{ex}") \
        if config.nil?
    end

    return config.to_json
  end

  # Update the plugin configuration
  app.put '/:type/:plugin' do
    authorized_for!(params[:name], Operations::ADMIN)

    payload = parse_request_body
    Cyclid.logger.debug payload

    org = Organization.find_by(name: params[:name])
    halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
      if org.nil?

    # Find the plugin
    plugin = Cyclid.plugins.find(params[:plugin], params[:type])
    halt_with_json_response(404, INVALID_PLUGIN, 'plugin does not exist') \
      if plugin.nil?

    # Ask the plugin for the current config for this organization. This
    # will include the config schema under the 'schema' attribute.
    begin
      plugin.set_config(payload, org)
    rescue StandardError => ex
      halt_with_json_response(404, \
                              INVALID_PLUGIN_CONFIG, \
                              "failed to set plugin config: #{ex}") \
    end
  end
end

Instance Method Details

#GET(/organizations/: organization/configs) ⇒ Object

Get the list of plugins which support per-organization configurations.

Parameters:

  • organization (String)

    Name of the organization.

Returns:

  • The list of plugins.

  • (404)

    The organization or plugin does not exist.



# File 'app/cyclid/controllers/organizations/config.rb', line 28

#GET(/organizations/: organization/configs/:type/:plugin) ⇒ Object

Get the current configuration for the given plugin.

Examples:

Get the ‘example’ plugin configuration from the ‘example’ organization

GET /organizations/example/configs/type/example => {"id":1,
                                                    "plugin":"example",
                                                    "version":"1.0.0",
                                                    "config":{<plugin specific object>},
                                                    "organization_id":2,
                                                    "schema":[<plugin configuration schema>]}

Parameters:

  • organization (String)

    Name of the organization.

  • type (String)

    The plugin type E.g. ‘api’ for an API plugin, ‘source’ for a Source plugin etc.

  • plugin (String)

    Name of the plugin.

Returns:

  • The plugin configuration for the given plugin.

  • (404)

    The organization or plugin does not exist.



# File 'app/cyclid/controllers/organizations/config.rb', line 36

#PUT(/organizations/: organization/configs/:type/:plugin) ⇒ 200, 404

Update the plugin configuration

Parameters:

  • organization (String)

    Name of the organization.

  • type (String)

    The plugin type E.g. ‘api’ for an API plugin, ‘source’ for a Source plugin etc.

  • plugin (String)

    Name of the plugin.

Returns:

  • (200)

    The plugin configuration was updated.

  • (404)

    The organization or plugin does not exist.



# File 'app/cyclid/controllers/organizations/config.rb', line 54