in source/packages/services/greengrass2-provisioning/src/cores/cores.dao.ts [146:274]
public async associateTemplate(
coreName: string,
templateName: string,
templateVersion: number,
state: 'desired' | 'reported',
deploymentStatus?: string,
deploymentStatusMessage?: string
): Promise<void> {
logger.debug(
`cores.dao associateTemplate: in: coreName:${coreName}, templateName:${templateName}, templateVersion:${templateVersion}, state:${state}, deploymentStatus:${deploymentStatus}, deploymentStatusMessage:${deploymentStatusMessage}`
);
ow(coreName, ow.string.nonEmpty);
ow(state, 'state', ow.string.oneOf(['reported', 'desired']));
const params: UpdateCommandInput = {
TableName: process.env.AWS_DYNAMODB_TABLE_NAME,
Key: {
pk: createDelimitedAttribute(PkType.CoreDevice, coreName),
sk: createDelimitedAttribute(PkType.CoreDevice, coreName),
},
};
const setExpressions: string[] = [];
const removeExpressions: string[] = [];
if (state === 'desired') {
// always update the desired template and version
setExpressions.push(
'#templateName=:templateName',
'#templateVersion=:templateVersion',
'#siKey2=:siKey2',
'#siKey3=:siKey3'
);
params.ExpressionAttributeNames = {
'#templateName': 'desiredTemplateName',
'#templateVersion': 'desiredTemplateVersion',
'#siKey2': 'siKey2',
'#siKey3': 'siKey3',
};
params.ExpressionAttributeValues = {
':templateName': templateName,
':templateVersion': templateVersion,
':siKey2': createDelimitedAttribute(
PkType.Template,
templateName,
PkType.CoreDevice
),
':siKey3': createDelimitedAttribute(
PkType.Template,
templateName,
PkType.TemplateVersion,
templateVersion
),
};
if (deploymentStatus !== undefined) {
// if a deployment status has been specified, update it
setExpressions.push('#deploymentStatus=:deploymentStatus');
params.ExpressionAttributeNames['#deploymentStatus'] = 'deploymentStatus';
params.ExpressionAttributeValues[':deploymentStatus'] = deploymentStatus;
params.ExpressionAttributeNames['#siKey4'] = 'siKey4';
params.ExpressionAttributeNames['#siKey5'] = 'siKey5';
params.ExpressionAttributeNames['#siKey6'] = 'siKey6';
if (deploymentStatus !== 'SUCCESSFUL') {
// if a deployment has failed, update siKey 4/5/6 which are used for tracking failed deployments
setExpressions.push('#siKey4=:siKey4', '#siKey5=:siKey5', '#siKey6=:siKey6');
params.ExpressionAttributeValues[':siKey4'] = createDelimitedAttribute(
PkType.DeploymentStatus,
'FAILED'
);
params.ExpressionAttributeValues[':siKey5'] = createDelimitedAttribute(
PkType.DeploymentStatus,
'FAILED',
PkType.Template,
templateName
);
params.ExpressionAttributeValues[':siKey6'] = createDelimitedAttribute(
PkType.DeploymentStatus,
'FAILED',
PkType.Template,
templateName,
PkType.TemplateVersion,
templateVersion
);
} else {
// else we remove the si keys
removeExpressions.push('#siKey4', '#siKey5', '#siKey6');
}
}
params.ExpressionAttributeNames['#deploymentStatusMessage'] =
'deploymentStatusMessage';
if (deploymentStatusMessage !== undefined) {
setExpressions.push('#deploymentStatusMessage=:deploymentStatusMessage');
params.ExpressionAttributeValues[':deploymentStatusMessage'] =
deploymentStatusMessage;
} else {
removeExpressions.push('#deploymentStatusMessage');
}
} else if (state === 'reported') {
// for repotred we always just update the template and version
setExpressions.push(
'#templateName=:templateName',
'#templateVersion=:templateVersion'
);
params.ExpressionAttributeNames = {
'#templateName': 'reportedTemplateName',
'#templateVersion': 'reportedTemplateVersion',
};
params.ExpressionAttributeValues = {
':templateName': templateName,
':templateVersion': templateVersion,
};
}
params.UpdateExpression = `SET ${setExpressions.join(',')}`;
if (removeExpressions.length > 0)
params.UpdateExpression += ` REMOVE ${removeExpressions.join(',')}`;
logger.silly(`cores.dao associateTemplate: params:${JSON.stringify(params)}`);
const r = await this.dbc.send(new UpdateCommand(params));
logger.silly(`cores.dao associateTemplate: r:${JSON.stringify(r)}`);
logger.debug(`cores.dao associateTemplate: exit:`);
}