Class to export a CloudFormation template
| 615 | |
| 616 | |
| 617 | class Template: |
| 618 | """ |
| 619 | Class to export a CloudFormation template |
| 620 | """ |
| 621 | |
| 622 | def __init__( |
| 623 | self, |
| 624 | template_path, |
| 625 | parent_dir, |
| 626 | uploader, |
| 627 | resources_to_export=RESOURCES_EXPORT_LIST, |
| 628 | metadata_to_export=METADATA_EXPORT_LIST, |
| 629 | ): |
| 630 | """ |
| 631 | Reads the template and makes it ready for export |
| 632 | """ |
| 633 | |
| 634 | if not (is_local_folder(parent_dir) and os.path.isabs(parent_dir)): |
| 635 | raise ValueError( |
| 636 | "parent_dir parameter must be " |
| 637 | f"an absolute path to a folder {parent_dir}" |
| 638 | ) |
| 639 | |
| 640 | abs_template_path = make_abs_path(parent_dir, template_path) |
| 641 | template_dir = os.path.dirname(abs_template_path) |
| 642 | |
| 643 | with compat_open(abs_template_path, "r") as handle: |
| 644 | template_str = handle.read() |
| 645 | |
| 646 | self.template_dict = yaml_parse(template_str) |
| 647 | self.template_dir = template_dir |
| 648 | self.resources_to_export = resources_to_export |
| 649 | self.metadata_to_export = metadata_to_export |
| 650 | self.uploader = uploader |
| 651 | |
| 652 | def export_global_artifacts(self, template_dict): |
| 653 | """ |
| 654 | Template params such as AWS::Include transforms are not specific to |
| 655 | any resource type but contain artifacts that should be exported, |
| 656 | here we iterate through the template dict and export params with a |
| 657 | handler defined in GLOBAL_EXPORT_DICT |
| 658 | """ |
| 659 | for key, val in template_dict.items(): |
| 660 | if key in GLOBAL_EXPORT_DICT: |
| 661 | template_dict[key] = GLOBAL_EXPORT_DICT[key]( |
| 662 | val, self.uploader, self.template_dir |
| 663 | ) |
| 664 | elif isinstance(val, dict): |
| 665 | self.export_global_artifacts(val) |
| 666 | elif isinstance(val, list): |
| 667 | for item in val: |
| 668 | if isinstance(item, dict): |
| 669 | self.export_global_artifacts(item) |
| 670 | return template_dict |
| 671 | |
| 672 | def export_metadata(self, template_dict): |
| 673 | """ |
| 674 | Exports the local artifacts referenced by the metadata section in |
no outgoing calls