Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert metadata service modules to functions #3954

Merged
merged 3 commits into from Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions lib/metadata_service/endpoint.js

This file was deleted.

14 changes: 0 additions & 14 deletions lib/metadata_service/endpoint_config_options.js

This file was deleted.

6 changes: 0 additions & 6 deletions lib/metadata_service/endpoint_mode.js

This file was deleted.

16 changes: 0 additions & 16 deletions lib/metadata_service/endpoint_mode_config_options.js

This file was deleted.

8 changes: 8 additions & 0 deletions 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;
12 changes: 12 additions & 0 deletions 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;
8 changes: 8 additions & 0 deletions lib/metadata_service/get_endpoint_mode.js
@@ -0,0 +1,8 @@
var getEndpointMode = function() {
return {
IPv4: 'IPv4',
IPv6: 'IPv6',
};
};

module.exports = getEndpointMode;
14 changes: 14 additions & 0 deletions 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;
8 changes: 4 additions & 4 deletions 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);
Expand Down
7 changes: 3 additions & 4 deletions 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() {
Expand Down
9 changes: 4 additions & 5 deletions 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() {
Expand Down
21 changes: 11 additions & 10 deletions 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;

Expand All @@ -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();
Expand Down