|
1 |
| -const { EC2 } = require("../../../clients/client-ec2"); |
| 1 | +const { EC2, waitForVolumeAvailable } = require("../../../clients/client-ec2"); |
2 | 2 | const { Before, Given, Then } = require("cucumber");
|
3 | 3 |
|
4 |
| -/** |
5 |
| - * Waits for the volumeAvailable state by periodically calling the underlying |
6 |
| - * EC2.describeVolumes() operation every 5 seconds (at most 40 times) |
7 |
| - */ |
8 |
| -const waitForVolumeAvailable = (ec2, volumeId, callback) => { |
9 |
| - const maxAttempts = 40; |
10 |
| - let currentAttempt = 0; |
11 |
| - const delay = 5000; |
12 |
| - |
13 |
| - const checkForVolumeAvailable = () => { |
14 |
| - currentAttempt++; |
15 |
| - ec2.describeVolumes({ VolumeIds: [volumeId] }, (err, data) => { |
16 |
| - if (currentAttempt > maxAttempts) { |
17 |
| - callback(new Error("waitForVolumeAvailable: max attempts exceeded")); |
18 |
| - } else if (data && data.Volumes) { |
19 |
| - if (data.Volumes[0].State === "available") { |
20 |
| - callback(); |
21 |
| - } else if (data.Volumes[0].State === "deleted") { |
22 |
| - callback(new Error(`VolumeId ${data.Volumes[i].VolumeId} is in failure state`)); |
23 |
| - } else { |
24 |
| - setTimeout(function () { |
25 |
| - checkForVolumeAvailable(); |
26 |
| - }, delay); |
27 |
| - } |
28 |
| - } else { |
29 |
| - setTimeout(function () { |
30 |
| - checkForVolumeAvailable(); |
31 |
| - }, delay); |
32 |
| - } |
33 |
| - }); |
34 |
| - }; |
35 |
| - checkForVolumeAvailable(); |
| 4 | +const waitForVolumeAvailableCallback = (ec2, volumeId, callback) => { |
| 5 | + waitForVolumeAvailable({ client: ec2 }, { VolumeIds: [volumeId] }).then( |
| 6 | + function (data) { |
| 7 | + callback(); |
| 8 | + }, |
| 9 | + function (err) { |
| 10 | + callback(err); |
| 11 | + } |
| 12 | + ); |
36 | 13 | };
|
37 | 14 |
|
38 | 15 | Before({ tags: "@ec2" }, function (scenario, callback) {
|
@@ -82,7 +59,7 @@ Given("I attempt to copy an encrypted snapshot across regions", function (callba
|
82 | 59 | }
|
83 | 60 | volId = data.VolumeId;
|
84 | 61 |
|
85 |
| - waitForVolumeAvailable(srcEc2, volId, function (err) { |
| 62 | + waitForVolumeAvailableCallback(srcEc2, volId, function (err) { |
86 | 63 | if (err) {
|
87 | 64 | teardown();
|
88 | 65 | return callback(err);
|
|
0 commit comments