Class: ZuoraConnect::AppInstanceBase

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/zuora_connect/app_instance_base.rb

Direct Known Subclasses

AppInstance

Constant Summary collapse

REFRESH_TIMEOUT =

Used to determine how long to wait on current refresh call before executing another

2.minute
INSTANCE_REFRESH_WINDOW =

Used to set how how long till app starts attempting to refresh cached task connect data

1.hours
INSTANCE_REDIS_CACHE_PERIOD =

Used to determine how long to cached task data will live for

24.hours
API_LIMIT_TIMEOUT =

Used to set the default for expiring timeout when api rate limiting is in effect

2.minutes
BLANK_OBJECT_ID_LOOKUP =
'BlankValueSupplied'
@@telegraf_host =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



799
800
801
802
803
804
805
806
# File 'app/models/zuora_connect/app_instance_base.rb', line 799

def method_missing(method_sym, *arguments, &block)
  if method_sym.to_s.include?("login")
    Rails.logger.fatal("Method Missing #{method_sym}")
    Rails.logger.fatal("Instance Data: #{self.task_data}")
    Rails.logger.fatal("Instance Logins: #{self.logins}")
  end
  super
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def api_version
  @api_version
end

#connect_userObject

Returns the value of attribute connect_user.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def connect_user
  @connect_user
end

#drop_messageObject

Returns the value of attribute drop_message.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def drop_message
  @drop_message
end

#last_refreshObject

Returns the value of attribute last_refresh.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def last_refresh
  @last_refresh
end

#loginsObject

Returns the value of attribute logins.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def logins
  @logins
end

#modeObject

Returns the value of attribute mode.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def mode
  @mode
end

#new_session_messageObject

Returns the value of attribute new_session_message.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def new_session_message
  @new_session_message
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def options
  @options
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def password
  @password
end

#s3_clientObject

START S3 Helping Methods #####



712
713
714
# File 'app/models/zuora_connect/app_instance_base.rb', line 712

def s3_client
  @s3_client
end

#task_dataObject

Returns the value of attribute task_data.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def task_data
  @task_data
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'app/models/zuora_connect/app_instance_base.rb', line 7

def username
  @username
end

Class Method Details

.decrypt_response(resp) ⇒ Object



790
791
792
# File 'app/models/zuora_connect/app_instance_base.rb', line 790

def self.decrypt_response(resp)
  OpenSSL::PKey::RSA.new(ZuoraConnect.configuration.private_key).private_decrypt(resp)
end

.get_metrics(type) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/models/zuora_connect/app_instance_base.rb', line 170

def self.get_metrics(type)
  namespace = ENV['DEIS_APP'].present? ? "#{ENV['DEIS_APP']}" : "#{Rails.application.class.parent_name}"

  @data = {}

  if type == "versions"
    @data = {
      app_name: namespace,
      url: "dummy",
      Version_Gem: ZuoraConnect::VERSION,
      Version_Zuora: ZuoraAPI::VERSION ,
      Version_Ruby: RUBY_VERSION,
      Version_Rails: Rails.version,
      hold: 1
    }
  elsif type == "stats"
    begin
      Resque.redis.ping
      @data = {
        app_name: namespace,
        url: "dummy",
        Resque:{
          Jobs_Finished: Resque.info[:processed] ,
          Jobs_Failed: Resque.info[:failed],
          Jobs_Pending: Resque.info[:pending],
          Workers_Active: Resque.info[:working],
          Workers_Total: Resque.info[:workers]
        }
      }
    rescue
    end
  end
  return @data
end

.refresh_aggregate_table(aggregate_name: 'all_tasks_processing', table_name: 'tasks', where_clause: "where status in ('Processing', 'Queued')", index_table: true) ⇒ Object

START Aggregate Grouping Helping Methods ####



735
736
737
738
739
740
# File 'app/models/zuora_connect/app_instance_base.rb', line 735

def self.refresh_aggregate_table(aggregate_name: 'all_tasks_processing', table_name: 'tasks', where_clause: "where status in ('Processing', 'Queued')", index_table: true)
  self.update_functions
  #Broke function into two parts to ensure transaction size was small enough
  ActiveRecord::Base.connection.execute('SELECT "shared_extensions".refresh_aggregate_table(\'%s\', \'%s\', %s, \'Table\');' % [aggregate_name, table_name, ActiveRecord::Base.connection.quote(where_clause)])
  ActiveRecord::Base.connection.execute('SELECT "shared_extensions".refresh_aggregate_table(\'%s\', \'%s\', %s, \'Index\');' % [aggregate_name, table_name, ActiveRecord::Base.connection.quote(where_clause)]) if index_table
end

.update_functionsObject



742
743
744
# File 'app/models/zuora_connect/app_instance_base.rb', line 742

def self.update_functions
  ActiveRecord::Base.connection.execute(File.read("#{Gem.loaded_specs["zuora_connect"].gem_dir}/app/views/sql/refresh_aggregate_table.txt"))
end

.write_to_telegraf(*args) ⇒ Object

START Metrics Mathods ####



163
164
165
166
167
168
# File 'app/models/zuora_connect/app_instance_base.rb', line 163

def self.write_to_telegraf(*args)
  if ZuoraConnect.configuration.enable_metrics
    @@telegraf_host = ZuoraConnect::Telegraf.new() if @@telegraf_host == nil
    return @@telegraf_host.write(*args)
  end
