From edacf4bca1ed723148453a878a30f2821fd12a03 Mon Sep 17 00:00:00 2001 From: David Wilkie Date: Wed, 4 Sep 2024 08:06:52 +0700 Subject: [PATCH] WIP --- .../jobs/update_opensips_permission_job.rb | 18 ++++++++++++++++ .../services/app/models/application_record.rb | 8 +++---- .../spec/requests/sqs_message_event_spec.rb | 21 +++++++++++++++++++ .../services/spec/support/factory_helpers.rb | 4 ++-- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 components/services/app/jobs/update_opensips_permission_job.rb diff --git a/components/services/app/jobs/update_opensips_permission_job.rb b/components/services/app/jobs/update_opensips_permission_job.rb new file mode 100644 index 000000000..0ed35fc24 --- /dev/null +++ b/components/services/app/jobs/update_opensips_permission_job.rb @@ -0,0 +1,18 @@ +class UpdateOpenSIPSPermissionJob + attr_reader :source_ip, :group_id + + def initialize(source_ip, options = {}) + @source_ip = source_ip + @group_id = options.fetch("group_id", 0) + end + + def call + OpenSIPSAddress.where(ip: source_ip, database_connection:).update(grp: group_id) + end + + private + + def database_connection + DatabaseConnections.find(:public_gateway) + end +end diff --git a/components/services/app/models/application_record.rb b/components/services/app/models/application_record.rb index fced72ee0..7aa60b2b1 100644 --- a/components/services/app/models/application_record.rb +++ b/components/services/app/models/application_record.rb @@ -2,12 +2,12 @@ class ApplicationRecord class << self attr_accessor :table_name - def exists?(database_connection:, **query) - where(database_connection:, **query).count.positive? + def exists?(**) + where(**).count.positive? end - def where(database_connection:, **query) - table(database_connection:).where(query) + def where(database_connection:, **) + table(database_connection:).where(**) end private diff --git a/components/services/spec/requests/sqs_message_event_spec.rb b/components/services/spec/requests/sqs_message_event_spec.rb index 7571db0f0..0ba8e1215 100644 --- a/components/services/spec/requests/sqs_message_event_spec.rb +++ b/components/services/spec/requests/sqs_message_event_spec.rb @@ -39,6 +39,27 @@ expect(address.count).to eq(0) end + it "updates an address message", :public_gateway do + create_address(ip: "165.57.32.1", grp: 2) + + payload = build_sqs_message_event_payload( + event_source_arn: "arn:aws:sqs:us-east-2:123456789012:somleng-switch-permissions", + body: { + "job_class" => "UpdateOpenSIPSPermissionJob", + "job_args" => [ "165.57.32.1", { "group_id" => 1 } ] + }.to_json + ) + + invoke_lambda(payload:) + + result = address.all + expect(result.count).to eq(1) + expect(result[0]).to include( + ip: "165.57.32.1", + grp: 1 + ) + end + it "adds a subscriber record", :client_gateway do payload = build_sqs_message_event_payload( event_source_arn: "arn:aws:sqs:us-east-2:123456789012:somleng-switch-permissions", diff --git a/components/services/spec/support/factory_helpers.rb b/components/services/spec/support/factory_helpers.rb index 0aa471e84..4195b2617 100644 --- a/components/services/spec/support/factory_helpers.rb +++ b/components/services/spec/support/factory_helpers.rb @@ -4,8 +4,8 @@ def create_load_balancer_target(dst_uri:, resources:) client_gateway_database_connection.table(:load_balancer).insert(dst_uri:, resources:) end - def create_address(ip:) - public_gateway_database_connection.table(:address).insert(ip:) + def create_address(**) + public_gateway_database_connection.table(:address).insert(**) end def create_rtpengine_target(socket:)