diff --git a/components/services/config/app_settings.yml b/components/services/config/app_settings.yml index 21144db55..ce4fea676 100644 --- a/components/services/config/app_settings.yml +++ b/components/services/config/app_settings.yml @@ -1,4 +1,6 @@ default: &default + region_data: "<%= ENV.fetch('REGION_DATA', '{}') %>" + stub_regions: false production: &production <<: *default @@ -9,6 +11,7 @@ staging: development: &development <<: *default + stub_regions: true test: <<: *development diff --git a/components/services/config/initializers/somleng_regions.rb b/components/services/config/initializers/somleng_regions.rb index 4d2366a4c..b92ecd066 100644 --- a/components/services/config/initializers/somleng_regions.rb +++ b/components/services/config/initializers/somleng_regions.rb @@ -1,3 +1,6 @@ +require "json" + SomlengRegions.configure do |config| - config.region_data = ENV.fetch("REGION_DATA") + config.region_data = JSON.parse(AppSettings.fetch(:region_data)) + config.stub_regions = AppSettings.fetch(:stub_regions) end diff --git a/components/services/lib/somleng_regions.rb b/components/services/lib/somleng_regions.rb index c8c677049..ace176491 100644 --- a/components/services/lib/somleng_regions.rb +++ b/components/services/lib/somleng_regions.rb @@ -1,5 +1,22 @@ +require_relative "somleng_regions/region" + module SomlengRegions class << self + MOCK_REGIONS = [ + Region.new( + identifier: "ap-southeast-1", + alias: "hydrogen", + group_id: 1, + human_name: "South East Asia (Singapore)" + ), + Region.new( + identifier: "us-east-1", + alias: "helium", + group_id: 2, + human_name: "North America (Virginia, US)" + ) + ] + def configure yield(configuration) configuration @@ -11,12 +28,10 @@ def configuration alias config configuration def regions - @regions ||= Collection.new(Parser.new.parse(configuration.region_data)) + @regions ||= Collection.new(configuration.stub_regions ? MOCK_REGIONS : configuration.region_data.map { |region| Region.new(region) }) end end end require_relative "somleng_regions/configuration" -require_relative "somleng_regions/parser" require_relative "somleng_regions/collection" -require_relative "somleng_regions/region" diff --git a/components/services/lib/somleng_regions/collection.rb b/components/services/lib/somleng_regions/collection.rb index 47662a442..8f72efd95 100644 --- a/components/services/lib/somleng_regions/collection.rb +++ b/components/services/lib/somleng_regions/collection.rb @@ -8,6 +8,10 @@ def initialize(regions) @regions = regions end + def all + regions + end + def find_by(attributes) regions.find do |region| attributes.all? { |key, value| region[key] == value } diff --git a/components/services/lib/somleng_regions/configuration.rb b/components/services/lib/somleng_regions/configuration.rb index f8c76a7a1..1c21a185f 100644 --- a/components/services/lib/somleng_regions/configuration.rb +++ b/components/services/lib/somleng_regions/configuration.rb @@ -1,5 +1,5 @@ module SomlengRegions class Configuration - attr_accessor :region_data + attr_accessor :region_data, :stub_regions end end diff --git a/components/services/lib/somleng_regions/parser.rb b/components/services/lib/somleng_regions/parser.rb deleted file mode 100644 index 92feb1a53..000000000 --- a/components/services/lib/somleng_regions/parser.rb +++ /dev/null @@ -1,7 +0,0 @@ -module SomlengRegions - class Parser - def parse(region_data) - JSON.parse(region_data).map { |region| Region.new(region) } - end - end -end diff --git a/components/services/spec/spec_helper.rb b/components/services/spec/spec_helper.rb index 5ebee8264..838508fbb 100644 --- a/components/services/spec/spec_helper.rb +++ b/components/services/spec/spec_helper.rb @@ -32,21 +32,6 @@ ENV["PUBLIC_GATEWAY_DB_NAME"] = "opensips_public_gateway_test" ENV["CLIENT_GATEWAY_DB_NAME"] = "opensips_client_gateway_test" ENV["MEDIA_PROXY_NG_PORT"] = "2223" -ENV["REGION_DATA"] = [ - { - "alias" => "hydrogen", - "identifier" => "ap-southeast-1", - "human_name" => "Asia", - "group_id" => 1 - }, - { - "alias" => "helium", - "identifier" => "us-east-1", - "human_name" => "North America", - "group_id" => 2 - } -].to_json - require_relative "../app" diff --git a/infrastructure/modules/services/iam.tf b/infrastructure/modules/services/iam.tf index 25350e6a5..243c382f8 100644 --- a/infrastructure/modules/services/iam.tf +++ b/infrastructure/modules/services/iam.tf @@ -36,7 +36,7 @@ resource "aws_iam_policy" "custom_policy" { "${var.freeswitch_event_socket_password_parameter.arn}", "${aws_ssm_parameter.application_master_key.arn}", "${var.db_password_parameter.arn}", - "${var.region_data_parameter.arn}" + "${data.aws_ssm_parameter.region_data.arn}" ] }, { diff --git a/infrastructure/modules/services/lambda.tf b/infrastructure/modules/services/lambda.tf index c2c3a718a..8ef204edc 100644 --- a/infrastructure/modules/services/lambda.tf +++ b/infrastructure/modules/services/lambda.tf @@ -26,7 +26,7 @@ resource "aws_lambda_function" "this" { MEDIA_PROXY_NG_PORT = var.media_proxy_ng_port DB_PASSWORD_SSM_PARAMETER_NAME = var.db_password_parameter.name APP_MASTER_KEY_SSM_PARAMETER_NAME = aws_ssm_parameter.application_master_key.name - REGION_DATA_SSM_PARAMETER_NAME = var.region_data_parameter.name + REGION_DATA_SSM_PARAMETER_NAME = data.aws_ssm_parameter.region_data.name APP_ENV = var.app_environment DB_HOST = var.db_host DB_PORT = var.db_port diff --git a/infrastructure/modules/services/ssm.tf b/infrastructure/modules/services/ssm.tf index b3eb62f34..a9138cc30 100644 --- a/infrastructure/modules/services/ssm.tf +++ b/infrastructure/modules/services/ssm.tf @@ -1,3 +1,7 @@ +data "aws_ssm_parameter" "region_data" { + name = "somleng.${var.app_environment}.region_data" +} + resource "aws_ssm_parameter" "application_master_key" { name = "somleng-switch-services.${var.app_environment}.application_master_key" type = "SecureString" diff --git a/infrastructure/modules/services/variables.tf b/infrastructure/modules/services/variables.tf index 51afdfb1b..d40f8bb43 100644 --- a/infrastructure/modules/services/variables.tf +++ b/infrastructure/modules/services/variables.tf @@ -6,7 +6,6 @@ variable "switch_group" {} variable "media_proxy_group" {} variable "client_gateway_group" {} variable "db_password_parameter" {} -variable "region_data_parameter" {} variable "freeswitch_event_socket_password_parameter" {} variable "db_security_group" {} variable "freeswitch_event_socket_port" {} diff --git a/infrastructure/production/services.tf b/infrastructure/production/services.tf index efcf23a03..7e8035c9f 100644 --- a/infrastructure/production/services.tf +++ b/infrastructure/production/services.tf @@ -14,7 +14,6 @@ module "services" { db_password_parameter = data.terraform_remote_state.core_infrastructure.outputs.db_master_password_parameter freeswitch_event_socket_password_parameter = data.aws_ssm_parameter.freeswitch_event_socket_password - region_data_parameter = data.aws_ssm_parameter.region_data db_security_group = data.terraform_remote_state.core_infrastructure.outputs.db_security_group db_username = data.terraform_remote_state.core_infrastructure.outputs.db_cluster.master_username diff --git a/infrastructure/production/ssm.tf b/infrastructure/production/ssm.tf index 5dd017cd7..feb6758f4 100644 --- a/infrastructure/production/ssm.tf +++ b/infrastructure/production/ssm.tf @@ -5,7 +5,3 @@ data "aws_ssm_parameter" "somleng_services_password" { data "aws_ssm_parameter" "freeswitch_event_socket_password" { name = "somleng-switch.production.freeswitch_event_socket_password" } - -data "aws_ssm_parameter" "region_data" { - name = "somleng.production.region_data" -} diff --git a/infrastructure/staging/services.tf b/infrastructure/staging/services.tf index efcf23a03..7e8035c9f 100644 --- a/infrastructure/staging/services.tf +++ b/infrastructure/staging/services.tf @@ -14,7 +14,6 @@ module "services" { db_password_parameter = data.terraform_remote_state.core_infrastructure.outputs.db_master_password_parameter freeswitch_event_socket_password_parameter = data.aws_ssm_parameter.freeswitch_event_socket_password - region_data_parameter = data.aws_ssm_parameter.region_data db_security_group = data.terraform_remote_state.core_infrastructure.outputs.db_security_group db_username = data.terraform_remote_state.core_infrastructure.outputs.db_cluster.master_username diff --git a/infrastructure/staging/ssm.tf b/infrastructure/staging/ssm.tf index 033b7bc5e..718982140 100644 --- a/infrastructure/staging/ssm.tf +++ b/infrastructure/staging/ssm.tf @@ -5,7 +5,3 @@ data "aws_ssm_parameter" "somleng_services_password" { data "aws_ssm_parameter" "freeswitch_event_socket_password" { name = "somleng-switch.staging.freeswitch_event_socket_password" } - -data "aws_ssm_parameter" "region_data" { - name = "somleng.staging.region_data" -}