def build_openstack_heat(base_config, posted_json, archive=False):
    defaults = load_defaults()

    if not openstack_utils.verify_data(posted_json):
        raise RequiredParametersError("Not all required keys for openstack are present")

    # create the openstack config object that will be used to populate the HEAT template
    openstack_config = openstack_utils.generate_config(defaults, posted_json)
    if archive:
        # the rendered heat template should reference local files that will be included in the archive
        openstack_config['init_cfg'] = 'init-cfg-static.txt'
        openstack_config['bootstrap_xml'] = 'bootstrap.xml'
        openstack_config['authcodes'] = 'authcodes'
    else:
        openstack_config['init_cfg'] = base_config['init-cfg-static.txt']['url']
        openstack_config['bootstrap_xml'] = base_config['bootstrap.xml']['url']
        openstack_config['authcodes'] = base_config['authcodes']['url']

    heat_env = render_template('openstack/heat-environment.yaml', **openstack_config)
    heat = render_template('openstack/heat.yaml', **base_config)

    he_key = cache_utils.set(heat_env)
    h_key = cache_utils.set(heat)

    base_config['heat-environment.yaml'] = dict()
    base_config['heat-environment.yaml']['key'] = he_key
    base_config['heat-environment.yaml']['archive_path'] = '.'

    base_config['heat-template.yaml'] = dict()
    base_config['heat-template.yaml']['key'] = h_key
    base_config['heat-template.yaml']['archive_path'] = '.'

    return base_config
def create_init_cfg(configuration_parameters):
    if 'init_cfg_str' in configuration_parameters:
        # user has supplied a base64 encoded init-cfg.txt file for our use
        init_cfg_str = configuration_parameters['init_cfg_str']
        init_cfg_bytes = urlsafe_b64decode(init_cfg_str)
        init_cfg_contents = init_cfg_bytes.decode('utf-8')

    else:
        if 'init_cfg_template' in configuration_parameters:
            # the user has supplied an init_cfg_template to use, now verify we have all the required variables
            init_cfg_name = configuration_parameters['init_cfg_template']
            # handled pre 1.0 payloads with incorrect default init-cfg template names
            if init_cfg_name == 'Default Init-Cfg Static':
                init_cfg_name = 'Default Init-Cfg'

            print(init_cfg_name)
            init_cfg_template = get_template(init_cfg_name)
            # print(init_cfg_template)
            if init_cfg_template is None:
                init_cfg_template = get_template('Default Init-Cfg')
        else:
            init_cfg_name = 'Default Init-Cfg'
            init_cfg_template = get_template(init_cfg_name)

        if init_cfg_template is None:
            # print('init-cfg-template template was None')
            raise TemplateNotFoundError('Could not load %s' % init_cfg_name)

        # dhcp by default if nothing specified
        if 'dhcp_or_static' not in configuration_parameters:
            if 'ip_address' in configuration_parameters and configuration_parameters[
                    'ip_address'] != '':
                configuration_parameters['dhcp_or_static'] = 'static'
            else:
                configuration_parameters['dhcp_or_static'] = 'dhcp-client'

        print('getting required_keys for init-cfg.txt')
        # if user specifies a type instead of our template specific variable name, let's help them out here
        if 'type' in configuration_parameters:
            configuration_parameters[
                'dhcp_or_static'] = configuration_parameters['type']

        # create a set of vars that must be present in the request
        init_cfg_vars = {'hostname', 'dhcp_or_static'}

        if not init_cfg_vars.issubset(configuration_parameters):
            print("Not all required keys are present for build_base_config!!")
            raise RequiredParametersError(
                "Not all required keys are present for init-cfg.txt!!")

        init_cfg_contents = render_template_string(init_cfg_template,
                                                   **configuration_parameters)

    print(init_cfg_contents)
    init_cfg_key = cache_utils.set(init_cfg_contents)

    return init_cfg_key
def create_bootstrap_xml(configuration_parameters):

    if 'bootstrap_str' in configuration_parameters \
            and configuration_parameters['bootstrap_str'] != 'None' \
            and configuration_parameters['bootstrap_str'] != '':
        # user has supplied a base64 encoded init-cfg.txt file for our use
        bootstrap_str = configuration_parameters['bootstrap_str']
        bootstrap_bytes = urlsafe_b64decode(bootstrap_str)
        bootstrap_contents = bootstrap_bytes.decode('utf-8')
        return cache_utils.set(bootstrap_contents)

    elif 'bootstrap_template' in configuration_parameters \
            and configuration_parameters['bootstrap_template'] != 'None' \
            and configuration_parameters['bootstrap_template'] != '':
        print('loading bootstrap_template')
        defaults = load_defaults()
        # print('Using a bootstrap_template here')
        # print(configuration_parameters['bootstrap_template'])
        bootstrap_template_name = configuration_parameters[
            'bootstrap_template']
        # print(bootstrap_template_name)
        bootstrap_config = generate_boostrap_config_with_defaults(
            defaults, configuration_parameters)

        bootstrap_template = get_template(bootstrap_template_name)
        if bootstrap_template is None:
            raise TemplateNotFoundError('Could not load bootstrap template!')

        # print("checking bootstrap required_variables")
        if not verify_data(bootstrap_template, bootstrap_config):
            raise RequiredParametersError(
                'Not all required keys for bootstrap.xml are present')

        bootstrap_contents = render_template_string(bootstrap_template,
                                                    **bootstrap_config)
        # set the bootstrap_xml file in the cache and return the key
        return cache_utils.set(bootstrap_contents)
    else:
        return None
