class SkullIsland::Resources::Consumer
The Consumer
resource class
@see docs.konghq.com/1.1.x/admin-api/#consumer-object Consumer
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 rubocop:disable Metrics/MethodLength
# File lib/skull_island/resources/consumer.rb, line 21 def self.batch_import(data, verbose: false, test: false, project: nil, time: nil) raise(Exceptions::InvalidArguments) unless data.is_a?(Array) known_ids = [] # rubocop:disable Metrics/BlockLength data.each_with_index do |resource_data, index| resource = new resource.username = resource_data['username'] resource.custom_id = resource_data['custom_id'] 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.import_update_or_skip(index: index, verbose: verbose, test: test) known_ids << resource.id known_basic_auths = BasicauthCredential.batch_import( ( resource_data.dig('credentials', 'basic-auth') || [] ).map { |t| t.merge('consumer' => { 'id' => resource.id }) }, verbose: verbose, test: test ) known_jwt_auths = JWTCredential.batch_import( ( resource_data.dig('credentials', 'jwt') || [] ).map { |t| t.merge('consumer' => { 'id' => resource.id }) }, verbose: verbose, test: test ) known_key_auths = KeyauthCredential.batch_import( ( resource_data.dig('credentials', 'key-auth') || [] ).map { |t| t.merge('consumer' => { 'id' => resource.id }) }, verbose: verbose, test: test ) known_acls = AccessControlList.batch_import( ( resource_data['acls'] || [] ).map { |t| t.merge('consumer' => { 'id' => resource.id }) }, verbose: verbose, test: test ) next unless project basic_creds = BasicauthCredential.all.select { |c| c.consumer == resource } basic_creds.reject { |res| known_basic_auths.include?(res.id) }.map do |res| puts "[WARN] ! Removing #{res.class.name} (#{res.id})" res.destroy end jwt_creds = JWTCredential.all.select { |c| c.consumer == resource } jwt_creds.reject { |res| known_jwt_auths.include?(res.id) }.map do |res| puts "[WARN] ! Removing #{res.class.name} (#{res.id})" res.destroy end key_creds = KeyauthCredential.all.select { |c| c.consumer == resource } key_creds.reject { |res| known_key_auths.include?(res.id) }.map do |res| puts "[WARN] ! Removing #{res.class.name} (#{res.id})" res.destroy end acls = AccessControlList.all.select { |acl| acl.consumer == resource } acls.reject { |res| known_acls.include?(res.id) }.map do |res| puts "[WARN] ! Removing #{res.class.name} (#{res.id})" res.destroy end end # rubocop:enable Metrics/BlockLength cleanup_except(project, known_ids) if project known_ids end
Public Instance Methods
acls()
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/skull_island/resources/consumer.rb, line 103 def acls AccessControlList.where(:consumer, self, api_client: api_client) end
add_acl!(details)
click to toggle source
# File lib/skull_island/resources/consumer.rb, line 107 def add_acl!(details) r = case details when AccessControlList details when String resource = AccessControlList.new(api_client: api_client) resource.group = details resource else resource = AccessControlList.new(api_client: api_client) resource.group = details[:group] resource end r.consumer = self r.save end
add_credential!(details)
click to toggle source
# File lib/skull_island/resources/consumer.rb, line 124 def add_credential!(details) r = if [BasicauthCredential, JWTCredential, KeyauthCredential].include? details.class details elsif details.is_a?(Hash) && details.key?(:algorithm) cred = JWTCredential.new(api_client: api_client) cred.algorithm = details[:algorithm] cred.key = details[:key] if details[:key] cred.secret = details[:secret] if details[:secret] cred.rsa_public_key = details[:rsa_public_key] if details[:rsa_public_key] cred elsif details.is_a?(Hash) && details.key?(:key) cred = KeyauthCredential.new(api_client: api_client) cred.key = details[:key] cred elsif details.is_a?(Hash) && details.key?(:username) cred = BasicauthCredential.new(api_client: api_client) cred.username = details[:username] cred.password = details[:password] cred end r.consumer = self r.save end
credentials()
click to toggle source
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity
# File lib/skull_island/resources/consumer.rb, line 152 def credentials creds = {} keyauth_creds = KeyauthCredential.where(:consumer, self, api_client: api_client) creds['key-auth'] = keyauth_creds if keyauth_creds basicauth_creds = BasicauthCredential.where(:consumer, self, api_client: api_client) creds['basic-auth'] = basicauth_creds if basicauth_creds jwt_creds = JWTCredential.where(:consumer, self, api_client: api_client) creds['jwt'] = jwt_creds if jwt_creds creds end
export(options = {})
click to toggle source
rubocop:disable Metrics/AbcSize
# File lib/skull_island/resources/consumer.rb, line 169 def export(options = {}) hash = { 'username' => username, 'custom_id' => custom_id } creds = credentials_for_export hash['credentials'] = creds unless creds.empty? hash['acls'] = acls.map { |acl| acl.export(exclude: 'consumer') } unless acls.empty? 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:enable Metrics/AbcSize
# File lib/skull_island/resources/consumer.rb, line 185 def modified_existing? return false unless new? # Find consumers of the same username same_username = self.class.where(:username, username) existing = same_username.size == 1 ? same_username.first : nil if existing @entity['id'] = existing.id save else false end end
plugins()
click to toggle source
Provides a collection of related {Plugin} instances
# File lib/skull_island/resources/consumer.rb, line 164 def plugins Plugin.where(:consumer, self, api_client: api_client) end
Private Instance Methods
credentials_for_export()
click to toggle source
# File lib/skull_island/resources/consumer.rb, line 203 def credentials_for_export creds = {} unless credentials['key-auth'].empty? creds['key-auth'] = credentials['key-auth'].collect do |cred| cred.export(exclude: 'consumer') end end unless credentials['jwt'].empty? creds['jwt'] = credentials['jwt'].collect do |cred| cred.export(exclude: 'consumer') end end unless credentials['basic-auth'].empty? creds['basic-auth'] = credentials['basic-auth'].collect do |cred| cred.export(exclude: 'consumer') end end creds end