/**
 * Load targets init8js module.
 * @module loadtargets
 */

var os = require("os");
var fs = require("fs");
var promise = require("deferred");
var yaml = require('yamljs');
var path = require('path');

var vccutil = require("./vccutil.js");
var logger = require("./log.js");


// This is an init8js module, it runs in-process with the init in order to
// register our cluster targets, so it must follow the service module pattern


module.exports = {
    /**
     * The path to search for the services-*.yml file
     * @type {String}
     */
    servicePath: '/etc/vcc',

    /**
     * Load the targets from the services-*.yml file into the service manager
     * @param {Object} service - The service object (provided by service manager)
     * @param {Object} config - The configuration object (provided by service manager)
     * @param {Object} targets - The targets object (provided by service manager)
     * @param {Function} f_register_services - The function exposed by the service manager to register targets
     * @returns {Promise}
     */
    LoadTargets: function (service, config, targets, f_register_services) {
        var deferred = promise();
        var servicefile = path.join(module.exports.servicePath, "services-"+config.service+".yml");
        // make sure service targets definition file exists
        fs.stat(servicefile, function(err, stat) {
            if(err) {
                if(err.code == 'ENOENT') {
                    // no service file
                    logger.error('There is no service definition for '+config.service);
                    logger.error('Please create '+servicefile);
                } else {
                    // something went wrong
                    logger.error('unhandled error hook stat', servicefile);
                }
                deferred.reject(err);
            } else {
                // parse the yaml file and register the targets
                logger.debug("Registering service provider targets for "+config.service);
                f_register_services(yaml.load(servicefile));
                deferred.resolve();
            }
        });
        return deferred.promise();
    }
}
@@ -1241,24 +1241,32 @@
 es-%22+config.
+cluster.
 service+%22.ym
@@ -1555,32 +1555,40 @@
 on for '+config.
+cluster.
 service);%0A      
@@ -2011,16 +2011,24 @@
 +config.
+cluster.
 service)

fix loadtargets config is full config, not cluster.config