Exemplo n.º 1
0
    def test_yaml_with_tags(self):
        output = yaml_parse(self.yaml_with_tags)
        self.assertEquals(self.parsed_yaml_dict, output)

        # Make sure formatter and parser work well with each other
        formatted_str = yaml_dump(output)
        output_again = yaml_parse(formatted_str)
        self.assertEquals(output, output_again)
Exemplo n.º 2
0
    def test_yaml_with_tags(self):
        output = yaml_parse(self.yaml_with_tags)
        self.assertEquals(self.parsed_yaml_dict, output)

        # Make sure formatter and parser work well with each other
        formatted_str = yaml_dump(output)
        output_again = yaml_parse(formatted_str)
        self.assertEquals(output, output_again)
Exemplo n.º 3
0
    def _download_swagger(self, location):
        """
        Download the file from given local or remote location and return it

        Parameters
        ----------
        location : str or dict
            Local path or S3 path to Swagger file to download. Consult the ``__init__.py`` documentation for specifics
            on structure of this property.

        Returns
        -------
        dict or None
            Downloaded and parsed Swagger document. None, if unable to download
        """

        if not location:
            return

        bucket, key, version = self._parse_s3_location(location)
        if bucket and key:
            LOG.debug(
                "Downloading Swagger document from Bucket=%s, Key=%s, Version=%s",
                bucket, key, version)
            swagger_str = self._download_from_s3(bucket, key, version)
            return yaml_parse(swagger_str)

        if not isinstance(location, string_types):
            # This is not a string and not a S3 Location dictionary. Probably something invalid
            LOG.debug("Unable to download Swagger file. Invalid location: %s",
                      location)
            return

        # ``location`` is a string and not a S3 path. It is probably a local path. Let's resolve relative path if any
        filepath = location
        if self.working_dir:
            # Resolve relative paths, if any, with respect to working directory
            filepath = os.path.join(self.working_dir, location)

        if not os.path.exists(filepath):
            LOG.debug(
                "Unable to download Swagger file. File not found at location %s",
                filepath)
            return

        LOG.debug("Reading Swagger document from local file at %s", filepath)
        with open(filepath, "r") as fp:
            return yaml_parse(fp.read())
Exemplo n.º 4
0
    def __init__(
            self,
            template_path: str,
            parent_dir: str,
            uploaders: Uploaders,
            code_signer: CodeSigner,
            resources_to_export=frozenset(
                RESOURCES_EXPORT_LIST +
                [CloudFormationStackResource, ServerlessApplicationResource]),
            metadata_to_export=frozenset(METADATA_EXPORT_LIST),
    ):
        """
        Reads the template and makes it ready for export
        """
        if not (is_local_folder(parent_dir) and os.path.isabs(parent_dir)):
            raise ValueError(
                "parent_dir parameter must be "
                "an absolute path to a folder {0}".format(parent_dir))

        abs_template_path = make_abs_path(parent_dir, template_path)
        template_dir = os.path.dirname(abs_template_path)

        with open(abs_template_path, "r") as handle:
            template_str = handle.read()

        self.template_dict = yaml_parse(template_str)
        self.template_dir = template_dir
        self.resources_to_export = resources_to_export
        self.metadata_to_export = metadata_to_export
        self.uploaders = uploaders
        self.code_signer = code_signer
Exemplo n.º 5
0
    def test_parse_yaml_preserve_elements_order(self):
        input_template = ("B_Resource:\n"
                          "  Key2:\n"
                          "    Name: name2\n"
                          "  Key1:\n"
                          "    Name: name1\n"
                          "A_Resource:\n"
                          "  Key2:\n"
                          "    Name: name2\n"
                          "  Key1:\n"
                          "    Name: name1\n")
        output_dict = yaml_parse(input_template)
        expected_dict = OrderedDict([
            ("B_Resource",
             OrderedDict([("Key2", {
                 "Name": "name2"
             }), ("Key1", {
                 "Name": "name1"
             })])),
            ("A_Resource",
             OrderedDict([("Key2", {
                 "Name": "name2"
             }), ("Key1", {
                 "Name": "name1"
             })])),
        ])
        self.assertEqual(expected_dict, output_dict)

        output_template = yaml_dump(output_dict)
        self.assertEqual(input_template, output_template)
