eco-helpers 1.0.13 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eco/api/common/people/entry_factory.rb +3 -1
  3. data/lib/eco/api/common/people/person_attribute_parser.rb +33 -10
  4. data/lib/eco/api/common/people/person_entry.rb +1 -1
  5. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +1 -1
  6. data/lib/eco/api/common/people/person_factory.rb +5 -1
  7. data/lib/eco/api/common/session/environment.rb +7 -3
  8. data/lib/eco/api/common/session/mailer.rb +4 -0
  9. data/lib/eco/api/common/session/sftp.rb +4 -3
  10. data/lib/eco/api/error.rb +1 -0
  11. data/lib/eco/api/organization/presets_factory.rb +1 -1
  12. data/lib/eco/api/organization/tag_tree.rb +1 -1
  13. data/lib/eco/api/session.rb +119 -74
  14. data/lib/eco/api/session/batch.rb +23 -25
  15. data/lib/eco/api/session/batch/base_policy.rb +283 -0
  16. data/lib/eco/api/session/batch/errors.rb +17 -3
  17. data/lib/eco/api/session/batch/feedback.rb +112 -0
  18. data/lib/eco/api/session/batch/job.rb +90 -87
  19. data/lib/eco/api/session/batch/policies.rb +22 -0
  20. data/lib/eco/api/session/batch/request_stats.rb +195 -0
  21. data/lib/eco/api/session/batch/status.rb +66 -19
  22. data/lib/eco/api/session/config.rb +10 -0
  23. data/lib/eco/api/session/config/workflow.rb +1 -1
  24. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +4 -3
  25. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +15 -10
  26. data/lib/eco/cli/config/default/filters.rb +3 -2
  27. data/lib/eco/cli/config/default/options.rb +12 -0
  28. data/lib/eco/cli/config/default/usecases.rb +6 -4
  29. data/lib/eco/cli/config/default/workflow.rb +3 -2
  30. data/lib/eco/cli/scripting/args_helpers.rb +1 -1
  31. data/lib/eco/version.rb +1 -1
  32. metadata +5 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e68122d88dbb1cf0eb770576a4fbd72b5f11b5edb0f80b2889179bf7c5e88cf9
4
- data.tar.gz: 8daf469f45bf214edc30a547e0b72920cb000b58b91f7eda812e4ded9ef49e5c
3
+ metadata.gz: 8187e0511f185e36a43a0e123938b1062045b812d747800b34e098a2c0b1bf62
4
+ data.tar.gz: a6f591af8ab1998a9476903e056a0f97c0ecaaaabbfdea87ceb31ef2a165570e
5
5
  SHA512:
6
- metadata.gz: a2c159a86d73d46b22c23f3af90a57150b817daa6d776100455684d03bcb9eba981dd2a7f6119fe7a83d8b39f8ba570f10b2ec132973eb804b38eedbd84e541f
7
- data.tar.gz: 35307777c1a5391c02ee847576e4bb2a4bf6a5f8371ee977ea2735c585fe23c90c19bfba1ddcc6d5a8cb9e64baa58c22838e1ae69c3709284db2e787412b4727
6
+ metadata.gz: d8b11d2f308202c4b122445ea348e7219dfe7a156f5e5d5fca7b3605c38215a1b8216a5447dce43626afa41cf28973459436594b520b4d4a491a776dc1a3e298
7
+ data.tar.gz: ee27de5eb9c71d394d6bc28e1e3966f8d3c88b4aeb0603c56e724b4865070bddef79175ed3b16c4aaafeb6d1474bb0fc2b768c4814039c61ba277d4b211cbf5e
@@ -2,10 +2,12 @@ module Eco
2
2
  module API
3
3
  module Common
4
4
  module People
5
+ # Helper factory class to generate entries (input entries).
6
+ # @attr_reader schema [Ecoportal::API::V1::PersonSchema] person schema to be used in this entry factory
5
7
  # @attr_reader person_parser [Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
6
8
  class EntryFactory
7
9
 
8
- attr_reader :person_parser
10
+ attr_reader :schema, :person_parser
9
11
 
10
12
  # @param schema [Ecoportal::API::V1::PersonSchema] schema of person details that the parser will be based upon.
11
13
  # @param person_parser [nil, Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
@@ -26,25 +26,48 @@ module Eco
26
26
  @active_when.call(source_data)
27
27
  end
28
28
 
