in functions/source/nic-attachment/lib/aws/index.js [3409:3523]
async updateTgwRouteTable(attachmentId) {
let params, waitTimeStart, data;
params = {
Filters: [
{
Name: 'transit-gateway-attachment-id',
Values: [attachmentId]
}
]
};
let promiseEmitter = () => {
return ec2
.describeTransitGatewayAttachments(params)
.promise()
.catch(error => {
logger.warn(
'error in describeTransitGatewayAttachments ' +
`>${JSON.stringify(
error instanceof Error
? {
message: error.message,
stack: error.stack
}
: error
)}`
);
});
};
let validator = result => {
logger.debug(`TransitGatewayAttachments: ${JSON.stringify(result)}`);
if (
result &&
result.TransitGatewayAttachments &&
result.TransitGatewayAttachments.length > 0
) {
// NOTE: by the time April 26, 2019. the AWS JavascriptSDK
// ec2.describeTransitGatewayAttachments cannot properly filter resource
// by resource-id. instead, it always return all resources so we must
// do the filtering in the function here.
// eslint-disable-next-line max-len
// ref link: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#describeTransitGatewayAttachments-property
let attachmentFound = null;
attachmentFound = result.TransitGatewayAttachments.find(attachment => {
return attachment.TransitGatewayAttachmentId === attachmentId;
});
logger.debug(
`attachmentFound: ${JSON.stringify(attachmentFound)}, ` +
`state: ${attachmentFound && attachmentFound.State}`
);
// need to wait for the attachment state become available
return attachmentFound && attachmentFound.State === 'available';
}
return false;
};
let counter = () => {
// force to end 30 seconds before script timeout.
if (Date.now() < process.env.SCRIPT_EXECUTION_EXPIRE_TIME - 30000) {
return false;
}
let waitTimeSec = (Date.now() - waitTimeStart) / 1000;
logger.error(
`VPN attachment cannot become available within ${waitTimeSec}` +
' seconds. Update failed.'
);
return true;
};
try {
waitTimeStart = Date.now();
// wait until transit gateway attachment become available
data = await AutoScaleCore.Functions.waitFor(promiseEmitter, validator, 5000, counter);
// update
let outboutRouteTable = await this.platform.getSettingItem(
'transit-gateway-route-table-outbound'
);
// add transit gateway route association to the inbound route table so all traffic
// going back to the TGW from any FGT will be routed to the right route (propagation)
// TODO: use the latest this._settings[] method
let inboutRouteTable = await this.platform.getSettingItem(
'transit-gateway-route-table-inbound'
);
let [propagationState, associationState] = await Promise.all([
this.platform.updateTgwRouteTablePropagation(attachmentId, outboutRouteTable),
this.platform.updateTgwRouteTableAssociation(attachmentId, inboutRouteTable)
]);
logger.info(
'transit gateway route table updated. ' +
'time used: ' +
`${(Date.now() - waitTimeStart) / 1000} seconds.` +
`propagation state: ${propagationState}, ` +
`association state: ${associationState}.`
);
return {
attachmentId: attachmentId,
propagationState: propagationState,
associationState: associationState
};
} catch (error) {
data = null;
logger.error(
JSON.stringify(
error instanceof Error ? { message: error.message, stack: error.stack } : error
)
);
logger.error(
'failed to wait for the transit gateway attachment ' +
`(id: ${attachmentId}) to become available.`
);
}
return data;
}