<commit_before>/**
 * Copyright 2016 F5 Networks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

var q = require('q');
var IControl = require('icontrol');
var util = require('./util');

var icontrol;

var getIControl = function(host, user, password) {
    return new IControl({
        host: host,
        user: user,
        pass: password,
        basePath: '/mgmt',
        strict: false
    });
};

function BigIp(host, user, password) {
    this.host = host;
    this.user = user;
    icontrol = getIControl(host, this.user, password);
}

/**
 * Low-level interface
 */

/**
 * Submits a list (GET) request
 *
 * @param {String} path - the path to get
 * @param {Object} options - see iControl list options
 *
 * @returns {Promise} A promise which is resolved when the request is complete
 *                    or rejected if an error occurs.
 */
BigIp.prototype.list = function (path, options) {
    var deferred = q.defer();

    try {
        icontrol.list(path, options, function(err, response) {
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(response);
            }
        });
    }
    catch (err) {
        deferred.reject(err);
    }

    return deferred.promise;
};

/**
 * Submits a create (POST) request
 *
 * @param {String} path - the path to get
 * @param {Object} body - the body for the POST request
 * @param {Object} options - see iControl create options
 *
 * @returns {Promise} A promise which is resolved when the request is complete
 *                    or rejected if an error occurs.
 */
BigIp.prototype.create = function(path, body, options) {
    var deferred = q.defer();

    try {
        icontrol.create(path, body, options, function(err, response) {
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(response);
            }
        });
    }
    catch (err) {
        deferred.reject(err);
    }

    return deferred.promise;
};

/**
 * Submits a modify (PATCH) request
 *
 * @param {String} path - the path to get
 * @param {Object} body - the body for the patch request
 * @param {Object} options - see iControl modify options
 *
 * @returns {Promise} A promise which is resolved when the request is complete
 *                    or rejected if an error occurs.
 */
BigIp.prototype.modify = function(path, body, options) {
    var deferred = q.defer();

    try {
        icontrol.modify(path, body, options, function(err, response) {
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(response);
            }
        });
    }
    catch (err) {
        deferred.reject(err);
    }

    return deferred.promise;
};

/**
 * Submits a delete (DELETE) request
 *
 * @param {String} path - the path to get
 * @param {Object} options - see iControl delete options
 *
 * @returns {Promise} A promise which is resolved when the request is complete
 *                    or rejected if an error occurs.
 */
BigIp.prototype.delete = function(path, options) {
    var deferred = q.defer();

    try {
        icontrol.delete(path, options, function(err, response) {
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(response);
            }
        });
    }
    catch (err) {
        deferred.reject(err);
    }

    return deferred.promise;
};

/**
 * Higher level interface
 */

/**
 * Sets global settings
 *
 * If settings include host name, a CM move of host name is also performed.
 *
 * @param {Object} globalSettings - An object containing the global settings
 *
 * @returns {Promise} A promise which is resolved when the global settings
 *                    are set or rejected if an error occurs.
 */
BigIp.prototype.globalSettings = function(globalSettings) {
    var hostName;

    // if host name is set, pull it out and use the hostName method
    if (globalSettings.hostName) {
        hostName = globalSettings.hostName;
        delete globalSettings.hostName;
    }

    return this.modify('/tm/sys/global-settings', globalSettings)
        .then(function(response) {
            if (hostName) {
                return this.hostName(hostName);
            }
            else {
                return q(response);
            }
        }.bind(this));

};

/**
 * Sets the host name
 *
 * Sets the global settings host-name and also does a cm mv from
 * old host name to new.
 *
 * @param {String} hostName - The host name to set
 *
 * @returns {Promise} A promise which is resolved when the host name
 *                    is set or rejected if an error occurs.
 */