end

Instance Method Details

#apartment_switch(method = nil, migrate = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/zuora_connect/app_instance_base.rb', line 29

def apartment_switch(method = nil, migrate = false)
  begin
    Apartment::Tenant.switch!(self.id) if self.persisted?
  rescue Apartment::TenantNotFound => ex
    Apartment::Tenant.create(self.id.to_s)
    retry
  end
  if migrate && ActiveRecord::Migrator.needs_migration?
    Apartment::Migrator.migrate(self.id)
  end
  Thread.current[:appinstance] = self
end

#api_limit(start: true, time: API_LIMIT_TIMEOUT.to_i) ⇒ Object

START Resque Helping Methods ####



462
463
464
465
466
467
468
# File 'app/models/zuora_connect/app_instance_base.rb', line 462

def api_limit(start: true, time: API_LIMIT_TIMEOUT.to_i)
  if start
    Redis.current.setex("APILimits:#{self.id}", time, true)
  else
    Redis.current.del("APILimits:#{self.id}")
  end
end

#api_limit?Boolean

Returns:

  • (Boolean)


470
471
472
# File 'app/models/zuora_connect/app_instance_base.rb', line 470

def api_limit?
  return Redis.current.get("APILimits:#{self.id}").to_bool
end

#attr_builder(field, val) ⇒ Object



794
795
796
797
# File 'app/models/zuora_connect/app_instance_base.rb', line 794

def attr_builder(field,val)
  singleton_class.class_eval { attr_accessor "#{field}" }
  send("#{field}=", val)
end

#build_task(task_data: {}, session: {}) ⇒ Object

START Task Mathods ####



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/models/zuora_connect/app_instance_base.rb', line 207

def build_task(task_data: {}, session: {})
  self.task_data = task_data
  self.mode = self.task_data["mode"]
  self.task_data.each do |k,v|
    if k.match(/^(.*)_login$/)
      tmp = ZuoraConnect::Login.new(v)
      if v["tenant_type"] == "Zuora"
        if tmp.entities.size > 0
          tmp.entities.each do |value|
            entity_id = value["id"]
            tmp.client(entity_id).current_session          = session["#{self.id}::#{k}::#{entity_id}:current_session"]               if session["#{self.id}::#{k}::#{entity_id}:current_session"]
            tmp.client(entity_id).bearer_token             = session["#{self.id}::#{k}::#{entity_id}:bearer_token"]                  if session["#{self.id}::#{k}::#{entity_id}:bearer_token"]
            tmp.client(entity_id).oauth_session_expires_at = session["#{self.id}::#{k}::#{entity_id}:oauth_session_expires_at"]      if session["#{self.id}::#{k}::#{entity_id}:oauth_session_expires_at"]
          end
        else
          tmp.client.current_session                       = session["#{self.id}::#{k}:current_session"]                             if session["#{self.id}::#{k}:current_session"]
          tmp.client.bearer_token                          = session["#{self.id}::#{k}:bearer_token"]                                if session["#{self.id}::#{k}:bearer_token"] && tmp.client.respond_to?(:bearer_token) ## need incase session id goes from basic to aouth in same redis store
          tmp.client.oauth_session_expires_at              = session["#{self.id}::#{k}:oauth_session_expires_at"]                    if session["#{self.id}::#{k}:oauth_session_expires_at"]  && tmp.client.respond_to?(:oauth_session_expires_at) 
        end
        self.logins[k] = tmp
        self.attr_builder(k, @logins[k])
      end
    elsif k == "options"
      v.each do |opt|
        self.options[opt["config_name"]] = opt
      end
    elsif k == "user_settings"
      self.timezone =  v["timezone"]
      self.locale = v["local"]
    end
  end
rescue => ex
  Rails.logger.error("Task Data: #{task_data}")
  Rails.logger.error("Task Session: #{session}")
  raise
end

#cache_app_instanceObject



381
382
383
384
385
386
387
388
389
390
# File 'app/models/zuora_connect/app_instance_base.rb', line 381

def cache_app_instance
  if defined?(Redis.current)
    #Task data must be present and the last refresh cannot be old. We dont want to overwite new cache data with old
    if self.task_data.present? &&  (self.last_refresh.to_i > INSTANCE_REFRESH_WINDOW.ago.to_i)
      Rails.logger.debug("[#{self.id}] Caching AppInstance")
      Redis.current.setex("AppInstance:#{self.id}", INSTANCE_REDIS_CACHE_PERIOD.to_i, encrypt_data(data: self.save_data))
    end
    Redis.current.del("Deleted:#{self.id}")
  end
end

#catalog_loaded?Boolean

Returns:

  • (Boolean)


568
569
570
# File 'app/models/zuora_connect/app_instance_base.rb', line 568

def catalog_loaded?
  return ActiveRecord::Base.connection.execute('SELECT id FROM "public"."zuora_connect_app_instances" WHERE "id" = %s AND catalog = \'{}\' LIMIT 1' % [self.id]).first.nil?
end

#catalog_lookup(entity_id: nil, object: :product, object_id: nil, child_objects: false, cache: false) ⇒ Object

Catalog lookup provides method to lookup zuora catalog efficiently. entity_id: If the using catalog json be field to store multiple entity product catalogs. object: The Object class desired to be returned. Available [:product, :rateplan, :charge] object_id: The id or id’s of the object/objects to be returned. child_objects: Whether to include child objects of the object in question. cache: Store individual “1” object lookup in redis for caching.



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'app/models/zuora_connect/app_instance_base.rb', line 578

