Skip to content

Commit

Permalink
Add ssm lookup as primary value to companyDomain block.
Browse files Browse the repository at this point in the history
  • Loading branch information
walery committed Aug 9, 2023
1 parent b632ff3 commit 13f196a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Block | Resolution | Original source
`${stencil(account):name}` | acme-playground | API(`Organizations describeAccount`)`.Account.Name`
`${stencil(account):unit}` | playground | `${stencil(account):name}.split('-').slice(1).join('-')`
`${stencil(account):companyName}` | acme | `${stencil(account):name}.split('-')[0]`
`${stencil(account):companyDomain}` | acme.cloud | `${stencil(account):companyName}`.`${stencil(account):companyTld}`
`${stencil(account):companyDomain}` | acme.cloud | `ssm(us-east-1):/stencil/aws/companyDomain` or `${stencil(account):companyName}`.`${stencil(account):companyTld}` if ssm value is not available
`${stencil(account):domain}` | playground.acme.cloud | `${stencil(account):unit}`.`${stencil(account):companyDomain}`
`${stencil(account):domainHostedZoneId}` | Z3XXYYAABBCCDD | API(`Route53 listHostedZonesByName`)`.Id`
16 changes: 14 additions & 2 deletions blocks/companyDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports.resolve = ({variableUtils}) => {
return companyDomain;
}

companyDomain = new Promise((resolve, reject) => {
const ssmCompanyDomain = variableUtils.resolveVariable('ssm(us-east-1):/stencil/aws/companyDomain');
const compoundCompanyDomain = () => new Promise((resolve, reject) => {
const companyName = variableUtils.resolveVariable('stencil(account):companyName');
const companyTld = variableUtils.resolveVariable('stencil(account):companyTld');

Expand All @@ -17,6 +18,17 @@ module.exports.resolve = ({variableUtils}) => {
reject(error);
});
});

companyDomain = new Promise((resolve, reject) => {
ssmCompanyDomain
.then(companyDomainValue => {
resolve(companyDomainValue);
})
.catch(_ => {
compoundCompanyDomain()
.then(companyDomainValue => resolve(companyDomainValue))
.catch(error => reject(error));
});
});
return companyDomain;
};

26 changes: 18 additions & 8 deletions test/companyDomain.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const test = require('ava');

test.serial('should resolve companyDomain if stencil:companyName and stencil:companyTld provides default values', async t => {
const actual = await resolveCompanyDomain();
test.serial('should resolve compound companyDomain if ssm:companyDomain is not resolvable and stencil:companyName and stencil:companyTld provides default values', async t => {
const mockOverwrites = {
'ssm(us-east-1):/stencil/aws/companyDomain': undefined,
};

const actual = await resolveCompanyDomain(mockOverwrites);
t.is(actual, 'acme.bar');
});

test.serial('should resolve companyDomain if stencil:companyName and stencil:companyTld are overwritten', async t => {
test.serial('should resolve compound companyDomain if ssm:companyDomain is not resolvable and stencil:companyName and stencil:companyTld are overwritten', async t => {
const mockOverwrites = {
'ssm(us-east-1):/stencil/aws/companyDomain': undefined,
'stencil(account):companyName': Promise.resolve('inc'),
'stencil(account):companyTld': Promise.resolve('com'),
};
Expand All @@ -15,8 +20,9 @@ test.serial('should resolve companyDomain if stencil:companyName and stencil:com
t.is(actual, 'inc.com');
});

test.serial('should reject if stencil:companyTld is not resolvable', async t => {
test.serial('should reject if ssm:companyDomain and stencil:companyTld is not resolvable', async t => {
const mockOverwrites = {
'ssm(us-east-1):/stencil/aws/companyDomain': undefined,
'stencil(account):companyTld': undefined,
};

Expand All @@ -27,8 +33,9 @@ test.serial('should reject if stencil:companyTld is not resolvable', async t =>
t.is(actual.message, 'Unknown variable expression \'stencil(account):companyTld\'.');
});

test.serial('should reject if stencil:companyName is not resolvable', async t => {
test.serial('should reject if ssm:companyDomain and stencil:companyName is not resolvable', async t => {
const mockOverwrites = {
'ssm(us-east-1):/stencil/aws/companyDomain': undefined,
'stencil(account):companyName': undefined,
};

Expand All @@ -39,8 +46,9 @@ test.serial('should reject if stencil:companyName is not resolvable', async t =>
t.is(actual.message, 'Unknown variable expression \'stencil(account):companyName\'.');
});

test.serial('should reject if stencil:companyName and stencil:companyTld is not resolvable', async t => {
test.serial('should reject if ssm:companyDomain and stencil:companyName and stencil:companyTld is not resolvable', async t => {
const mockOverwrites = {
'ssm(us-east-1):/stencil/aws/companyDomain': undefined,
'stencil(account):companyName': undefined,
'stencil(account):companyTld': undefined,
};
Expand All @@ -57,16 +65,17 @@ test.serial('should return cached result if resolution happens more than once',
const actual = await underTest.resolve({
variableUtils: createVariableUtilsMock(),
});
t.is(actual, 'acme.bar');
t.is(actual, 'ssm.foo');

const mockOverwrites = {
'ssm(us-east-1):/stencil/aws/companyDomain': undefined,
'stencil(account):companyName': undefined,
};

const actualCached = await underTest.resolve({
variableUtils: createVariableUtilsMock(mockOverwrites),
});
t.is(actualCached, 'acme.bar');
t.is(actualCached, 'ssm.foo');
});

const resolveCompanyDomain = (overwrites = {}) => {
Expand All @@ -83,6 +92,7 @@ const createUncachedInstance = () => {

const createVariableUtilsMock = overwrites => {
const resolveVariableValues = {
'ssm(us-east-1):/stencil/aws/companyDomain': Promise.resolve('ssm.foo'),
'stencil(account):companyName': Promise.resolve('acme'),
'stencil(account):companyTld': Promise.resolve('bar'),
};
Expand Down

0 comments on commit 13f196a

Please sign in to comment.