BigIp.prototype.hostName = function(hostName) {
    return this.list('/tm/cm/device')
        .then(function(response) {
            var oldName = response[0].name;
            var newName = hostName;
            var commands = [];

            if (oldName !== newName) {
                commands.push(
                    {
                        method: 'create',
                        path: '/tm/cm/device',
                        body: {
                            command: 'mv',<commit_msg>Remove initialConfig and add methods for hostname and global-settings<commit_after>/**
 * Copyright 2016 F5 Networks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

var q = require('q');
var IControl = require('icontrol');
var util = require('./util');

var icontrol;

var getIControl = function(host, user, password) {
    return new IControl({
        host: host,
        user: user,
        pass: password,
        basePath: '/mgmt',
        strict: false
    });
};

function BigIp(host, user, password) {
    this.host = host;
    this.user = user;
    icontrol = getIControl(host, this.user, password);
}

/**
 * Low-level interface
 */

/**
 * Submits a list (GET) request
 *
 * @param {String} path - the path to get
 * @param {Object} options - see iControl list options
 *
 * @returns {Promise} A promise which is resolved when the request is complete
 *                    or rejected if an error occurs.
 */
BigIp.prototype.list = function (path, options) {
    var deferred = q.defer();

    try {
        icontrol.list(path, options, function(err, response) {
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(response);
            }
        });
    }
    catch (err) {
        deferred.reject(err);
    }

    return deferred.promise;
};

/**
 * Submits a create (POST) request
 *
 * @param {String} path - the path to get
 * @param {Object} body - the body for the POST request
 * @param {Object} options - see iControl create options
 *
 * @returns {Promise} A promise which is resolved when the request is complete
 *                    or rejected if an error occurs.
 */
BigIp.prototype.create = function(path, body, options) {
    var deferred = q.defer();

    try {
        icontrol.create(path, body, options, function(err, response) {
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(response);
            }
        });
    }
    catch (err) {
        deferred.reject(err);
    }

    return deferred.promise;
};

/**
 * Submits a modify (PATCH) request
 *
 * @param {String} path - the path to get
 * @param {Object} body - the body for the patch request
 * @param {Object} options - see iControl modify options
 *
 * @returns {Promise} A promise which is resolved when the request is complete
 *                    or rejected if an error occurs.
 */
BigIp.prototype.modify = function(path, body, options) {
    var deferred = q.defer();

    try {
        icontrol.modify(path, body, options, function(err, response) {
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(response);
            }
        });
    }
    catch (err) {
        deferred.reject(err);
    }

    return deferred.promise;
};

/**
 * Submits a delete (DELETE) request
 *
 * @param {String} path - the path to get
 * @param {Object} options - see iControl delete options
 *
 * @returns {Promise} A promise which is resolved when the request is complete
 *                    or rejected if an error occurs.
 */
BigIp.prototype.delete = function(path, options) {
    var deferred = q.defer();

    try {
        icontrol.delete(path, options, function(err, response) {
            if (err) {
                deferred.reject(err);
            }
            else {
                deferred.resolve(response);
            }
        });
    }
    catch (err) {
        deferred.reject(err);
    }

    return deferred.promise;
};

/**
 * Higher level interface
 */

/**
 * Sets global settings
 *
 * If settings include host name, a CM move of host name is also performed.
 *
 * @param {Object} globalSettings - An object containing the global settings
 *
 * @returns {Promise} A promise which is resolved when the global settings
 *                    are set or rejected if an error occurs.
 */
BigIp.prototype.globalSettings = function(globalSettings) {
    var hostName;

    // if host name is set, pull it out and use the hostName method
    if (globalSettings.hostName) {
        hostName = globalSettings.hostName;
        delete globalSettings.hostName;
    }

    return this.modify('/tm/sys/global-settings', globalSettings)
        .then(function() {
            if (hostName) {
                return this.hostName(hostName);
            }
            else {
                return q();
            }
        }.bind(this));

};

/**
 * Sets the host name
 *
 * Sets the global settings host-name and also does a cm mv from
 * old host name to new.
 *
 * @param {String} hostName - The host name to set
 *
 * @returns {Promise} A promise which is resolved when the host name
 *                    is set or rejected if an error occurs.
 */
BigIp.prototype.hostName = function(hostName) {
    return this.list('/tm/cm/device')
        .then(function(response) {
            var oldName = response[0].name;
            var newName = hostName;
            var commands = [];

            if (oldName !== newName) {
                commands.push(
                    {
                        method: 'create',
                        path: '/tm/cm/device',
                        body: {
                            command: 'mv',
{'fraction_non_alphanumeric': 0.10537690179806362, 'fraction_numerical': 0.0017289073305670815, 'mean_word_length': 1.9573108384458078, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2426647', 'n_tokens_mistral': 3215, 'n_tokens_neox': 3094, 'n_words': 1085}