def catalog_lookup(entity_id: nil, object: :product, object_id: nil, child_objects: false, cache: false)
  entity_reference = entity_id.blank? ? 'Default' : entity_id

  if object_id.present? && ![Array, String].include?(object_id.class)
    raise "Object Id can only be a string or an array of strings"
  end

  if defined?(Redis.current) && object_id.present? && object_id.class == String && object_id.present?
    stub_catalog = decrypt_data(data: Redis.current.get("Catalog:#{self.id}:#{object_id}:Children:#{child_objects}"))
    object_hierarchy = decrypt_data(data: Redis.current.get("Catalog:#{self.id}:#{object_id}:Hierarchy"))
  end

  if defined?(object_hierarchy)
    object_hierarchy ||= (JSON.parse(ActiveRecord::Base.connection.execute('SELECT catalog_mapping #> \'{%s}\' AS item FROM "public"."zuora_connect_app_instances" WHERE "id" = %s LIMIT 1' % [entity_reference, self.id]).first["item"] || "{}") [object_id] || {"productId" => "SAFTEY", "productRatePlanId" => "SAFTEY", "productRatePlanChargeId" => "SAFTEY"})
  end

  case object
  when :product
    if object_id.nil?
      string =
        "SELECT "\
          "json_object_agg(product_id, product #{child_objects ? '' : '- \'productRatePlans\''}) AS item "\
        "FROM "\
          "\"public\".\"zuora_connect_app_instances\", "\
          "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product) "\
        "WHERE "\
          "\"id\" = %s" % [entity_reference, self.id]
    else
      if object_id.class == String
        string =
          "SELECT "\
            "(catalog #> '{%s, %s}') #{child_objects ? '' : '- \'productRatePlans\''} AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\" "\
          "WHERE "\
            "\"id\" = %s" % [entity_reference, object_id.blank? ? BLANK_OBJECT_ID_LOOKUP : object_id, self.id]
      elsif object_id.class == Array
        string =
          "SELECT "\
            "json_object_agg(product_id, product #{child_objects ? '' : '- \'productRatePlans\''}) AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\", "\
            "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product) "\
          "WHERE "\
            "\"product_id\" IN (\'%s\') AND "\
            "\"id\" = %s" % [entity_reference, object_id.join("\',\'"), self.id]
      end
    end

  when :rateplan
    if object_id.nil?
      string =
        "SELECT "\
          "json_object_agg(rateplan_id, rateplan #{child_objects ? '' : '- \'productRatePlanCharges\''}) AS item "\
        "FROM "\
          "\"public\".\"zuora_connect_app_instances\", "\
          "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product), "\
          "jsonb_each(product #> '{productRatePlans}') AS ee(rateplan_id, rateplan) "\
        "WHERE "\
          "\"id\" = %s" % [entity_reference, self.id]
    else
      if object_id.class == String
        string =
          "SELECT "\
            "(catalog #> '{%s, %s, productRatePlans, %s}') #{child_objects ? '' : '- \'productRatePlanCharges\''} AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\" "\
          "WHERE "\
            "\"id\" = %s" % [entity_reference, object_hierarchy['productId'], object_id.blank? ? BLANK_OBJECT_ID_LOOKUP : object_id,  self.id]
      elsif object_id.class == Array
        string =
          "SELECT "\
            "json_object_agg(rateplan_id, rateplan #{child_objects ? '' : '- \'productRatePlanCharges\''}) AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\", "\
            "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product), "\
            "jsonb_each(product #> '{productRatePlans}') AS ee(rateplan_id, rateplan) "\
          "WHERE "\
            "\"rateplan_id\" IN (\'%s\') AND "\
            "\"id\" = %s" % [entity_reference, object_id.join("\',\'"), self.id]
      end
    end

  when :charge
    if object_id.nil?
      string =
        "SELECT "\
          "json_object_agg(charge_id, charge) as item "\
        "FROM "\
          "\"public\".\"zuora_connect_app_instances\", "\
          "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product), "\
          "jsonb_each(product #> '{productRatePlans}') AS ee(rateplan_id, rateplan), "\
          "jsonb_each(rateplan #> '{productRatePlanCharges}') AS eee(charge_id, charge) "\
        "WHERE "\
          "\"id\" = %s" % [entity_reference, self.id]
    else
      if object_id.class == String
        string =
          "SELECT "\
            "catalog #> '{%s, %s, productRatePlans, %s, productRatePlanCharges, %s}' AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\" "\
          "WHERE "\
            "\"id\" = %s" % [entity_reference, object_hierarchy['productId'], object_hierarchy['productRatePlanId'], object_id.blank? ? BLANK_OBJECT_ID_LOOKUP : object_id , self.id]

      elsif object_id.class == Array
        string =
          "SELECT "\
            "json_object_agg(charge_id, charge) AS item "\
          "FROM "\
            "\"public\".\"zuora_connect_app_instances\", "\
            "jsonb_each((\"public\".\"zuora_connect_app_instances\".\"catalog\" #> '{%s}' )) AS e(product_id, product), "\
            "jsonb_each(product #> '{productRatePlans}') AS ee(rateplan_id, rateplan), "\
            "jsonb_each(rateplan #> '{productRatePlanCharges}') AS eee(charge_id, charge) "\
          "WHERE "\
            "\"charge_id\" IN (\'%s\') AND "\
            "\"id\" = %s" % [entity_reference, object_id.join("\',\'"), self.id]
      end
    end
  else
    raise "Available objects include [:product, :rateplan, :charge]"
  end

  stub_catalog ||= JSON.parse(ActiveRecord::Base.connection.execute(string).first["item"] || "{}")

  if defined?(Redis.current) && object_id.present? && object_id.class == String && object_id.present?
    Redis.current.set("Catalog:#{self.id}:#{object_id}:Hierarchy", encrypt_data(data: object_hierarchy))
    Redis.current.set("Catalog:#{self.id}:#{object_id}:Children:#{child_objects}", encrypt_data(data: stub_catalog))  if cache
  end
  return stub_catalog
