Skip to content

Commit 3a18b80

Browse files
committed
Initial import.
1 parent 3dfc308 commit 3a18b80

File tree

12 files changed

+208
-0
lines changed

12 files changed

+208
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# stencil-aws-account-resolver
2+
3+
Stencil blocks to resolve account name and related values of AWS account.

blocks/alias.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let alias = null;
2+
3+
module.exports.resolve = ({slsHelper}) => {
4+
if (alias !== null) {
5+
return alias;
6+
}
7+
8+
alias = new Promise(async (resolve, reject) => {
9+
try {
10+
const {AccountAliases} = await slsHelper.sendAwsRequest('IAM', 'listAccountAliases');
11+
resolve(AccountAliases[0]);
12+
} catch (error) {
13+
reject(error);
14+
}
15+
});
16+
return alias;
17+
};

blocks/companyDomain.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let companyDomain = null;
2+
3+
module.exports.resolve = ({variableUtils}) => {
4+
if (companyDomain !== null) {
5+
return companyDomain;
6+
}
7+
8+
companyDomain = new Promise(async (resolve, reject) => {
9+
try {
10+
// TODO resolve in parallel
11+
const companyName = await variableUtils.resolveVariable('stencil(account):companyName');
12+
const companyTld = await variableUtils.resolveVariable('stencil(account):companyTld');
13+
14+
resolve(`${companyName}.${companyTld}`);
15+
} catch (error) {
16+
reject(error);
17+
}
18+
});
19+
return companyDomain;
20+
};
21+

blocks/companyName.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let companyName = null;
2+
3+
module.exports.resolve = ({variableUtils}) => {
4+
if (companyName !== null) {
5+
return companyName;
6+
}
7+
8+
companyName = new Promise(async (resolve, reject) => {
9+
try {
10+
const accountName = await variableUtils.resolveVariable('stencil(account):name');
11+
resolve(accountName.split('-')[0]);
12+
} catch (error) {
13+
reject(error);
14+
}
15+
});
16+
return companyName;
17+
};
18+

blocks/companyTld.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports.resolve = ({variableUtils}) => {
2+
return variableUtils.resolveVariable('ssm(us-east-1):/ib2/aws/companyTld'); // TODO ib2 ref
3+
};

blocks/domain.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let accountDomain = null;
2+
3+
module.exports.resolve = ({variableUtils}) => {
4+
if (accountDomain !== null) {
5+
return accountDomain;
6+
}
7+
8+
accountDomain = new Promise(async (resolve, reject) => {
9+
try {
10+
// TODO resolve in parallel
11+
const accountUnit = await variableUtils.resolveVariable('stencil(account):unit');
12+
const companyDomain = await variableUtils.resolveVariable('stencil(account):companyDomain');
13+
14+
resolve(`${accountUnit}.${companyDomain}`);
15+
} catch (error) {
16+
reject(error);
17+
}
18+
});
19+
return accountDomain;
20+
};

blocks/domainHostedZoneId.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let accountDomainHostedZoneId = null;
2+
3+
module.exports.resolve = ({variableUtils, slsHelper}) => {
4+
if (accountDomainHostedZoneId !== null) {
5+
return accountDomainHostedZoneId;
6+
}
7+
8+
accountDomainHostedZoneId = new Promise(async (resolve, reject) => {
9+
try {
10+
const accountDomain = await variableUtils.resolveVariable('stencil(account):domain');
11+
12+
// TODO read all pages if response is paginated
13+
const {HostedZones} = await slsHelper.sendAwsRequest('Route53', 'listHostedZonesByName', {DNSName: `${accountDomain}.`});
14+
15+
const zoneIds = HostedZones
16+
.filter(e => e.Name === `${accountDomain}.` && e.Config.PrivateZone === false)
17+
.map(e => e.Id)
18+
.map(e => e.split('/')[2]);
19+
20+
if (zoneIds.length === 1) {
21+
resolve(zoneIds[0]);
22+
} else {
23+
reject(`Expected exact one match of public route53 hosted zone for '${accountDomain}' domain. Found '${zoneIds.length}' (${zoneIds})."`)
24+
}
25+
} catch (error) {
26+
reject(error);
27+
}
28+
});
29+
return accountDomainHostedZoneId;
30+
};

blocks/id.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let id = null;
2+
3+
module.exports.resolve = ({slsHelper}) => {
4+
if (id !== null) {
5+
return id;
6+
}
7+
8+
id = new Promise(async (resolve, reject) => {
9+
try {
10+
const {Account} = await slsHelper.sendAwsRequest('STS', 'getCallerIdentity');
11+
resolve(Account);
12+
} catch (error) {
13+
reject(error);
14+
}
15+
});
16+
return id;
17+
};

blocks/name.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let name = null;
2+
3+
module.exports.resolve = ({variableUtils, slsHelper}) => {
4+
if (name !== null) {
5+
return name;
6+
}
7+
8+
name = new Promise(async (resolve, reject) => {
9+
try {
10+
const accountId = await variableUtils.resolveVariable('stencil(account):id');
11+
const {Account} = await slsHelper.sendAwsRequest('Organizations', 'describeAccount', {AccountId: accountId});
12+
resolve(Account.Name);
13+
} catch (error) {
14+
reject(error);
15+
}
16+
});
17+
return name;
18+
};

blocks/unit.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let unit = null;
2+
3+
module.exports.resolve = ({variableUtils}) => {
4+
if (unit !== null) {
5+
return unit;
6+
}
7+
8+
unit = new Promise(async (resolve, reject) => {
9+
try {
10+
const accountName = await variableUtils.resolveVariable('stencil(account):name');
11+
let accountUnit = accountName.split('-').slice(1).join('-');
12+
13+
if (accountUnit === '') {
14+
accountUnit = 'master';
15+
}
16+
17+
resolve(accountUnit);
18+
} catch (error) {
19+
reject(error);
20+
}
21+
});
22+
return unit;
23+
};
24+

0 commit comments

Comments
 (0)