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,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ported from the AWS CDK.
4
+ #
5
+ # Origin: packages/aws-cdk-lib/matching/lib/private/sorting.ts
6
+ # Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
7
+ # Licence: Apache-2.0
8
+ # Changed: a comparator built from a key block, for Array#sort. Strings compare
9
+ # with localeCompare there, which pure Ruby has no ICU for; the approximation is
10
+ # Js.locale_compare.
11
+
12
+ require_relative "js"
13
+
14
+ module RubyCdk
15
+ module Test
16
+ module Private
17
+ module_function
18
+
19
+ # A comparator on a key block that returns numbers or strings: by the first
20
+ # key that differs, then by the longer key.
21
+ def sort_key_comparator(&key)
22
+ ->(left, right) do
23
+ left_keys = key.call(left)
24
+ right_keys = key.call(right)
25
+ left_keys.zip(right_keys).each do |left_key, right_key|
26
+ difference = if left_key.is_a?(Numeric) && right_key.is_a?(Numeric)
27
+ left_key - right_key
28
+ elsif left_key.is_a?(String) && right_key.is_a?(String)
29
+ Js.locale_compare(left_key, right_key)
30
+ else
31
+ 0
32
+ end
33
+ return difference unless difference.zero?
34
+ end
35
+ right_keys.length - left_keys.length
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ported from the AWS CDK.
4
+ #
5
+ # Origin: packages/aws-cdk-lib/matching/lib/private/sparse-matrix.ts
6
+ # Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
7
+ # Licence: Apache-2.0
8
+ # Changed: nothing in behaviour. A Hash of Hashes stands for the Map of Maps,
9
+ # and a row is a list of [ column, value ] pairs.
10
+
11
+ module RubyCdk
12
+ module Test
13
+ module Private
14
+ # Values set at a few [ row, column ] positions of a grid.
15
+ class SparseMatrix
16
+ def initialize
17
+ @matrix = {}
18
+ end
19
+
20
+ def get(row, column)
21
+ @matrix[row]&.[](column)
22
+ end
23
+
24
+ # [ column, value ] pairs of a row, in the order they were set. Empty when none was.
25
+ def row(row)
26
+ (@matrix[row] || {}).to_a
27
+ end
28
+
29
+ def set(row, column, value)
30
+ (@matrix[row] ||= {})[column] = value
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ported from the AWS CDK.
4
+ #
5
+ # Origin: packages/aws-cdk-lib/matching/lib/private/type.ts
6
+ # Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
7
+ # Licence: Apache-2.0
8
+ # Changed: JavaScript's `typeof` is RubyCdk::Test::Private::Js.typeof.
9
+
10
+ require_relative "js"
11
+
12
+ module RubyCdk
13
+ module Test
14
+ module Private
15
+ module_function
16
+
17
+ # 'array' for an array, otherwise JavaScript's typeof.
18
+ def get_type(value)
19
+ value.is_a?(Array) ? "array" : Js.typeof(value)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,364 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ported from the AWS CDK.
4
+ #
5
+ # Origin: packages/aws-cdk-lib/assertions/lib/template.ts
6
+ # Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
7
+ # Licence: Apache-2.0
8
+ # Changed: camelCase to snake_case. fromStack(stack), which synthesizes the
9
+ # stage the stack belongs to, is template(source): anything with a to_template
10
+ # method, asked by duck typing, or a Hash, or a JSON string. This tool has no
11
+ # Stage, App or NestedStack, so the nested-stack branch of toTemplate() has no
12
+ # counterpart. TemplateParsingOptions is a second argument, an options Hash
13
+ # (with a keyword parameter of its own, `from_json("Resources" => { ... })`
14
+ # could not be written). A template that is not a JSON object is an
15
+ # ArgumentError here, where the CDK accepts it and fails later. The Symbols of a
16
+ # Hash become strings (Js.normalize).
17
+ #
18
+ # Not in the CDK, added here: get(path), because a Ruby test has no jest
19
+ # snapshot to compare a template with; and the second half of to_json, which
20
+ # answers Ruby's JSON library with text when it is the caller.
21
+
22
+ require_relative "capture"
23
+ require_relative "match"
24
+ require_relative "matcher"
25
+ require_relative "private/conditions"
26
+ require_relative "private/cyclic"
27
+ require_relative "private/error"
28
+ require_relative "private/js"
29
+ require_relative "private/mappings"
30
+ require_relative "private/outputs"
31
+ require_relative "private/parameters"
32
+ require_relative "private/resources"
33
+
34
+ module RubyCdk
35
+ module Test
36
+ # Suite of assertions that can be run on a CDK stack.
37
+ # Typically used, as part of unit tests, to validate that the rendered
38
+ # CloudFormation template has expected resources and properties.
39
+ #
40
+ # Build one with RubyCdk::Test.template, from_string or from_json. Every
41
+ # method that asserts raises RubyCdk::Test::AssertionError when it fails.
42
+ #
43
+ # The three ways of building one take their options as a second argument, the
44
+ # CDK's TemplateParsingOptions, and not as keywords of their own: with a
45
+ # keyword parameter, Ruby would read `from_json("Resources" => { ... })`, a
46
+ # Hash written without braces, as keywords and find no template. So both of
47
+ # these give the same Template:
48
+ #
49
+ # RubyCdk::Test.from_json(template, skip_cyclical_dependencies_check: true)
50
+ # RubyCdk::Test.from_json(template, { skip_cyclical_dependencies_check: true })
51
+ class Template
52
+ # Base your assertions on the CloudFormation template of a stack.
53
+ #
54
+ # In the CDK: Template.fromStack(stack). source is anything with a
55
+ # `to_template` method, a Hash, or a template as a JSON string.
56
+ #
57
+ # options: :skip_cyclical_dependencies_check, true to skip checking for
58
+ # cyclical / circular dependencies. Leave it out other than for templates
59
+ # that are valid despite containing cycles, such as unprocessed transform
60
+ # stacks.
61
+ def self.template(source, options = {})
62
+ if source.respond_to?(:to_template)
63
+ json = source.to_template
64
+ elsif source.is_a?(String)
65
+ json = JSON.parse(source, max_nesting: false)
66
+ elsif source.is_a?(Hash)
67
+ json = source
68
+ else
69
+ raise ArgumentError,
70
+ "Expected a template: anything with a to_template method, a Hash or a JSON string, got #{source.class}"
71
+ end
72
+ new(json, options[:skip_cyclical_dependencies_check])
73
+ end
74
+
75
+ # Base your assertions from an existing CloudFormation template formatted as a
76
+ # JSON string.
77
+ #
78
+ # options: as in template.
79
+ def self.from_string(template, options = {})
80
+ new(JSON.parse(template, max_nesting: false), options[:skip_cyclical_dependencies_check])
81
+ end
82
+
83
+ # Base your assertions from an existing CloudFormation template formatted as
84
+ # an in-memory JSON object.
85
+ #
86
+ # options: as in template.
87
+ def self.from_json(template, options = {})
88
+ new(template, options[:skip_cyclical_dependencies_check])
89
+ end
90
+
91
+ def initialize(template, skip_cyclical_dependencies_check)
92
+ unless template.is_a?(Hash)
93
+ raise ArgumentError, "A CloudFormation template is a JSON object, got #{Private.get_type(template)}"
94
+ end
95
+
96
+ @template = Private::Js.normalize(template)
97
+ Private.check_template_for_cyclic_dependencies(@template) unless skip_cyclical_dependencies_check
98
+ end
99
+ private_class_method :new
100
+
101
+ # The CloudFormation template deserialized into an object: a Hash.
102
+ #
103
+ # This is the CDK's toJSON(). Ruby's JSON library also calls `to_json` on
104
+ # whatever it is asked to serialize, passing its state object and expecting
105
+ # text back; when a state is given, the template answers with its JSON text.
106
+ def to_json(state = nil, *)
107
+ state ? @template.to_json(state) : @template
108
+ end
109
+
110
+ # Assert that the given number of resources of the given type exist in the
111
+ # template.
112
+ #
113
+ # type: the resource type; ex: `AWS::S3::Bucket`
114
+ # count: number of expected instances
115
+ def resource_count_is(type, count)
116
+ counted = Private.count_resources(@template, type)
117
+ if counted != count
118
+ raise AssertionError, "Expected #{count} resources of type #{type} but found #{counted}"
119
+ end
120
+ nil
121
+ end
122
+
123
+ # Assert that the given number of resources of the given type and properties
124
+ # exists in the CloudFormation template.
125
+ #
126
+ # type: the resource type; ex: `AWS::S3::Bucket`
127
+ # props: the 'Properties' section of the resource as should be expected in the template
128
+ # count: number of expected instances
129
+ def resource_properties_count_is(type, props, count)
130
+ counted = Private.count_resources_properties(@template, type, props)
131
+ if counted != count
132
+ raise AssertionError, "Expected #{count} resources of type #{type} but found #{counted}"
133
+ end
134
+ nil
135
+ end
136
+
137
+ # Assert that a resource of the given type and properties exists in the
138
+ # CloudFormation template.
139
+ # By default, performs partial matching on the `Properties` key of the resource,
140
+ # via the `Match.object_like()`. To configure different behavior, use other
141
+ # matchers in the `Match` class.
142
+ #
143
+ # type: the resource type; ex: `AWS::S3::Bucket`
144
+ # props: the 'Properties' section of the resource as should be expected in the template
145
+ def has_resource_properties(type, props)
146
+ match_error = Private.has_resource_properties(@template, type, props)
147
+ raise AssertionError, match_error if match_error
148
+
149
+ nil
150
+ end
151
+
152
+ # Assert that a resource of the given type and given definition exists in the
153
+ # CloudFormation template.
154
+ # By default, performs partial matching on the resource, via the `Match.object_like()`.
155
+ # To configure different behavior, use other matchers in the `Match` class.
156
+ #
157
+ # type: the resource type; ex: `AWS::S3::Bucket`
158
+ # props: the entire definition of the resource as should be expected in the template
159
+ def has_resource(type, props)
160
+ match_error = Private.has_resource(@template, type, props)
161
+ raise AssertionError, match_error if match_error
162
+
163
+ nil
164
+ end
165
+
166
+ # Get the set of matching resources of a given type and properties in the
167
+ # CloudFormation template.
168
+ #
169
+ # type: the type to match in the CloudFormation template
170
+ # props: by default, matches all resources with the given type.
171
+ # When a literal is provided, performs a partial match via `Match.object_like()`.
172
+ # Use the `Match` APIs to configure a different behaviour.
173
+ def find_resources(type, props = {})
174
+ Private.find_resources(@template, type, props)
175
+ end
176
+
177
+ # Get the Resource ID of a matching resource, expects only to find one match.
178
+ # Throws AssertionError if none or multiple resources were found.
179
+ #
180
+ # type: the resource type; ex: `AWS::S3::Bucket`
181
+ # props: by default, matches all resources with the given type.
182
+ # Performs a partial match via `Match.object_like()`.
183
+ #
184
+ # Returns the resource id of the matched resource.
185
+ def get_resource_id(type, props = {})
186
+ resource_id, match_error = Private.get_resource_id(@template, type, props)
187
+
188
+ raise AssertionError, match_error if match_error
189
+ raise AssertionError, "unexpected: resourceId was undefined" unless resource_id
190
+
191
+ resource_id
192
+ end
193
+
194
+ # Assert that all resources of the given type contain the given definition in
195
+ # the CloudFormation template.
196
+ # By default, performs partial matching on the resource, via the `Match.object_like()`.
197
+ # To configure different behavior, use other matchers in the `Match` class.
198
+ #
199
+ # type: the resource type; ex: `AWS::S3::Bucket`
200
+ # props: the entire definition of the resources as they should be expected in the template
201
+ def all_resources(type, props)
202
+ match_error = Private.all_resources(@template, type, props)
203
+ raise AssertionError, match_error if match_error
204
+
205
+ nil
206
+ end
207
+
208
+ # Assert that all resources of the given type contain the given properties
209
+ # CloudFormation template.
210
+ # By default, performs partial matching on the `Properties` key of the
211
+ # resource, via the `Match.object_like()`. To configure different behavior, use
212
+ # other matchers in the `Match` class.
213
+ #
214
+ # type: the resource type; ex: `AWS::S3::Bucket`
215
+ # props: the 'Properties' section of the resource as should be expected in the template
216
+ def all_resources_properties(type, props)
217
+ match_error = Private.all_resources_properties(@template, type, props)
218
+ raise AssertionError, match_error if match_error
219
+
220
+ nil
221
+ end
222
+
223
+ # Assert that a Parameter with the given properties exists in the CloudFormation template.
224
+ # By default, performs partial matching on the parameter, via the `Match.object_like()`.
225
+ # To configure different behavior, use other matchers in the `Match` class.
226
+ #
227
+ # logical_id: the name of the parameter, provide `'*'` to match all parameters in the template
228
+ # props: the parameter as should be expected in the template
229
+ def has_parameter(logical_id, props)
230
+ match_error = Private.has_parameter(@template, logical_id, props)
231
+ raise AssertionError, match_error if match_error
232
+
233
+ nil
234
+ end
235
+
236
+ # Get the set of matching Parameters that match the given properties in the
237
+ # CloudFormation template.
238
+ #
239
+ # logical_id: the name of the parameter, provide `'*'` to match all parameters in the template
240
+ # props: by default, matches all Parameters in the template. When a literal
241
+ # object is provided, performs a partial match via `Match.object_like()`.
242
+ # Use the `Match` APIs to configure a different behaviour.
243
+ def find_parameters(logical_id, props = {})
244
+ Private.find_parameters(@template, logical_id, props)
245
+ end
246
+
247
+ # Assert that an Output with the given properties exists in the CloudFormation template.
248
+ # By default, performs partial matching on the resource, via the `Match.object_like()`.
249
+ # To configure different behavior, use other matchers in the `Match` class.
250
+ #
251
+ # logical_id: the name of the output, provide `'*'` to match all outputs in the template
252
+ # props: the output as should be expected in the template
253
+ def has_output(logical_id, props)
254
+ match_error = Private.has_output(@template, logical_id, props)
255
+ raise AssertionError, match_error if match_error
256
+
257
+ nil
258
+ end
259
+
260
+ # Get the set of matching Outputs that match the given properties in the
261
+ # CloudFormation template.
262
+ #
263
+ # logical_id: the name of the output, provide `'*'` to match all outputs in the template
264
+ # props: by default, matches all Outputs in the template. When a literal
265
+ # object is provided, performs a partial match via `Match.object_like()`.
266
+ # Use the `Match` APIs to configure a different behaviour.
267
+ def find_outputs(logical_id, props = {})
268
+ Private.find_outputs(@template, logical_id, props)
269
+ end
270
+
271
+ # Assert that a Mapping with the given properties exists in the CloudFormation template.
272
+ # By default, performs partial matching on the resource, via the `Match.object_like()`.
273
+ # To configure different behavior, use other matchers in the `Match` class.
274
+ #
275
+ # logical_id: the name of the mapping, provide `'*'` to match all mappings in the template
276
+ # props: the mapping as should be expected in the template
277
+ def has_mapping(logical_id, props)
278
+ match_error = Private.has_mapping(@template, logical_id, props)
279
+ raise AssertionError, match_error if match_error
280
+
281
+ nil
282
+ end
283
+
284
+ # Get the set of matching Mappings that match the given properties in the
285
+ # CloudFormation template.
286
+ #
287
+ # logical_id: the name of the mapping, provide `'*'` to match all mappings in the template
288
+ # props: by default, matches all Mappings in the template. When a literal
289
+ # object is provided, performs a partial match via `Match.object_like()`.
290
+ # Use the `Match` APIs to configure a different behaviour.
291
+ def find_mappings(logical_id, props = {})
292
+ Private.find_mappings(@template, logical_id, props)
293
+ end
294
+
295
+ # Assert that a Condition with the given properties exists in the CloudFormation template.
296
+ # By default, performs partial matching on the resource, via the `Match.object_like()`.
297
+ # To configure different behavior, use other matchers in the `Match` class.
298
+ #
299
+ # logical_id: the name of the condition, provide `'*'` to match all conditions in the template
300
+ # props: the condition as should be expected in the template
301
+ def has_condition(logical_id, props)
302
+ match_error = Private.has_condition(@template, logical_id, props)
303
+ raise AssertionError, match_error if match_error
304
+
305
+ nil
306
+ end
307
+
308
+ # Get the set of matching Conditions that match the given properties in the
309
+ # CloudFormation template.
310
+ #
311
+ # logical_id: the name of the condition, provide `'*'` to match all conditions in the template
312
+ # props: by default, matches all Conditions in the template. When a literal
313
+ # object is provided, performs a partial match via `Match.object_like()`.
314
+ # Use the `Match` APIs to configure a different behaviour.
315
+ def find_conditions(logical_id, props = {})
316
+ Private.find_conditions(@template, logical_id, props)
317
+ end
318
+
319
+ # Assert that the CloudFormation template matches the given value.
320
+ #
321
+ # expected: the expected CloudFormation template as key-value pairs. A
322
+ # partial match through `Match.object_like()`, unless it is a Matcher.
323
+ def template_matches(expected)
324
+ matcher = Matcher.matcher?(expected) ? expected : Match.object_like(expected)
325
+ result = matcher.test(@template)
326
+
327
+ if result.has_failed?
328
+ raise AssertionError,
329
+ [ "Template did not match as expected. The following mismatches were found:", *result.to_human_strings.map { |string| "\t#{string}" } ].join("\n")
330
+ end
331
+ nil
332
+ end
333
+
334
+ # The value at a path of the template, as a Capture: the `as_*` methods of
335
+ # the capture give it back with its type checked.
336
+ #
337
+ # template.get("Resources.Fn.Properties.Timeout").as_number
338
+ # template.get([ "Resources", "Role", "Properties", "ManagedPolicyArns", 0 ]).as_string
339
+ #
340
+ # path: the keys from the root, separated by dots, or a list of keys and
341
+ # array indexes. A null in the template is a value: `get` hands it to
342
+ # the capture, whose `as_*` refuse it as the CDK's do. A key that is
343
+ # not there is not.
344
+ #
345
+ # Raises RubyCdk::Test::AssertionError when the template has no value there.
346
+ #
347
+ # Not in the CDK; added here because a Ruby test has no jest snapshot.
348
+ def get(path)
349
+ segments = path.is_a?(String) ? path.split(".") : path.map(&:to_s)
350
+ value = @template
351
+ segments.each do |segment|
352
+ value = Private::Js.get(value, segment)
353
+ if value.equal?(UNDEFINED)
354
+ raise AssertionError, "Template has no value at #{segments.join(".")}"
355
+ end
356
+ end
357
+
358
+ capture = Capture.new
359
+ capture.test(value).finished
360
+ capture
361
+ end
362
+ end
363
+ end
364
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ported from the AWS CDK.
4
+ #
5
+ # Origin: packages/aws-cdk-lib/assertions/lib/index.ts (the public surface of the module)
6
+ # packages/aws-cdk-lib/matching/lib/match.ts (Match)
7
+ # Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
8
+ # Licence: Apache-2.0
9
+ # Changed: the module is RubyCdk::Test. The factories of Match are module
10
+ # functions of RubyCdk::Test as well, which is where the test plan of this tool
11
+ # writes them; `not` is `not_`, because `not` is a Ruby keyword. template,
12
+ # from_string and from_json stand for Template.fromStack, Template.fromString
13
+ # and Template.fromJSON. Annotations and Tags are not ported: this tool has no
14
+ # synth-time annotations and no Tags aspect.
15
+ #
16
+ # This file requires nothing else of the gem. It loads alone.
17
+
18
+ require_relative "test/capture"
19
+ require_relative "test/match"
20
+ require_relative "test/matcher"
21
+ require_relative "test/template"
22
+
23
+ module RubyCdk
24
+ # What a user's tests ask of a synthesized template: the equivalent of the
25
+ # CDK's aws-cdk-lib/assertions.
26
+ #
27
+ # template = RubyCdk::Test.template(stack) # or from_string(json), from_json(hash)
28
+ # template.resource_count_is(2)
29
+ # template.has_resource("AWS::S3::Bucket",
30
+ # "DeletionPolicy" => "RetainExceptOnCreate",
31
+ # "Properties" => RubyCdk::Test.object_like("BucketName" => "data"))
32
+ # template.has_resource_properties("AWS::Lambda::Function",
33
+ # "Runtime" => "ruby4.0",
34
+ # "Code" => { "S3Bucket" => RubyCdk::Test.any_value })
35
+ # template.get("Resources.Fn.Properties.Timeout").as_number
36
+ #
37
+ # Every method that asserts raises RubyCdk::Test::AssertionError when it fails.
38
+ # In a Minitest run it is a Minitest::Assertion, so the failure is reported as a
39
+ # failure, provided "minitest/autorun" is required before "ruby_cdk/test".
40
+ module Test
41
+ class << self
42
+ # Base your assertions on the CloudFormation template of a stack, of a Hash,
43
+ # or of a JSON string. See RubyCdk::Test::Template.template.
44
+ def template(source, options = {})
45
+ Template.template(source, options)
46
+ end
47
+
48
+ # Base your assertions from a CloudFormation template as a JSON string.
49
+ def from_string(template, options = {})
50
+ Template.from_string(template, options)
51
+ end
52
+
53
+ # Base your assertions from a CloudFormation template as a Hash.
54
+ def from_json(template, options = {})
55
+ Template.from_json(template, options)
56
+ end
57
+
58
+ # Use this matcher in the place of a field's value, if the field must not be present.
59
+ def absent
60
+ Match.absent
61
+ end
62
+
63
+ # Matches the specified pattern with the array found in the same relative
64
+ # path of the target. The elements (or matchers) must be in the order they
65
+ # are found in.
66
+ def array_with(pattern)
67
+ Match.array_with(pattern)
68
+ end
69
+
70
+ # Matches the specified pattern with the array found in the same relative
71
+ # path of the target. The elements (or matchers) must match exactly and in order.
72
+ def array_equals(pattern)
73
+ Match.array_equals(pattern)
74
+ end
75
+
76
+ # Deep exact matching of the specified pattern to the target.
77
+ def exact(pattern)
78
+ Match.exact(pattern)
79
+ end
80
+
81
+ # Matches the specified pattern to an object found in the same relative path
82
+ # of the target. The keys and their values (or matchers) must be present in
83
+ # the target, which can be a superset.
84
+ def object_like(pattern)
85
+ Match.object_like(pattern)
86
+ end
87
+
88
+ # Matches the specified pattern to an object found in the same relative path
89
+ # of the target. The keys and their values (or matchers) must match exactly.
90
+ def object_equals(pattern)
91
+ Match.object_equals(pattern)
92
+ end
93
+
94
+ # Matches any target which does NOT follow the specified pattern.
95
+ # `not_`, because `not` is a Ruby keyword.
96
+ def not_(pattern)
97
+ Match.not_(pattern)
98
+ end
99
+
100
+ # Matches any string-encoded JSON and applies the specified pattern after parsing it.
101
+ def serialized_json(pattern)
102
+ Match.serialized_json(pattern)
103
+ end
104
+
105
+ # Matches any non-null value at the target.
106
+ def any_value
107
+ Match.any_value
108
+ end
109
+
110
+ # Matches targets according to a regular expression.
111
+ def string_like_regexp(pattern)
112
+ Match.string_like_regexp(pattern)
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The values a template is made of: CloudFormation functions, the deferred value
4
+ # and a concrete target. Loads alone: depends only on the errors.
5
+ require_relative "errors"
6
+ require_relative "fn"
7
+ require_relative "lazy"
8
+ require_relative "env"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCdk
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ruby_cdk.rb ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ruby_cdk/version"
4
+ require_relative "ruby_cdk/values"
5
+ require_relative "ruby_cdk/core"
6
+ require_relative "ruby_cdk/cfn"
7
+ require_relative "ruby_cdk/synth"
8
+ require_relative "ruby_cdk/schema"
9
+ require_relative "ruby_cdk/schema/validator"
10
+ require_relative "ruby_cdk/assets"
11
+ require_relative "ruby_cdk/constructs"
12
+
13
+ # Infrastructure as code in pure Ruby, in the style of the AWS CDK.
14
+ #
15
+ # Requiring this file loads the synthesis path only. No AWS SDK is loaded until a
16
+ # deploy command asks for it, so `synth` needs no credentials and starts fast.
17
+ #
18
+ # This is also where the synthesis path is put together. The components know
19
+ # each other only as their rows in the component map say, and a lower one never
20
+ # names a higher one, so the hooks that join them are registered here.
21
+ module RubyCdk
22
+ end
23
+
24
+ # The assets of a stack, for its entry in the manifest: the hashes of what it
25
+ # resolved. Only the assets component can list them, and only the pipeline
26
+ # writes the manifest; neither may name the other.
27
+ RubyCdk::Synth.register_entry_fact(RubyCdk::Synth::Manifest::ASSETS_KEY) do |stack, _template|
28
+ stack.asset_hashes.map { |hash| "sha256:#{hash}" }
29
+ end
30
+
31
+ # Which model was consulted, for the manifest. It moves only when the model does.
32
+ RubyCdk::Synth.register_manifest_fact(RubyCdk::Synth::Manifest::SCHEMA_VERSION_KEY) do
33
+ RubyCdk::Schema.schema_version
34
+ end
35
+
36
+ # What CloudFormation would reject, found before it leaves the machine: the
37
+ # normalizer and the rules, registered with the pipeline here, where two
38
+ # components can be joined without either naming the other. V2 and V13 stay
39
+ # out of the pipeline's list: the tree reports them at synth already. The
40
+ # validators run with warnings: true, so V1's unknown-type and V15's
41
+ # stateful-deletion warnings reach synthesis without failing it; the pipeline
42
+ # keeps them apart on the result, under warnings.
43
+ RubyCdk::Synth.register_normalizer(RubyCdk::Validation.normalizers.first)
44
+ RubyCdk::Validation.validators(
45
+ stateful_types: RubyCdk::Core::Stateful::RETAINED_TYPES + RubyCdk::Core::Stateful::SNAPSHOT_TYPES,
46
+ warnings: true
47
+ ).each { |validator| RubyCdk::Synth.register_validator(validator) }