Exemplo n.º 6
0
    def _verify_resource_property(self, template_path, logical_id, property, expected_value):

        with open(template_path, "r") as fp:
            template_dict = yaml_parse(fp.read())
            self.assertEqual(
                expected_value, jmespath.search(f"Resources.{logical_id}.Properties.{property}", template_dict)
            )
Exemplo n.º 7
0
    def _verify_resource_property(self, template_path, logical_id, property,
                                  expected_value):

        with open(template_path, 'r') as fp:
            template_dict = yaml_parse(fp.read())
            self.assertEquals(
                expected_value,
                template_dict["Resources"][logical_id]["Properties"][property])
Exemplo n.º 8
0
    def run(self):

        # Parse parameters
        with open(self.template_file, "r") as handle:
            template_str = handle.read()

        template_dict = yaml_parse(template_str)

        if not isinstance(template_dict, dict):
            raise deploy_exceptions.DeployFailedError(
                stack_name=self.stack_name, msg="{} not in required format".format(self.template_file)
            )

        parameters = self.merge_parameters(template_dict, self.parameter_overrides)

        template_size = os.path.getsize(self.template_file)
        if template_size > 51200 and not self.s3_bucket:
            raise deploy_exceptions.DeployBucketRequiredError()
        boto_config = get_boto_config_with_user_agent()
        cloudformation_client = boto3.client(
            "cloudformation", region_name=self.region if self.region else None, config=boto_config
        )

        s3_client = None
        if self.s3_bucket:
            s3_client = boto3.client("s3", region_name=self.region if self.region else None, config=boto_config)

            self.s3_uploader = S3Uploader(
                s3_client, self.s3_bucket, self.s3_prefix, self.kms_key_id, self.force_upload, self.no_progressbar
            )

        self.deployer = Deployer(cloudformation_client)

        region = s3_client._client_config.region_name if s3_client else self.region  # pylint: disable=W0212
        print_deploy_args(
            self.stack_name,
            self.s3_bucket,
            self.image_repository,
            region,
            self.capabilities,
            self.parameter_overrides,
            self.confirm_changeset,
            self.signing_profiles,
        )
        return self.deploy(
            self.stack_name,
            template_str,
            parameters,
            self.capabilities,
            self.no_execute_changeset,
            self.role_arn,
            self.notification_arns,
            self.s3_uploader,
            [{"Key": key, "Value": value} for key, value in self.tags.items()] if self.tags else [],
            region,
            self.fail_on_empty_changeset,
            self.confirm_changeset,
        )
Exemplo n.º 9
0
 def test_yaml_merge_tag(self):
     test_yaml = """
     base: &base
         property: value
     test:
         <<: *base
     """
     output = yaml_parse(test_yaml)
     self.assertTrue(isinstance(output, OrderedDict))
     self.assertEqual(output.get("test").get("property"), "value")
Exemplo n.º 10
0
    def test_valid_api_request_model_template(self, template_path):
        with open(template_path) as f:
            template = yamlhelper.yaml_parse(f.read())
        managed_policy_mock = Mock()
        managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"}

        validator = SamTemplateValidator(template, managed_policy_mock)

        # Should not throw an exception
        validator.is_valid()
Exemplo n.º 11
0
    def _download_swagger(self, location):
        """
        Download the file from given local or remote location and return it

        Parameters
        ----------
        location : str or dict
            Local path or S3 path to Swagger file to download. Consult the ``__init__.py`` documentation for specifics
            on structure of this property.

        Returns
        -------
        dict or None
            Downloaded and parsed Swagger document. None, if unable to download
        """

        if not location:
            return

        bucket, key, version = self._parse_s3_location(location)
        if bucket and key:
            LOG.debug("Downloading Swagger document from Bucket=%s, Key=%s, Version=%s", bucket, key, version)
            swagger_str = self._download_from_s3(bucket, key, version)
            return yaml_parse(swagger_str)

        if not isinstance(location, string_types):
            # This is not a string and not a S3 Location dictionary. Probably something invalid
            LOG.debug("Unable to download Swagger file. Invalid location: %s", location)
            return

        # ``location`` is a string and not a S3 path. It is probably a local path. Let's resolve relative path if any
        filepath = location
        if self.working_dir:
            # Resolve relative paths, if any, with respect to working directory
            filepath = os.path.join(self.working_dir, location)

        if not os.path.exists(filepath):
            LOG.debug("Unable to download Swagger file. File not found at location %s", filepath)
            return

        LOG.debug("Reading Swagger document from local file at %s", filepath)
        with open(filepath, "r") as fp:
            return yaml_parse(fp.read())