end

#catalog_outdated?(time: Time.now - 12.hours) ⇒ Boolean

Returns:

  • (Boolean)


564
565
566
# File 'app/models/zuora_connect/app_instance_base.rb', line 564

def catalog_outdated?(time: Time.now - 12.hours)
  return self.catalog_updated_at.blank? || (self.catalog_updated_at < time)
end

#check_oauth_state(method) ⇒ Object

START Connect OAUTH methods ####



285
286
287
288
289
290
291
# File 'app/models/zuora_connect/app_instance_base.rb', line 285

def check_oauth_state(method)
  #Refresh token if already expired
  if self.oauth_expired?
    Rails.logger.debug("[#{self.id}] Before '#{method}' method, Oauth expired")
    self.refresh_oauth
  end
end

#data_lookup(session: {}) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'app/models/zuora_connect/app_instance_base.rb', line 366

def data_lookup(session: {})
  if defined?(Redis.current)
    cached_instance = Redis.current.get("AppInstance:#{self.id}")
    if cached_instance.blank?
      Rails.logger.debug("[#{self.id}] Cached AppInstance Missing")
      return session
    else
      Rails.logger.debug("[#{self.id}] Cached AppInstance Found")
      return decrypt_data(data: cached_instance, rescue_return: session).merge(session)
    end
  else
    return session
  end
end

#decrypt_data(data: nil, rescue_return: nil) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'app/models/zuora_connect/app_instance_base.rb', line 432

def decrypt_data(data: nil, rescue_return: nil)
  return data if data.blank?
  begin
    if Rails.env == 'development'
      return JSON.parse(data)
    else
      begin
        return JSON.parse(encryptor.decrypt_and_verify(CGI::unescape(data)))
      rescue ActiveSupport::MessageVerifier::InvalidSignature => ex
        Rails.logger.fatal('Error Decrypting')
        return rescue_return
      end
    end
  rescue JSON::ParserError => ex
    Rails.logger.fatal('Error Parsing')
    return rescue_return
  end
end

#drop_instanceObject

Method for overiding droping of an app instance



760
761
762
763
# File 'app/models/zuora_connect/app_instance_base.rb', line 760

def drop_instance
  self.drop_message = 'Ok to drop'
  return true
end

#encrypt_data(data: nil) ⇒ Object



451
452
453
454
455
456
457
458
# File 'app/models/zuora_connect/app_instance_base.rb', line 451

def encrypt_data(data: nil)
  return data if data.blank?
  if Rails.env == 'development'
    return data.to_json
  else
    return encryptor.encrypt_and_sign(data.to_json)
  end
end

#encryptorObject



424
425
426
427
428
429
430
# File 'app/models/zuora_connect/app_instance_base.rb', line 424

def encryptor
  # Default values for Rails 4 apps
  key_iter_num, key_size, salt, signed_salt = [1000, 64, "encrypted cookie", "signed encrypted cookie"]
  key_generator = ActiveSupport::KeyGenerator.new(Rails.application.secrets.secret_key_base, iterations: key_iter_num)
  secret, sign_secret = [key_generator.generate_key(salt), key_generator.generate_key(signed_salt)]
  return ActiveSupport::MessageEncryptor.new(secret, sign_secret)
end

#get_catalog(page_size: 5, zuora_login: self.login_lookup(type: "Zuora").first, entity_id: nil) ⇒ Object

START Catalog Helping Methods #####



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'app/models/zuora_connect/app_instance_base.rb', line 498

