def normalize_application_tags(app_original, app_updated):
    """ Simple function to normalize application tags when application is created or updated.
        It aims to ensure that required tags are always well defined.

        :param  app_original  string: The ghost "app" object before modification.
        :param  app_updated   string: The ghost "app" object with the new modifications.
        :return list  A list of dict. Each dict define a tag

        Test with only the default tag Name

        >>> from copy import deepcopy
        >>> from pprint import pprint
        >>> app_original = {'_id': 1111, 'env': 'prod', 'name': 'app1', 'role': 'webfront', 'modules': [{'name': 'mod1', 'git_repo': 'git@github.com/test/mod1'}, {'name': 'mod2', 'git_repo': 'git@github.com/test/mod2'}], 'environment_infos': {'instance_tags':[]}}
        >>> app_updated = deepcopy(app_original)
        >>> pprint(sorted(normalize_application_tags(app_original, app_updated), key=lambda d: d['tag_name']))
        [{'tag_name': 'Name', 'tag_value': 'ec2.prod.webfront.app1'}]

        Test with a custom Tag Name

        >>> app_original = {'_id': 1111, 'env': 'prod', 'name': 'app1', 'role': 'webfront', 'modules': [{'name': 'mod1', 'git_repo': 'git@github.com/test/mod1'}, {'name': 'mod2', 'git_repo': 'git@github.com/test/mod2'}], 'environment_infos': {'instance_tags':[]}}
        >>> app_updated = deepcopy(app_original)
        >>> app_updated['environment_infos']['instance_tags'] = [{'tag_name': 'Name', 'tag_value': 'Prod.Server1'}]
        >>> pprint(sorted(normalize_application_tags(app_original, app_updated), key=lambda d: d['tag_name']))
        [{'tag_name': 'Name', 'tag_value': 'Prod.Server1'}]

        Test with a custom Tag Name build with variables

        >>> app_original = {'_id': 1111, 'env': 'prod', 'name': 'app1', 'role': 'webfront', 'modules': [{'name': 'mod1', 'git_repo': 'git@github.com/test/mod1'}, {'name': 'mod2', 'git_repo': 'git@github.com/test/mod2'}], 'environment_infos': {'instance_tags':[{'tag_name': 'Name', 'tag_value': 'Prod.Server1'}]}}
        >>> pprint(sorted(normalize_application_tags(app_original, app_updated), key=lambda d: d['tag_name']))
        [{'tag_name': 'Name', 'tag_value': 'Prod.Server1'}]

        Test with a custom tag

        >>> app_original = {'_id': 1111, 'env': 'prod', 'name': 'app1', 'role': 'webfront', 'modules': [{'name': 'mod1', 'git_repo': 'git@github.com/test/mod1'}, {'name': 'mod2', 'git_repo': 'git@github.com/test/mod2'}], 'environment_infos': {'instance_tags':[]}}
        >>> app_updated = {'_id': 1111, 'env': 'prod', 'name': 'app1', 'role': 'webfront', 'modules': [{'name': 'mod1', 'git_repo': 'git@github.com/test/mod1'}, {'name': 'mod2', 'git_repo': 'git@github.com/test/mod2'}], 'environment_infos': {'instance_tags':[{'tag_name': 'billing', 'tag_value': 'account1'}]}}
        >>> pprint(sorted(normalize_application_tags(app_original, app_updated), key=lambda d: d['tag_name']))
        [{'tag_name': 'Name', 'tag_value': 'ec2.prod.webfront.app1'},
         {'tag_name': 'billing', 'tag_value': 'account1'}]

        Test with a custom tag updated

        >>> app_original = {'_id': 1111, 'env': 'prod', 'name': 'app1', 'role': 'webfront', 'modules': [{'name': 'mod1', 'git_repo': 'git@github.com/test/mod1'}, {'name': 'mod2', 'git_repo': 'git@github.com/test/mod2'}], 'environment_infos': {'instance_tags':[{'tag_name': 'billing', 'tag_value': 'account1'}]}}
        >>> app_updated = {'_id': 1111, 'env': 'prod', 'name': 'app1', 'role': 'webfront', 'modules': [{'name': 'mod1', 'git_repo': 'git@github.com/test/mod1'}, {'name': 'mod2', 'git_repo': 'git@github.com/test/mod2'}], 'environment_infos': {'instance_tags':[{'tag_name': 'billing', 'tag_value': 'account2'}]}}
        >>> pprint(sorted(normalize_application_tags(app_original, app_updated), key=lambda d: d['tag_name']))
        [{'tag_name': 'Name', 'tag_value': 'ec2.prod.webfront.app1'},
         {'tag_name': 'billing', 'tag_value': 'account2'}]

    """
    app_tags = []
    reserved_ghost_tags = ['app', 'app_id', 'env', 'role', 'color']
    default_tag_name_value = "ec2.{env}.{role}.{app}".format(env=app_original['env'], role=app_original['role'],
                                                             app=app_original['name'])

    custom_tags = (app_updated['environment_infos']['instance_tags']
                   if 'instance_tags' in app_updated['environment_infos'] else [])

    if 'Name' not in [i['tag_name'] for i in custom_tags]:
        app_tags.append({'tag_name': 'Name', 'tag_value': default_tag_name_value})

    for tag in custom_tags:
        if tag['tag_name'] not in reserved_ghost_tags:
            app_tags.append({'tag_name': tag['tag_name'], 'tag_value': tag['tag_value']})
    return app_tags