Exemplo n.º 12
0
    def test_yaml_getatt(self):
        # This is an invalid syntax for !GetAtt. But make sure the code does not crash when we encouter this syntax
        # Let CloudFormation interpret this value at runtime
        input = """
        Resource:
            Key: !GetAtt ["a", "b"]
        """

        output = {"Resource": {"Key": {"Fn::GetAtt": ["a", "b"]}}}

        actual_output = yaml_parse(input)
        self.assertEquals(actual_output, output)
Exemplo n.º 13
0
 def test_warning_check(self, template_txt, expected_warning_msg, message_present):
     if template_txt:
         template_dict = yaml_parse(template_txt)
     else:
         template_dict = None
     current_warning_checker = TemplateWarningsChecker()
     actual_warning_msg = current_warning_checker.check_template_for_warning(
         CodeDeployWarning.__name__, template_dict
     )
     if not message_present:
         self.assertIsNone(actual_warning_msg)
     else:
         self.assertEqual(expected_warning_msg, actual_warning_msg)
Exemplo n.º 14
0
def get_template_data(template_file):
    """
    Read the template file, parse it as JSON/YAML and return the template as a dictionary.

    :param string template_file: Path to the template to read
    :return dict: Template data as a dictionary
    """

    if not pathlib.Path(template_file).exists():
        raise ValueError("Template file not found at {}".format(template_file))

    with open(template_file, 'r') as fp:
        try:
            return yaml_parse(fp.read())
        except (ValueError, yaml.YAMLError) as ex:
            raise ValueError("Failed to parse template: {}".format(str(ex)))
Exemplo n.º 15
0
def _read_sam_file(template):
    """
    Reads the file (json and yaml supported) provided and returns the dictionary representation of the file.

    :param str template: Path to the template file
    :return dict: Dictionary representing the SAM Template
    :raises: SamTemplateNotFoundException when the template file does not exist
    """
    if not os.path.exists(template):
        click.secho("SAM Template Not Found", bg='red')
        raise SamTemplateNotFoundException("Template at {} is not found".format(template))

    with click.open_file(template, 'r') as sam_template:
        sam_template = yaml_parse(sam_template.read())

    return sam_template
Exemplo n.º 16
0
def _read_sam_file(template):
    """
    Reads the file (json and yaml supported) provided and returns the dictionary representation of the file.

    :param str template: Path to the template file
    :return dict: Dictionary representing the SAM Template
    :raises: SamTemplateNotFoundException when the template file does not exist
    """
    if not os.path.exists(template):
        click.secho("SAM Template Not Found", bg='red')
        raise SamTemplateNotFoundException("Template at {} is not found".format(template))

    with click.open_file(template, 'r') as sam_template:
        sam_template = yaml_parse(sam_template.read())

    return sam_template
Exemplo n.º 17
0
    def _get_template_data(template_file):
        """
        Read the template file, parse it as JSON/YAML and return the template as a dictionary.

        :param string template_file: Path to the template to read
        :return dict: Template data as a dictionary
        :raises InvokeContextException: If template file was not found or the data was not a JSON/YAML
        """

        if not os.path.exists(template_file):
            raise InvokeContextException("Template file not found at {}".format(template_file))

        with open(template_file, 'r') as fp:
            try:
                return yaml_parse(fp.read())
            except (ValueError, yaml.YAMLError) as ex:
                raise InvokeContextException("Failed to parse template: {}".format(str(ex)))
Exemplo n.º 18
0
    def test_yaml_getatt(self):
        # This is an invalid syntax for !GetAtt. But make sure the code does not crash when we encouter this syntax
        # Let CloudFormation interpret this value at runtime
        input = """
        Resource:
            Key: !GetAtt ["a", "b"]
        """

        output = {
            "Resource": {
                "Key": {
                    "Fn::GetAtt": ["a", "b"]
                }
            }
        }

        actual_output = yaml_parse(input)
        self.assertEquals(actual_output, output)