def get_catalog(page_size: 5, zuora_login: self.(type: "Zuora").first, entity_id: nil)
  self.update_column(:catalog_update_attempt_at, Time.now.utc)

  entity_reference = entity_id.blank? ? 'Default' : entity_id
  Rails.logger.debug("Fetch Catalog")
  Rails.logger.debug("Zuora Entity: #{entity_id.blank? ? 'default' : entity_id}")

   = .client(entity_reference)

  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
  ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{tmp}\', \'{}\'), "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp}\', \'{}\') where "id" = %{id}' % {:id => self.id})

  response = {'nextPage' => .rest_endpoint("catalog/products?pageSize=#{page_size}")}
  while !response["nextPage"].blank?
    url = .rest_endpoint(response["nextPage"].split('/v1/').last)
    Rails.logger.debug("Fetch Catalog URL #{url}")
    output_json, response = .rest_call(:debug => false, :url => url, :errors => [ZuoraAPI::Exceptions::ZuoraAPISessionError], :timeout_retry => true)
    Rails.logger.debug("Fetch Catalog Response Code #{response.code}")

    if !output_json['success'] =~ (/(true|t|yes|y|1)$/i) || output_json['success'].class != TrueClass
      Rails.logger.error("Fetch Catalog DATA #{output_json.to_json}")
      raise ZuoraAPI::Exceptions::ZuoraAPIError.new("Error Getting Catalog: #{output_json}")
    end

    output_json["products"].each do |product|
      ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp, %s}\', \'%s\') where "id" = %s' % [product["id"], {"productId" => product["id"]}.to_json.gsub("'", "''"), self.id])
      rateplans = {}

      product["productRatePlans"].each do |rateplan|
        ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp, %s}\', \'%s\') where "id" = %s' % [rateplan["id"],  {"productId" => product["id"], "productRatePlanId" => rateplan["id"]}.to_json.gsub("'", "''"), self.id])
        charges = {}

        rateplan["productRatePlanCharges"].each do |charge|
          ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp, %s}\', \'%s\') where "id" = %s' % [charge["id"],  {"productId" => product["id"], "productRatePlanId" => rateplan["id"], "productRatePlanChargeId" => charge["id"]}.to_json.gsub("'", "''"), self.id])

          charges[charge["id"]] = charge.merge({"productId" => product["id"], "productName" => product["name"], "productRatePlanId" => rateplan["id"], "productRatePlanName" => rateplan["name"] })
        end

        rateplan["productRatePlanCharges"] = charges
        rateplans[rateplan["id"]] = rateplan.merge({"productId" => product["id"], "productName" => product["name"]})
      end
      product["productRatePlans"] = rateplans

      ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{tmp, %s}\', \'%s\') where "id" = %s' % [product["id"], product.to_json.gsub("'", "''"), self.id])
    end
  end

  # Move from tmp to actual
  ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{%{entity}}\', "catalog" #> \'{tmp}\'), "catalog_mapping" = jsonb_set("catalog_mapping", \'{%{entity}}\',  "catalog_mapping" #> \'{tmp}\') where "id" = %{id}' % {:entity => entity_reference, :id => self.id})
  if defined?(Redis.current)
    Redis.current.keys("Catalog:#{self.id}:*").each do |key|
      Redis.current.del(key.to_s)
    end
  end
  # Clear tmp holder
  ActiveRecord::Base.connection.execute('UPDATE "public"."zuora_connect_app_instances" SET "catalog" = jsonb_set("catalog", \'{tmp}\', \'{}\'), "catalog_mapping" = jsonb_set("catalog_mapping", \'{tmp}\', \'{}\') where "id" = %{id}' % {:id => self.id})

  ActiveRecord::Base.logger = old_logger
  self.update_column(:catalog_updated_at, Time.now.utc)
  self.touch

  # DO NOT RETURN CATALOG. THIS IS NOT SCALABLE WITH LARGE CATALOGS. USE THE  CATALOG_LOOKUP method provided
  return true
end

#get_s3_file_url(key) ⇒ Object



727
728
729
730
731
# File 'app/models/zuora_connect/app_instance_base.rb', line 727

def get_s3_file_url(key)
  require 'aws-sdk-s3'
  signer = Aws::S3::Presigner.new(client: self.s3_client)
  url = signer.presigned_url(:get_object, bucket: ZuoraConnect.configuration.s3_bucket_name, key: "#{ZuoraConnect.configuration.s3_folder_name}/#{self.id.to_s}/#{key}")
end

#initObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/zuora_connect/app_instance_base.rb', line 15

def init
  self.connect_user = 'Nobody'
  self.options = Hash.new
  self.logins = Hash.new
  self.api_version = "v2"
  self.attr_builder("timezone", ZuoraConnect.configuration.default_time_zone)
  self.attr_builder("locale", ZuoraConnect.configuration.default_locale)
  PaperTrail.whodunnit = "Backend" if defined?(PaperTrail)
  if INSTANCE_REFRESH_WINDOW > INSTANCE_REDIS_CACHE_PERIOD
    raise "The instance refresh window cannot be greater than the instance cache period"
  end
  self.apartment_switch(nil, true)
end

#instance_failure(failure) ⇒ Object



775
776
777
# File 'app/models/zuora_connect/app_instance_base.rb', line 775

def instance_failure(failure)
  raise failure
end

#login_lookup(type: "Zuora") ⇒ Object



782
783
784
785
786
787
788
# File 'app/models/zuora_connect/app_instance_base.rb', line 782

def (type: "Zuora")
  results = []
  self.logins.each do |name, |
    results <<  if .tenant_type == type
  end
  return results
end

#mark_for_refreshObject



362
363
364
# File 'app/models/zuora_connect/app_instance_base.rb', line 362

def mark_for_refresh
  return defined?(Redis.current) ? Redis.current.set("AppInstance:#{self.id}:Refreshing", true, {:nx => true, :ex => REFRESH_TIMEOUT.to_i}) : true
end

#marked_for_refresh?Boolean

START AppInstance Temporary Persistance Methods ####

Returns:

  • (Boolean)


346
347
348
# File 'app/models/zuora_connect/app_instance_base.rb', line 346

def marked_for_refresh?
  return defined?(Redis.current) ? Redis.current.get("AppInstance:#{self.id}:Refreshing").to_bool : false
end

#new_session(session: self.data_lookup, username: self.access_token, password: self.refresh_token, holding_pattern: false, log_level: Logger::DEBUG) ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/zuora_connect/app_instance_base.rb', line 42

