Class: SkullIsland::Resources::Consumer

Inherits:
SkullIsland::Resource show all
Includes:
Helpers::Meta
Defined in:
lib/skull_island/resources/consumer.rb

Overview

The Consumer resource class

Instance Attribute Summary

Attributes inherited from SkullIsland::Resource

#api_client, #entity, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Meta

#add_meta, #import_time, #import_time=, #metatags, #postprocess_tags, #preprocess_tags, #project, #project=, #raw_tags, #remove_meta, #supports_meta?

Methods inherited from SkullIsland::Resource

all, cleanup_except, 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, #digest_properties, #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, #recursive_erubi, #reload, #required_properties, #save, #save_uri, #supports_meta?, #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, project: nil, time: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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.dig('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
end

Instance Method Details

#aclsObject

rubocop:enable Metrics/MethodLength



101
102
103
# File 'lib/skull_island/resources/consumer.rb', line 101

def acls
  AccessControlList.where(:consumer, self, api_client: api_client)
end

#add_acl!(details) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/skull_island/resources/consumer.rb', line 105

def add_acl!(details)
  r = if details.is_a?(AccessControlList)
        details
      elsif details.is_a?(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) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/skull_island/resources/consumer.rb', line 121

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

#created_atObject

The created_at property



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

property :created_at, read_only: true, postprocess: true

#credentialsObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



149
150
151
152
153
154
155
156
157
158
# File 'lib/skull_island/resources/consumer.rb', line 149

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

#custom_idObject

The custom_id property



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

property :custom_id

#export(options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/skull_island/resources/consumer.rb', line 165

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?Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/skull_island/resources/consumer.rb', line 180

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

#pluginsObject

Provides a collection of related Plugin instances



161
162
163
# File 'lib/skull_island/resources/consumer.rb', line 161

def plugins
  Plugin.where(:consumer, self, api_client: api_client)
end

#tagsObject

The tags property



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

property :tags, validate: true, preprocess: true, postprocess: true

#usernameObject

The username property



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

property :username