Source: aws-component.js

  1. const assert = require('assert')
  2. const _ = require('lodash');
  3. const AWSObject = require('./aws-object').AWSObject
  4. const Policy = require('./policy').Policy;
  5. /**
  6. * A super class to represent an AWS component.
  7. * Some functions are to be implemented by sub classes.
  8. * @abstract
  9. */
  10. class AWSComponent extends AWSObject {
  11. /**
  12. * Don't call this manually. Use creator function in {@link CloudFormation}
  13. * @param {string} stackName
  14. * @param {string} baseName
  15. */
  16. constructor(stackName, baseName) {
  17. super(stackName, baseName)
  18. this.policyStatements = []
  19. this.assumeRolePolicyDocument = null
  20. this._crossStackValues = {}
  21. }
  22. get crossStackValues() {
  23. return _.clone(this._crossStackValues)
  24. }
  25. setCrossStackValue(name, value) {
  26. this._crossStackValues[name] = value
  27. }
  28. get sameStackValues() {
  29. return {
  30. 'ID': { "Ref": this.fullName },
  31. 'ARN': {
  32. "Fn::GetAtt": [
  33. this.fullName,
  34. "Arn"
  35. ]
  36. }
  37. }
  38. }
  39. /**
  40. * Get stack value of this component, variables can be:
  41. * ARN, ID, PROVIDERNAME, etc
  42. * If this component is deployed and checked, the actual value will be provided.
  43. * Otherwise, AWS CloudFormation intrinsic specs will be returned, such as {"Ref": <logicName>}
  44. *
  45. * @param {String} key ARN | ID | PROVIDERNAME
  46. */
  47. getValue(key) {
  48. return this.crossStackValues[key] || this.sameStackValues[key]
  49. }
  50. get outputVariableKeys() {
  51. return _.keys(this.outputSpecs)
  52. }
  53. get outputSpecs() {
  54. //Default: get ARN and ID
  55. let specs = {}
  56. const id_spec = `${this.stackName}${this.fullName}ID`;
  57. specs[id_spec] = {
  58. "Description": `The ID for resource ${this.fullName} of stack ${this.stackName}`,
  59. "Value": { "Ref": this.fullName },
  60. "Export": {
  61. "Name": id_spec
  62. }
  63. }
  64. const arn_spec = `${this.stackName}${this.fullName}ARN`;
  65. specs[arn_spec] = {
  66. "Description": `The ARN for resource ${this.fullName} of stack ${this.stackName}`,
  67. "Value": {
  68. "Fn::GetAtt": [
  69. this.fullName,
  70. "Arn"
  71. ]
  72. },
  73. "Export": {
  74. "Name": arn_spec
  75. }
  76. }
  77. return specs
  78. }
  79. roleName() {
  80. const theRole = this.defaultRole;
  81. return theRole ? theRole.fullName : null;
  82. }
  83. getDefaultRole() {
  84. console.warn('Deprecated. Use .defaultRole() instead');
  85. return this.defaultRole;
  86. }
  87. get defaultRole() {
  88. if (!_.isEmpty(this.policyStatements) && this.assumeRolePolicyDocument) {
  89. const Role = require('./role').Role;
  90. let role = new Role(this.stackName, this.fullName)
  91. role.policyStatements = this.policyStatements
  92. if (this.assumeRolePolicyDocument) {
  93. role.assumeRolePolicyDocument = this.assumeRolePolicyDocument
  94. }
  95. return role
  96. }
  97. else {
  98. return null;
  99. }
  100. }
  101. /**
  102. * Get the policy statement object for the given access levels
  103. * @returns {object} policy statement JSON
  104. * @param {array} accessLevels array with string element which possible values are: ACCESS_LEVEL_READ | ACCESS_LEVEL_WRITE | ACCESS_LEVEL_ADMIN
  105. * @param {string} item optional
  106. */
  107. policyStatementForAccess(accessLevels, item) {
  108. accessLevels.forEach((accessLevel) => {
  109. assert.ok([AWSComponent.ACCESS_LEVEL_READ,
  110. AWSComponent.ACCESS_LEVEL_WRITE,
  111. AWSComponent.ACCESS_LEVEL_ADMIN
  112. ].indexOf(accessLevel) >= 0, `Invalid access level: ${accessLevel}`)
  113. })
  114. return this.policyStatementForAccessImpl(accessLevels, item)
  115. }
  116. /**
  117. * Get the policy statement object for the given access levels. Should be implemented by sub classes.
  118. * @abstract
  119. * @param {array} accessLevels array with string element which possible values are: ACCESS_LEVEL_READ | ACCESS_LEVEL_WRITE | ACCESS_LEVEL_ADMIN
  120. * @param {string} item optional
  121. */
  122. policyStatementForAccessImpl(accessLevels, item) {
  123. throw new Error('This function must be overriden in sub classes')
  124. }
  125. }
  126. //Static fields
  127. AWSComponent.ACCESS_LEVEL_READ = 'ACCESS_LEVEL_READ'
  128. AWSComponent.ACCESS_LEVEL_WRITE = 'ACCESS_LEVEL_WRITE'
  129. AWSComponent.ACCESS_LEVEL_ADMIN = 'ACCESS_LEVEL_ADMIN'
  130. exports.AWSComponent = AWSComponent