def new_session(session: self.data_lookup, username: self.access_token, password: self.refresh_token, holding_pattern: false, log_level: Logger::DEBUG)
  self.api_version = "v2"
  self.username = username
  self.password = password
  self.last_refresh = session["#{self.id}::last_refresh"]
  self.connect_user = session["#{self.id}::user::email"]
  PaperTrail.whodunnit = self.connect_user if defined?(PaperTrail)

  ## DEV MODE TASK DATA MOCKUP
  if ZuoraConnect.configuration.mode != "Production"
    mock_task_data = {
      "mode" => ZuoraConnect.configuration.dev_mode_mode
    }

    case ZuoraConnect.configuration.dev_mode_options.class
    when Hash
      self.options = ZuoraConnect.configuration.dev_mode_options
    when Array
      mock_task_data["options"] = ZuoraConnect.configuration.dev_mode_options
    end

    ZuoraConnect.configuration.dev_mode_logins.each do |k,v|
      v = v.merge({"entities": [] }) if !v.keys.include?("entities")
      mock_task_data[k] = v
    end

    self.build_task(task_data: mock_task_data, session: session)
  else
    time_expire = (session["#{self.id}::last_refresh"] || Time.now).to_i - INSTANCE_REFRESH_WINDOW.ago.to_i

    if session.empty?
      self.new_session_message = "REFRESHING - Session Empty"
      raise ZuoraConnect::Exceptions::HoldingPattern if holding_pattern && !self.mark_for_refresh
      self.refresh(session)

    elsif (self.id != session["appInstance"].to_i)
      self.new_session_message = "REFRESHING - AppInstance ID(#{self.id}) does not match session id(#{session["appInstance"].to_i})"
      raise ZuoraConnect::Exceptions::HoldingPattern if holding_pattern && !self.mark_for_refresh
      self.refresh(session)

    elsif session["#{self.id}::task_data"].blank?
      self.new_session_message = "REFRESHING - Task Data Blank"
      raise ZuoraConnect::Exceptions::HoldingPattern if holding_pattern && !self.mark_for_refresh
      self.refresh(session)

    elsif session["#{self.id}::last_refresh"].blank?
      self.new_session_message = "REFRESHING - No Time on Cookie"
      raise ZuoraConnect::Exceptions::HoldingPattern if holding_pattern && !self.mark_for_refresh
      self.refresh(session)

    # If the cache is expired and we can aquire a refresh lock
    elsif (session["#{self.id}::last_refresh"].to_i < INSTANCE_REFRESH_WINDOW.ago.to_i) && self.mark_for_refresh
      self.new_session_message = "REFRESHING - Session Old by #{time_expire.abs} second"
      self.refresh(session)
    else
      if time_expire < 0
        self.new_session_message = ["REBUILDING - Expired by #{time_expire} seconds", self.marked_for_refresh? ? " cache updating as of #{self.reset_mark_refreshed_at} seconds ago" : nil].compact.join(',')
      else
        self.new_session_message = "REBUILDING - Expires in #{time_expire} seconds"
      end
      self.build_task(task_data: session["#{self.id}::task_data"], session: session)
    end
  end
  begin
    I18n.locale = self.locale
  rescue I18n::InvalidLocale => ex
    Rails.logger.debug("Invalid Locale: #{ex.message}")
  end
  Time.zone = self.timezone
  return self
rescue ZuoraConnect::Exceptions::HoldingPattern => ex
  while self.marked_for_refresh?
    Rails.logger.add(log_level, "Holding - Expires in #{self.reset_mark_expires_at}")
    sleep(5)
  end
  self.reload_attributes([:refresh_token, :oauth_expires_at, :access_token])
  session = self.data_lookup(session: session)
  retry

ensure
  Rails.logger.add(log_level, self.new_session_message)
end

#new_session_for_api_requests(params: {}) ⇒ Object

Overide this method to avoid the new session call for api requests that use the before filter authenticate_app_api_request. This can be usefull for apps that dont need connect metadata call, or credentials, to operate for api requests



749
750
751
# File 'app/models/zuora_connect/app_instance_base.rb', line 749

def new_session_for_api_requests(params: {})
  return true
end

#new_session_for_ui_requests(params: {}) ⇒ Object

Overide this method to avoid the new session call for ui requests that use the before filter authenticate_connect_app_request. This can be usefull for apps that dont need connect metadata call, or credentials, to operate for ui requests



755
756
757
# File 'app/models/zuora_connect/app_instance_base.rb', line 755

def new_session_for_ui_requests(params: {})
  return true
end

#oauth_expired?Boolean

Returns:

  • (Boolean)


293
294
295
# File 'app/models/zuora_connect/app_instance_base.rb', line 293

def oauth_expired?
  return self.oauth_expires_at.present? ? (self.oauth_expires_at < Time.now) : true
end

#queue_pause(time: nil, current_user: 'Default') ⇒ Object



478
479
480
481
482
483
484
485
# File 'app/models/zuora_connect/app_instance_base.rb', line 478

def queue_pause(time: nil, current_user: 'Default')
  if time.present?
    raise "Time must be fixnum of seconds." if time.class != Fixnum
    Redis.current.setex("resque:PauseQueue:#{self.id}", time, current_user)
  else
    Redis.current.set("resque:PauseQueue:#{self.id}", current_user)
  end
