Skip to content
This repository was archived by the owner on Jun 5, 2024. It is now read-only.

Commit eaf876a

Browse files
authoredOct 7, 2022
Merge pull request #150 from bizob2828/fix-dynamo-port
default dynamodb port to 443 when undefined on endpoint instance
2 parents ce907cc + fd1f5e3 commit eaf876a

File tree

7 files changed

+87
-17
lines changed

7 files changed

+87
-17
lines changed
 

‎lib/util.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
'use strict'
7+
const UNKNOWN = 'Unknown'
8+
79
function grabLastUrlSegment(url = '/') {
810
// cast URL as string, and an empty
911
// string for null, undefined, NaN etc.
@@ -14,6 +16,22 @@ function grabLastUrlSegment(url = '/') {
1416
return lastItem
1517
}
1618

19+
/**
20+
* Retrieves the db segment params from endpoint and command parameters
21+
*
22+
* @param {Object} endpoint instance of ddb endpoint
23+
* @param {Object} params parameters passed to a ddb command
24+
* @returns {Object}
25+
*/
26+
function setDynamoParameters(endpoint, params) {
27+
return {
28+
host: endpoint && (endpoint.host || endpoint.hostname),
29+
port_path_or_id: (endpoint && endpoint.port) || 443,
30+
collection: (params && params.TableName) || UNKNOWN
31+
}
32+
}
33+
1734
module.exports = {
18-
grabLastUrlSegment
35+
grabLastUrlSegment,
36+
setDynamoParameters
1937
}

‎lib/v2/dynamodb.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const DOC_CLIENT_OPERATIONS = [
2929
'scan'
3030
]
3131

32+
const { setDynamoParameters } = require('../util')
33+
3234
function instrument(shim, AWS) {
3335
shim.setDatastore(shim.DYNAMODB)
3436

@@ -43,11 +45,7 @@ function instrument(shim, AWS) {
4345

4446
return {
4547
name: operationName,
46-
parameters: {
47-
host: this.endpoint && this.endpoint.host,
48-
port_path_or_id: this.endpoint && this.endpoint.port,
49-
collection: (params && params.TableName) || 'Unknown'
50-
},
48+
parameters: setDynamoParameters(this.endpoint, params),
5149
callback: shim.LAST,
5250
opaque: true
5351
}

‎lib/v3/dynamodb-util.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
*/
55

66
'use strict'
7-
8-
const UNKNOWN = 'Unknown'
7+
const { setDynamoParameters } = require('../util')
98

109
/**
1110
* Returns the spec for Dynamo commands
@@ -18,12 +17,9 @@ const UNKNOWN = 'Unknown'
1817
*/
1918
function getDynamoSpec(shim, original, name, args) {
2019
const [{ input }] = args
21-
const collection = (input && input.TableName) || UNKNOWN
22-
const host = this.endpoint && this.endpoint.hostname
23-
const portPathOrId = this.endpoint && this.endpoint.port
2420
return {
2521
name: this.commandName,
26-
parameters: { host, port_path_or_id: portPathOrId, collection },
22+
parameters: setDynamoParameters(this.endpoint, input),
2723
callback: shim.LAST,
2824
opaque: true,
2925
promise: true

‎tests/unit/util.tap.js

+56-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict'
77
const tap = require('tap')
8-
const { grabLastUrlSegment } = require('../../lib/util')
8+
const { grabLastUrlSegment, setDynamoParameters } = require('../../lib/util')
99
tap.test('Utility Functions', (t) => {
1010
t.ok(grabLastUrlSegment, 'imported function successfully')
1111

@@ -30,7 +30,61 @@ tap.test('Utility Functions', (t) => {
3030

3131
for (const [, fixture] of fixtures.entries()) {
3232
const result = grabLastUrlSegment(fixture.input)
33-
t.equals(result, fixture.output, `expecting ${result} to equal ${fixture.output}`)
33+
t.equal(result, fixture.output, `expecting ${result} to equal ${fixture.output}`)
3434
}
3535
t.end()
3636
})
37+
38+
tap.test('DB parameters', (t) => {
39+
t.autoend()
40+
41+
t.test('default values', (t) => {
42+
const input = {}
43+
const endpoint = {}
44+
const result = setDynamoParameters(endpoint, input)
45+
t.same(
46+
result,
47+
{
48+
host: undefined,
49+
port_path_or_id: 443,
50+
collection: 'Unknown'
51+
},
52+
'should set default values for parameters'
53+
)
54+
t.end()
55+
})
56+
57+
// v2 uses host key
58+
t.test('host, port, collection', (t) => {
59+
const input = { TableName: 'unit-test' }
60+
const endpoint = { host: 'unit-test-host', port: '123' }
61+
const result = setDynamoParameters(endpoint, input)
62+
t.same(
63+
result,
64+
{
65+
host: endpoint.host,
66+
port_path_or_id: endpoint.port,
67+
collection: input.TableName
68+
},
69+
'should set appropriate parameters'
70+
)
71+
t.end()
72+
})
73+
74+
// v3 uses hostname key
75+
t.test('hostname, port, collection', (t) => {
76+
const input = { TableName: 'unit-test' }
77+
const endpoint = { hostname: 'unit-test-host', port: '123' }
78+
const result = setDynamoParameters(endpoint, input)
79+
t.same(
80+
result,
81+
{
82+
host: endpoint.hostname,
83+
port_path_or_id: endpoint.port,
84+
collection: input.TableName
85+
},
86+
'should set appropriate parameters'
87+
)
88+
t.end()
89+
})
90+
})

‎tests/versioned/v2/dynamodb.tap.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ function finish(t, tests, tx) {
128128
'should have operation in segment name'
129129
)
130130
const attrs = segment.attributes.get(common.SEGMENT_DESTINATION)
131+
attrs.port_path_or_id = parseInt(attrs.port_path_or_id, 10)
131132
t.matches(
132133
attrs,
133134
{
134135
'host': String,
135-
'port_path_or_id': String,
136+
'port_path_or_id': Number,
136137
'product': 'DynamoDB',
137138
'collection': String,
138139
'aws.operation': operation,

‎tests/versioned/v3/client-dynamodb.tap.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,13 @@ tap.test('DynamoDB', (t) => {
202202
'should have operation in segment name'
203203
)
204204
const attrs = segment.attributes.get(common.SEGMENT_DESTINATION)
205+
attrs.port_path_or_id = parseInt(attrs.port_path_or_id, 10)
206+
205207
t.match(
206208
attrs,
207209
{
208210
'host': String,
209-
'port_path_or_id': String,
211+
'port_path_or_id': Number,
210212
'product': 'DynamoDB',
211213
'collection': String,
212214
'aws.operation': command.constructor.name,

‎tests/versioned/v3/lib-dynamodb.tap.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,12 @@ function finish(t, tests, tx) {
165165
'should have operation in segment name'
166166
)
167167
const attrs = segment.attributes.get(common.SEGMENT_DESTINATION)
168+
attrs.port_path_or_id = parseInt(attrs.port_path_or_id, 10)
168169
t.match(
169170
attrs,
170171
{
171172
'host': String,
172-
'port_path_or_id': String,
173+
'port_path_or_id': Number,
173174
'product': 'DynamoDB',
174175
'collection': String,
175176
'aws.operation': operation,

0 commit comments

Comments
 (0)
This repository has been archived.