module Eco module API class Session class Config < Hash attr_reader :name def initialize(name = :default) super(nil) @name = name self["org"] = {} end # @!group Config instance pure methods def clone(name) keys.each_with_object(self.class.new(name)) do |key, cnf| begin cnf[key] = self[key].clone(config: cnf) rescue ArgumentError cnf[key] = self[key].clone end end end def reopen yield(self) end # @!endgroup # @!group Additional resources # Helper to manage `SFTP` files and folders. # @return [Eco::API::Session::Config::SFTP] def sftp self["sftp"] ||= Eco::API::Session::Config::SFTP.new(config: self) end # Helper to upload files and folders to `S3`. # @return [Eco::API::Session::Config::S3Storage] def s3storage self["s3_storage"] ||= Eco::API::Session::Config::S3Storage.new(config: self) end # Helper to send emails. # @return [Eco::API::Session::Config::Mailer] def mailer self["mailer"] ||= Eco::API::Session::Config::Mailer.new(config: self) end # Helper scope reporting structures. # @return [Eco::API::Session::Config::TagTree] def tagtree_config org["tagtree_config"] ||= Eco::API::Session::Config::TagTree.new(config: self) end # @!endgroup # @!group Logger # @return [Eco::API::Session::Config::Logger] def logger self["logger"] ||= Eco::API::Session::Config::Logger.new(config: self) end def log_console_level=(value) logger.console_level= value end def log_file_level=(value) logger.file_level = value end def log_file=(file) logger.file = file end def timestamp_console=(value) logger.timestamp_console = value end def log_connection=(value) logger.log_connection = value end # @!endgroup # @!group Session and API # @return [Eco::API::Session] the `session` linked to this `config` def session @session ||= Eco::API::Session.new(self) end # @return [Eco::API::Session::Config::Apis] def apis self["apis"] ||= Eco::API::Session::Config::Apis.new(config: self) end # @return [Boolean] `true` if there is any api configuration defined, `false` otherwise def apis? apis.apis? end # @param (see Eco::API::Session::Config::Apis#add) # @return [Eco::API::Session::Config] this configuration def add_api(name, **kargs) apis.add(name, **kargs) self end # Set the active api by `name` # @see Eco::API::Session::Config::Apis#active_api= # @return [Eco::API::Session::Config] this configuration def active_api(name) apis.active_name = name self end # @see Eco::API::Session::Config::Apis#active_root_name def active_enviro apis.active_root_name end # @see Eco::API::Session::Config::Apis#api # @return [Eco::API::Session::Config::Api] the currently active api def api(logger = ::Logger.new(IO::NULL), version: nil) apis.api(logger, version: version) end # Sets the `mode` of the active api # @see Eco::API::Session::Config::Api#mode # @param (see Eco::API::Session::Config::Api#mode) def run_mode=(mode) apis.active_api.mode = mode end def run_mode_local? apis.active_api.local? end def run_mode_remote? apis.active_api.remote? end # @deprecated old helper to fix the dry-run mode # @note this is now done via `options[:dry_run]`, parsed as an option def dry_run! self["dry-run"] = true end # @deprecated old helper to check if we are in dry-run mode # @note this is now done via `options[:dry_run]`, which is parsed as an option def dry_run? self["dry-run"] end # @!endgroup # @!group Files # @return [Eco::API::Session::Config::Files] def files self["files"] ||= Eco::API::Session::Config::Files.new(config: self) end # Defines in the base folder from where files are expected to be found when relative paths are used def working_directory=(path) files.working_directory = path end def working_directory(mode: nil) return files.working_directory if apis.active_api&.one_off? unless mode wd = files.working_directory return wd unless wd.to_s.strip.empty? end mode ||= :active_api if mode == :active_api apis.active_root_name elsif mode == :raw_api_name apis.active_name else files.working_directory end end def file_timestamp_pattern=(pattern) files.timestamp_pattern = pattern end def file_manager Eco::API::Common::Session::FileManager.new(self) end def require(file = nil, match: nil) begin if match file_manager.dir.dir_files(pattern: match).each do |file| require_relative File.expand_path(file) end else target = File.expand_path(file_manager.dir.file(file)) require_relative target end rescue LoadError => e if apis.active_api.one_off? pp e.to_s else raise end end end # @!endgroup # @!group Organization related shortcuts def org self["org"] end def location_codes=(file) org["location_codes"] = file end def location_codes org["location_codes"] end def locations_mapper return @locations_mapper if instance_variable_defined?(:@locations_mapper) file = file_manager.newest(location_codes) @locations_mapper = Eco::Data::Mapper.new(file_manager.load_json(file), internal: :first) end def tagtree=(file) tagtree_config.file = file end # It uses the `tagtree.json` file and in its absence, if `graphql` enabled, the largest `life_tagtree` # @return [Eco::API::Organization::TagTree] def tagtree(enviro: nil) @tagtree ||= tagtree_config.scope_tree(enviro: enviro) end # It obtains the first of the live tagtree in the org # @note it requires graphql connection configuration parameters # @return [Eco::API::Organization::TagTree] def live_tree(id: nil, enviro: nil, **kargs, &block) if id tagtree_config.live_tree_get(id: id, enviro: enviro, **kargs, &block) else tagtree_config.live_tree(enviro: enviro, **kargs, &block) end end # @return [Eco::API::Organization::PolicyGroups] def policy_groups return @policy_groups if instance_variable_defined?(:@policy_groups) pgs = api&.policy_groups.to_a @policy_groups = Eco::API::Organization::PolicyGroups.new(pgs) end # @return pEco::API::Organization::PersonSchemas def schemas return @schemas if instance_variable_defined?(:@schemas) schs = api&.person_schemas.to_a @schemas = Eco::API::Organization::PersonSchemas.new(schs) end # @return [Eco::API::Organization::LoginProviders] def login_providers return @login_providers if instance_variable_defined?(:@login_providers) provs = api&.login_providers.to_a @login_providers = Eco::API::Organization::LoginProviders.new(provs) end # @!endgroup # @!group People shortcuts # @return [Eco::API::Session::Config::People] def people self["people"] ||= Eco::API::Session::Config::People.new(config: self) end # Define the default usergroup that should be given to people with no usergroups. def default_usergroup=(value) people.default_usergroup = value end # Specify the file that holds the `csv` with people to be excluded from `API` updates. def discarded_people_file=(value) people.discarded_file = value end # Set the base folder/name.ext name of the fieles where people will be cached. def people_cache=(file) people.cache = file end # Set the base folder name where requests launched to the server will be saved for future reference. def requests_backup_folder=(folder) people.requests_folder = folder end # Specify the `.json` file name with the mappings [`DataInputColumnName`, `internal-name`]. def person_fields_mapper=(file) people.fields_mapper = file end # Set the **default schema** this `api` org configuration should work on. def default_schema=(name) people.default_schema = name end # Set the **default login method** that users should acquire. def default_login_method=(name) people.default_login_method = name end # @see Eco::API::Session::Config::People # @param (see Eco::API::Session::Config::People) # @return [Eco::API::Common::People::PersonParser] parser/serializer for the defined `format`. def person_parser(format: :csv, &block) people.parser(format: format, &block) end # @!endgroup # @!group Session workflow and batch job launces # @return [Eco::API::UseCases] def usecases @usecases = self["usecases"] ||= Eco::API::UseCases.new if block_given? yield(@usecases) self else @usecases end end # @return [Eco::API::Session::Config::PostLaunch] def post_launch self["post_launch"] ||= Eco::API::Session::Config::PostLaunch.new(config: self) end # @return [Eco::API::Policies] def policies @policies = self["policies"] ||= Eco::API::Policies.new if block_given? yield(@policies) self else @policies end end # @return [Eco::API::Session::Batch::Policies] def batch_policies @batch_policies = self["batch_policies"] ||= Eco::API::Session::Batch::Policies.new("batch_policy") if block_given? yield(@batch_policies) self else @batch_policies end end # @return [Eco::API::Error::Handlers] def error_handlers @error_handlers = self["error_handlers"] ||= Eco::API::Error::Handlers.new if block_given? yield(@error_handlers) self else @error_handlers end end # @return [Eco::API::Session::Config::Workflow] def workflow @workflow = self["workflow"] ||= Eco::API::Session::Config::Workflow.new(config: self) @workflow.tap do |wf| yield(wf) if block_given? end end # @return [nil, Interger] seconds between jobs def delay_between_jobs(seconds = nil) self["delay_between_jobs"] = seconds if seconds self["delay_between_jobs"] end # @return [nil, Interger] seconds between job groups def delay_between_job_groups(seconds = nil) self["delay_between_job_groups"] = seconds if seconds self["delay_between_job_groups"] end # @!endgroup end end end end require_relative 'config/base_config' require_relative 'config/api' require_relative 'config/apis' require_relative 'config/logger' require_relative 'config/mailer' require_relative 'config/sftp' require_relative 'config/s3_storage' require_relative 'config/files' require_relative 'config/people' require_relative 'config/tagtree' require_relative 'config/post_launch' require_relative 'config/workflow'