ruby-cdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +87 -0
  3. data/LICENSE +21 -0
  4. data/README.md +303 -0
  5. data/THIRD_PARTY_NOTICES.md +327 -0
  6. data/data/spec/META.json +33 -0
  7. data/data/spec/model.json.gz +0 -0
  8. data/exe/ruby-cdk +9 -0
  9. data/lib/ruby-cdk.rb +4 -0
  10. data/lib/ruby_cdk/assets/builder.rb +122 -0
  11. data/lib/ruby_cdk/assets/hasher.rb +175 -0
  12. data/lib/ruby_cdk/assets/record.rb +49 -0
  13. data/lib/ruby_cdk/assets/uploader.rb +139 -0
  14. data/lib/ruby_cdk/assets/zipper.rb +108 -0
  15. data/lib/ruby_cdk/assets.rb +116 -0
  16. data/lib/ruby_cdk/bootstrap.rb +289 -0
  17. data/lib/ruby_cdk/cfn/resource.rb +93 -0
  18. data/lib/ruby_cdk/cfn.rb +10 -0
  19. data/lib/ruby_cdk/cli/arguments.rb +41 -0
  20. data/lib/ruby_cdk/cli/aws.rb +35 -0
  21. data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
  22. data/lib/ruby_cdk/cli/diff.rb +98 -0
  23. data/lib/ruby_cdk/cli/failure.rb +80 -0
  24. data/lib/ruby_cdk/cli/io.rb +70 -0
  25. data/lib/ruby_cdk/cli/list.rb +46 -0
  26. data/lib/ruby_cdk/cli/new.rb +313 -0
  27. data/lib/ruby_cdk/cli/operation.rb +124 -0
  28. data/lib/ruby_cdk/cli/pairs.rb +32 -0
  29. data/lib/ruby_cdk/cli/report.rb +118 -0
  30. data/lib/ruby_cdk/cli/selection.rb +134 -0
  31. data/lib/ruby_cdk/cli/session.rb +290 -0
  32. data/lib/ruby_cdk/cli/shell.rb +30 -0
  33. data/lib/ruby_cdk/cli/synth.rb +58 -0
  34. data/lib/ruby_cdk/cli/validate.rb +101 -0
  35. data/lib/ruby_cdk/cli.rb +242 -0
  36. data/lib/ruby_cdk/constructs/bucket.rb +191 -0
  37. data/lib/ruby_cdk/constructs/function.rb +163 -0
  38. data/lib/ruby_cdk/constructs/grant.rb +90 -0
  39. data/lib/ruby_cdk/constructs/log_group.rb +115 -0
  40. data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
  41. data/lib/ruby_cdk/constructs/queue.rb +118 -0
  42. data/lib/ruby_cdk/constructs/role.rb +282 -0
  43. data/lib/ruby_cdk/constructs/table.rb +160 -0
  44. data/lib/ruby_cdk/constructs/topic.rb +88 -0
  45. data/lib/ruby_cdk/constructs.rb +56 -0
  46. data/lib/ruby_cdk/core/app.rb +146 -0
  47. data/lib/ruby_cdk/core/construct.rb +105 -0
  48. data/lib/ruby_cdk/core/elements.rb +107 -0
  49. data/lib/ruby_cdk/core/ids.rb +117 -0
  50. data/lib/ruby_cdk/core/stack.rb +291 -0
  51. data/lib/ruby_cdk/core/stateful.rb +156 -0
  52. data/lib/ruby_cdk/core.rb +15 -0
  53. data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
  54. data/lib/ruby_cdk/deploy/deployer.rb +805 -0
  55. data/lib/ruby_cdk/deploy/errors.rb +72 -0
  56. data/lib/ruby_cdk/deploy/fakes.rb +546 -0
  57. data/lib/ruby_cdk/deploy/output/base.rb +167 -0
  58. data/lib/ruby_cdk/deploy/output/human.rb +37 -0
  59. data/lib/ruby_cdk/deploy/output/json.rb +80 -0
  60. data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
  61. data/lib/ruby_cdk/deploy/parameters.rb +86 -0
  62. data/lib/ruby_cdk/deploy/poller.rb +72 -0
  63. data/lib/ruby_cdk/deploy/ports.rb +205 -0
  64. data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
  65. data/lib/ruby_cdk/deploy/states.rb +62 -0
  66. data/lib/ruby_cdk/deploy.rb +25 -0
  67. data/lib/ruby_cdk/diff/compare.rb +141 -0
  68. data/lib/ruby_cdk/diff/normalizer.rb +83 -0
  69. data/lib/ruby_cdk/diff/render.rb +242 -0
  70. data/lib/ruby_cdk/diff/security.rb +322 -0
  71. data/lib/ruby_cdk/diff/statement.rb +256 -0
  72. data/lib/ruby_cdk/diff/template.rb +524 -0
  73. data/lib/ruby_cdk/diff.rb +62 -0
  74. data/lib/ruby_cdk/env.rb +42 -0
  75. data/lib/ruby_cdk/errors.rb +38 -0
  76. data/lib/ruby_cdk/fn.rb +409 -0
  77. data/lib/ruby_cdk/lazy.rb +55 -0
  78. data/lib/ruby_cdk/nag/config.rb +303 -0
  79. data/lib/ruby_cdk/nag/engine.rb +152 -0
  80. data/lib/ruby_cdk/nag/report.rb +86 -0
  81. data/lib/ruby_cdk/nag/rule.rb +171 -0
  82. data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
  83. data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
  84. data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
  85. data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
  86. data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
  87. data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
  88. data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
  89. data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
  90. data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
  91. data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
  92. data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
  93. data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
  94. data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
  95. data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
  96. data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
  97. data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
  98. data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
  99. data/lib/ruby_cdk/nag/rules.rb +51 -0
  100. data/lib/ruby_cdk/nag/values.rb +305 -0
  101. data/lib/ruby_cdk/nag.rb +60 -0
  102. data/lib/ruby_cdk/schema/normalizer.rb +164 -0
  103. data/lib/ruby_cdk/schema/property_schema.rb +22 -0
  104. data/lib/ruby_cdk/schema/schema.rb +195 -0
  105. data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
  106. data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
  107. data/lib/ruby_cdk/schema/sources/download.rb +86 -0
  108. data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
  109. data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
  110. data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
  111. data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
  112. data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
  113. data/lib/ruby_cdk/schema/sources/update.rb +178 -0
  114. data/lib/ruby_cdk/schema/sources.rb +20 -0
  115. data/lib/ruby_cdk/schema/type_schema.rb +109 -0
  116. data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
  117. data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
  118. data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
  119. data/lib/ruby_cdk/schema/validator/report.rb +92 -0
  120. data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
  121. data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
  122. data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
  123. data/lib/ruby_cdk/schema/validator/template.rb +203 -0
  124. data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
  125. data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
  126. data/lib/ruby_cdk/schema/validator.rb +159 -0
  127. data/lib/ruby_cdk/schema.rb +19 -0
  128. data/lib/ruby_cdk/synth/manifest.rb +128 -0
  129. data/lib/ruby_cdk/synth/resolver.rb +326 -0
  130. data/lib/ruby_cdk/synth/rules.rb +83 -0
  131. data/lib/ruby_cdk/synth/serializer.rb +120 -0
  132. data/lib/ruby_cdk/synth/writer.rb +31 -0
  133. data/lib/ruby_cdk/synth.rb +255 -0
  134. data/lib/ruby_cdk/test/capture.rb +133 -0
  135. data/lib/ruby_cdk/test/match.rb +426 -0
  136. data/lib/ruby_cdk/test/matcher.rb +290 -0
  137. data/lib/ruby_cdk/test/private/absent.rb +34 -0
  138. data/lib/ruby_cdk/test/private/conditions.rb +39 -0
  139. data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
  140. data/lib/ruby_cdk/test/private/error.rb +48 -0
  141. data/lib/ruby_cdk/test/private/js.rb +326 -0
  142. data/lib/ruby_cdk/test/private/mappings.rb +39 -0
  143. data/lib/ruby_cdk/test/private/outputs.rb +39 -0
  144. data/lib/ruby_cdk/test/private/parameters.rb +39 -0
  145. data/lib/ruby_cdk/test/private/resources.rb +116 -0
  146. data/lib/ruby_cdk/test/private/section.rb +98 -0
  147. data/lib/ruby_cdk/test/private/sorting.rb +40 -0
  148. data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
  149. data/lib/ruby_cdk/test/private/type.rb +23 -0
  150. data/lib/ruby_cdk/test/template.rb +364 -0
  151. data/lib/ruby_cdk/test.rb +116 -0
  152. data/lib/ruby_cdk/values.rb +8 -0
  153. data/lib/ruby_cdk/version.rb +5 -0
  154. data/lib/ruby_cdk.rb +47 -0
  155. metadata +256 -0
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "construct"
4
+ require_relative "stateful"
5
+
6
+ module RubyCdk
7
+ # The root of the tree.
8
+ class App < Construct
9
+ # Where synthesis writes when neither the app file nor the command says.
10
+ DEFAULT_OUT_DIR = "cdk.out"
11
+
12
+ attr_reader :env, :out_dir, :stack_name_prefix, :stateful_default
13
+
14
+ class << self
15
+ # The App the command line runs: the one build! returned, or when there
16
+ # is none the only App in the process. Errors if there is none, or several.
17
+ def current
18
+ return @current if @current
19
+
20
+ case instances.length
21
+ when 1
22
+ instances.first
23
+ when 0
24
+ raise UsageError, "There is no RubyCdk::App: the app file creates one with RubyCdk::App.new"
25
+ else
26
+ raise UsageError, "There are #{instances.length} RubyCdk::Apps and none was built by App.build!: a command needs exactly one"
27
+ end
28
+ end
29
+
30
+ # Loads the app file and returns the one App it created. May be called
31
+ # again in the same process: each call builds a new App and makes it current.
32
+ #
33
+ # out_dir is where the command that called wants its files written, as
34
+ # the value of `--out`. An App the file creates with an explicit
35
+ # out_dir: keeps its own; one it creates without takes this; and without
36
+ # this either, DEFAULT_OUT_DIR.
37
+ def build!(app_file:, out_dir: nil)
38
+ path = File.expand_path(app_file.to_s)
39
+ unless File.file?(path)
40
+ raise UsageError, "There is no app file at #{path}"
41
+ end
42
+ if out_dir && !(out_dir.is_a?(String) && !out_dir.empty?)
43
+ raise UsageError, "App.build!: out_dir must be the path of a directory or nil, got #{out_dir.inspect}"
44
+ end
45
+
46
+ created = with_out_dir_of_command(out_dir) { newly_created { evaluate_app_file(path) } }
47
+ case created.length
48
+ when 1
49
+ @current = created.first
50
+ when 0
51
+ raise UsageError, "#{path} created no RubyCdk::App: the app file must create exactly one, with RubyCdk::App.new"
52
+ else
53
+ raise UsageError, "#{path} created #{created.length} RubyCdk::Apps: the app file must create exactly one"
54
+ end
55
+ end
56
+
57
+ # The user's app file: the one load in the gem that is not a literal require.
58
+ def evaluate_app_file(path)
59
+ load(path)
60
+ end
61
+
62
+ # The directory the command asked for while the app file runs, as the
63
+ # out_dir: given to build!. nil the rest of the time, so that an App
64
+ # created outside a command writes under DEFAULT_OUT_DIR. It is read,
65
+ # never written: build! sets it for the duration of the app file only.
66
+ def out_dir_of_command
67
+ @out_dir_of_command
68
+ end
69
+
70
+ # Every App created in this process, in creation order.
71
+ def instances
72
+ (@instances ||= []).dup.freeze
73
+ end
74
+
75
+ # Forgets every App, and which one is current. For tests, and for a tool
76
+ # that builds one app after another in one process.
77
+ def reset!
78
+ @instances = []
79
+ @current = nil
80
+ @out_dir_of_command = nil
81
+ nil
82
+ end
83
+
84
+ # Records an App as it is created. App#initialize calls it; nothing else does.
85
+ def register(app)
86
+ (@instances ||= []) << app
87
+ end
88
+
89
+ private
90
+
91
+ # What the block created, as Apps.
92
+ def newly_created
93
+ before = instances.length
94
+ yield
95
+ (@instances || []).drop(before)
96
+ end
97
+
98
+ # The block runs with the command's directory in place, and whatever
99
+ # happens in it, including an app file that raises, the value there
100
+ # before is put back: nothing carries over to the next build!.
101
+ def with_out_dir_of_command(out_dir)
102
+ before = @out_dir_of_command
103
+ @out_dir_of_command = out_dir
104
+ yield
105
+ ensure
106
+ @out_dir_of_command = before
107
+ end
108
+ end
109
+
110
+ def initialize(env: nil, out_dir: nil, stack_name_prefix: nil, stateful_default: :retain_except_on_create, &block)
111
+ if env && !env.is_a?(Env)
112
+ raise UsageError, "App.new: env must be a RubyCdk::Env, got #{env.inspect}"
113
+ end
114
+ directory = out_dir || App.out_dir_of_command || DEFAULT_OUT_DIR
115
+ unless directory.is_a?(String) && !directory.empty?
116
+ raise UsageError, "App.new: out_dir must be the path of a directory or nil, got #{out_dir.inspect}"
117
+ end
118
+ unless stack_name_prefix.nil? || stack_name_prefix.is_a?(String)
119
+ raise UsageError, "App.new: stack_name_prefix must be a String or nil, got #{stack_name_prefix.inspect}"
120
+ end
121
+
122
+ super(nil, "App")
123
+ @env = env
124
+ @out_dir = directory
125
+ @stack_name_prefix = stack_name_prefix
126
+ @stateful_default = Core::Stateful.validate_default!(stateful_default)
127
+ App.register(self)
128
+ block.call(self) if block
129
+ end
130
+
131
+ # The stacks of the app, in the order they were created.
132
+ def stacks
133
+ @children.select { |child| child.is_a?(Stack) }.freeze
134
+ end
135
+
136
+ # Runs every phase on every stack and writes the templates and the manifest.
137
+ # validate: false skips the registered validators; the value model of §4
138
+ # still applies, so a template is always plain data.
139
+ #
140
+ # Declared here because it is the tree's contract. The synth component
141
+ # implements it by reopening this class: core does not depend on synth.
142
+ def synth(validate: true)
143
+ raise NotImplementedError, "added by the synth component"
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../errors"
4
+ require_relative "../fn"
5
+ require_relative "ids"
6
+
7
+ module RubyCdk
8
+ # Any node in the tree. Building is immediate: a construct exists as soon as
9
+ # its constructor returns.
10
+ class Construct
11
+ # So that `Fn.ref` resolves inside any construct or Stack subclass.
12
+ #
13
+ # A block given to Stack.new is evaluated in the stack's context, but its
14
+ # constants are looked up where the block was written, so `Fn` is not in
15
+ # scope there. The idiom is `Fn = RubyCdk::Fn` at the top of the app file,
16
+ # the way a Ruby file imports a class, and the examples show it.
17
+ Fn = RubyCdk::Fn
18
+
19
+ # Letters, digits, underscore and hyphen. Logical IDs are narrower: alphanumeric only.
20
+ ID_PATTERN = /\A[A-Za-z0-9_-]+\z/
21
+
22
+ # The longest an id can be. It is CloudFormation's limit for a logical name:
23
+ # an id is a logical ID when it sits in a Stack, and Core::Ids owns the number.
24
+ MAX_ID_LENGTH = Core::Ids::MAX_LOGICAL_ID_LENGTH
25
+
26
+ attr_reader :scope, :id, :path
27
+
28
+ # scope is an App, a Stack or another Construct.
29
+ def initialize(scope, id)
30
+ unless scope.is_a?(Construct) || (scope.nil? && is_a?(App))
31
+ raise UsageError, "#{self.class} needs a scope: an App, a Stack or a Construct, got #{scope.inspect}"
32
+ end
33
+
34
+ @scope = scope
35
+ @id = Core::Ids.validate_id!(id, scope: scope)
36
+ @path = scope ? "#{scope.path}/#{@id}" : @id
37
+ @children = []
38
+ @prepared = false
39
+ scope.add(self) if scope
40
+ end
41
+
42
+ # The nodes added to this one, in the order they were created.
43
+ def children
44
+ @children.dup.freeze
45
+ end
46
+
47
+ # Short, so that an error message shows where a node is and not its whole tree:
48
+ # "#<RubyCdk::Cfn::Resource App/blog/Table>".
49
+ def inspect
50
+ "#<#{self.class.name} #{path}>"
51
+ end
52
+
53
+ # Validates the id and rejects duplicates among siblings. Returns node.
54
+ def add(node)
55
+ unless node.is_a?(Construct)
56
+ raise UsageError, "#{path} takes a Construct as a child, got #{node.inspect}"
57
+ end
58
+
59
+ Core::Ids.validate_id!(node.id, scope: self)
60
+ if @children.any? { |child| child.id == node.id }
61
+ raise UsageError, "There is already a construct with id \"#{node.id}\" in #{path}"
62
+ end
63
+
64
+ @children << node
65
+ node
66
+ end
67
+
68
+ # The nearest enclosing Stack, or nil when there is none.
69
+ def stack
70
+ return self if is_a?(Stack)
71
+
72
+ scope&.stack
73
+ end
74
+
75
+ # Hook, children first. Curated constructs add implicit resources here.
76
+ def prepare
77
+ end
78
+
79
+ # Runs the prepare hook on this node and on everything under it, each node
80
+ # once and children before their parent. Synthesis calls it; user code
81
+ # overrides prepare instead.
82
+ def prepare_tree
83
+ return if @prepared
84
+
85
+ @prepared = true
86
+ count = @children.length
87
+ @children.each(&:prepare_tree)
88
+ prepare
89
+ # What the hook itself added, a resource most often.
90
+ @children.drop(count).each(&:prepare_tree)
91
+ end
92
+
93
+ # Registers an L1 resource in the nearest Stack.
94
+ #
95
+ # opts are the properties, in snake_case. properties: carries exact
96
+ # CloudFormation names instead, for the rare property called Metadata or
97
+ # Condition, and cannot be combined with other keywords.
98
+ #
99
+ # Declared here because it is the tree's contract. The cfn component
100
+ # implements it by reopening this class: core does not depend on cfn.
101
+ def resource(id, type, properties: nil, depends_on: [], condition: nil, deletion_policy: nil, update_replace_policy: nil, metadata: {}, **opts)
102
+ raise NotImplementedError, "added by the cfn component"
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../errors"
4
+
5
+ module RubyCdk
6
+ module Core
7
+ # Something a Stack declares under a name of its own, in a section other
8
+ # than Resources: a parameter, a mapping, a condition or an output. It is a
9
+ # handle: Fn.ref, Fn.if, Fn.find_in_map and `condition:` take it.
10
+ class Element
11
+ attr_reader :stack, :id
12
+
13
+ def initialize(stack, id)
14
+ @stack = stack
15
+ @id = id
16
+ end
17
+
18
+ # Its name in the template.
19
+ def logical_id
20
+ id
21
+ end
22
+ alias name logical_id
23
+
24
+ def to_s
25
+ id
26
+ end
27
+
28
+ private
29
+
30
+ # A String or a Symbol, as a String.
31
+ def word(value, attribute)
32
+ return value if value.is_a?(String)
33
+ return value.name if value.is_a?(Symbol)
34
+
35
+ raise UsageError, "#{stack.path}: #{id}.#{attribute} must be a String or a Symbol, got #{value.inspect}"
36
+ end
37
+ end
38
+
39
+ # A Parameters entry. `type:` is a CloudFormation parameter type: "String",
40
+ # "Number", "CommaDelimitedList", "List<Number>", or an AWS-specific one.
41
+ class Parameter < Element
42
+ attr_reader :type, :default, :description, :allowed_values, :no_echo
43
+
44
+ def initialize(stack, id, type:, default:, description:, allowed_values:, no_echo:)
45
+ super(stack, id)
46
+ @type = word(type, "type")
47
+ @default = default
48
+ @description = description && word(description, "description")
49
+ unless allowed_values.nil? || allowed_values.is_a?(Array)
50
+ raise UsageError, "#{stack.path}: #{id}.allowed_values must be an Array, got #{allowed_values.inspect}"
51
+ end
52
+
53
+ @allowed_values = allowed_values
54
+ @no_echo = no_echo ? true : false
55
+ end
56
+ end
57
+
58
+ # A Mappings entry: a two-level hash.
59
+ class Mapping < Element
60
+ attr_reader :hash
61
+
62
+ def initialize(stack, id, hash)
63
+ unless hash.is_a?(Hash)
64
+ raise UsageError, "#{stack.path}: mapping #{id.inspect} takes a Hash, got #{hash.inspect}"
65
+ end
66
+
67
+ super(stack, id)
68
+ @hash = hash
69
+ end
70
+ end
71
+
72
+ # A Conditions entry. fn is what a condition function returns: Fn.equals,
73
+ # Fn.and_, Fn.not_ and so on.
74
+ class Condition < Element
75
+ attr_reader :fn
76
+
77
+ def initialize(stack, id, fn)
78
+ unless fn.is_a?(Hash)
79
+ raise UsageError, "#{stack.path}: condition #{id.inspect} takes what Fn.equals, Fn.and_ or Fn.not_ return, got #{fn.inspect}"
80
+ end
81
+
82
+ super(stack, id)
83
+ @fn = fn
84
+ end
85
+ end
86
+
87
+ # An Outputs entry. export is true when its Export name is stack.export_name(id).
88
+ class Output < Element
89
+ attr_reader :value, :description, :export, :condition
90
+
91
+ def initialize(stack, id, value:, description:, export:, condition:)
92
+ if value.nil?
93
+ raise UsageError, "#{stack.path}: output #{id.inspect} needs a value"
94
+ end
95
+ unless export == true || export == false
96
+ raise UsageError, "#{stack.path}: output #{id.inspect} takes export: true or false, got #{export.inspect}"
97
+ end
98
+
99
+ super(stack, id)
100
+ @value = value
101
+ @description = description && word(description, "description")
102
+ @export = export
103
+ @condition = condition
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../errors"
4
+ require_relative "elements"
5
+
6
+ module RubyCdk
7
+ module Core
8
+ # The id rules of the tree, and how a resource's logical ID follows from the
9
+ # ids around it (architecture §3, D-004 and D-029).
10
+ module Ids
11
+ # A logical ID in a template: letters and digits, nothing else.
12
+ LOGICAL_ID_PATTERN = /\A[A-Za-z0-9]+\z/
13
+
14
+ # The longest a logical ID can be: CloudFormation's limit for the name of
15
+ # a parameter, a resource, an output or a mapping. Construct::MAX_ID_LENGTH
16
+ # is this number: an id is a logical ID when it sits in a Stack.
17
+ MAX_LOGICAL_ID_LENGTH = 255
18
+
19
+ module_function
20
+
21
+ # Raises UsageError unless id can name a construct: a String or a Symbol
22
+ # of 1 to Construct::MAX_ID_LENGTH letters, digits, "_" or "-". scope,
23
+ # when given, names the parent in the message. Returns the id as a String.
24
+ def validate_id!(id, scope: nil)
25
+ where = scope ? " under #{scope.path}" : ""
26
+ unless id.is_a?(String) || id.is_a?(Symbol)
27
+ Kernel.raise UsageError, "The id of a construct#{where} must be a String or a Symbol, got #{id.inspect}"
28
+ end
29
+
30
+ name = id.is_a?(Symbol) ? id.name : id
31
+ if name.length > Construct::MAX_ID_LENGTH
32
+ Kernel.raise UsageError, "The id \"#{name[0, 32]}...\"#{where} is longer than #{Construct::MAX_ID_LENGTH} characters"
33
+ end
34
+ unless Construct::ID_PATTERN.match?(name)
35
+ Kernel.raise UsageError, "The id #{name.inspect}#{where} is not valid: an id holds letters, digits, \"_\" and \"-\", and at least one of them"
36
+ end
37
+
38
+ name
39
+ end
40
+
41
+ # Raises UsageError unless name can be the logical ID of an element of a
42
+ # template, a parameter, an output, a mapping, a condition or a resource:
43
+ # what rule V2 asks of it. where names the element and its id, as
44
+ # 'App/blog: the output "table_name"'. Returns name.
45
+ def validate_logical_id!(name, where:)
46
+ problem = logical_id_message(name)
47
+ Kernel.raise UsageError, "#{where}: #{problem}" if problem
48
+
49
+ name
50
+ end
51
+
52
+ # The whole of what is wrong with name as a logical ID, in the words of
53
+ # rule V2, or nil when nothing is: the fault, and how to write it instead.
54
+ # A name too long has no way out to suggest, so the fault stands alone.
55
+ def logical_id_message(name)
56
+ fault = logical_id_fault(name)
57
+ return nil unless fault
58
+ return fault if name.length > MAX_LOGICAL_ID_LENGTH
59
+
60
+ "#{fault}. #{logical_id_way_out(name)}"
61
+ end
62
+
63
+ # The fault alone, as rule V2 states it: the characters, or the length.
64
+ def logical_id_fault(name)
65
+ if name.length > MAX_LOGICAL_ID_LENGTH
66
+ "the logical ID \"#{name[0, 32]}...\" is longer than #{MAX_LOGICAL_ID_LENGTH} characters"
67
+ elsif LOGICAL_ID_PATTERN.match?(name)
68
+ nil
69
+ else
70
+ "the logical ID #{name.inspect} must hold letters and digits only"
71
+ end
72
+ end
73
+
74
+ # How to write it instead, for a logical ID that holds something else
75
+ # than letters and digits.
76
+ def logical_id_way_out(name)
77
+ "CloudFormation takes no \"_\", \"-\" or \".\" in a logical ID: write #{spelled_without_punctuation(name)}"
78
+ end
79
+
80
+ # The logical ID of a node: the ids of the enclosing constructs, from the
81
+ # Stack down to the node, joined. A node directly in a Stack keeps its own
82
+ # id; `Api.new(stack, "Api")` creating `Role` gives `ApiRole`.
83
+ def logical_id(node)
84
+ return "" if node.is_a?(Stack) || node.is_a?(App) || !node.is_a?(Construct)
85
+
86
+ "#{logical_id(node.scope)}#{node.id}"
87
+ end
88
+
89
+ # The logical ID a reference stands for: a String, a Symbol, a resource
90
+ # or a parameter of a Stack. what names the call in the message, as
91
+ # 'resource "Table" in App/blog: depends_on'.
92
+ def logical_id_of(target, what)
93
+ return target if target.is_a?(String)
94
+ return target.name if target.is_a?(Symbol)
95
+ return target.logical_id if target.respond_to?(:logical_id)
96
+
97
+ Kernel.raise UsageError, "#{what} takes a resource, a parameter or a logical ID, got #{target.inspect}"
98
+ end
99
+
100
+ # The name a `condition:` stands for: a String, a Symbol or a condition the
101
+ # stack declared. nil stays nil. what names the call in the message.
102
+ def condition_name(condition, what)
103
+ return nil if condition.nil?
104
+ return condition if condition.is_a?(String)
105
+ return condition.name if condition.is_a?(Symbol) || condition.is_a?(Condition)
106
+
107
+ Kernel.raise UsageError, "#{what} takes a condition the stack declared, or its name, got #{condition.inspect}"
108
+ end
109
+
110
+ # "my_table" as "MyTable".
111
+ def spelled_without_punctuation(name)
112
+ name.split(/[^A-Za-z0-9]+/).map(&:capitalize).join
113
+ end
114
+ private_class_method :spelled_without_punctuation
115
+ end
116
+ end
117
+ end