end

#queue_paused?Boolean

Returns:

  • (Boolean)


474
475
476
# File 'app/models/zuora_connect/app_instance_base.rb', line 474

def queue_paused?
  return Redis.current.get("resque:PauseQueue:#{self.id}").present?
end

#queue_start(current_user: 'Default') ⇒ Object



487
488
489
490
491
492
493
494
# File 'app/models/zuora_connect/app_instance_base.rb', line 487

def queue_start(current_user: 'Default')
  paused_user = Redis.current.get("resque:PauseQueue:#{self.id}") 
  if paused_user == current_user || paused_user.blank?
    Redis.current.del("resque:PauseQueue:#{self.id}")
  else
    raise "Can only unpause for user #{paused_user}." 
  end
end

#refresh(session = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/zuora_connect/app_instance_base.rb', line 125

def refresh(session = nil)
  refresh_count ||= 0
  start = Time.now
  response = HTTParty.get(ZuoraConnect.configuration.url + "/api/#{self.api_version}/tools/tasks/#{self.id}.json",:body => {:access_token => self.access_token})
  response_time = Time.now - start

  Rails.logger.debug("[#{self.id}] REFRESH TASK - Connect Task Info Request Time #{response_time.round(2).to_s}")
  if response.code == 200
    self.build_task(task_data: JSON.parse(response.body), session: session)
    self.last_refresh = Time.now.to_i
    self.cache_app_instance
    self.reset_mark_for_refresh
  else
    Rails.logger.fatal("[#{self.id}] REFRESH TASK - Failed Code #{response.code}")
    raise ZuoraConnect::Exceptions::ConnectCommunicationError.new("Error Communicating with Connect", response.body, response.code)
  end
rescue Net::ReadTimeout, Net::OpenTimeout, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => ex
  if (refresh_count += 1) < 3
    Rails.logger.info("[#{self.id}] REFRESH TASK - #{ex.class} Retrying(#{refresh_count})")
    retry
  else
    Rails.logger.fatal("[#{self.id}] REFRESH TASK - #{ex.class} Failed #{refresh_count}x")
    raise
  end
rescue ZuoraConnect::Exceptions::ConnectCommunicationError => ex
  if (refresh_count += 1) < 3
    Rails.logger.info("[#{self.id}] REFRESH TASK - Failed Retrying(#{refresh_count})")
    if ex.code == 401
      self.refresh_oauth
    end
    retry
  else
    Rails.logger.fatal("[#{self.id}] REFRESH TASK - Failed #{refresh_count}x")
    raise
  end
end

#refresh_oauthObject



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'app/models/zuora_connect/app_instance_base.rb', line 297

def refresh_oauth
  refresh_oauth_count ||= 0
  start = Time.now
  params = {
            :grant_type => "refresh_token",
            :redirect_uri => ZuoraConnect.configuration.oauth_client_redirect_uri,
            :refresh_token => self.refresh_token
          }
  response = HTTParty.post("#{ZuoraConnect.configuration.url}/oauth/token",:body => params)
  response_time = Time.now - start
  Rails.logger.info("[#{self.id}] REFRESH OAUTH - In #{response_time.round(2).to_s}")

  if response.code == 200
    response_body = JSON.parse(response.body)

    self.refresh_token = response_body["refresh_token"]
    self.access_token = response_body["access_token"]
    self.oauth_expires_at = Time.at(response_body["created_at"].to_i) + response_body["expires_in"].seconds
    self.save(:validate => false)
  else
    Rails.logger.fatal("[#{self.id}] REFRESH OAUTH - Failed Code #{response.code}")
    raise ZuoraConnect::Exceptions::ConnectCommunicationError.new("Error Refreshing Access Token", response.body, response.code)
  end
rescue Net::ReadTimeout, Net::OpenTimeout, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => ex
  if (refresh_oauth_count += 1) < 3
    Rails.logger.info("[#{self.id}] REFRESH OAUTH - #{ex.class} Retrying(#{refresh_oauth_count})")
    retry
  else
    Rails.logger.fatal("[#{self.id}] REFRESH OAUTH - #{ex.class} Failed #{refresh_oauth_count}x")
    raise
  end
rescue ZuoraConnect::Exceptions::ConnectCommunicationError => ex
  sleep(5)
  self.reload_attributes([:refresh_token, :oauth_expires_at, :access_token]) #Reload only the refresh token for retry

  #After reload, if nolonger expired return
  return if !self.oauth_expired?

  if (refresh_oauth_count += 1) < 3
    Rails.logger.info("[#{self.id}] REFRESH OAUTH - Failed Retrying(#{refresh_oauth_count})")
    retry
  else
    Rails.logger.fatal("[#{self.id}] REFRESH OAUTH - Failed #{refresh_oauth_count}x")
    raise
  end
end

#reload_attributes(selected_attributes) ⇒ Object



765
766
767
768
769
770
771
772
773
# File 'app/models/zuora_connect/app_instance_base.rb', line 765

def reload_attributes(selected_attributes)
  raise "Attibutes must be array" if selected_attributes.class != Array
  value_attributes = self.class.unscoped.where(:id=>id).select(selected_attributes).first.attributes
  value_attributes.each do |key, value|
    next if key == "id" && value.blank?
    self.send(:write_attribute, key, value)
  end
  return self
end

