module Eco module API class Session class Config class Apis < BaseConfig def apis self["apis"] ||= {} end def apis? apis.keys.length > 0 end def enviros apis.keys end def defined?(name) apis.key?(name) end def any_defined?(*names) [names].flatten.any? do |name| self.defined?(name) end end def add(name, key:, host:, version: :internal, mode: :local, user_key: nil, external_key: nil, email: nil, pass: nil, org_id: nil) apis[name] = Session::Config::Api.new( name, key: key, host: host, version: version, mode: mode, root: self, user_key: user_key, external_key: external_key, email: email, pass: pass, org_id: org_id ) self end def active_api self["active-api"] end def active_name=(name) raise "'#{name}' Api environment not defined" if !apis[name] self["active-name"] = name self["active-api"] = apis[name] self end def active_name self["active-name"] end # the active environment def active_root_name active_name end def service_up? @api_test ||= Session::Config::Api.api_class(active_api.version).new("foobar", host: active_api.host, logger: ::Logger.new(IO::NULL)) status = @api_test.client.get("/policy_groups").status # 401 Unauthorized "Permission denied. API key may be invalid." status == 401 end def api(logger = ::Logger.new(IO::NULL), version: nil) unless active_api raise "There's no 'active_api'. Use apis.active_name='api_enviro_name' to set the active api" end active_api.api(version: version, logger: logger) end def default_user_key=(key) self["user_key"] = key end def default_user_key self["user_key"] end def default_email=(email) self["default_email"] = email end def default_email self["default_email"] || ENV['USER_EMAIL'] end def default_pass=(pass) self["default_pass"] = pass end def default_pass self["default_pass"] || ENV['USER_PASS'] end # Method to support CLI one-off API requests def one_off if one_off? add(one_off_org, key: one_off_key, host: "#{one_off_enviro}.ecoportal.com") return one_off_org end end private def one_off? @is_one_off ||= SCR.get_arg("-api-key") || SCR.get_arg("-one-off") end def one_off_key return @one_off_key if instance_variable_defined?(:@one_off_key) if one_off? Dotenv.load("./.env_one_off") SCR.get_arg("-api-key", with_param: true).yield_self do |key| one_off_key_env(key) end end end def one_off_key_env(key) if one_off? if key env_file_set_var("./.env_one_off", one_off_key_env_var, key) key else Dotenv.load("./.env_one_off") ENV[one_off_key_env_var].tap do |k| raise "At least the first time, you should provide the -api-key" unless k end end end end def one_off_key_env_var @one_off_key_env_var ||= "#{one_off_org}_KEY" end def one_off_org return @one_off_org if instance_variable_defined?(:@one_off_org) unless org = SCR.get_arg("-org", with_param: true) raise("You should specify -org NAME when using -api-key or -one-off") end @one_off_org ||= "#{org.downcase.split(/[^a-z]+/).join("_")}_#{one_off_enviro.gsub(".", "_")}".to_sym end def one_off_enviro return @one_off_enviro if instance_variable_defined?(:@one_off_enviro) enviro = SCR.get_arg("-enviro") ? SCR.get_arg("-enviro", with_param: true) : "live" @one_off_enviro ||= enviro.downcase end def env_file_set_var(file, var, value) begin pattern = /"#{var}=(?[^ \r\n]+)"/ File.open(file, "w+") do |fd| found = false fd.each_line do |line| if match = line.match(pattern) found = true # IO::SEEK_CUR => Seeks to _amount_ plus current position fd.seek(-(line.length + 1), IO::SEEK_CUR) fd.write line.gsub(match[:value], value) end end fd << "#{var}=#{value}" unless found end rescue StandardError => e puts "#{e}" return false end return true end end end end end end