-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add local module support to the cloudformation package command #9124
Draft
ericzbeard
wants to merge
26
commits into
aws:develop
Choose a base branch
from
ericzbeard:cfn-modules
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0438bdc
Initial commit with TODOs for implementing modules
ericzbeard 84cb385
Unit test configured
ericzbeard 9eac23a
Start resolving refs
ericzbeard 0b79526
Resolve Refs
ericzbeard 6f24f03
parse_sub
ericzbeard 86a5d29
Sub strings
ericzbeard 7b72e29
Basic tests pass
ericzbeard df507cd
Fixed pylint issues
ericzbeard 7808f62
Fix path to unit tests
ericzbeard e2131d2
Add the ability to specify a module as a Resource
ericzbeard 49fb392
Fix issues with nested Subs and lists
ericzbeard ec32ecf
Recursive modules
ericzbeard 160be7c
Implement module outputs
ericzbeard e517720
Handle spaces in parse_sub
ericzbeard e8e6d96
Enum
ericzbeard 5b0ee49
Code review fixes
ericzbeard 4eef2c4
Code review fixes
ericzbeard 72659f5
Fix merging lists
ericzbeard 5cb126c
Vpc module example with a map
ericzbeard 3ac0577
Use Map in the parent template
ericzbeard e0cfcfc
Moving docs to a readme
ericzbeard c6851c9
updating readme
ericzbeard dd16375
Handle extra dots in getatts
ericzbeard e119576
Fix manifest error
ericzbeard 39ac9a7
Add basic conditional support
ericzbeard 2f0143b
Added more complete unit test for conditionals
ericzbeard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,5 @@ doc/source/tutorial/services.rst | |
|
||
# Pyenv | ||
.python-version | ||
.env | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
awscli/customizations/cloudformation/module_conditions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
|
||
# pylint: disable=fixme | ||
|
||
""" | ||
Parse the Conditions section in a module | ||
|
||
This section is not emitted into the output. | ||
We have to be able to fully resolve it locally. | ||
""" | ||
|
||
from awscli.customizations.cloudformation import exceptions | ||
|
||
AND = "Fn::And" | ||
EQUALS = "Fn::Equals" | ||
IF = "Fn::If" | ||
NOT = "Fn::Not" | ||
OR = "Fn::Or" | ||
REF = "Ref" | ||
CONDITION = "Condition" | ||
|
||
|
||
def parse_conditions(d, find_ref): | ||
"""Parse conditions and return a map of name:boolean""" | ||
retval = {} | ||
|
||
for k, v in d.items(): | ||
retval[k] = istrue(v, find_ref, retval) | ||
|
||
return retval | ||
|
||
|
||
def resolve_if(v, find_ref, prior): | ||
"Resolve Fn::If" | ||
msg = f"If expression should be a list with 3 elements: {v}" | ||
if not isinstance(v, list): | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
if len(v) != 3: | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
if istrue(v[0], find_ref, prior): | ||
return v[1] | ||
return v[2] | ||
|
||
|
||
# pylint: disable=too-many-branches,too-many-statements | ||
def istrue(v, find_ref, prior): | ||
"Recursive function to evaluate a Condition" | ||
retval = False | ||
if EQUALS in v: | ||
eq = v[EQUALS] | ||
if len(eq) == 2: | ||
val0 = eq[0] | ||
val1 = eq[1] | ||
if IF in val0: | ||
val0 = resolve_if(val0[IF], find_ref, prior) | ||
if IF in val1: | ||
val1 = resolve_if(val1[IF], find_ref, prior) | ||
if REF in val0: | ||
val0 = find_ref(val0[REF]) | ||
if REF in val1: | ||
val1 = find_ref(val1[REF]) | ||
retval = val0 == val1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm reading this correct its possible this becomes val0 = "foo", val1 = {"Ref": "SomeResource"}. This would evaluate to false but could be true if this resource returns the value |
||
else: | ||
msg = f"Equals expression should be a list with 2 elements: {eq}" | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
if NOT in v: | ||
if not isinstance(v[NOT], list): | ||
msg = f"Not expression should be a list with 1 element: {v[NOT]}" | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
retval = not istrue(v[NOT][0], find_ref, prior) | ||
if AND in v: | ||
vand = v[AND] | ||
msg = f"And expression should be a list with 2 elements: {vand}" | ||
if not isinstance(vand, list): | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
if len(vand) != 2: | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
retval = istrue(vand[0], find_ref, prior) and istrue( | ||
vand[1], find_ref, prior | ||
) | ||
if OR in v: | ||
vor = v[OR] | ||
msg = f"Or expression should be a list with 2 elements: {vor}" | ||
if not isinstance(vor, list): | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
if len(vor) != 2: | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
retval = istrue(vor[0], find_ref, prior) or istrue( | ||
vor[1], find_ref, prior | ||
) | ||
if IF in v: | ||
# Shouldn't ever see an IF here | ||
msg = f"Unexpected If: {v[IF]}" | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
if CONDITION in v: | ||
condition_name = v[CONDITION] | ||
if condition_name in prior: | ||
retval = prior[condition_name] | ||
else: | ||
msg = f"Condition {condition_name} was not evaluated yet" | ||
raise exceptions.InvalidModuleError(msg=msg) | ||
# TODO: Should we re-order the conditions? | ||
# We are depending on the author putting them in order | ||
|
||
return retval |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Today CloudFormation doesn't support this. The only concern I have with this is it could cause infinite loops.
Later on we call out that
Fn::If
isn't supported so not sure why it is here.