Create a stack (heat stack-create)

    name
        Name of the new stack

    template_file
        File of template

    environment
        File of environment

    parameters
        Parameter dict used to create the stack

    poll
        Poll and report events until stack complete

    rollback
        Enable rollback on create failure

    timeout
        Stack creation timeout in minutes

    profile
        Profile to build on

    CLI Example:

    .. code-block:: bash

        salt '*' heat.create_stack name=mystack \\
                 template_file=salt://template.yaml \\
                 environment=salt://environment.yaml \\
                 parameters="{"image": "Debian 8", "flavor": "m1.small"}" \\
                 poll=5 rollback=False timeout=60 profile=openstack1

    .. versionadded:: 2017.7.5,2018.3.1

        The spelling mistake in parameter `enviroment` was corrected to `environment`.
        The misspelled version is still supported for backward compatibility, but will
        be removed in Salt Neon.

def create_stack(name=None, template_file=None, environment=None,
                 parameters=None, poll=0, rollback=False, timeout=60,
                 profile=None, enviroment=None):
    '''
    Create a stack (heat stack-create)

    name
        Name of the new stack

    template_file
        File of template

    environment
        File of environment

    parameters
        Parameter dict used to create the stack

    poll
        Poll and report events until stack complete

    rollback
        Enable rollback on create failure

    timeout
        Stack creation timeout in minutes

    profile
        Profile to build on

    CLI Example:

    .. code-block:: bash

        salt '*' heat.create_stack name=mystack \\
                 template_file=salt://template.yaml \\
                 environment=salt://environment.yaml \\
                 parameters="{"image": "Debian 8", "flavor": "m1.small"}" \\
                 poll=5 rollback=False timeout=60 profile=openstack1

    .. versionadded:: 2017.7.5,2018.3.1

        The spelling mistake in parameter `enviroment` was corrected to `environment`.
        The misspelled version is still supported for backward compatibility, but will
        be removed in Salt Neon.

    '''
    if environment is None and enviroment is not None:
        salt.utils.versions.warn_until('Neon', (
            "Please use the 'environment' parameter instead of the misspelled 'enviroment' "
            "parameter which will be removed in Salt Neon."
        ))
        environment = enviroment
    h_client = _auth(profile)
    ret = {
        'result': True,
        'comment': ''
    }
    if not parameters:
        parameters = {}
    if template_file:
        template_tmp_file = salt.utils.files.mkstemp()
        tsfn, source_sum, comment_ = __salt__['file.get_managed'](
            name=template_tmp_file,
            template=None,
            source=template_file,
            source_hash=None,
            user=None,
            group=None,
            mode=None,
            saltenv='base',
            context=None,
            defaults=None,
            skip_verify=False,
            kwargs=None)

        template_manage_result = __salt__['file.manage_file'](
            name=template_tmp_file,
            sfn=tsfn,
            ret=None,
            source=template_file,
            source_sum=source_sum,
            user=None,
            group=None,
            mode=None,
            saltenv='base',
            backup=None,
            makedirs=True,
            template=None,
            show_changes=False,
            contents=None,
            dir_mode=None)
        if template_manage_result['result']:
            with salt.utils.files.fopen(template_tmp_file, 'r') as tfp_:
                tpl = salt.utils.stringutils.to_unicode(tfp_.read())
                salt.utils.files.safe_rm(template_tmp_file)
                try:
                    template = _parse_template(tpl)
                except ValueError as ex:
                    ret['result'] = False
                    ret['comment'] = 'Error parsing template {0}'.format(ex)
        else:
            ret['result'] = False
            ret['comment'] = 'Can not open template: {0} {1}'.format(template_file, comment_)
    else:
        ret['result'] = False
        ret['comment'] = 'Can not open template'
    if ret['result'] is False:
        return ret

    kwargs = {}
    kwargs['template'] = template
    try:
        h_client.stacks.validate(**kwargs)
    except Exception as ex:
        log.exception('Template not valid')
        ret['result'] = False
        ret['comment'] = 'Template not valid: {0}'.format(ex)
        return ret
    env = {}
    if environment:
        environment_tmp_file = salt.utils.files.mkstemp()
        esfn, source_sum, comment_ = __salt__['file.get_managed'](
            name=environment_tmp_file,
            template=None,
            source=environment,
            source_hash=None,
            user=None,
            group=None,
            mode=None,
            saltenv='base',
            context=None,
            defaults=None,
            skip_verify=False,
            kwargs=None)

        environment_manage_result = __salt__['file.manage_file'](
            name=environment_tmp_file,
            sfn=esfn,
            ret=None,
            source=environment,
            source_sum=source_sum,
            user=None,
            group=None,
            mode=None,
            saltenv='base',
            backup=None,
            makedirs=True,
            template=None,
            show_changes=False,
            contents=None,
            dir_mode=None)
        if environment_manage_result['result']:
            with salt.utils.files.fopen(environment_tmp_file, 'r') as efp_:
                env_str = salt.utils.stringutils.to_unicode(efp_.read())
                salt.utils.files.safe_rm(environment_tmp_file)
                try:
                    env = _parse_environment(env_str)
                except ValueError as ex:
                    ret['result'] = False
                    ret['comment'] = 'Error parsing template {0}'.format(ex)
        else:
            ret['result'] = False
            ret['comment'] = 'Can not open environment: {0}, {1}'.format(environment, comment_)
    if ret['result'] is False:
        return ret

    fields = {
        'stack_name': name,
        'disable_rollback': not rollback,
        'parameters': parameters,
        'template': template,
        'environment': env,
        'timeout_mins': timeout
    }

    # If one or more environments is found, pass the listing to the server
    try:
        h_client.stacks.create(**fields)
    except Exception as ex:  # pylint: disable=W0703
        log.exception('Create failed')
        ret['result'] = False
        ret['comment'] = six.text_type(ex)
        return ret
    if poll > 0:
        stack_status, msg = _poll_for_events(h_client, name, action='CREATE',
                                             poll_period=poll, timeout=timeout)
        if stack_status == 'CREATE_FAILED':
            ret['result'] = False
            ret['comment'] = 'Created stack FAILED\'{0}\'{1}.'.format(name, msg)
    if ret['result'] is True:
        ret['comment'] = 'Created stack \'{0}\'.'.format(name)
    return ret