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,409 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "errors"
5
+ require_relative "lazy"
6
+
7
+ module RubyCdk
8
+ # Every value here is a plain Hash in CloudFormation syntax.
9
+ #
10
+ # Fn.ref("Table") => {"Ref" => "Table"}
11
+ # Fn.get_att(role, "Arn") => {"Fn::GetAtt" => ["FnRole", "Arn"]}
12
+ # Fn.sub("${Table}") => {"Fn::Sub" => "${Table}"}
13
+ # Fn.join("", ["a", "b"]) => {"Fn::Join" => ["", ["a", "b"]]}
14
+ #
15
+ # A target may be a String, a Symbol or anything with a logical_id.
16
+ #
17
+ # The functions, and what each writes, were read in the CDK's
18
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts at commit
19
+ # 78649cb7cf7399204ed63f629964779eeaa8b37a. That file is the CDK's only Fn.
20
+ #
21
+ # As in the CDK, a function that can answer from its arguments does so at
22
+ # once when nothing in them is deferred, and Fn.join is simplified when the
23
+ # template is resolved: see minimal_join. Deferred means one of two things
24
+ # here. An intrinsic composes at once, as a token does in the CDK. A Lazy,
25
+ # which this tool has and the CDK has not, makes the function answer a Lazy
26
+ # that runs again on what the argument resolves to: see now_or_later.
27
+ module Fn
28
+ # {"Ref" => "AWS::NoValue"}: leaves a property out of the template.
29
+ NO_VALUE = { "Ref" => "AWS::NoValue" }.freeze
30
+
31
+ module_function
32
+
33
+ def ref(target)
34
+ { "Ref" => logical_id_of(target, "Fn.ref") }
35
+ end
36
+
37
+ def get_att(target, attribute)
38
+ { "Fn::GetAtt" => [ logical_id_of(target, "Fn.get_att"), attribute.to_s ] }
39
+ end
40
+
41
+ # With variables: Fn.sub("Hi ${Name}", "Name" => value).
42
+ def sub(string, variables = {})
43
+ if variables.empty?
44
+ { "Fn::Sub" => string }
45
+ else
46
+ { "Fn::Sub" => [ string, variables.to_h { |key, value| [ name_of(key, "Fn.sub variables"), value ] } ] }
47
+ end
48
+ end
49
+
50
+ # parts is the list of values to join, as one Array or as several arguments.
51
+ # An empty list is an error, as it is in the CDK.
52
+ #
53
+ # A lone Hash is the list itself, not a list of one: Fn.join(",", Fn.get_azs)
54
+ # joins what Fn::GetAZs answers. That is how a list CloudFormation computes
55
+ # is joined, and it keeps such a join whole when it sits inside another one.
56
+ # A lone Lazy is the list once it has resolved.
57
+ def join(separator, *parts)
58
+ list =
59
+ if parts.length == 1 && parts.first.is_a?(Hash)
60
+ parts.first
61
+ else
62
+ parts.flatten(1)
63
+ end
64
+ if parts.length == 1 && parts.first.is_a?(Lazy)
65
+ return Lazy.new { |stack| join(separator, lazys_resolved(parts.first, stack)) }
66
+ end
67
+
68
+ Kernel.raise UsageError, "Fn.join requires at least one value to be provided" if list.empty?
69
+
70
+ { "Fn::Join" => [ separator, list ] }
71
+ end
72
+
73
+ # Splits a string into a list. A literal string is split at once.
74
+ #
75
+ # With assumed_length the result is a list of known length, the first
76
+ # assumed_length elements of the split, each an Fn::Select. Without it the
77
+ # result is the Fn::Split itself, of a length only CloudFormation knows, and
78
+ # Fn.select is then the only way to take an element out of it.
79
+ def split(separator, string, assumed_length = nil)
80
+ split_value = now_or_later(separator, string) do |with, source|
81
+ if source.is_a?(String)
82
+ Kernel.raise UsageError, "Fn.split takes a String separator, got #{with.inspect}" unless with.is_a?(String)
83
+
84
+ source.empty? ? [ "" ] : (with.empty? ? source.chars : source.split(/#{Regexp.escape(with)}/, -1))
85
+ else
86
+ { "Fn::Split" => [ with, source ] }
87
+ end
88
+ end
89
+
90
+ # A literal string was split at once, and the assumed length is not needed, as in the CDK.
91
+ assumed_length.nil? || split_value.is_a?(Array) ? split_value : known_length(split_value, assumed_length)
92
+ end
93
+
94
+ # Like Fn.import_value, for a list of known length: the imported value is
95
+ # split on delimiter, and the list holds its first assumed_length elements.
96
+ def import_list_value(name, assumed_length, delimiter = ",")
97
+ split(delimiter, import_value(name), assumed_length)
98
+ end
99
+
100
+ # The element at index of a list that is entirely known at once. Otherwise
101
+ # an Fn::Select. The index is written as given: the CDK writes a number.
102
+ #
103
+ # Known at once is the CDK's rule: nothing anywhere in the list is an
104
+ # intrinsic. A plain Hash or Array in it is fine.
105
+ def select(index, list)
106
+ now_or_later(index, list) do |at, from|
107
+ check_list!(from, "Fn.select")
108
+ if at.is_a?(Integer) && from.is_a?(Array) && from.none? { |item| intrinsic_inside?(item) }
109
+ from[at]
110
+ else
111
+ { "Fn::Select" => [ at, from ] }
112
+ end
113
+ end
114
+ end
115
+
116
+ # The domain of a URL: Fn.parse_domain_name("https://test.com/") is
117
+ # "test.com". A literal URL is parsed at once.
118
+ #
119
+ # Read in packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.parseDomainName.
120
+ def parse_domain_name(url)
121
+ now_or_later(url) do |address|
122
+ select(0, split("/", select(1, split("//", address))))
123
+ end
124
+ end
125
+
126
+ # count CIDR blocks out of ip_block, each with size_mask subnet bits, as
127
+ # Fn::Cidr. count is 1 to 256. The result is a list CloudFormation computes:
128
+ # take an element with Fn.select. Read in
129
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.cidr and FnCidr.
130
+ def cidr(ip_block, count, size_mask = nil)
131
+ now_or_later(ip_block, count, size_mask) do |block, wanted, mask|
132
+ if wanted.is_a?(Integer) && !(1..256).cover?(wanted)
133
+ Kernel.raise UsageError, "Fn::Cidr's count attribute must be between 1 and 256, #{wanted} was provided."
134
+ end
135
+
136
+ { "Fn::Cidr" => [ block, wanted, *([ mask ] unless mask.nil?) ] }
137
+ end
138
+ end
139
+
140
+ # The value of the output output_name of the stack stack_name, without an
141
+ # export. region defaults to the stack's own and role_arn to its execution
142
+ # role. Read in packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.getStackOutput and FnGetStackOutput.
143
+ def get_stack_output(stack_name, output_name, region = nil, role_arn = nil)
144
+ { "Fn::GetStackOutput" => { "StackName" => stack_name, "OutputName" => output_name, "Region" => region, "RoleArn" => role_arn }.compact }
145
+ end
146
+
147
+ # The value at two keys of a mapping. default_value is what CloudFormation
148
+ # answers when a key is missing; without it a missing key is an error at
149
+ # deploy. Read in packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.findInMap and FnFindInMap.
150
+ def find_in_map(mapping, top_level_key, second_level_key, default_value: nil)
151
+ keys = [ mapping.to_s, top_level_key, second_level_key ]
152
+ keys << { "DefaultValue" => default_value } unless default_value.nil?
153
+
154
+ { "Fn::FindInMap" => keys }
155
+ end
156
+
157
+ # The macro macro_name, run on this template with parameters: Fn::Transform.
158
+ # The template must declare the macro, with Stack#transform. Read in
159
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.transform and FnTransform.
160
+ def transform(macro_name, parameters)
161
+ unless parameters.is_a?(Hash)
162
+ Kernel.raise UsageError, "Fn.transform takes a Hash of parameters, got #{parameters.inspect}"
163
+ end
164
+
165
+ { "Fn::Transform" => { "Name" => macro_name, "Parameters" => parameters } }
166
+ end
167
+
168
+ # The object as a JSON string. One that holds an intrinsic is written as
169
+ # Fn::ToJsonString, for CloudFormation to do at deploy; it needs the
170
+ # AWS::LanguageExtensions transform, which synthesis adds. Read in
171
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.toJsonString and FnToJsonString.
172
+ def to_json_string(object)
173
+ now_or_later(object) do |value|
174
+ intrinsic_inside?(value) ? { "Fn::ToJsonString" => value } : JSON.generate(value)
175
+ end
176
+ end
177
+
178
+ # The number of elements of an Array. Of anything else that is a list
179
+ # CloudFormation computes, an intrinsic, it is Fn::Length, which needs the
180
+ # AWS::LanguageExtensions transform, which synthesis adds. Read in
181
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.len and FnLength.
182
+ def len(array)
183
+ now_or_later(array) do |value|
184
+ if value.is_a?(Array)
185
+ value.length
186
+ elsif value.is_a?(Hash)
187
+ { "Fn::Length" => value }
188
+ else
189
+ Kernel.raise UsageError, "Fn.len needs an array, got #{value.inspect}"
190
+ end
191
+ end
192
+ end
193
+
194
+ # All the values of a parameter of an AWS-specific type, such as
195
+ # "AWS::EC2::VPC::Id": Fn::RefAll. Read in
196
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.refAll and FnRefAll.
197
+ def ref_all(parameter_type)
198
+ { "Fn::RefAll" => parameter_type }
199
+ end
200
+
201
+ # An attribute of a parameter of an AWS-specific type: Fn::ValueOf. Read in
202
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.valueOf and FnValueOf.
203
+ def value_of(parameter_or_logical_id, attribute)
204
+ { "Fn::ValueOf" => [ parameter_or_logical_id, attribute ] }
205
+ end
206
+
207
+ # An attribute of every value of a parameter of an AWS-specific type:
208
+ # Fn::ValueOfAll. Read in packages/aws-cdk-lib/core/lib/cfn-fn.ts,
209
+ # Fn.valueOfAll and FnValueOfAll.
210
+ def value_of_all(parameter_type, attribute)
211
+ { "Fn::ValueOfAll" => [ parameter_type, attribute ] }
212
+ end
213
+
214
+ # What the CDK does to an Fn::Join when it resolves a template
215
+ # (minimalCloudFormationJoin, in packages/aws-cdk-lib/core/lib/private/cloudformation-lang.ts):
216
+ # an Fn::Join nested inside another one with the same delimiter is spliced
217
+ # in, and two neighbouring literals become one string. Returns the
218
+ # simplified list: one element long when the whole join is that element.
219
+ def minimal_join(delimiter, values)
220
+ values = values.dup
221
+ index = 0
222
+ while index < values.length
223
+ element = values[index]
224
+ if nested_join?(element, delimiter)
225
+ values[index, 1] = element["Fn::Join"][1]
226
+ elsif index.positive? && concatable?(values[index - 1]) && concatable?(element)
227
+ values[index - 1] = "#{literal(values[index - 1])}#{delimiter}#{literal(element)}"
228
+ values.delete_at(index)
229
+ else
230
+ index += 1
231
+ end
232
+ end
233
+ values
234
+ end
235
+
236
+ def base64(string)
237
+ { "Fn::Base64" => string }
238
+ end
239
+
240
+ def import_value(name)
241
+ { "Fn::ImportValue" => name.to_s }
242
+ end
243
+
244
+ # With no region, the region of the stack, as in the CDK: an empty string.
245
+ def get_azs(region = "")
246
+ { "Fn::GetAZs" => region || "" }
247
+ end
248
+
249
+ # condition: the name of a condition, or anything with a name.
250
+ def if(condition, if_true, if_false)
251
+ { "Fn::If" => [ condition_name(condition, "Fn.if"), if_true, if_false ] }
252
+ end
253
+
254
+ def equals(left, right)
255
+ { "Fn::Equals" => [ left, right ] }
256
+ end
257
+
258
+ def and_(*conditions)
259
+ { "Fn::And" => conditions }
260
+ end
261
+
262
+ def or_(*conditions)
263
+ { "Fn::Or" => conditions }
264
+ end
265
+
266
+ def not_(condition)
267
+ { "Fn::Not" => [ condition ] }
268
+ end
269
+
270
+ # True when value is in the list: Fn::Contains. Read in
271
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.conditionContains and FnContains.
272
+ def contains(list, value)
273
+ { "Fn::Contains" => [ list, value ] }
274
+ end
275
+
276
+ # True when value equals every member of the list: Fn::EachMemberEquals. Read in
277
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.conditionEachMemberEquals and FnEachMemberEquals.
278
+ def each_member_equals(list, value)
279
+ { "Fn::EachMemberEquals" => [ list, value ] }
280
+ end
281
+
282
+ # True when every member of list is also in matches: Fn::EachMemberIn. Read in
283
+ # packages/aws-cdk-lib/core/lib/cfn-fn.ts, Fn.conditionEachMemberIn and FnEachMemberIn.
284
+ def each_member_in(list, matches)
285
+ { "Fn::EachMemberIn" => [ list, matches ] }
286
+ end
287
+
288
+ # private below
289
+
290
+ # The rule of every function that computes from its arguments. With no Lazy
291
+ # in them the block runs now. With one anywhere in them the function answers
292
+ # a Lazy that runs the block once, when the template is resolved, on what
293
+ # every Lazy among them has resolved to. The block must answer, never
294
+ # return: it outlives the function that wrote it.
295
+ def now_or_later(*arguments, &compute)
296
+ return compute.call(*arguments) unless arguments.any? { |argument| lazy_inside?(argument) }
297
+
298
+ Lazy.new { |stack| compute.call(*arguments.map { |argument| lazys_resolved(argument, stack) }) }
299
+ end
300
+
301
+ # The first assumed_length elements of a list CloudFormation computes, each an Fn::Select.
302
+ def known_length(list, assumed_length)
303
+ unless assumed_length.is_a?(Integer) && assumed_length >= 0
304
+ Kernel.raise UsageError, "Fn.split: assumed_length must be an Integer of at least 0, got #{assumed_length.inspect}"
305
+ end
306
+
307
+ Array.new(assumed_length) { |index| select(index, list) }
308
+ end
309
+
310
+ def check_list!(list, caller)
311
+ return if list.is_a?(Array) || list.is_a?(Hash)
312
+
313
+ Kernel.raise UsageError, "#{caller} takes an Array or a function that returns a list, got #{list.inspect}"
314
+ end
315
+
316
+ def logical_id_of(target, caller)
317
+ return target if target.is_a?(String)
318
+ return target.name if target.is_a?(Symbol)
319
+ return target.logical_id.to_s if target.respond_to?(:logical_id)
320
+
321
+ Kernel.raise UsageError, "#{caller} takes a resource, a parameter or a logical ID, got #{target.inspect}"
322
+ end
323
+
324
+ def condition_name(condition, caller)
325
+ return condition if condition.is_a?(String)
326
+ return condition.name if condition.is_a?(Symbol)
327
+ return condition.name.to_s if condition.respond_to?(:name)
328
+
329
+ Kernel.raise UsageError, "#{caller} takes a condition the stack declared, or its name, got #{condition.inspect}"
330
+ end
331
+
332
+ def name_of(key, caller)
333
+ return key if key.is_a?(String)
334
+ return key.name if key.is_a?(Symbol)
335
+
336
+ Kernel.raise UsageError, "The keys of #{caller} must be Strings or Symbols, got #{key.inspect}"
337
+ end
338
+
339
+ def nested_join?(value, delimiter)
340
+ return false unless value.is_a?(Hash) && value.size == 1 && value.key?("Fn::Join")
341
+
342
+ pair = value["Fn::Join"]
343
+ pair.is_a?(Array) && pair[0] == delimiter && pair[1].is_a?(Array)
344
+ end
345
+
346
+ def concatable?(value)
347
+ value.is_a?(String) || value.is_a?(Integer) || value.is_a?(Float)
348
+ end
349
+
350
+ # An intrinsic: a Hash with one key, "Ref" or a name that starts with
351
+ # "Fn::". The CDK's isIntrinsic, in
352
+ # packages/aws-cdk-lib/core/lib/private/cloudformation-lang.ts.
353
+ def intrinsic?(value)
354
+ return false unless value.is_a?(Hash) && value.size == 1
355
+
356
+ key = value.keys.first
357
+ key == "Ref" || (key.is_a?(String) && key.start_with?("Fn::"))
358
+ end
359
+
360
+ # An intrinsic anywhere inside: a value CloudFormation computes at deploy.
361
+ def intrinsic_inside?(value)
362
+ case value
363
+ when Hash
364
+ intrinsic?(value) || value.values.any? { |item| intrinsic_inside?(item) }
365
+ when Array
366
+ value.any? { |item| intrinsic_inside?(item) }
367
+ else
368
+ false
369
+ end
370
+ end
371
+
372
+ # A Lazy anywhere inside.
373
+ def lazy_inside?(value)
374
+ case value
375
+ when Lazy
376
+ true
377
+ when Hash
378
+ value.values.any? { |item| lazy_inside?(item) }
379
+ when Array
380
+ value.any? { |item| lazy_inside?(item) }
381
+ else
382
+ false
383
+ end
384
+ end
385
+
386
+ # value with every Lazy in it resolved, and what each resolved to gone
387
+ # through as well.
388
+ def lazys_resolved(value, stack)
389
+ case value
390
+ when Lazy
391
+ lazys_resolved(value.resolve(stack), stack)
392
+ when Hash
393
+ value.to_h { |key, item| [ key, lazys_resolved(item, stack) ] }
394
+ when Array
395
+ value.map { |item| lazys_resolved(item, stack) }
396
+ else
397
+ value
398
+ end
399
+ end
400
+
401
+ # A literal the way a join writes it: the way JavaScript prints a number,
402
+ # so 1.0 is written "1".
403
+ def literal(value)
404
+ value.is_a?(Float) && value.to_i.to_f == value ? value.to_i.to_s : value.to_s
405
+ end
406
+ private_class_method :now_or_later, :known_length, :check_list!, :logical_id_of, :condition_name, :name_of, :nested_join?,
407
+ :concatable?, :intrinsic?, :intrinsic_inside?, :lazy_inside?, :lazys_resolved, :literal
408
+ end
409
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "errors"
4
+
5
+ module RubyCdk
6
+ # A value computed once, when its stack is resolved. Resolution happens after
7
+ # the tree is complete, so a Lazy can refer to a resource defined later than
8
+ # itself.
9
+ class Lazy
10
+ MAX_DEPTH = 10
11
+
12
+ # A chain of Lazy values that leads back to itself, or that is deeper than MAX_DEPTH.
13
+ class ChainError < Error; end
14
+
15
+ def initialize(&block)
16
+ raise ArgumentError, "Lazy needs a block" unless block
17
+
18
+ @block = block
19
+ @state = :pending
20
+ @value = nil
21
+ end
22
+
23
+ # Evaluates the block with the stack. A chain of Lazy values is followed up
24
+ # to MAX_DEPTH levels, so that a cycle is an error and not a hang.
25
+ def resolve(stack)
26
+ evaluate(stack, 0)
27
+ end
28
+
29
+ # True once the block has run.
30
+ def resolved?
31
+ @state == :resolved
32
+ end
33
+
34
+ private
35
+
36
+ def evaluate(stack, depth)
37
+ return @value if @state == :resolved
38
+
39
+ raise ChainError, "A chain of Lazy values leads back to itself" if @state == :resolving
40
+ raise ChainError, "A chain of Lazy values deeper than #{MAX_DEPTH} levels" if depth > MAX_DEPTH
41
+
42
+ @state = :resolving
43
+ begin
44
+ value = @block.call(stack)
45
+ value = value.send(:evaluate, stack, depth + 1) if value.is_a?(Lazy)
46
+ rescue Exception
47
+ @state = :pending
48
+ raise
49
+ end
50
+ @value = value
51
+ @state = :resolved
52
+ @value
53
+ end
54
+ end
55
+ end