#reset_mark_expires_atObject



358
359
360
# File 'app/models/zuora_connect/app_instance_base.rb', line 358

def reset_mark_expires_at
  return defined?(Redis.current) ? Redis.current.ttl("AppInstance:#{self.id}:Refreshing") : 0
end

#reset_mark_for_refreshObject



350
351
352
# File 'app/models/zuora_connect/app_instance_base.rb', line 350

def reset_mark_for_refresh
  Redis.current.del("AppInstance:#{self.id}:Refreshing") if defined?(Redis.current)
end

#reset_mark_refreshed_atObject



354
355
356
# File 'app/models/zuora_connect/app_instance_base.rb', line 354

def reset_mark_refreshed_at
  return defined?(Redis.current) ? REFRESH_TIMEOUT.to_i - Redis.current.ttl("AppInstance:#{self.id}:Refreshing") : 0
end

#save_data(session = Hash.new) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'app/models/zuora_connect/app_instance_base.rb', line 392

def save_data(session = Hash.new)
  self.logins.each do |key, |
    if .tenant_type == "Zuora"
      if .available_entities.size > 1 && Rails.application.config.session_store != ActionDispatch::Session::CookieStore
        .available_entities.each do |entity_key|
          session["#{self.id}::#{key}::#{entity_key}:current_session"]            = .client(entity_key).current_session            if .client.respond_to?(:current_session)
          session["#{self.id}::#{key}::#{entity_key}:bearer_token"]               = .client(entity_key).bearer_token               if .client.respond_to?(:bearer_token)
          session["#{self.id}::#{key}::#{entity_key}:oauth_session_expires_at"]   = .client(entity_key).oauth_session_expires_at   if .client.respond_to?(:oauth_session_expires_at)
        end
      else
        session["#{self.id}::#{key}:current_session"]             = .client.current_session            if .client.respond_to?(:current_session)
        session["#{self.id}::#{key}:bearer_token"]                = .client.bearer_token               if .client.respond_to?(:bearer_token)
        session["#{self.id}::#{key}:oauth_session_expires_at"]    = .client.oauth_session_expires_at   if .client.respond_to?(:oauth_session_expires_at)
      end
    end
  end

  session["#{self.id}::task_data"] = self.task_data

  #Redis is not defined strip out old data
  if !defined?(Redis.current)
    session["#{self.id}::task_data"].delete('applications')
    session["#{self.id}::task_data"].select {|k,v| k.include?('login') && v['tenant_type'] == 'Zuora'}.each do |, |
      session["#{self.id}::task_data"][]['entities'] = (.dig('entities') || []).map {|entity| entity.slice('id', 'tenantId')}
    end
  end

  session["#{self.id}::last_refresh"] = self.last_refresh
  session["appInstance"] = self.id
  return session
end

#send_emailObject



779
780
# File 'app/models/zuora_connect/app_instance_base.rb', line 779

def send_email
end

#update_logins(options) ⇒ Object

This can update an existing login, add a new login, change to another existing login EXAMPLE: “ftp_login_14”,“username”: “ftplogin7”,“tenant_type”: “Custom”,“password”: “test2”,“url”: “www.ftp.com”,“custom_data”: { “path”: “/var/usr/test”}



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'app/models/zuora_connect/app_instance_base.rb', line 250

def update_logins(options)
   ||= 0
  response = HTTParty.post(ZuoraConnect.configuration.url + "/api/#{self.api_version}/tools/tasks/#{self.id}/logins",:body => {:access_token => self.username}.merge(options))
  parsed_json =  JSON.parse(response.body)
  if response.code == 200
    if defined?(Redis.current)
      self.build_task(task_data: parsed_json, session: self.data_lookup)
      self.last_refresh = Time.now.to_i
      self.cache_app_instance
    end
    return parsed_json
  elsif response.code == 400
    raise ZuoraConnect::Exceptions::APIError.new(message: parsed_json['errors'].join(' '), response: response.body, code: response.code)
  else
    raise ZuoraConnect::Exceptions::ConnectCommunicationError.new("Error Communicating with Connect", response.body, response.code)
  end
rescue Net::ReadTimeout, Net::OpenTimeout, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => ex
  if ( += 1) < 3
    retry
  else
    raise
  end
rescue ZuoraConnect::Exceptions::ConnectCommunicationError => ex
  if ( += 1) < 3
    if ex.code == 401
      self.refresh_oauth
    end
    retry
  else
    raise
  end
end

#updateOption(optionId, value) ⇒ Object



244
245
246
# File 'app/models/zuora_connect/app_instance_base.rb', line 244

def updateOption(optionId, value)
  response = HTTParty.get(ZuoraConnect.configuration.url + "/api/#{self.api_version}/tools/application_options/#{optionId}/edit?value=#{value}",:body => {:access_token => self.username})
end

#upload_to_s3(local_file, s3_path = nil) ⇒ Object



721
722
723
724
725
# File 'app/models/zuora_connect/app_instance_base.rb', line 721

def upload_to_s3(local_file,s3_path = nil)
  s3_path = local_file.split("/").last if s3_path.nil?
  obj = self.s3_client.bucket(ZuoraConnect.configuration.s3_bucket_name).object("#{ZuoraConnect.configuration.s3_folder_name}/#{self.id.to_s}/#{s3_path}}")
  obj.upload_file(local_file, :server_side_encryption => 'AES256')
end