From f2b5aaabd44e9bdbff0fedc46c9768bc0c82d12e Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 11 Nov 2021 11:49:38 -0800 Subject: [PATCH] Convert metadata service modules to functions (#3954) --- lib/metadata_service/endpoint.js | 6 ------ .../endpoint_config_options.js | 14 ------------- lib/metadata_service/endpoint_mode.js | 6 ------ .../endpoint_mode_config_options.js | 16 -------------- lib/metadata_service/get_endpoint.js | 8 +++++++ .../get_endpoint_config_options.js | 12 +++++++++++ lib/metadata_service/get_endpoint_mode.js | 8 +++++++ .../get_endpoint_mode_config_options.js | 14 +++++++++++++ .../get_metadata_service_endpoint.js | 8 +++---- .../endpoint_config_options.spec.js | 7 +++---- .../endpoint_mode_config_options.spec.js | 9 ++++---- .../get_metadata_service_endpoint.spec.js | 21 ++++++++++--------- 12 files changed, 64 insertions(+), 65 deletions(-) delete mode 100644 lib/metadata_service/endpoint.js delete mode 100644 lib/metadata_service/endpoint_config_options.js delete mode 100644 lib/metadata_service/endpoint_mode.js delete mode 100644 lib/metadata_service/endpoint_mode_config_options.js create mode 100644 lib/metadata_service/get_endpoint.js create mode 100644 lib/metadata_service/get_endpoint_config_options.js create mode 100644 lib/metadata_service/get_endpoint_mode.js create mode 100644 lib/metadata_service/get_endpoint_mode_config_options.js diff --git a/lib/metadata_service/endpoint.js b/lib/metadata_service/endpoint.js deleted file mode 100644 index b7c62598e0..0000000000 --- a/lib/metadata_service/endpoint.js +++ /dev/null @@ -1,6 +0,0 @@ -var Endpoint = { - IPv4: 'http://169.254.169.254', - IPv6: 'http://[fd00:ec2::254]', -}; - -module.exports = Endpoint; diff --git a/lib/metadata_service/endpoint_config_options.js b/lib/metadata_service/endpoint_config_options.js deleted file mode 100644 index 94059f4af8..0000000000 --- a/lib/metadata_service/endpoint_config_options.js +++ /dev/null @@ -1,14 +0,0 @@ -var ENV_ENDPOINT_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT'; -var CONFIG_ENDPOINT_NAME = 'ec2_metadata_service_endpoint'; - -var ENDPOINT_CONFIG_OPTIONS = { - environmentVariableSelector: function(env) { return env[ENV_ENDPOINT_NAME]; }, - configFileSelector: function(profile) { return profile[CONFIG_ENDPOINT_NAME]; }, - default: undefined, -}; - -module.exports = { - ENV_ENDPOINT_NAME: ENV_ENDPOINT_NAME, - CONFIG_ENDPOINT_NAME: CONFIG_ENDPOINT_NAME, - ENDPOINT_CONFIG_OPTIONS: ENDPOINT_CONFIG_OPTIONS -}; diff --git a/lib/metadata_service/endpoint_mode.js b/lib/metadata_service/endpoint_mode.js deleted file mode 100644 index a70b113db8..0000000000 --- a/lib/metadata_service/endpoint_mode.js +++ /dev/null @@ -1,6 +0,0 @@ -var EndpointMode = { - IPv4: 'IPv4', - IPv6: 'IPv6', -}; - -module.exports = EndpointMode; diff --git a/lib/metadata_service/endpoint_mode_config_options.js b/lib/metadata_service/endpoint_mode_config_options.js deleted file mode 100644 index 55e383b93f..0000000000 --- a/lib/metadata_service/endpoint_mode_config_options.js +++ /dev/null @@ -1,16 +0,0 @@ -var EndpointMode = require('./endpoint_mode'); - -var ENV_ENDPOINT_MODE_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE'; -var CONFIG_ENDPOINT_MODE_NAME = 'ec2_metadata_service_endpoint_mode'; - -var ENDPOINT_MODE_CONFIG_OPTIONS = { - environmentVariableSelector: function(env) { return env[ENV_ENDPOINT_MODE_NAME]; }, - configFileSelector: function(profile) { return profile[CONFIG_ENDPOINT_MODE_NAME]; }, - default: EndpointMode.IPv4, -}; - -module.exports = { - ENV_ENDPOINT_MODE_NAME: ENV_ENDPOINT_MODE_NAME, - CONFIG_ENDPOINT_MODE_NAME: CONFIG_ENDPOINT_MODE_NAME, - ENDPOINT_MODE_CONFIG_OPTIONS: ENDPOINT_MODE_CONFIG_OPTIONS -}; diff --git a/lib/metadata_service/get_endpoint.js b/lib/metadata_service/get_endpoint.js new file mode 100644 index 0000000000..98a6cfbccb --- /dev/null +++ b/lib/metadata_service/get_endpoint.js @@ -0,0 +1,8 @@ +var getEndpoint = function() { + return { + IPv4: 'http://169.254.169.254', + IPv6: 'http://[fd00:ec2::254]', + }; +}; + +module.exports = getEndpoint; diff --git a/lib/metadata_service/get_endpoint_config_options.js b/lib/metadata_service/get_endpoint_config_options.js new file mode 100644 index 0000000000..0e00cb3a24 --- /dev/null +++ b/lib/metadata_service/get_endpoint_config_options.js @@ -0,0 +1,12 @@ +var ENV_ENDPOINT_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT'; +var CONFIG_ENDPOINT_NAME = 'ec2_metadata_service_endpoint'; + +var getEndpointConfigOptions = function() { + return { + environmentVariableSelector: function(env) { return env[ENV_ENDPOINT_NAME]; }, + configFileSelector: function(profile) { return profile[CONFIG_ENDPOINT_NAME]; }, + default: undefined, + }; +}; + +module.exports = getEndpointConfigOptions; diff --git a/lib/metadata_service/get_endpoint_mode.js b/lib/metadata_service/get_endpoint_mode.js new file mode 100644 index 0000000000..a9d839e955 --- /dev/null +++ b/lib/metadata_service/get_endpoint_mode.js @@ -0,0 +1,8 @@ +var getEndpointMode = function() { + return { + IPv4: 'IPv4', + IPv6: 'IPv6', + }; +}; + +module.exports = getEndpointMode; diff --git a/lib/metadata_service/get_endpoint_mode_config_options.js b/lib/metadata_service/get_endpoint_mode_config_options.js new file mode 100644 index 0000000000..abb0ea8cc4 --- /dev/null +++ b/lib/metadata_service/get_endpoint_mode_config_options.js @@ -0,0 +1,14 @@ +var EndpointMode = require('./get_endpoint_mode')(); + +var ENV_ENDPOINT_MODE_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE'; +var CONFIG_ENDPOINT_MODE_NAME = 'ec2_metadata_service_endpoint_mode'; + +var getEndpointModeConfigOptions = function() { + return { + environmentVariableSelector: function(env) { return env[ENV_ENDPOINT_MODE_NAME]; }, + configFileSelector: function(profile) { return profile[CONFIG_ENDPOINT_MODE_NAME]; }, + default: EndpointMode.IPv4, + }; +}; + +module.exports = getEndpointModeConfigOptions; diff --git a/lib/metadata_service/get_metadata_service_endpoint.js b/lib/metadata_service/get_metadata_service_endpoint.js index f63ccebbaf..5f4328ced4 100644 --- a/lib/metadata_service/get_metadata_service_endpoint.js +++ b/lib/metadata_service/get_metadata_service_endpoint.js @@ -1,10 +1,10 @@ var AWS = require('../core'); -var Endpoint = require('./endpoint'); -var EndpointMode = require('./endpoint_mode'); +var Endpoint = require('./get_endpoint')(); +var EndpointMode = require('./get_endpoint_mode')(); -var ENDPOINT_CONFIG_OPTIONS = require('./endpoint_config_options').ENDPOINT_CONFIG_OPTIONS; -var ENDPOINT_MODE_CONFIG_OPTIONS = require('./endpoint_mode_config_options').ENDPOINT_MODE_CONFIG_OPTIONS; +var ENDPOINT_CONFIG_OPTIONS = require('./get_endpoint_config_options')(); +var ENDPOINT_MODE_CONFIG_OPTIONS = require('./get_endpoint_mode_config_options')(); var getMetadataServiceEndpoint = function() { var endpoint = AWS.util.loadConfig(ENDPOINT_CONFIG_OPTIONS); diff --git a/test/metadata_service/endpoint_config_options.spec.js b/test/metadata_service/endpoint_config_options.spec.js index 1853766454..ab24b28d54 100644 --- a/test/metadata_service/endpoint_config_options.spec.js +++ b/test/metadata_service/endpoint_config_options.spec.js @@ -1,10 +1,9 @@ var helpers = require('./../helpers'); -var options = require('../../lib/metadata_service/endpoint_config_options'); +var ENDPOINT_CONFIG_OPTIONS = require('../../lib/metadata_service/get_endpoint_config_options')(); var AWS = helpers.AWS; -var ENDPOINT_CONFIG_OPTIONS = options.ENDPOINT_CONFIG_OPTIONS; -var ENV_ENDPOINT_NAME = options.ENV_ENDPOINT_NAME; -var CONFIG_ENDPOINT_NAME = options.CONFIG_ENDPOINT_NAME; +var ENV_ENDPOINT_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT'; +var CONFIG_ENDPOINT_NAME = 'ec2_metadata_service_endpoint'; if (AWS.util.isNode()) { describe('endpointConfigOptions', function() { diff --git a/test/metadata_service/endpoint_mode_config_options.spec.js b/test/metadata_service/endpoint_mode_config_options.spec.js index 8db265ec67..36f4174f1d 100644 --- a/test/metadata_service/endpoint_mode_config_options.spec.js +++ b/test/metadata_service/endpoint_mode_config_options.spec.js @@ -1,11 +1,10 @@ var helpers = require('./../helpers'); -var options = require('../../lib/metadata_service/endpoint_mode_config_options'); -var EndpointMode = require('../../lib/metadata_service/endpoint_mode'); +var ENDPOINT_MODE_CONFIG_OPTIONS = require('../../lib/metadata_service/get_endpoint_mode_config_options')(); +var EndpointMode = require('../../lib/metadata_service/get_endpoint_mode')(); var AWS = helpers.AWS; -var ENDPOINT_MODE_CONFIG_OPTIONS = options.ENDPOINT_MODE_CONFIG_OPTIONS; -var ENV_ENDPOINT_MODE_NAME = options.ENV_ENDPOINT_MODE_NAME; -var CONFIG_ENDPOINT_MODE_NAME = options.CONFIG_ENDPOINT_MODE_NAME; +var ENV_ENDPOINT_MODE_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE'; +var CONFIG_ENDPOINT_MODE_NAME = 'ec2_metadata_service_endpoint_mode'; if (AWS.util.isNode()) { describe('endpointModeConfigOptions', function() { diff --git a/test/metadata_service/get_metadata_service_endpoint.spec.js b/test/metadata_service/get_metadata_service_endpoint.spec.js index f6ee4d6a83..7986b3c2a8 100644 --- a/test/metadata_service/get_metadata_service_endpoint.spec.js +++ b/test/metadata_service/get_metadata_service_endpoint.spec.js @@ -1,9 +1,8 @@ var helpers = require('./../helpers'); var getMetadataServiceEndpoint = require('../../lib/metadata_service/get_metadata_service_endpoint'); -var Endpoint = require('../../lib/metadata_service/endpoint'); -var EndpointMode = require('../../lib/metadata_service/endpoint_mode'); -var ENDPOINT_CONFIG_OPTIONS = require('../../lib/metadata_service/endpoint_config_options').ENDPOINT_CONFIG_OPTIONS; +var Endpoint = require('../../lib/metadata_service/get_endpoint')(); +var EndpointMode = require('../../lib/metadata_service/get_endpoint_mode')(); var AWS = helpers.AWS; @@ -22,26 +21,28 @@ if (AWS.util.isNode()) { [Endpoint.IPv4, EndpointMode.IPv4], [Endpoint.IPv6, EndpointMode.IPv6], ].forEach(function([endpoint, endpointMode]) { - it('returns endpoint:'+ endpoint + ' for endpointMode:' + endpointMode, function() { + it('returns endpoint: "'+ endpoint + '" for endpointMode: ' + endpointMode, function() { + let loadConfigFirstCall = true; helpers.spyOn(AWS.util, 'loadConfig').andCallFake(function(options) { - if (options === ENDPOINT_CONFIG_OPTIONS) { + if (loadConfigFirstCall) { + loadConfigFirstCall = false; return undefined; - } else { - return endpointMode; } + return endpointMode; }); expect(getMetadataServiceEndpoint()).to.equal(endpoint); }); }); it('throws error for invalid endpointMode:invalid', function() { + let loadConfigFirstCall = true; const invalidEndpointMode = 'invalid'; helpers.spyOn(AWS.util, 'loadConfig').andCallFake(function(options) { - if (options === ENDPOINT_CONFIG_OPTIONS) { + if (loadConfigFirstCall) { + loadConfigFirstCall = false; return undefined; - } else { - return invalidEndpointMode; } + return invalidEndpointMode; }); expect(function() { getMetadataServiceEndpoint();