Skip to content

Commit

Permalink
chore: de-promisify that lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolk committed Jul 18, 2023
1 parent 823036a commit 343b9ea
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,81 +1,76 @@
const response = require('cfn-response');
const aws = require('aws-sdk');
const identity = new aws.CognitoIdentityServiceProvider();
exports.handler = (event, context, callback) => {

exports.handler = (event, context) => {
// Don't return promise, response.send() marks context as done internally
const ignoredPromise = handleEvent(event, context);
};

async function checkDomainAvailability(domainName) {
const params = { Domain: domainName };
try {
const res = await identity.describeUserPoolDomain(params).promise();
if (res.DomainDescription && res.DomainDescription.UserPool) {
return false;
}
return true;
} catch (err) {
return false;
}
}

async function deleteUserPoolDomain(domainName, userPoolId) {
const params = { Domain: domainName, UserPoolId: userPoolId };
await identity.deleteUserPoolDomain(params).promise();
}

async function createUserPoolDomain(domainName, userPoolId) {
const params = {
Domain: domainName,
UserPoolId: userPoolId,
};
await identity.createUserPoolDomain(params).promise();
}

async function handleEvent(event, context) {
const userPoolId = event.ResourceProperties.userPoolId;
const inputDomainName = event.ResourceProperties.hostedUIDomainName;
let deleteUserPoolDomain = (domainName) => {
let params = { Domain: domainName, UserPoolId: userPoolId };
return identity.deleteUserPoolDomain(params).promise();
};
if (event.RequestType == 'Delete') {
deleteUserPoolDomain(inputDomainName)
.then(() => {
response.send(event, context, response.SUCCESS, {});
})
.catch((err) => {
console.log(err);
response.send(event, context, response.FAILED, { err });
});
}
if (event.RequestType == 'Update' || event.RequestType == 'Create') {
let checkDomainAvailability = (domainName) => {
let params = { Domain: domainName };
return identity
.describeUserPoolDomain(params)
.promise()
.then((res) => {
if (res.DomainDescription && res.DomainDescription.UserPool) {
return false;
}
return true;
})
.catch((err) => {
return false;
});
};
let createUserPoolDomain = (domainName) => {
let params = { Domain: domainName, UserPoolId: userPoolId };
return identity.createUserPoolDomain(params).promise();
};
identity
.describeUserPool({ UserPoolId: userPoolId })
.promise()
.then((result) => {
if (inputDomainName) {
if (result.UserPool.Domain === inputDomainName) {
return;
try {
if (event.RequestType === 'Delete') {
await deleteUserPoolDomain(inputDomainName, userPoolId);
} else if (event.RequestType === 'Update' || event.RequestType === 'Create') {
const result = await identity.describeUserPool({ UserPoolId: userPoolId }).promise();
if (inputDomainName) {
if (result.UserPool.Domain === inputDomainName) {
return;
} else {
if (!result.UserPool.Domain) {
const isDomainAvailable = await checkDomainAvailability(inputDomainName);
if (isDomainAvailable) {
await createUserPoolDomain(inputDomainName, userPoolId);
} else {
throw new Error('Domain not available');
}
} else {
if (!result.UserPool.Domain) {
return checkDomainAvailability(inputDomainName).then((isDomainAvailable) => {
if (isDomainAvailable) {
return createUserPoolDomain(inputDomainName);
} else {
throw new Error('Domain not available');
}
});
const isDomainAvailable = await checkDomainAvailability(inputDomainName);
if (isDomainAvailable) {
await deleteUserPoolDomain(result.UserPool.Domain, userPoolId);
await createUserPoolDomain(inputDomainName, userPoolId);
} else {
return checkDomainAvailability(inputDomainName).then((isDomainAvailable) => {
if (isDomainAvailable) {
return deleteUserPoolDomain(result.UserPool.Domain).then(() => createUserPoolDomain(inputDomainName));
} else {
throw new Error('Domain not available');
}
});
throw new Error('Domain not available');
}
}
} else {
if (result.UserPool.Domain) {
return deleteUserPoolDomain(result.UserPool.Domain);
}
}
})
.then(() => {
response.send(event, context, response.SUCCESS, {});
})
.catch((err) => {
console.log(err);
response.send(event, context, response.FAILED, { err });
});
} else {
if (result.UserPool.Domain) {
await deleteUserPoolDomain(result.UserPool.Domain, userPoolId);
}
}
}
response.send(event, context, response.SUCCESS, {});
} catch (err) {
console.log(err);
response.send(event, context, response.FAILED, { err });
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -786,84 +786,79 @@ exports[`Check Auth Template Generated authstack template in manual flow 1`] = `
"ZipFile": "const response = require('cfn-response');
const aws = require('aws-sdk');
const identity = new aws.CognitoIdentityServiceProvider();
exports.handler = (event, context, callback) => {
exports.handler = (event, context) => {
// Don't return promise, response.send() marks context as done internally
const ignoredPromise = handleEvent(event, context);
};
async function checkDomainAvailability(domainName) {
const params = { Domain: domainName };
try {
const res = await identity.describeUserPoolDomain(params).promise();
if (res.DomainDescription && res.DomainDescription.UserPool) {
return false;
}
return true;
} catch (err) {
return false;
}
}
async function deleteUserPoolDomain(domainName, userPoolId) {
const params = { Domain: domainName, UserPoolId: userPoolId };
await identity.deleteUserPoolDomain(params).promise();
}
async function createUserPoolDomain(domainName, userPoolId) {
const params = {
Domain: domainName,
UserPoolId: userPoolId,
};
await identity.createUserPoolDomain(params).promise();
}
async function handleEvent(event, context) {
const userPoolId = event.ResourceProperties.userPoolId;
const inputDomainName = event.ResourceProperties.hostedUIDomainName;
let deleteUserPoolDomain = (domainName) => {
let params = { Domain: domainName, UserPoolId: userPoolId };
return identity.deleteUserPoolDomain(params).promise();
};
if (event.RequestType == 'Delete') {
deleteUserPoolDomain(inputDomainName)
.then(() => {
response.send(event, context, response.SUCCESS, {});
})
.catch((err) => {
console.log(err);
response.send(event, context, response.FAILED, { err });
});
}
if (event.RequestType == 'Update' || event.RequestType == 'Create') {
let checkDomainAvailability = (domainName) => {
let params = { Domain: domainName };
return identity
.describeUserPoolDomain(params)
.promise()
.then((res) => {
if (res.DomainDescription && res.DomainDescription.UserPool) {
return false;
}
return true;
})
.catch((err) => {
return false;
});
};
let createUserPoolDomain = (domainName) => {
let params = { Domain: domainName, UserPoolId: userPoolId };
return identity.createUserPoolDomain(params).promise();
};
identity
.describeUserPool({ UserPoolId: userPoolId })
.promise()
.then((result) => {
if (inputDomainName) {
if (result.UserPool.Domain === inputDomainName) {
return;
try {
if (event.RequestType === 'Delete') {
await deleteUserPoolDomain(inputDomainName, userPoolId);
} else if (event.RequestType === 'Update' || event.RequestType === 'Create') {
const result = await identity.describeUserPool({ UserPoolId: userPoolId }).promise();
if (inputDomainName) {
if (result.UserPool.Domain === inputDomainName) {
return;
} else {
if (!result.UserPool.Domain) {
const isDomainAvailable = await checkDomainAvailability(inputDomainName);
if (isDomainAvailable) {
await createUserPoolDomain(inputDomainName, userPoolId);
} else {
throw new Error('Domain not available');
}
} else {
if (!result.UserPool.Domain) {
return checkDomainAvailability(inputDomainName).then((isDomainAvailable) => {
if (isDomainAvailable) {
return createUserPoolDomain(inputDomainName);
} else {
throw new Error('Domain not available');
}
});
const isDomainAvailable = await checkDomainAvailability(inputDomainName);
if (isDomainAvailable) {
await deleteUserPoolDomain(result.UserPool.Domain, userPoolId);
await createUserPoolDomain(inputDomainName, userPoolId);
} else {
return checkDomainAvailability(inputDomainName).then((isDomainAvailable) => {
if (isDomainAvailable) {
return deleteUserPoolDomain(result.UserPool.Domain).then(() => createUserPoolDomain(inputDomainName));
} else {
throw new Error('Domain not available');
}
});
throw new Error('Domain not available');
}
}
} else {
if (result.UserPool.Domain) {
return deleteUserPoolDomain(result.UserPool.Domain);
}
}
})
.then(() => {
response.send(event, context, response.SUCCESS, {});
})
.catch((err) => {
console.log(err);
response.send(event, context, response.FAILED, { err });
});
} else {
if (result.UserPool.Domain) {
await deleteUserPoolDomain(result.UserPool.Domain, userPoolId);
}
}
}
response.send(event, context, response.SUCCESS, {});
} catch (err) {
console.log(err);
response.send(event, context, response.FAILED, { err });
}
};
}
",
},
"Handler": "index.handler",
Expand Down

0 comments on commit 343b9ea

Please sign in to comment.