Exemplo n.º 19
0
def _read_sam_file(template):
    """
    Reads the file (json and yaml supported) provided and returns the dictionary representation of the file.

    :param str template: Path to the template file
    :return dict: Dictionary representing the SAM Template
    :raises: SamTemplateNotFoundException when the template file does not exist
    """

    from samcli.commands.local.cli_common.user_exceptions import SamTemplateNotFoundException
    from samcli.yamlhelper import yaml_parse

    if not os.path.exists(template):
        click.secho("SAM Template Not Found", bg="red")
        raise SamTemplateNotFoundException("Template at {} is not found".format(template))

    with click.open_file(template, "r", encoding="utf-8") as sam_template:
        sam_template = yaml_parse(sam_template.read())

    return sam_template
Exemplo n.º 20
0
def get_template_data(template_file):
    """
    Read the template file, parse it as JSON/YAML and return the template as a dictionary.

    Parameters
    ----------
    template_file : string
        Path to the template to read

    Returns
    -------
    Template data as a dictionary
    """

    if not pathlib.Path(template_file).exists():
        raise TemplateNotFoundException("Template file not found at {}".format(template_file))

    with open(template_file, "r", encoding="utf-8") as fp:
        try:
            return yaml_parse(fp.read())
        except (ValueError, yaml.YAMLError) as ex:
            raise TemplateFailedParsingException("Failed to parse template: {}".format(str(ex))) from ex
Exemplo n.º 21
0
def get_template_data(template_file):
    """
    Read the template file, parse it as JSON/YAML and return the template as a dictionary.

    Parameters
    ----------
    template_file : string
        Path to the template to read

    Returns
    -------
    Template data as a dictionary
    """

    if not pathlib.Path(template_file).exists():
        raise ValueError("Template file not found at {}".format(template_file))

    with open(template_file, 'r') as fp:
        try:
            return yaml_parse(fp.read())
        except (ValueError, yaml.YAMLError) as ex:
            raise ValueError("Failed to parse template: {}".format(str(ex)))
Exemplo n.º 22
0
 def test_parse_json_preserve_elements_order(self):
     input_template = """
     {
         "B_Resource": {
             "Key2": {
                 "Name": "name2"
             },
             "Key1": {
                 "Name": "name1"
             }
         },
         "A_Resource": {
             "Key2": {
                 "Name": "name2"
             },
             "Key1": {
                 "Name": "name1"
             }
         }
     }
     """
     expected_dict = OrderedDict([
         ("B_Resource",
          OrderedDict([("Key2", {
              "Name": "name2"
          }), ("Key1", {
              "Name": "name1"
          })])),
         ("A_Resource",
          OrderedDict([("Key2", {
              "Name": "name2"
          }), ("Key1", {
              "Name": "name1"
          })])),
     ])
     output_dict = yaml_parse(input_template)
     self.assertEqual(expected_dict, output_dict)
Exemplo n.º 23
0
 def test_code_deploy_warning(self, template, expected):
     code_deploy_warning = CodeDeployWarning()
     (is_warning, message) = code_deploy_warning.check(yaml_parse(template))
     self.assertEqual(expected, is_warning)
Exemplo n.º 24
0
 def test_parse_json_with_tabs(self):
     template = '{\n\t"foo": "bar"\n}'
     output = yaml_parse(template)
     self.assertEqual(output, {'foo': 'bar'})
Exemplo n.º 25
0
 def test_warning_check_invalid_warning_name(self):
     template_dict = yaml_parse(ALL_ENABLED_TEMPLATE)
     current_warning_checker = TemplateWarningsChecker()
     actual_warning_msg = current_warning_checker.check_template_for_warning("SomeRandomName", template_dict)
     self.assertIsNone(actual_warning_msg)
Exemplo n.º 26
0
 def test_parse_json_with_tabs(self):
     template = '{\n\t"foo": "bar"\n}'
     output = yaml_parse(template)
     self.assertEqual(output, {'foo': 'bar'})
Exemplo n.º 27
0
    def _verify_resource_property(self, template_path, logical_id, property, expected_value):

        with open(template_path, 'r') as fp:
            template_dict = yaml_parse(fp.read())
            self.assertEquals(expected_value, template_dict["Resources"][logical_id]["Properties"][property])