29
+ # Helper to build the `active_when` condition
30
+ def active_when_any?(*attrs)
31
+ Proc.new do |source_data|
32
+ keys = data_keys(source_data)
33
+ attrs.any? {|key| keys.include?(key)}
34
+ end
35
+ end
36
+
37
+ # Helper to build the `active_when` condition
38
+ def active_when_all?(*attrs)
39
+ Proc.new do |source_data|
40
+ keys = data_keys(source_data)
41
+ attrs.all? {|key| keys.include?(key)}
42
+ end
43
+ end
44
+
29
45
  private
30
46
 
31
47
  # by default, an attribute paraser is active if in the entry to parse
32
48
  # the internal attribute is present
33
49
  def attribute_present(active_when)
34
50
  Proc.new do |source_data|
35
- case source_data
36
- when Array
37
- keys = source_data
38
- when Hash
39
- keys = source_data.keys
40
- else
41
- keys = []
42
- end
43
-
44
- (source_data && keys.include?(self.attr)) ||
51
+ data_keys(source_data).include?(self.attr) ||
45
52
  (active_when && active_when.call(source_data))
46
53
  end
47
54
  end
55
+
56
+ # Helper to obtain the current internal named attributes of the data
57
+ # @param source_data [Array<String>, Hash] if `Array` those are already the `keys`, if `Hash` it gets the `keys`
58
+ # @return [Array<String>] `keys` of `source_data`
59
+ def data_keys(source_data)
60
+ case source_data
61
+ when Array
62
+ keys = source_data
63
+ when Hash
64
+ keys = source_data.keys
65
+ else
66
+ keys = []
67
+ end
68
+ end
69
+
70
+
48
71
  end
49
72
  end
50
73
  end
@@ -384,7 +384,7 @@ module Eco
384
384
 
385
385
  def fatal(msg)
386
386
  logger.fatal(msg)
387
- exit
387
+ raise msg
388
388
  end
389
389
 
390
390
  # HELPERS
@@ -202,7 +202,7 @@ module Eco
202
202
 
203
203
  def fatal(msg)
204
204
  logger.fatal(msg)
205
- exit
205
+ raise msg
206
206
  end
207
207
 
208
208
  end
@@ -2,9 +2,13 @@ module Eco
2
2
  module API
3
3
  module Common
4
4
  module People
5
+
6
+ # Helper factory to build `Ecoportal::API::V1::Person` or `Ecoportal::API::Internal::Person` objects
7
+ # @attr_reader schema [Ecoportal::API::V1::PersonSchema] person schema to be used in this person factory
8
+ # @attr_reader schema_attrs [Array<String>] _inernal names_ of the schema fields/attributes
5
9
  class PersonFactory
6
10
 
7
- attr_reader :schema_attrs
11
+ attr_reader :schema, :schema_attrs
8
12
 
9
13
  def initialize(person: {}, schema: {}, account: {}, modifier: Common::People::PersonModifier.new)
10
14
  @modifier = Common::People::PersonModifier.new(modifier)
@@ -13,6 +13,8 @@ module Eco
13
13
 
14
14
  alias_method :fm, :file_manager
15
15
 
16
+ #@param init [Eco::API::Common::Session::Environment] object to ini the session environment
17
+ #@param session [Eco::API::Session, nil] the current session
16
18
  def initialize(init = {}, session:)
17
19
  init = init.conf if init.is_a?(Environment)
18
20
  msg = "Expected object Eco::API::Session::Config or Environment. Given: #{init}"
@@ -23,8 +25,6 @@ module Eco
23
25
  @session = session
24
26
  @file_manager = Eco::API::Common::Session::FileManager.new(enviro: self)
25
27
  @logger = Eco::API::Common::Session::Logger.new(enviro: self)
26
-
27
- new_api
28
28
  end
29
29
 
30
30
  def mailer
@@ -34,11 +34,15 @@ module Eco
34
34
  def sftp
35
35
  @sftp ||= Eco::API::Common::Session::SFTP.new(enviro: self)
36
36
  end
37
-
37
+
38
38
  def s3upoader
39
39
  @s3uploader ||= Eco::API::Common::Session::S3Uploader.new(enviro: self)
40
40
  end
41
41
 
42
+ def api
43
+ @api || new_api
44
+ end
45
+
42
46
  def new_api
43
47
  return nil unless config.apis.active_api
44
48
 
@@ -12,6 +12,10 @@ module Eco
12
12
  @enviro = enviro
13
13
  end
14
14
 
15
+ # Sends an email
16
+ # @param to [String] destination email address
17
+ # @param subject [String] subject of the email
18
+ # @param body [String] `html` or plain text message
15
19
  def mail(to:, subject:, body:)
