| 31 | |
| 32 | @dataclass |
| 33 | class InfraDiff: |
| 34 | infra_object_diffs: List[InfraObjectDiff] |
| 35 | |
| 36 | def __init__(self): |
| 37 | self.infra_object_diffs = [] |
| 38 | |
| 39 | def update(self, progress_ctx: Optional["ApplyProgressContext"] = None): |
| 40 | """Apply the infrastructure changes specified in this object.""" |
| 41 | |
| 42 | for infra_object_diff in self.infra_object_diffs: |
| 43 | if infra_object_diff.transition_type in [ |
| 44 | TransitionType.DELETE, |
| 45 | TransitionType.UPDATE, |
| 46 | ]: |
| 47 | infra_object = InfraObject.from_proto( |
| 48 | infra_object_diff.current_infra_object |
| 49 | ) |
| 50 | if progress_ctx: |
| 51 | progress_ctx.update_phase_progress( |
| 52 | f"Tearing down {infra_object_diff.name}" |
| 53 | ) |
| 54 | infra_object.teardown() |
| 55 | elif infra_object_diff.transition_type in [ |
| 56 | TransitionType.CREATE, |
| 57 | TransitionType.UPDATE, |
| 58 | ]: |
| 59 | infra_object = InfraObject.from_proto( |
| 60 | infra_object_diff.new_infra_object |
| 61 | ) |
| 62 | if progress_ctx: |
| 63 | progress_ctx.update_phase_progress( |
| 64 | f"Creating/updating {infra_object_diff.name}" |
| 65 | ) |
| 66 | infra_object.update() |
| 67 | |
| 68 | def to_string(self): |
| 69 | from colorama import Fore, Style |
| 70 | |
| 71 | log_string = "" |
| 72 | |
| 73 | message_action_map = { |
| 74 | TransitionType.CREATE: ("Created", Fore.GREEN), |
| 75 | TransitionType.DELETE: ("Deleted", Fore.RED), |
| 76 | TransitionType.UNCHANGED: ("Unchanged", Fore.LIGHTBLUE_EX), |
| 77 | TransitionType.UPDATE: ("Updated", Fore.YELLOW), |
| 78 | } |
| 79 | for infra_object_diff in self.infra_object_diffs: |
| 80 | if infra_object_diff.transition_type == TransitionType.UNCHANGED: |
| 81 | continue |
| 82 | action, color = message_action_map[infra_object_diff.transition_type] |
| 83 | log_string += f"{action} {infra_object_diff.infra_object_type} {Style.BRIGHT + color}{infra_object_diff.name}{Style.RESET_ALL}\n" |
| 84 | if infra_object_diff.transition_type == TransitionType.UPDATE: |
| 85 | for _p in infra_object_diff.infra_object_property_diffs: |
| 86 | log_string += f"\t{_p.property_name}: {Style.BRIGHT + color}{_p.val_existing}{Style.RESET_ALL} -> {Style.BRIGHT + Fore.LIGHTGREEN_EX}{_p.val_declared}{Style.RESET_ALL}\n" |
| 87 | |
| 88 | log_string = ( |
| 89 | f"{Style.BRIGHT + Fore.LIGHTBLUE_EX}No changes to infrastructure" |
| 90 | if not log_string |