diff --git a/lib/sandbox.js b/lib/sandbox.js index 7b30f42..a102239 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -515,7 +515,25 @@ function lookupSandbox(spec, callback) { return; } - callback(new Error('Found ' + filtered.length + ' matching sandboxes.')); + // multiple sandboxes should be the very exception and in those cases the most recently created + // sandbox should be addressed with the requested operation. in all other cases the operation should + // be skipped and error messages returned. + filtered = filtered.sort( function sortFilteredSandboxesDesc(sb1, sb2) { + return sb1.createdAt.localeCompare(sb2.createdAt) * -1; + }); + + var sandboxIndex = 1; + filtered.forEach( function(sbxInfo) { + if (sandboxIndex === 1) { + console.warn('More than one sandbox found. Executing the operation only for ' + + 'the sandbox tha has been created on ' + sbxInfo.createdAt); + callback(undefined , sbxInfo); + } else { + callback(new Error('Ambigious sandbox ID. Skipping operation for the sandbox that has ' + + 'been created on ' + sbxInfo.createdAt)); + } + sandboxIndex++; + }); }); }