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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +87 -0
- data/LICENSE +21 -0
- data/README.md +303 -0
- data/THIRD_PARTY_NOTICES.md +327 -0
- data/data/spec/META.json +33 -0
- data/data/spec/model.json.gz +0 -0
- data/exe/ruby-cdk +9 -0
- data/lib/ruby-cdk.rb +4 -0
- data/lib/ruby_cdk/assets/builder.rb +122 -0
- data/lib/ruby_cdk/assets/hasher.rb +175 -0
- data/lib/ruby_cdk/assets/record.rb +49 -0
- data/lib/ruby_cdk/assets/uploader.rb +139 -0
- data/lib/ruby_cdk/assets/zipper.rb +108 -0
- data/lib/ruby_cdk/assets.rb +116 -0
- data/lib/ruby_cdk/bootstrap.rb +289 -0
- data/lib/ruby_cdk/cfn/resource.rb +93 -0
- data/lib/ruby_cdk/cfn.rb +10 -0
- data/lib/ruby_cdk/cli/arguments.rb +41 -0
- data/lib/ruby_cdk/cli/aws.rb +35 -0
- data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
- data/lib/ruby_cdk/cli/diff.rb +98 -0
- data/lib/ruby_cdk/cli/failure.rb +80 -0
- data/lib/ruby_cdk/cli/io.rb +70 -0
- data/lib/ruby_cdk/cli/list.rb +46 -0
- data/lib/ruby_cdk/cli/new.rb +313 -0
- data/lib/ruby_cdk/cli/operation.rb +124 -0
- data/lib/ruby_cdk/cli/pairs.rb +32 -0
- data/lib/ruby_cdk/cli/report.rb +118 -0
- data/lib/ruby_cdk/cli/selection.rb +134 -0
- data/lib/ruby_cdk/cli/session.rb +290 -0
- data/lib/ruby_cdk/cli/shell.rb +30 -0
- data/lib/ruby_cdk/cli/synth.rb +58 -0
- data/lib/ruby_cdk/cli/validate.rb +101 -0
- data/lib/ruby_cdk/cli.rb +242 -0
- data/lib/ruby_cdk/constructs/bucket.rb +191 -0
- data/lib/ruby_cdk/constructs/function.rb +163 -0
- data/lib/ruby_cdk/constructs/grant.rb +90 -0
- data/lib/ruby_cdk/constructs/log_group.rb +115 -0
- data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
- data/lib/ruby_cdk/constructs/queue.rb +118 -0
- data/lib/ruby_cdk/constructs/role.rb +282 -0
- data/lib/ruby_cdk/constructs/table.rb +160 -0
- data/lib/ruby_cdk/constructs/topic.rb +88 -0
- data/lib/ruby_cdk/constructs.rb +56 -0
- data/lib/ruby_cdk/core/app.rb +146 -0
- data/lib/ruby_cdk/core/construct.rb +105 -0
- data/lib/ruby_cdk/core/elements.rb +107 -0
- data/lib/ruby_cdk/core/ids.rb +117 -0
- data/lib/ruby_cdk/core/stack.rb +291 -0
- data/lib/ruby_cdk/core/stateful.rb +156 -0
- data/lib/ruby_cdk/core.rb +15 -0
- data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
- data/lib/ruby_cdk/deploy/deployer.rb +805 -0
- data/lib/ruby_cdk/deploy/errors.rb +72 -0
- data/lib/ruby_cdk/deploy/fakes.rb +546 -0
- data/lib/ruby_cdk/deploy/output/base.rb +167 -0
- data/lib/ruby_cdk/deploy/output/human.rb +37 -0
- data/lib/ruby_cdk/deploy/output/json.rb +80 -0
- data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
- data/lib/ruby_cdk/deploy/parameters.rb +86 -0
- data/lib/ruby_cdk/deploy/poller.rb +72 -0
- data/lib/ruby_cdk/deploy/ports.rb +205 -0
- data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
- data/lib/ruby_cdk/deploy/states.rb +62 -0
- data/lib/ruby_cdk/deploy.rb +25 -0
- data/lib/ruby_cdk/diff/compare.rb +141 -0
- data/lib/ruby_cdk/diff/normalizer.rb +83 -0
- data/lib/ruby_cdk/diff/render.rb +242 -0
- data/lib/ruby_cdk/diff/security.rb +322 -0
- data/lib/ruby_cdk/diff/statement.rb +256 -0
- data/lib/ruby_cdk/diff/template.rb +524 -0
- data/lib/ruby_cdk/diff.rb +62 -0
- data/lib/ruby_cdk/env.rb +42 -0
- data/lib/ruby_cdk/errors.rb +38 -0
- data/lib/ruby_cdk/fn.rb +409 -0
- data/lib/ruby_cdk/lazy.rb +55 -0
- data/lib/ruby_cdk/nag/config.rb +303 -0
- data/lib/ruby_cdk/nag/engine.rb +152 -0
- data/lib/ruby_cdk/nag/report.rb +86 -0
- data/lib/ruby_cdk/nag/rule.rb +171 -0
- data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
- data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
- data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
- data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
- data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
- data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
- data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
- data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
- data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
- data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
- data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
- data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
- data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
- data/lib/ruby_cdk/nag/rules.rb +51 -0
- data/lib/ruby_cdk/nag/values.rb +305 -0
- data/lib/ruby_cdk/nag.rb +60 -0
- data/lib/ruby_cdk/schema/normalizer.rb +164 -0
- data/lib/ruby_cdk/schema/property_schema.rb +22 -0
- data/lib/ruby_cdk/schema/schema.rb +195 -0
- data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
- data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
- data/lib/ruby_cdk/schema/sources/download.rb +86 -0
- data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
- data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
- data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
- data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
- data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
- data/lib/ruby_cdk/schema/sources/update.rb +178 -0
- data/lib/ruby_cdk/schema/sources.rb +20 -0
- data/lib/ruby_cdk/schema/type_schema.rb +109 -0
- data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
- data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
- data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
- data/lib/ruby_cdk/schema/validator/report.rb +92 -0
- data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
- data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
- data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
- data/lib/ruby_cdk/schema/validator/template.rb +203 -0
- data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
- data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
- data/lib/ruby_cdk/schema/validator.rb +159 -0
- data/lib/ruby_cdk/schema.rb +19 -0
- data/lib/ruby_cdk/synth/manifest.rb +128 -0
- data/lib/ruby_cdk/synth/resolver.rb +326 -0
- data/lib/ruby_cdk/synth/rules.rb +83 -0
- data/lib/ruby_cdk/synth/serializer.rb +120 -0
- data/lib/ruby_cdk/synth/writer.rb +31 -0
- data/lib/ruby_cdk/synth.rb +255 -0
- data/lib/ruby_cdk/test/capture.rb +133 -0
- data/lib/ruby_cdk/test/match.rb +426 -0
- data/lib/ruby_cdk/test/matcher.rb +290 -0
- data/lib/ruby_cdk/test/private/absent.rb +34 -0
- data/lib/ruby_cdk/test/private/conditions.rb +39 -0
- data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
- data/lib/ruby_cdk/test/private/error.rb +48 -0
- data/lib/ruby_cdk/test/private/js.rb +326 -0
- data/lib/ruby_cdk/test/private/mappings.rb +39 -0
- data/lib/ruby_cdk/test/private/outputs.rb +39 -0
- data/lib/ruby_cdk/test/private/parameters.rb +39 -0
- data/lib/ruby_cdk/test/private/resources.rb +116 -0
- data/lib/ruby_cdk/test/private/section.rb +98 -0
- data/lib/ruby_cdk/test/private/sorting.rb +40 -0
- data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
- data/lib/ruby_cdk/test/private/type.rb +23 -0
- data/lib/ruby_cdk/test/template.rb +364 -0
- data/lib/ruby_cdk/test.rb +116 -0
- data/lib/ruby_cdk/values.rb +8 -0
- data/lib/ruby_cdk/version.rb +5 -0
- data/lib/ruby_cdk.rb +47 -0
- metadata +256 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../errors"
|
|
4
|
+
|
|
5
|
+
module RubyCdk
|
|
6
|
+
module Deploy
|
|
7
|
+
# The errors the engine raises on purpose, one per situation, each a
|
|
8
|
+
# subclass of the class in errors.rb that carries its exit code. The base
|
|
9
|
+
# class is what a component that did not raise the error can know, so the
|
|
10
|
+
# command line and the engine rescue by it.
|
|
11
|
+
|
|
12
|
+
# Exit 1. The credentials are for another account or region than the stack's env.
|
|
13
|
+
class TargetMismatch < RubyCdk::PreconditionError; end
|
|
14
|
+
|
|
15
|
+
# Exit 1. --account names an account other than the one the credentials are for.
|
|
16
|
+
class AccountMismatch < RubyCdk::PreconditionError; end
|
|
17
|
+
|
|
18
|
+
# Exit 1. An unattended deploy (--yes) of a stack without env, and no --account.
|
|
19
|
+
class AccountRequired < RubyCdk::UsageError; end
|
|
20
|
+
|
|
21
|
+
# Exit 1. No account could be resolved from the credentials.
|
|
22
|
+
class AccountUnresolved < RubyCdk::PreconditionError; end
|
|
23
|
+
|
|
24
|
+
# Exit 1. An operation is already running on the stack.
|
|
25
|
+
class StackBusy < RubyCdk::PreconditionError; end
|
|
26
|
+
|
|
27
|
+
# Exit 1. The stack is in a state that has to be repaired before anything else.
|
|
28
|
+
class StackNeedsRepair < RubyCdk::PreconditionError; end
|
|
29
|
+
|
|
30
|
+
# Exit 1. A CloudFormation status this engine does not know how to place.
|
|
31
|
+
class UnknownStackStatus < RubyCdk::PreconditionError; end
|
|
32
|
+
|
|
33
|
+
# Exit 1. The template is not the JSON this engine sends to CloudFormation.
|
|
34
|
+
class InvalidTemplate < RubyCdk::PreconditionError; end
|
|
35
|
+
|
|
36
|
+
# Exit 1. The template is too large to go inline and nothing else places it yet.
|
|
37
|
+
class TemplateTooLarge < RubyCdk::PreconditionError; end
|
|
38
|
+
|
|
39
|
+
# Exit 1. A parameter of the template has neither a value nor a default.
|
|
40
|
+
class ParametersMissing < RubyCdk::UsageError; end
|
|
41
|
+
|
|
42
|
+
# Exit 1. destroy was asked to delete a stack that is protected.
|
|
43
|
+
class DestroyRefused < RubyCdk::PreconditionError; end
|
|
44
|
+
|
|
45
|
+
# Exit 1. A wait reached its overall limit.
|
|
46
|
+
class WaitTimedOut < RubyCdk::PreconditionError; end
|
|
47
|
+
|
|
48
|
+
# Exit 1. --no-rollback was given for an update whose change set replaces a
|
|
49
|
+
# resource: a replaced resource cannot be recovered when there is no
|
|
50
|
+
# rollback to restore it from.
|
|
51
|
+
class ReplacementNeedsRollback < RubyCdk::PreconditionError; end
|
|
52
|
+
|
|
53
|
+
# Exit 3. CloudFormation rejected the change set, or did not create it.
|
|
54
|
+
class ChangeSetFailed < RubyCdk::ChangeSetRejected; end
|
|
55
|
+
|
|
56
|
+
# Exit 4. Approval was refused, or needed and not given.
|
|
57
|
+
class ApprovalRequired < RubyCdk::ApprovalRefused; end
|
|
58
|
+
|
|
59
|
+
# The exit code of section 14 for an error raised on purpose. Anything that
|
|
60
|
+
# is not a RubyCdk::Error is a bug or an unexpected failure, and also exits 1.
|
|
61
|
+
def self.exit_code_for(error)
|
|
62
|
+
case error
|
|
63
|
+
when RubyCdk::RulesFailed then 6
|
|
64
|
+
when RubyCdk::DeploymentFailed then 5
|
|
65
|
+
when RubyCdk::ApprovalRefused then 4
|
|
66
|
+
when RubyCdk::ChangeSetRejected then 3
|
|
67
|
+
when RubyCdk::ValidationError then 2
|
|
68
|
+
else 1
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../deploy"
|
|
4
|
+
require_relative "states"
|
|
5
|
+
|
|
6
|
+
module RubyCdk
|
|
7
|
+
module Deploy
|
|
8
|
+
# In-memory stand-ins for the two ports and the output, for tests. Nothing
|
|
9
|
+
# here reaches AWS, and nothing here needs a credential.
|
|
10
|
+
#
|
|
11
|
+
# The CloudFormation fake works the way the CDK's own tests do: answers are
|
|
12
|
+
# scripted per operation and consumed in order, and every call the engine
|
|
13
|
+
# made is kept for the assertions. Underneath that it keeps a small state,
|
|
14
|
+
# so a test that needs nothing special writes almost nothing:
|
|
15
|
+
#
|
|
16
|
+
# - a stack registered with a status answers stack_state, stack_info,
|
|
17
|
+
# get_template, stack_parameters and stack_tags from it;
|
|
18
|
+
# - creating a change set brings a new stack into REVIEW_IN_PROGRESS;
|
|
19
|
+
# - executing a change set ends in the status the test chose
|
|
20
|
+
# (execute_result), CREATE_COMPLETE or UPDATE_COMPLETE by default;
|
|
21
|
+
# - deleting a stack ends in DELETE_COMPLETE.
|
|
22
|
+
#
|
|
23
|
+
# client = Fakes::CfnClient.new
|
|
24
|
+
# client.stack("blog", status: "ROLLBACK_COMPLETE")
|
|
25
|
+
# client.on(:describe_change_set, ChangeSet.new(status: "FAILED", status_reason: "wrong type"))
|
|
26
|
+
# out = Fakes::Output.new(approvals: false)
|
|
27
|
+
# error = assert_raises(RubyCdk::Deploy::ChangeSetFailed) do
|
|
28
|
+
# Deployer.new(client: client, store: Fakes::ObjectStore.new, output: out, poller: Poller.instant,
|
|
29
|
+
# operation_poller: Poller.instant).deploy(stack:, template:, identity: client.identity,
|
|
30
|
+
# options: DeployOptions.new(account: Fakes::CfnClient::ACCOUNT))
|
|
31
|
+
# end
|
|
32
|
+
# assert_equal %i[stack_state stack_info delete_stack stack_state create_change_set describe_change_set stack_info
|
|
33
|
+
# operation_events delete_change_set stack_state delete_stack stack_state],
|
|
34
|
+
# client.operations
|
|
35
|
+
module Fakes
|
|
36
|
+
# One call the engine made: the operation, its positional arguments and its keywords.
|
|
37
|
+
Call = Struct.new(:operation, :args, :kwargs, keyword_init: true)
|
|
38
|
+
|
|
39
|
+
# A stack as the fake keeps it.
|
|
40
|
+
Record = Struct.new(:name, :id, :status, :template, :parameters, :tags, :outputs, :termination_protection, :events,
|
|
41
|
+
:resources, keyword_init: true)
|
|
42
|
+
|
|
43
|
+
# A change set as the fake keeps it. empty marks one CloudFormation would
|
|
44
|
+
# refuse: created for a stack whose template is already the new one.
|
|
45
|
+
ChangeSetRecord = Struct.new(:id, :type, :template, :template_url, :parameters, :tags, :capabilities, :on_stack_failure,
|
|
46
|
+
:empty, keyword_init: true)
|
|
47
|
+
|
|
48
|
+
# The CloudFormation port, scripted.
|
|
49
|
+
class CfnClient
|
|
50
|
+
include RubyCdk::Deploy::CfnClient
|
|
51
|
+
|
|
52
|
+
ACCOUNT = "111122223333"
|
|
53
|
+
REGION = "eu-west-1"
|
|
54
|
+
STACK_TYPE = "AWS::CloudFormation::Stack"
|
|
55
|
+
|
|
56
|
+
attr_reader :calls, :executions, :identity
|
|
57
|
+
# { change set id => ChangeSetRecord }, in the order they were created.
|
|
58
|
+
attr_reader :change_sets
|
|
59
|
+
|
|
60
|
+
# identity is what resolve_identity answers unless it is scripted.
|
|
61
|
+
def initialize(identity: Identity.new(account: ACCOUNT, region: REGION, arn: "arn:aws:iam::#{ACCOUNT}:user/deployer"))
|
|
62
|
+
@identity = identity
|
|
63
|
+
@stacks = {}
|
|
64
|
+
@scripts = Hash.new { |hash, operation| hash[operation] = [] }
|
|
65
|
+
@calls = []
|
|
66
|
+
@change_sets = {}
|
|
67
|
+
@executions = []
|
|
68
|
+
@tick = 0
|
|
69
|
+
@execute_result = nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# --- scripting
|
|
73
|
+
|
|
74
|
+
# The next answers of an operation, in order. An answer is a value,
|
|
75
|
+
# which is returned; an Exception, which is raised; or a callable, which
|
|
76
|
+
# is called with the Call and answers what it returns. The last answer
|
|
77
|
+
# repeats, which is what a poll needs.
|
|
78
|
+
def on(operation, *answers)
|
|
79
|
+
@scripts[operation].concat(answers)
|
|
80
|
+
self
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Registers a stack as CloudFormation describes it. Registering a name
|
|
84
|
+
# again keeps its ID and its events.
|
|
85
|
+
def stack(name, status: "CREATE_COMPLETE", template: nil, parameters: {}, tags: [], outputs: {}, termination_protection: false,
|
|
86
|
+
resources: [], id: nil)
|
|
87
|
+
old = lookup(name)
|
|
88
|
+
@stacks[name] = Record.new(
|
|
89
|
+
name: name, id: id || old&.id || "arn:aws:cloudformation:#{REGION}:#{ACCOUNT}:stack/#{name}/id-#{next_number}",
|
|
90
|
+
status: status, template: template, parameters: parameters, tags: tags, outputs: outputs,
|
|
91
|
+
termination_protection: termination_protection, events: old&.events || [], resources: resources
|
|
92
|
+
)
|
|
93
|
+
self
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Adds an event to a stack's history, after the ones already there.
|
|
97
|
+
# at is a Time; by default each event is a second later than the last.
|
|
98
|
+
def emit(name, resource_status, logical_id: name, resource_type: STACK_TYPE, status_reason: nil, at: nil)
|
|
99
|
+
stack(name, status: "CREATE_IN_PROGRESS") unless lookup(name)
|
|
100
|
+
lookup(name).events << Event.new(
|
|
101
|
+
resource_status: resource_status, logical_id: logical_id, resource_type: resource_type,
|
|
102
|
+
status_reason: status_reason, timestamp: at || later
|
|
103
|
+
)
|
|
104
|
+
self
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The status a stack ends in when its change set is executed. nil is the
|
|
108
|
+
# default: CREATE_COMPLETE for a creation, UPDATE_COMPLETE for an update.
|
|
109
|
+
def execute_result(status)
|
|
110
|
+
@execute_result = status
|
|
111
|
+
self
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# --- what the engine did
|
|
115
|
+
|
|
116
|
+
# The operations, in the order they were called.
|
|
117
|
+
def operations
|
|
118
|
+
calls.map(&:operation)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def calls_to(operation)
|
|
122
|
+
calls.select { |call| call.operation == operation }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The keywords of each call of an operation.
|
|
126
|
+
def kwargs_of(operation)
|
|
127
|
+
calls_to(operation).map(&:kwargs)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def last(operation)
|
|
131
|
+
calls_to(operation).last
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def stack_record(name)
|
|
135
|
+
lookup(name)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# --- the port
|
|
139
|
+
|
|
140
|
+
def resolve_identity
|
|
141
|
+
scripted(:resolve_identity) { @identity }
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def stack_state(name)
|
|
145
|
+
scripted(:stack_state, name) do
|
|
146
|
+
record = lookup(name)
|
|
147
|
+
record ? States.classify(record.status) : :absent
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def stack_info(name)
|
|
152
|
+
scripted(:stack_info, name) do
|
|
153
|
+
record = lookup(name)
|
|
154
|
+
record && info_of(record)
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def stack_parameters(name)
|
|
159
|
+
scripted(:stack_parameters, name) { lookup(name)&.parameters || {} }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def stack_tags(name)
|
|
163
|
+
scripted(:stack_tags, name) { lookup(name)&.tags || [] }
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def create_change_set(name, change_set_name, template:, change_set_type:, parameters:, tags:, capabilities:,
|
|
167
|
+
on_stack_failure: nil, template_url: nil)
|
|
168
|
+
scripted(:create_change_set, name, change_set_name, template: template, change_set_type: change_set_type,
|
|
169
|
+
parameters: parameters, tags: tags, capabilities: capabilities, on_stack_failure: on_stack_failure,
|
|
170
|
+
template_url: template_url) do
|
|
171
|
+
id = "arn:aws:cloudformation:#{REGION}:#{ACCOUNT}:changeSet/#{change_set_name}/#{next_number}"
|
|
172
|
+
record = lookup(name)
|
|
173
|
+
# CloudFormation refuses a change set that would change nothing: the
|
|
174
|
+
# set is created FAILED, with the reason (the CDK's own fake marks
|
|
175
|
+
# it the same way, fake-cloudformation.ts). Nothing changes only
|
|
176
|
+
# when the template, the parameters and the tags are all the same.
|
|
177
|
+
# A template placed in the bucket cannot be compared here, so its
|
|
178
|
+
# change set is never marked empty. A test that wants another
|
|
179
|
+
# answer scripts it.
|
|
180
|
+
new_parameters = pairs_of(parameters, record)
|
|
181
|
+
unchanged = template_url.nil? && change_set_type == :update && record && record.template == template &&
|
|
182
|
+
new_parameters == (record.parameters || {}) && tags == (record.tags || [])
|
|
183
|
+
@change_sets[id] = ChangeSetRecord.new(id: id, type: change_set_type, template: template, template_url: template_url,
|
|
184
|
+
parameters: new_parameters, tags: tags, capabilities: capabilities,
|
|
185
|
+
on_stack_failure: on_stack_failure, empty: unchanged)
|
|
186
|
+
bring_into_review(name, template, parameters, tags) if change_set_type == :create
|
|
187
|
+
ChangeSet.new(id: id)
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def describe_change_set(name, change_set_id)
|
|
192
|
+
scripted(:describe_change_set, name, change_set_id) do
|
|
193
|
+
change_set = @change_sets[change_set_id]
|
|
194
|
+
if change_set.nil?
|
|
195
|
+
# A change set that is gone answers as a deleted one, as
|
|
196
|
+
# CloudFormation answers for a deleted set.
|
|
197
|
+
ChangeSet.new(id: change_set_id, status: "DELETE_COMPLETE")
|
|
198
|
+
elsif change_set.empty
|
|
199
|
+
ChangeSet.new(id: change_set_id, status: "FAILED",
|
|
200
|
+
status_reason: "The submitted information didn't contain changes.", changes: [])
|
|
201
|
+
else
|
|
202
|
+
ChangeSet.new(id: change_set_id, status: "CREATE_COMPLETE", execution_status: "AVAILABLE",
|
|
203
|
+
changes: [ Change.new(action: change_set.type == :update ? "Modify" : "Add", logical_id: "Bucket",
|
|
204
|
+
resource_type: "AWS::S3::Bucket", replacement: "False", detail: nil) ])
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def execute_change_set(name, change_set_id, disable_rollback: false, retain_except_on_create: true)
|
|
210
|
+
scripted(:execute_change_set, name, change_set_id, disable_rollback: disable_rollback,
|
|
211
|
+
retain_except_on_create: retain_except_on_create) do
|
|
212
|
+
@executions << { name: name, change_set_id: change_set_id, disable_rollback: disable_rollback,
|
|
213
|
+
retain_except_on_create: retain_except_on_create }
|
|
214
|
+
change_set = @change_sets[change_set_id]
|
|
215
|
+
record = lookup(name)
|
|
216
|
+
if change_set && record
|
|
217
|
+
record.template = change_set.template
|
|
218
|
+
record.parameters = change_set.parameters
|
|
219
|
+
record.tags = change_set.tags
|
|
220
|
+
record.status = @execute_result || (change_set.type == :create ? "CREATE_COMPLETE" : "UPDATE_COMPLETE")
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def delete_change_set(name, change_set_id)
|
|
226
|
+
scripted(:delete_change_set, name, change_set_id) { @change_sets.delete(change_set_id) }
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# A stack protected from deletion is not deleted, as in CloudFormation.
|
|
230
|
+
def delete_stack(name)
|
|
231
|
+
scripted(:delete_stack, name) do
|
|
232
|
+
record = lookup(name)
|
|
233
|
+
if record
|
|
234
|
+
refuse_protected(record)
|
|
235
|
+
record.status = "DELETE_COMPLETE"
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def list_resources(name_or_id)
|
|
241
|
+
scripted(:list_resources, name_or_id) { lookup(name_or_id)&.resources || [] }
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Every registered stack that is not deleted.
|
|
245
|
+
def list_stacks
|
|
246
|
+
scripted(:list_stacks) do
|
|
247
|
+
@stacks.values.reject { |record| record.status == StackStatus::DELETED }
|
|
248
|
+
.map { |record| ListedStack.new(name: record.name, id: record.id, status: record.status) }
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def events(name_or_id, after:)
|
|
253
|
+
scripted(:events, name_or_id, after: after) do
|
|
254
|
+
(lookup(name_or_id)&.events || []).select { |event| after.nil? || event.timestamp > after }
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def get_template(name)
|
|
259
|
+
scripted(:get_template, name) { lookup(name)&.template }
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def update_termination_protection(name, enabled:)
|
|
263
|
+
scripted(:update_termination_protection, name, enabled: enabled) do
|
|
264
|
+
record = lookup(name)
|
|
265
|
+
record.termination_protection = enabled if record
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# CloudFormation's own events are only ever scripted here.
|
|
270
|
+
def operation_events(name, change_set_id)
|
|
271
|
+
scripted(:operation_events, name, change_set_id) { [] }
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
private
|
|
275
|
+
|
|
276
|
+
# Records the call, answers from the script when there is one, and
|
|
277
|
+
# otherwise leaves it to the block.
|
|
278
|
+
def scripted(operation, *args, **kwargs)
|
|
279
|
+
@calls << Call.new(operation: operation, args: args, kwargs: kwargs)
|
|
280
|
+
queue = @scripts[operation]
|
|
281
|
+
if queue.any?
|
|
282
|
+
answer = queue.length > 1 ? queue.shift : queue.first
|
|
283
|
+
raise answer if answer.is_a?(Exception)
|
|
284
|
+
return answer.call(Call.new(operation: operation, args: args, kwargs: kwargs)) if answer.respond_to?(:call)
|
|
285
|
+
|
|
286
|
+
return answer
|
|
287
|
+
end
|
|
288
|
+
yield
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def lookup(name_or_id)
|
|
292
|
+
@stacks.each_value.find { |record| record.name == name_or_id || record.id == name_or_id }
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# A CREATE change set is what brings a stack into existence, with nothing
|
|
296
|
+
# in it yet, as in CloudFormation. A stack that does not exist, or was
|
|
297
|
+
# deleted, comes into being with an ID of its own; one that is already
|
|
298
|
+
# waiting in REVIEW_IN_PROGRESS keeps its ID and takes the new template.
|
|
299
|
+
def bring_into_review(name, template, parameters, tags)
|
|
300
|
+
record = lookup(name)
|
|
301
|
+
fresh = record.nil? || record.status == StackStatus::DELETED
|
|
302
|
+
stack(name, status: "REVIEW_IN_PROGRESS", template: template, parameters: pairs_of(parameters, fresh ? nil : record),
|
|
303
|
+
tags: tags,
|
|
304
|
+
id: fresh ? "arn:aws:cloudformation:#{REGION}:#{ACCOUNT}:stack/#{name}/id-#{next_number}" : nil,
|
|
305
|
+
outputs: record&.outputs || {}, resources: record&.resources || [])
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def info_of(record)
|
|
309
|
+
StackInfo.new(name: record.name, id: record.id, status: record.status, outputs: record.outputs,
|
|
310
|
+
termination_protection: record.termination_protection)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# The parameters of a change set as the stack keeps them: { key => value }.
|
|
314
|
+
# A use_previous_value entry keeps what the stack has, which is how the
|
|
315
|
+
# masked value of a NoEcho parameter goes back as it came.
|
|
316
|
+
def pairs_of(parameters, record = nil)
|
|
317
|
+
parameters.to_a.map do |pair|
|
|
318
|
+
key = pair[:parameter_key] || pair["parameter_key"]
|
|
319
|
+
if pair[:use_previous_value]
|
|
320
|
+
[ key, record&.parameters&.dig(key) ]
|
|
321
|
+
else
|
|
322
|
+
[ key, pair[:parameter_value] || pair["parameter_value"] ]
|
|
323
|
+
end
|
|
324
|
+
end.to_h
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
def refuse_protected(record)
|
|
328
|
+
return unless record.termination_protection
|
|
329
|
+
|
|
330
|
+
raise RubyCdk::PreconditionError, "Stack #{record.name} is protected from deletion (termination protection)"
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def next_number
|
|
334
|
+
@tick += 1
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def later
|
|
338
|
+
@tick += 1
|
|
339
|
+
Time.utc(2026, 1, 1) + @tick
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
# The S3 port. Buckets have owners, and a call that names the wrong owner
|
|
344
|
+
# is refused, which is what the owner check is for.
|
|
345
|
+
class ObjectStore
|
|
346
|
+
include RubyCdk::Deploy::ObjectStore
|
|
347
|
+
|
|
348
|
+
attr_reader :calls
|
|
349
|
+
# { [ bucket, key ] => body }
|
|
350
|
+
attr_reader :objects
|
|
351
|
+
|
|
352
|
+
def initialize
|
|
353
|
+
@owners = {}
|
|
354
|
+
@objects = {}
|
|
355
|
+
@calls = []
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
# Creates a bucket owned by an account. owner defaults to the account of the CloudFormation fake.
|
|
359
|
+
def bucket(name, owner: CfnClient::ACCOUNT)
|
|
360
|
+
@owners[name] = owner
|
|
361
|
+
self
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def bucket_state(name, expected_owner:)
|
|
365
|
+
record(:bucket_state, name, nil, expected_owner)
|
|
366
|
+
owner = @owners[name]
|
|
367
|
+
return :absent if owner.nil?
|
|
368
|
+
|
|
369
|
+
owner == expected_owner ? :mine : :foreign
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
def object_exists?(bucket, key, expected_owner:)
|
|
373
|
+
record(:object_exists?, bucket, key, expected_owner)
|
|
374
|
+
check_owner(bucket, expected_owner)
|
|
375
|
+
@objects.key?([ bucket, key ])
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
def put(bucket, key, body:, expected_owner:)
|
|
379
|
+
record(:put, bucket, key, expected_owner)
|
|
380
|
+
check_owner(bucket, expected_owner)
|
|
381
|
+
@objects[[ bucket, key ]] = body
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
# The operations, in the order they were called.
|
|
385
|
+
def operations
|
|
386
|
+
calls.map(&:operation)
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
private
|
|
390
|
+
|
|
391
|
+
# The state of a bucket without recording the question.
|
|
392
|
+
def state_of(bucket, expected_owner)
|
|
393
|
+
owner = @owners[bucket]
|
|
394
|
+
return :absent if owner.nil?
|
|
395
|
+
|
|
396
|
+
owner == expected_owner ? :mine : :foreign
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
def check_owner(bucket, expected_owner)
|
|
400
|
+
state = state_of(bucket, expected_owner)
|
|
401
|
+
return if state == :mine
|
|
402
|
+
|
|
403
|
+
reason = state == :absent ? "does not exist" : "belongs to another account than #{expected_owner}"
|
|
404
|
+
raise RubyCdk::PreconditionError, "Bucket #{bucket} #{reason}, so the operation stops."
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
def record(operation, bucket, key, expected_owner)
|
|
408
|
+
@calls << Call.new(operation: operation, args: [ bucket, key ].compact, kwargs: { expected_owner: expected_owner })
|
|
409
|
+
end
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
# The output, recorded. What a question is answered is chosen when the
|
|
413
|
+
# fake is built; everything else is kept for the assertions.
|
|
414
|
+
class Output
|
|
415
|
+
include RubyCdk::Deploy::Output
|
|
416
|
+
|
|
417
|
+
attr_reader :calls
|
|
418
|
+
|
|
419
|
+
# approvals answers ask and confirmations answers confirm: true, false,
|
|
420
|
+
# or an Array consumed in order whose last answer repeats.
|
|
421
|
+
def initialize(approvals: false, confirmations: false)
|
|
422
|
+
@calls = []
|
|
423
|
+
@approvals = answers_of(approvals)
|
|
424
|
+
@confirmations = answers_of(confirmations)
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
def target(account:, region:, profile:)
|
|
428
|
+
record(:target, account: account, region: region, profile: profile)
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
def changes(list, capabilities: nil)
|
|
432
|
+
record(:changes, list, capabilities: capabilities)
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
# The answer is a boolean, as the port promises: exactly true for yes.
|
|
436
|
+
def ask(question)
|
|
437
|
+
record(:ask, question)
|
|
438
|
+
next_answer(@approvals) == true
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
# confirmations are the texts that get typed.
|
|
442
|
+
def confirm(text)
|
|
443
|
+
record(:confirm, text)
|
|
444
|
+
next_answer(@confirmations) == text
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
def event(event)
|
|
448
|
+
record(:event, event)
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
def outputs(hash)
|
|
452
|
+
# Nothing is printed for an empty set of outputs, so nothing is kept.
|
|
453
|
+
record(:outputs, hash) unless hash.nil? || hash.empty?
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
def failure(detail)
|
|
457
|
+
record(:failure, detail)
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
def retained(list)
|
|
461
|
+
record(:retained, list)
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def announce(text)
|
|
465
|
+
record(:announce, text)
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
# The calls of one operation, most recent last.
|
|
469
|
+
def said(operation)
|
|
470
|
+
calls.select { |call| call.operation == operation }
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
# Everything that was said, as text: what was printed, asked and announced.
|
|
474
|
+
def transcript
|
|
475
|
+
calls.flat_map { |call| call.args.map(&:to_s) }
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
# --- what was said, by kind. The names differ from the port's so that
|
|
479
|
+
# a reader never hides the method the engine calls.
|
|
480
|
+
|
|
481
|
+
# { account:, region:, profile: } for each target line.
|
|
482
|
+
def targets
|
|
483
|
+
said(:target).map(&:kwargs)
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
# The yes-or-no questions that were asked.
|
|
487
|
+
def questions
|
|
488
|
+
said(:ask).flat_map(&:args)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
# The texts the person was asked to type.
|
|
492
|
+
def confirmations
|
|
493
|
+
said(:confirm).flat_map(&:args)
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
# The capabilities shown with each change view.
|
|
497
|
+
def shown_capabilities
|
|
498
|
+
said(:changes).map { |call| call.kwargs[:capabilities] }
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
# The list of changes of each change view.
|
|
502
|
+
def shown_changes
|
|
503
|
+
said(:changes).map { |call| call.args.first }
|
|
504
|
+
end
|
|
505
|
+
|
|
506
|
+
def shown_events
|
|
507
|
+
said(:event).flat_map(&:args)
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
# The outputs of each time they were given.
|
|
511
|
+
def shown_outputs
|
|
512
|
+
said(:outputs).flat_map(&:args)
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
def failures
|
|
516
|
+
said(:failure).flat_map(&:args)
|
|
517
|
+
end
|
|
518
|
+
|
|
519
|
+
# The resources each destroy said it would keep.
|
|
520
|
+
def shown_retained
|
|
521
|
+
said(:retained).flat_map(&:args).flatten
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
def announcements
|
|
525
|
+
said(:announce).flat_map(&:args)
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
private
|
|
529
|
+
|
|
530
|
+
def record(operation, *args, **kwargs)
|
|
531
|
+
@calls << Call.new(operation: operation, args: args, kwargs: kwargs)
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
# A list is taken as it is; anything else is the one answer, repeated.
|
|
535
|
+
def answers_of(answer)
|
|
536
|
+
answer.is_a?(Array) ? answer : [ answer ]
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
# The next of the scripted answers; the last one repeats.
|
|
540
|
+
def next_answer(answers)
|
|
541
|
+
answers.length > 1 ? answers.shift : answers.first
|
|
542
|
+
end
|
|
543
|
+
end
|
|
544
|
+
end
|
|
545
|
+
end
|
|
546
|
+
end
|