const db = require('qmi-cloud-common/mongo');
const tf = require("./docker/tf");

module.exports = async function(job) {
        
    var prov = await db.provision.getById(job.data.provId);

    if ( !prov ) {
        console.log(`ProcessorStopContainer# Error: Not found Provision object in Database (it should exist!), provisionId is: ${job.data.id}` );
        return Promise.reject({"success": false, "err": "Not found Provision object in Worker"});
    }

    // TERRAFORM INIT
    return tf.stop(prov)
    .then( function(res) {
        return Promise.resolve( { "success": true, "output": res });
    } ).catch( function(err) {
        console.log("ProcessorStopContainer# Error:", err);
        return Promise.reject({"success": false, "error": err});
    } );
}