16
20
  ses.send_email(
17
21
  destination: {
@@ -23,8 +23,9 @@ module Eco
23
23
  non_interactive: true
24
24
  )
25
25
  rescue Exception => e
26
- logger.error("Could not open SFTP session. Possible misconfiguration: #{e}")
27
- exit(1)
26
+ msg = "Could not open SFTP session. Possible misconfiguration: #{e}"
27
+ logger.error(msg)
28
+ raise msg
28
29
  end
29
30
  @sftp_session
30
31
  end
@@ -57,7 +58,7 @@ module Eco
57
58
  end
58
59
 
59
60
  # @see Net::SFTP::Session#rename
60
- def move(fullname_source, fullname_dest, flags=nil, &callback)
61
+ def move(fullname_source, fullname_dest, flags=0x0001, &callback)
61
62
  sftp_session.rename!(fullname_source, fullname_dest, flags, &callback)
62
63
  end
63
64
 
data/lib/eco/api/error.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Eco
2
2
  module API
3
+ # To identify api server errors
3
4
  class Error < Exception
4
5
  class UnknownErrorClass < Exception
5
6
  def initialize(msg = nil, klass:)
@@ -94,7 +94,7 @@ module Eco
94
94
  def fatal(msg)
95
95
  raise msg if !@enviro
96
96
  @enviro.logger.fatal(msg)
97
- exit
97
+ raise msg
98
98
  end
99
99
 
100
100
  def warn(msg)
@@ -184,7 +184,7 @@ module Eco
184
184
  def fatal(msg)
185
185
  raise msg if !@enviro
186
186
  @enviro.logger.fatal(msg)
187
- exit
187
+ raise msg
188
188
  end
189
189
 
190
190
  def warn(msg)
@@ -1,14 +1,9 @@
1
1
  module Eco
2
2
  module API
3
+ # Class to manage the current session.
4
+ # Central helper of resources.
3
5
  class Session < Common::Session::BaseSession
4
- attr_reader :batch
5
- attr_accessor :schema
6
-
7
- # Class to manage the current session.
8
- # Central helper of resources.
9
- #
10
- # @attr_reader batch [Eco::API::Session::Batch] provides helper to launch batch operations.
11
- # @attr_reader schema [Ecoportal::API::V1::PersonSchema] currently active schema.
6
+ #@param init [Eco::API::Session::Config, Eco::API::Common::Session::Environment] object to ini the session
12
7
  def initialize(init = {})
13
8
  e = init
14
9
  msg = "Expected object Eco::API::Session::Config or Eco::API::Common::Session::Environment. Given: #{init}"
@@ -16,43 +11,21 @@ module Eco
16
11
  e = Eco::API::Common::Session::Environment.new(init, session: self) if !e.is_a?(Eco::API::Common::Session::Environment)
17
12
  super(e)
18
13
 
19
- logger.debug("LINE COMMAND: #{$0} #{ARGV.join(" ")}")
20
-
21
- @batch = Batch.new(enviro)
22
- @task = Task.new(enviro)
23
-
24
- self.schema = config.people.default_schema || schemas.first
14
+ @entry_factories = {}
15
+ @person_factories = {}
25
16
 
26
- presets_custom_file = config.people.presets_custom
27
- presets_map_file = config.people.presets_map
28
-
29
- @presets_factory = Eco::API::Organization::PresetsFactory.new({
30
- presets_custom: file_manager.dir.file(presets_custom_file, should_exist: true),
31
- presets_map: file_manager.dir.file(presets_map_file, should_exist: true),
32
- enviro: enviro
33
- })
17
+ logger.debug("LINE COMMAND: #{$0} #{ARGV.join(" ")}")
34
18
  end
35
19
 
36
20
  # Helper to perform multiple operations in one go.
37
21
  # @return [Eco::API::Session::Task] provides shortcuts to manage certain kind of operations.
38
22
  def do
39
- @task
23
+ @task ||= Task.new(enviro)
40
24
  end
41
25
 
42
- def mail_to(**kargs)
43
- mail.mail(**kargs)
44
- end
45
-
46
- def s3upload(content: nil, file: nil, directory: nil)
47
- if content && file
48
- s3uploader.upload(file, content)
49
- elsif file
50
- s3uploader.upload_file(file)
51
- elsif directory
52
- s3uploader.upload_directory(directory)
53
- else
54
- logger.error("To use Session.s3upload, you must specify either directory, file or content and file name")
55
- end
26
+ # @return [Eco::API::Session::Batch] provides helper to launch batch operations.
27
+ def batch
28
+ @batch ||= Batch.new(enviro)
56
29
  end
57
30
 
58
31
  # @see Eco::API::Session::Config#policy_groups
@@ -75,40 +48,67 @@ module Eco
75
48
  config.schemas
76
49
  end
77
50
 
51
+ # @return [String, Ecoportal::API::V1::PersonSchema] current active session's schema
52
+ def schema
53
+ self.schema = config.people.default_schema || schemas.first unless @schema
54
+ @schema
55
+ end
56
+
78
57
  # Sets the current target `PersonSchema` of this session.
79
58
  # @note observe that it is essential for the parsers/serialisers to identify target/present attributes.
80
59
  # @param schema [String, Ecoportal::API::V1::PersonSchema] where `String` can be the _name_ or the _id_ of the schema.
81
60
  def schema=(value)
82
- case value
83
- when String
84
- unless @schema = schemas.schema(value)
85
- fatal "The schema with id or name '#{value}' does not exist"
86
- end
87
- when Ecoportal::API::V1::PersonSchema
88
- @schema = value
61
+ @schema = to_schema(value)
62
+ self
63
+ end
64
+
65
+ # Builds the presets using the usergroup ids of the input.
66
+ # @note for each flag/ability it will take the highest among those mapped for the present usergroups.
67
+ # @param [Ecoportal::API::Internal::Person, Array<String>] the array should be of usegroup names or ids.
68
+ # @return [Hash] with custom presets.
69
+ def new_preset(input)
70
+ case input
71
+ when Ecoportal::API::Internal::Person
72
+ presets_factory.new(*input&.account&.policy_group_ids)
73
+ when Array
74
+ presets_factory.new(*input)
89
75
  else
90
- logger.error("Session: Missuse of 'schema=' method. Given: #{value}")
91
- return
76
+ presets_factory.new(input)
92
77
  end
78
+ end
93
79
 
94
- @person_factory = Eco::API::Common::People::PersonFactory.new(schema: @schema)
80
+ # Helper to obtain a EntryFactory
81
+ # @param schema [String, Ecoportal::API::V1::PersonSchema] `schema` to which associate the EntryFactory,
82
+ # where `String` can be the _name_ or the _id_ of the schema.
83
+ # @return [Eco::API::Common::People::EntryFactory] associated to `schema`.
84
+ # If `schema` is `nil` or not provided it uses the currently associated to the `session`
85
+ def entry_factory(schema: nil)
86
+ schema = to_schema(schema) || self.schema
87
+ return @entry_factories[schema&.id] if @entry_factories.key?(schema&.id)
95
88
 
96
89
  # TODO: check PersonEntry#to_internal and #to_external in init_attr_trackers
97
90
  # => when attr_map is avoided, it doesn't work as it should
91
+ mappings = []
98
92
  if map_file = config.people.fields_mapper
99
93
  mappings = map_file ? file_manager.load_json(map_file) : []
100
- else
101
- mappings = []
102
94
  end
103
- attr_map = Eco::Data::Mapper.new(mappings)
104
95
 
105
- @entry_factory = Eco::API::Common::People::EntryFactory.new({
106
- schema: @schema,
96
+ @entry_factories[schema&.id] = Eco::API::Common::People::EntryFactory.new({
97
+ schema: schema,
107
98
  person_parser: config.people.parser,
108
- attr_map: attr_map,
99
+ attr_map: Eco::Data::Mapper.new(mappings),
109
100
  logger: logger
110
101
  })
111
- self
102
+ end
103
+
104
+ # Helper to obtain a PersonFactory
105
+ # @param schema [String, Ecoportal::API::V1::PersonSchema] `schema` to which associate the PersonFactory,
106
+ # where `String` can be the _name_ or the _id_ of the schema.
107
+ # @return [Eco::API::Common::People::PersonFactory] associated to `schema`.
108
+ # If `schema` is `nil` or not provided it uses the currently associated to the `session`
109
+ def person_factory(schema: nil)
110
+ schema = to_schema(schema) || self.schema
111
+ @person_factories[schema&.id] ||= Eco::API::Common::People::PersonFactory.new(schema: schema)
112
112
  end
113
113
 
114
114
  # Allows to use the defined parsers
@@ -116,48 +116,33 @@ module Eco
116
116
  # @param attr [String] type (`Symbol`) or attribute (`String`) to target a specific parser.
117
117
  # @param source [Any] source value to be parsed.
118
118
  def parse_attribute(attr, source)
119
- unless parsers = @entry_factory.person_parser
119
+ unless parsers = entry_factory.person_parser
120
120
  raise "There are no parsers defined"
121
121
  end
122
122
  parsers.parse(attr, source)
123
123
  end
124
124
 
125
- # Builds the presets using the usergroup ids of the input.
126
- # @note for each flag/ability it will take the highest among those mapped for the present usergroups.
127
- # @param [Ecoportal::API::Internal::Person, Array<String>] the array should be of usegroup names or ids.
128
- # @return [Hash] with custom presets.
129
- def new_preset(input)
130
- case input
131
- when Ecoportal::API::Internal::Person
132
- @presets_factory.new(*input&.account&.policy_group_ids)
133
- when Array
134
- @presets_factory.new(*input)
135
- else
136
- @presets_factory.new(input)
137
- end
138
- end
139
-
140
125
  def export(*args)
141
- @entry_factory.export(*args)
126
+ entry_factory.export(*args)
142
127
  end
143
128
 
144
129
  # @see Eco::API::Common::People::EntryFactory#new
145
130
  # @return [Ecoportal::API::Internal::Person]
146
131
  def new_person(**keyed_args)
147
- @person_factory.new(**keyed_args)
132
+ person_factory.new(**keyed_args)
148
133
  end
149
134
 
150
135
  # Builds the entry for the given data.
151
136
  # @see Eco::API::Common::People::EntryFactory#new
152
137
  # @return [Eco::API::Common::People::PersonEntry] parsed entry.
153
138
  def new_entry(data, dependencies: {})
154
- @entry_factory.new(data, dependencies: dependencies)
139
+ entry_factory.new(data, dependencies: dependencies)
155
140
  end
156
141
 
157
142
  # @see Eco::API::Common::People::EntryFactory#entries
158
143
  # @return [Eco::API::Common::People::Entries] collection of entries.
159
144
  def entries(*args)
160
- @entry_factory.entries(*args).tap do |collection|
145
+ entry_factory.entries(*args).tap do |collection|
161
146
  logger.info("Loaded #{collection.length} input entries.")
162
147
  end
163
148
  end
@@ -216,6 +201,66 @@ module Eco
216
201
  job_groups.launch(simulate: simulate)
217
202
  end
218
203
 
204
+ # Sends an email
205
+ # @see Eco::API::Common::Session::Mailer#mail
206
+ def mail_to(**kargs)
207
+ mail.mail(**kargs)
208
+ end
209
+
210
+ # Uploads content into a file, a file or a directory to S3
211
+ # @see Eco::API::Common::Session::S3Uploader#upload
212
+ # @see Eco::API::Common::Session::S3Uploader#upload_file
213
+ # @see Eco::API::Common::Session::S3Uploader#upload_directory
214
+ # @param content [String] content to be uploaded (requires `file`)
215
+ # @param file [String] name of the file to be uploaded
216
+ # @param directory [String] name of source directory to be uploaded
217
+ def s3upload(content: nil, file: nil, directory: nil)
218
+ if content && file
219
+ s3uploader.upload(file, content)
220
+ elsif file
221
+ s3uploader.upload_file(file)
222
+ elsif directory
223
+ s3uploader.upload_directory(directory)
224
+ else
225
+ logger.error("To use Session.s3upload, you must specify either directory, file or content and file name")
226
+ end
227
+ end
228
+
229
+ private
230
+
231
+ # Helper to state the abilities that a person should have with given their usergroups
232
+ def presets_factory
233
+ @presets_factory ||= Eco::API::Organization::PresetsFactory.new({
234
+ presets_custom: file_manager.dir.file(config.people.presets_custom, should_exist: true),
235
+ presets_map: file_manager.dir.file(config.people.presets_map, should_exist: true),
236
+ enviro: enviro
237
+ })
238
+ end
239
+
240
+ # Comparer to state if 2 schemas are different
241
+ def same_schema? (schema1, schema2)
242
+ eq = schema1&.id == schema2&.id
243
+ eq ||= schema1&.name&.downcase == schema2&.name&.downcase
244
+ schema1 && schema2 && eq
245
+ end
246
+
247
+ # from schema `id` or `name` to a PersonSchema object
248
+ def to_schema(value)
249
+ return nil unless value
250
+ sch = nil
251
+ case value
252
+ when String
253
+ unless sch = schemas.schema(value)
254
+ fatal "The schema with id or name '#{value}' does not exist."
255
+ end
256
+ when Ecoportal::API::V1::PersonSchema
257
+ sch = value
258
+ else
259
+ fatal "Required String or Ecoportal::API::V1::PersonSchema. Given: #{value}"
260
+ end
261
+ sch
262
+ end
263
+
219
264
  end
220
265
  end
221
266
  end