def build_base_configs(configuration_parameters):
    """
    Takes a dict of parameters and builds the base configurations
    :param configuration_parameters:  Simple dict of parameters
    :return: dict containing 'bootstrap.xml', 'authcodes', and 'init-cfg-static.txt' keys
    """

    print('here we go')
    config = load_config()
    # first create an init-cfg.txt from the supplied parameters
    init_cfg_key = create_init_cfg(configuration_parameters)

    base_config = dict()
    base_config['init-cfg.txt'] = dict()
    base_config['init-cfg.txt']['key'] = init_cfg_key
    base_config['init-cfg.txt']['archive_path'] = 'config'
    base_config['init-cfg.txt'][
        'url'] = config["base_url"] + '/get/' + init_cfg_key

    # use a consistent variable name for authcodes, but keep the old auth_key for backwards compat
    if 'auth_code' in configuration_parameters:
        configuration_parameters['auth_key'] = configuration_parameters[
            'auth_code']

    if 'authcode' in configuration_parameters:
        configuration_parameters['auth_key'] = configuration_parameters[
            'authcode']

    if 'authcodes' in configuration_parameters:
        configuration_parameters['auth_key'] = configuration_parameters[
            'authcodes']

    if 'auth_key' in configuration_parameters:
        authcode = render_template('panos/authcodes',
                                   **configuration_parameters)
        authcode_key = cache_utils.set(authcode)
        base_config['authcodes'] = dict()
        base_config['authcodes']['key'] = authcode_key
        base_config['authcodes']['archive_path'] = 'license'
        base_config['authcodes'][
            'url'] = config["base_url"] + '/get/' + init_cfg_key

    bootstrap_cache_key = create_bootstrap_xml(configuration_parameters)
    if bootstrap_cache_key is not None:
        base_config['bootstrap.xml'] = dict()
        base_config['bootstrap.xml']['key'] = bootstrap_cache_key
        base_config['bootstrap.xml']['archive_path'] = 'config'
        base_config['bootstrap.xml'][
            'url'] = config["base_url"] + '/get/' + bootstrap_cache_key

    return base_config
Example #5
0
def set_object():
    """
    Adds an serializable object to the cache
    :return: json encoded string with dict containing key and success keys
    """
    posted_json = request.get_json(force=True)
    contents = posted_json.get('contents', None)
    if contents is None:
        r = jsonify(message="Not all required keys are present", success=False, status_code=400)
        r.status_code = 400
        return r

    key = cache_utils.set(contents)
    return jsonify(key=key, success=True)
def build_base_configs(configuration_parameters):
    """
    Takes a dict of parameters and builds the base configurations
    :param configuration_parameters:  Simple dict of parameters
    :return: dict containing 'bootstrap.xml', 'authcodes', and 'init-cfg-static.txt' keys
    """

    config = load_config()
    defaults = load_defaults()
    print('WTF')
    # first check for a custom init-cfg file passed in as a parameter
    if 'init_cfg_template' in configuration_parameters:
        print('found a valid init_cfg_template')
        init_cfg_name = configuration_parameters['init_cfg_template']
        init_cfg_template = get_template(init_cfg_name)
        print(init_cfg_template)
        if init_cfg_template is None:
            init_cfg_template = get_template(config.get('default_init_cfg'), 'init-cfg-static.txt')
    else:
        print('using default init-cfg')
        init_cfg_name = config.get('default_init_cfg', 'init-cfg-static.txt')
        init_cfg_template = get_template(init_cfg_name)

    if init_cfg_template is None:
        print('init-cfg-template template was None')
        raise TemplateNotFoundError('Could not load %s' % init_cfg_name)

    print('getting required_keys')
    common_required_keys = get_required_vars_from_template(init_cfg_name)

    if not common_required_keys.issubset(configuration_parameters):
        print("Not all required keys are present for build_base_config!!")
        raise RequiredParametersError("Not all required keys are present for build_base_config!!")

    init_cfg_contents = render_template_string(init_cfg_template, **configuration_parameters)
    init_cfg_key = cache_utils.set(init_cfg_contents)

    base_config = dict()
    base_config['init-cfg.txt'] = dict()
    base_config['init-cfg.txt']['key'] = init_cfg_key
    base_config['init-cfg.txt']['archive_path'] = 'config'
    base_config['init-cfg.txt']['url'] = config["base_url"] + '/get/' + init_cfg_key

    if 'auth_key' in configuration_parameters:
        authcode = render_template('panos/authcodes', **configuration_parameters)
        authcode_key = cache_utils.set(authcode)
        base_config['authcodes'] = dict()
        base_config['authcodes']['key'] = authcode_key
        base_config['authcodes']['archive_path'] = 'license'
        base_config['authcodes']['url'] = config["base_url"] + '/get/' + init_cfg_key

    if 'bootstrap_template' in configuration_parameters and configuration_parameters['bootstrap_template'] != 'None':
        print('Using a bootstrap_template here')
        print(configuration_parameters['bootstrap_template'])
        bootstrap_template_name = configuration_parameters['bootstrap_template']
        print(bootstrap_template_name)
        bootstrap_config = generate_boostrap_config_with_defaults(defaults, configuration_parameters)

        bootstrap_template = get_template(bootstrap_template_name)
        if bootstrap_template is None:
            raise TemplateNotFoundError('Could not load bootstrap template!')

        print("checking bootstrap required_variables")
        if not verify_data(bootstrap_template, bootstrap_config):
            raise RequiredParametersError('Not all required keys for bootstrap.xml are present')

        bootstrap_xml = render_template_string(bootstrap_template, **bootstrap_config)
        bs_key = cache_utils.set(bootstrap_xml)

        base_config['bootstrap.xml'] = dict()
        base_config['bootstrap.xml']['key'] = bs_key
        base_config['bootstrap.xml']['archive_path'] = 'config'
        base_config['bootstrap.xml']['url'] = config["base_url"] + '/get/' + bs_key

    return base_config