Skip to content

Commit f2b5aaa

Browse files
authored
Convert metadata service modules to functions (#3954)
1 parent 3e4e499 commit f2b5aaa

12 files changed

+64
-65
lines changed

lib/metadata_service/endpoint.js

-6
This file was deleted.

lib/metadata_service/endpoint_config_options.js

-14
This file was deleted.

lib/metadata_service/endpoint_mode.js

-6
This file was deleted.

lib/metadata_service/endpoint_mode_config_options.js

-16
This file was deleted.

lib/metadata_service/get_endpoint.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var getEndpoint = function() {
2+
return {
3+
IPv4: 'http://169.254.169.254',
4+
IPv6: 'http://[fd00:ec2::254]',
5+
};
6+
};
7+
8+
module.exports = getEndpoint;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var ENV_ENDPOINT_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT';
2+
var CONFIG_ENDPOINT_NAME = 'ec2_metadata_service_endpoint';
3+
4+
var getEndpointConfigOptions = function() {
5+
return {
6+
environmentVariableSelector: function(env) { return env[ENV_ENDPOINT_NAME]; },
7+
configFileSelector: function(profile) { return profile[CONFIG_ENDPOINT_NAME]; },
8+
default: undefined,
9+
};
10+
};
11+
12+
module.exports = getEndpointConfigOptions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var getEndpointMode = function() {
2+
return {
3+
IPv4: 'IPv4',
4+
IPv6: 'IPv6',
5+
};
6+
};
7+
8+
module.exports = getEndpointMode;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var EndpointMode = require('./get_endpoint_mode')();
2+
3+
var ENV_ENDPOINT_MODE_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE';
4+
var CONFIG_ENDPOINT_MODE_NAME = 'ec2_metadata_service_endpoint_mode';
5+
6+
var getEndpointModeConfigOptions = function() {
7+
return {
8+
environmentVariableSelector: function(env) { return env[ENV_ENDPOINT_MODE_NAME]; },
9+
configFileSelector: function(profile) { return profile[CONFIG_ENDPOINT_MODE_NAME]; },
10+
default: EndpointMode.IPv4,
11+
};
12+
};
13+
14+
module.exports = getEndpointModeConfigOptions;

lib/metadata_service/get_metadata_service_endpoint.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var AWS = require('../core');
22

3-
var Endpoint = require('./endpoint');
4-
var EndpointMode = require('./endpoint_mode');
3+
var Endpoint = require('./get_endpoint')();
4+
var EndpointMode = require('./get_endpoint_mode')();
55

6-
var ENDPOINT_CONFIG_OPTIONS = require('./endpoint_config_options').ENDPOINT_CONFIG_OPTIONS;
7-
var ENDPOINT_MODE_CONFIG_OPTIONS = require('./endpoint_mode_config_options').ENDPOINT_MODE_CONFIG_OPTIONS;
6+
var ENDPOINT_CONFIG_OPTIONS = require('./get_endpoint_config_options')();
7+
var ENDPOINT_MODE_CONFIG_OPTIONS = require('./get_endpoint_mode_config_options')();
88

99
var getMetadataServiceEndpoint = function() {
1010
var endpoint = AWS.util.loadConfig(ENDPOINT_CONFIG_OPTIONS);

test/metadata_service/endpoint_config_options.spec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
var helpers = require('./../helpers');
2-
var options = require('../../lib/metadata_service/endpoint_config_options');
2+
var ENDPOINT_CONFIG_OPTIONS = require('../../lib/metadata_service/get_endpoint_config_options')();
33

44
var AWS = helpers.AWS;
5-
var ENDPOINT_CONFIG_OPTIONS = options.ENDPOINT_CONFIG_OPTIONS;
6-
var ENV_ENDPOINT_NAME = options.ENV_ENDPOINT_NAME;
7-
var CONFIG_ENDPOINT_NAME = options.CONFIG_ENDPOINT_NAME;
5+
var ENV_ENDPOINT_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT';
6+
var CONFIG_ENDPOINT_NAME = 'ec2_metadata_service_endpoint';
87

98
if (AWS.util.isNode()) {
109
describe('endpointConfigOptions', function() {

test/metadata_service/endpoint_mode_config_options.spec.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
var helpers = require('./../helpers');
2-
var options = require('../../lib/metadata_service/endpoint_mode_config_options');
3-
var EndpointMode = require('../../lib/metadata_service/endpoint_mode');
2+
var ENDPOINT_MODE_CONFIG_OPTIONS = require('../../lib/metadata_service/get_endpoint_mode_config_options')();
3+
var EndpointMode = require('../../lib/metadata_service/get_endpoint_mode')();
44

55
var AWS = helpers.AWS;
6-
var ENDPOINT_MODE_CONFIG_OPTIONS = options.ENDPOINT_MODE_CONFIG_OPTIONS;
7-
var ENV_ENDPOINT_MODE_NAME = options.ENV_ENDPOINT_MODE_NAME;
8-
var CONFIG_ENDPOINT_MODE_NAME = options.CONFIG_ENDPOINT_MODE_NAME;
6+
var ENV_ENDPOINT_MODE_NAME = 'AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE';
7+
var CONFIG_ENDPOINT_MODE_NAME = 'ec2_metadata_service_endpoint_mode';
98

109
if (AWS.util.isNode()) {
1110
describe('endpointModeConfigOptions', function() {

test/metadata_service/get_metadata_service_endpoint.spec.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
var helpers = require('./../helpers');
22
var getMetadataServiceEndpoint = require('../../lib/metadata_service/get_metadata_service_endpoint');
33

4-
var Endpoint = require('../../lib/metadata_service/endpoint');
5-
var EndpointMode = require('../../lib/metadata_service/endpoint_mode');
6-
var ENDPOINT_CONFIG_OPTIONS = require('../../lib/metadata_service/endpoint_config_options').ENDPOINT_CONFIG_OPTIONS;
4+
var Endpoint = require('../../lib/metadata_service/get_endpoint')();
5+
var EndpointMode = require('../../lib/metadata_service/get_endpoint_mode')();
76

87
var AWS = helpers.AWS;
98

@@ -22,26 +21,28 @@ if (AWS.util.isNode()) {
2221
[Endpoint.IPv4, EndpointMode.IPv4],
2322
[Endpoint.IPv6, EndpointMode.IPv6],
2423
].forEach(function([endpoint, endpointMode]) {
25-
it('returns endpoint:'+ endpoint + ' for endpointMode:' + endpointMode, function() {
24+
it('returns endpoint: "'+ endpoint + '" for endpointMode: ' + endpointMode, function() {
25+
let loadConfigFirstCall = true;
2626
helpers.spyOn(AWS.util, 'loadConfig').andCallFake(function(options) {
27-
if (options === ENDPOINT_CONFIG_OPTIONS) {
27+
if (loadConfigFirstCall) {
28+
loadConfigFirstCall = false;
2829
return undefined;
29-
} else {
30-
return endpointMode;
3130
}
31+
return endpointMode;
3232
});
3333
expect(getMetadataServiceEndpoint()).to.equal(endpoint);
3434
});
3535
});
3636

3737
it('throws error for invalid endpointMode:invalid', function() {
38+
let loadConfigFirstCall = true;
3839
const invalidEndpointMode = 'invalid';
3940
helpers.spyOn(AWS.util, 'loadConfig').andCallFake(function(options) {
40-
if (options === ENDPOINT_CONFIG_OPTIONS) {
41+
if (loadConfigFirstCall) {
42+
loadConfigFirstCall = false;
4143
return undefined;
42-
} else {
43-
return invalidEndpointMode;
4444
}
45+
return invalidEndpointMode;
4546
});
4647
expect(function() {
4748
getMetadataServiceEndpoint();

0 commit comments

Comments
 (0)