const fetch = require('node-fetch');
const Promise = require('promise');
const config = require('../config');
const log = require('../util/log');

function createDroplet(i) {
    log(`Creating droplet lamp${i}`);

    return fetch('https://api.digitalocean.com/v2/droplets', {
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${config.API_TOKEN}`,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            name: `lamp${i}`,
            region: 'fra1',
            size: '512mb',
            image: 20947873,
            ssh_keys: [4743654],
            backups: false,
            ipv6: false,
            private_networking: true,
            user_data: null,
            volume: [],
            tags: ['lamp']
        })
    }).then(response => response.json())
        .then(json => json.droplet)
        .then(droplet => {
            return waitTillItsAlive(droplet);
        });
}

function waitTillItsAlive(droplet) {
    return fetch('https://api.digitalocean.com/v2/droplets/' + droplet.id, {
        headers: {
            'Authorization': `Bearer ${config.API_TOKEN}`
        }
    })
        .then(response => response.json())
        .then(json => json.droplet)
        .then(droplet => {
            if (droplet.status !== 'active' || droplet.networks.v4.length === 0) {
                return new Promise((resolve) => {
                    setTimeout(() => {
                        waitTillItsAlive(droplet).then(() => resolve(droplet));
                    }, 5000);
                });
            }
            log(`${droplet.name} is booted up`);
            console.log(droplet);
            return droplet;
        });
}

module.exports = createDroplet;

@@ -954,49 +954,145 @@
   %7D)
-;%0A%7D%0A%0Afunction waitTillItsAlive(droplet) %7B
+%0A        .catch((err) =%3E console.log(err));%0A%7D%0A%0Afunction waitTillItsAlive(droplet) %7B%0A    log(%60Waiting for $%7Bdroplet.name%7D to boot up...%60);
 %0A   

improve logging