Skip to content
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

[ARM] Fix #19124: az deployment what-if: Handle unsupported and no effect change types #19144

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/azure-cli/azure/cli/command_modules/resource/_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
ChangeType.deploy: Color.BLUE,
ChangeType.no_change: Color.RESET,
ChangeType.ignore: Color.GRAY,
ChangeType.unsupported: Color.GRAY,
PropertyChangeType.no_effect: Color.GRAY,
}

_property_change_type_to_color = {
PropertyChangeType.create: Color.GREEN,
PropertyChangeType.delete: Color.ORANGE,
PropertyChangeType.modify: Color.PURPLE,
PropertyChangeType.array: Color.PURPLE,
PropertyChangeType.no_effect: Color.GRAY,
}

_change_type_to_symbol = {
Expand All @@ -34,13 +37,16 @@
ChangeType.deploy: Symbol.EXCLAMATION_POINT,
ChangeType.no_change: Symbol.EQUAL,
ChangeType.ignore: Symbol.ASTERISK,
ChangeType.unsupported: Symbol.CROSS,
PropertyChangeType.no_effect: Symbol.CROSS,
}

_property_change_type_to_symbol = {
PropertyChangeType.create: Symbol.PLUS,
PropertyChangeType.delete: Symbol.MINUS,
PropertyChangeType.modify: Symbol.TILDE,
PropertyChangeType.array: Symbol.TILDE,
PropertyChangeType.no_effect: Symbol.CROSS,
}

_change_type_to_weight = {
Expand All @@ -49,14 +55,17 @@
ChangeType.deploy: 2,
ChangeType.modify: 3,
ChangeType.no_change: 4,
ChangeType.ignore: 5,
ChangeType.unsupported: 5,
ChangeType.ignore: 6,
PropertyChangeType.no_effect: 7,
}

_property_change_type_to_weight = {
PropertyChangeType.delete: 0,
PropertyChangeType.create: 1,
PropertyChangeType.modify: 2,
PropertyChangeType.array: 2,
PropertyChangeType.no_effect: 3,
}


Expand Down Expand Up @@ -127,7 +136,7 @@ def _format_resource_changes_stats(builder, resource_changes):
builder.append(", ".join(change_type_stats)).append(".")


def _format_change_type_count(change_type, count):
def _format_change_type_count(change_type, count): # pylint: disable=too-many-return-statements
if change_type == ChangeType.create:
return f"{count} to create"
if change_type == ChangeType.delete:
Expand All @@ -140,6 +149,8 @@ def _format_change_type_count(change_type, count):
return f"{count} to ignore"
if change_type == ChangeType.no_change:
return f"{count} no change"
if change_type == ChangeType.unsupported:
return f"{count} unsupported"

raise ValueError(f"Invalid ChangeType: {change_type}")

Expand Down Expand Up @@ -190,7 +201,7 @@ def _format_resource_change(builder, resource_change, is_last):
elif change_type == ChangeType.delete and resource_change.before:
_format_json(builder, resource_change.before, indent_level=2)

elif change_type == ChangeType.modify and resource_change.delta:
elif resource_change.delta:
with builder.new_color_scope(Color.RESET):
builder.append_line()
_format_property_changes(
Expand Down Expand Up @@ -256,6 +267,9 @@ def _format_property_change(builder, property_change, max_path_length, indent_le
elif property_change_type == PropertyChangeType.array:
_format_property_change_path(builder, property_change, "children", max_path_length, indent_level)
_format_property_array_change(builder, children, indent_level + 1)
elif property_change_type == PropertyChangeType.no_effect:
_format_property_change_path(builder, property_change, "after", max_path_length, indent_level)
_format_property_no_effect(builder, after, indent_level + 1)
else:
raise ValueError(f"Unknown property change type: {property_change_type}.")

Expand Down Expand Up @@ -290,6 +304,11 @@ def _format_property_change_type(builder, property_change_type):
builder.append(property_change_symbol, property_change_color).append(Symbol.WHITE_SPACE)


def _format_property_no_effect(builder, value, indent_level):
with builder.new_color_scope(_property_change_type_to_color[PropertyChangeType.no_effect]):
_format_json(builder, value, indent_level=indent_level)


def _format_property_create(builder, value, indent_level):
with builder.new_color_scope(_property_change_type_to_color[PropertyChangeType.create]):
_format_json(builder, value, indent_level=indent_level)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Symbol(Enum):
MINUS = "-"
TILDE = "~"
EXCLAMATION_POINT = "!"
CROSS = "x"

def __str__(self):
return self.value
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ def test_sort_resource_ids_within_a_scope(self):
resource_id="/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p7/foo",
change_type=ChangeType.delete,
),
WhatIfChange(
resource_id="/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p8/foo",
change_type=ChangeType.unsupported,
),
]

expected = f"""
Expand All @@ -275,6 +279,7 @@ def test_sort_resource_ids_within_a_scope(self):
+ p2/foo{Color.RESET}{Color.BLUE}
! p4/foo{Color.RESET}{Color.RESET}
= p3/foo{Color.RESET}{Color.GRAY}
x p8/foo{Color.RESET}{Color.GRAY}
* p1/foo
{Color.RESET}"""
result = format_what_if_operation_result(WhatIfOperationResult(changes=changes))
Expand Down Expand Up @@ -359,6 +364,11 @@ def test_property_modify(self):
"tag2": "value"
},
),
WhatIfPropertyChange(
path="path.a.to.change3",
property_change_type=PropertyChangeType.no_effect,
after=12345,
),
WhatIfPropertyChange(
path="path.b.to.nested.change",
property_change_type=PropertyChangeType.array,
Expand Down Expand Up @@ -393,7 +403,7 @@ def test_property_modify(self):
Scope: /subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1
{Color.PURPLE}
~ p1/foo{Color.RESET}
{Color.PURPLE}~{Color.RESET} path.a.to.change{Color.RESET}:{Color.RESET} {Color.ORANGE}"foo"{Color.RESET} => {Color.GREEN}"bar"{Color.RESET}
{Color.PURPLE}~{Color.RESET} path.a.to.change{Color.RESET}:{Color.RESET} {Color.ORANGE}"foo"{Color.RESET} => {Color.GREEN}"bar"{Color.RESET}
{Color.PURPLE}~{Color.RESET} path.a.to.change2{Color.RESET}:{Color.RESET}{Color.ORANGE}

tag1{Color.RESET}:{Color.ORANGE} "value"
Expand All @@ -413,6 +423,7 @@ def test_property_modify(self):

{Color.ORANGE}-{Color.RESET} 5{Color.RESET}:{Color.RESET} {Color.ORANGE}12345{Color.RESET}
]
{Color.GRAY}x{Color.RESET} path.a.to.change3{Color.RESET}:{Color.RESET} {Color.GRAY}12345{Color.RESET}
{Color.PURPLE}{Color.RESET}"""

result = format_what_if_operation_result(WhatIfOperationResult(changes=changes))
Expand Down