PaaS: Managing Your Cloud Infrastructure through RESTful API in Node.js

Some time ago when somebody needs to distribute their own site on the web,

they needed to go purchase costly server trusting that presentation will be sufficient for

developing client base or purchase much increasingly costly server that they were not going to

use at its full limit. In any case, this is just a 20% of the work, at that point they needed to contact an

ISP and purchase an open IP for the server and take care of the month to month web tab and power bill and

afterward ensure the server is running 24×7.

ing to use at its full limit. In any case, this is just a 20% of the work, at that point they

needed to contact an ISP and purchase an open IP for the server and take care of the

month to month web tab and power bill and afterwards ensure the server is running 24×7.

Be that as it may, today we have a facilitating administration to ensure each and every

thing is dealt with for us at a sensible cost. Regardless of whether you are a blogger,

business person, affiliate or an engineer you simply need to take it easy. Regardless of

whether you are growing 1 million clients for each day or having a tough situation, you just need to pay for what you have utilized.

For instance, envision that you have constructed a rocket to go to blemishes and you are building a portable

application to sell tickets. You distributed your application today and when you get up tomorrow

there are a large number of individuals who need to purchase a ticket, however, your application is broken since your VPS can’t deal with the

heap or you are a VPS affiliate who has a site and physically requesting servers from different stages.

Be that as it may, imagine a scenario in which, you can mechanize this

while never being needed to sign in to your dashboard again. In this article, we are going

to perceive how to create it utilizing NodeJS. For this article, I will utilize Veesp’s API to do that.

Further insights regarding this API can be found in the official documentation at the following connection

So how about we begin!

Introduce the npm Request Module

So as to play out any sort of HTTP demands, you need an HTTP customer library.

You are allowed to utilize your own NodeJS HTTP customer. For this article, we are going to utilize npm demand module.

So first how about we begin by introducing the solicitation module utilizing npm,

npm i --save request

At that point make a record called app.js and import the solicitation module.

const request = require('request');

Requesting a VPS

Veesp gives an assortment of administration classifications including VPS facilitating, area names,

DNS facilitating and scarcely any different administrations. Additionally, VPS facilitating is isolated

into a couple of types, Linux SSD VPS, Linux HDD VPS, Windows VPS and devoted servers.

Veesp API utilizes HTTP fundamental client confirmation for verifying clients.

You ought to send your username and secret phrase with the solicitation headers so as to utilize endpoints.

We will forget about this module import and username secret key verification after this model.

However, you ought to send these confirmation subtleties in each solicitation as we referenced previously,

So here is the code to get a rundown of administration classes from the API.

const request = require('request');
 
const username = 'your@gmail.com';
const password = 'yourpassword';
 
const url = 'https://secure.veesp.com/api/category';
 
const auth = new Buffer.from(username + ':' + password).toString('base64');
 
const req = {
    url: url,
    method: 'GET',
    headers: {
        'Authorization': 'Basic ' + auth,
        'Content-Type': 'application/json'
    }
};
 
request(req, (err, res) => {
    const body = JSON.parse(res.body);
    console.log(body);
});

At the point when you need to execute the code you can open your terminal and run hub app.js.

Your comfort yield should look something like this,

{ categories:
   [ { id: '18', name: 'Linux SSD VPS', description: '', slug: 'vps' },
     { id: '19',
       name: 'Linux HDD VPS',
       description: '',There is also requests and reonses
       slug: 'hdd-vps' },
     { id: '17',
       name: 'Windows VPS',
       description: '',
       slug: 'windows-vps' },
     …

So we have a rundown of classifications and pertinent ID for every classification.

For this model, we are going to arrange another Linux SSD VPS where the ID is 18 as should be obvious in the reaction.

At that point, we can utilize this class ID to get a rundown of items under this classification as underneath.

const url = 'https://secure.veesp.com/api/category/18/product';
 
const req = {
    url: url,
    method: 'GET',
    headers: {
        'Authorization': 'Basic ' + auth,
        'Content-Type': 'application/json'
    }
};
 
request(req, (err, res) => {
    const body = JSON.parse(res.body);
    console.log(body);
});

In the wake of executing this code, your support yield should look something like this,

{ products:
   [ { id: '212',
       type: '11',
       name: 'SSD Sandbox',
       stock: false,
       paytype: 'Regular',
       description:
        'CPU:1 vCore<br>RAM:512MB<br>SSD:10GB<br>Traffic:100GB<br>Bandwidth:200Mbps<br><ul></ul>',
       qty: -603,
       tags: [],
       periods: [Array] },
     { id: '236',
       type: '11',
       name: 'SSD 1',
       stock: false,
       paytype: 'Regular',
       description:
        'CPU:1 vCore<br>RAM:1GB<br>SSD:25GB<br>Traffic:Unlimited<br>Bandwidth:200Mbps<br><ul></ul>',       qty: -436,
       tags: [],
       periods: [Array] },
…

Presently we can arrange the item. Pass your hostname as a parameter ‘space’ in the solicitation body.

You can choose the ID of the item you need to arrange, for this model we are going to arrange 512 Mb Ram,

10 Gb SSD with 100 Gb traffic and 200 Mbps organize interface.

ID important to that item is 212 as should be obvious in the reaction.

const url = 'https://secure.veesp.com/api/order/212';
 
const options = {
    method: 'POST',
    url: url,
    headers: {
        'Authorization': 'Basic ' + auth,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        domain: 'myhostname'
    })
};
 
request(options, function (err, res, body) {
    console.log(body);
});

On the off chance that you don’t get any blunder from the server, you VPS should now be fully operational.

Posting the Current Services

You might need to get a rundown of current VPS to examine your present utilization,

const url = 'https://secure.veesp.com/api/service';
 
const req = {
   url: url,
   method: 'GET',
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
 
request(req, (err, res) => {
   const body = JSON.parse(res.body);
   console.log(body);
});

and furthermore to control stop or start a VPS when required.

How about we take a rundown of administrations by calling the/administration API as beneath

const url = 'https://secure.veesp.com/api/service';
 
const req = {
   url: url,
   method: 'GET',
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
 
request(req, (err, res) => {
   const body = JSON.parse(res.body);
   console.log(body);
});

Execute this code and you ought to get a reaction like this,

{ services:
   [ { id: '29618',
       domain: 'xd002',
       total: '4.000',
       status: 'Active',
       billingcycle: 'Monthly',
       next_due: '2019-07-22',
       category: 'Linux SSD VPS',
       category_url: 'vps',
       name: 'SSD Sandbox' },
     { id: '29611',
       domain: 'xdhost',
       total: '4.000',
       status: 'Active',
       billingcycle: 'Monthly',
       next_due: '2019-07-21',
       category: 'Linux SSD VPS',
       category_url: 'vps',
       name: 'SSD Sandbox' } ] }

Since we have made a Linux SSD VPS above we will get that and a rundown of every

single other assistance we are utilizing. In the event that you just need VPS servers or a specific classification no one but,

you can channel by the category_url or classification as you wish.

For instance, in the event that you just need to choose just Linux SSD VPS classification

request(req, (err, res) => {
   const body = JSON.parse(res.body);
   const services = body.services;
 
   const linuxSSDVpsList = services.filter((item) => {
       if (item.category === 'Linux SSD VPS') {
           return true;
       }
   });
 
   console.log(linuxSSDVpsList);
});

Stop and Start a VPS

Presently we have a rundown of administrations and their IDs. At long last, presently we

can get a rundown of VMs identified with the administration as underneath.

You should pass your administration ID with the accompanying URL. It would be ideal if you note that the administration

ID of the above solicitation 29618 is utilized to get the VM list.

const url = 'https://secure.veesp.com/api/service/29618/vms';
 
const req = {
   url: url,
   method: 'GET',
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
 
request(req, (err, res) => {
   const body = JSON.parse(res.body);
   console.log(body);
});

You ought to get a reaction like this from the server with VM IDs and data including the password,

{
 
  "vms": {
 
    "17228": {
 
      "password": "afakepassword",
 
      "template": "linux-centos-6-i386-min-gen2-v1",
 
      "template_label": "CentOS 6 32 bit\t",
 
      "disk": 10,
 
      "memory": 512,
 
      "burstmem": -512,
 
      "bandwidth": 100,
 
      "pae": 0,
 
      "pxe": 0,
 
      "id": "17228",
 
      "state": "online",
 
      "ipv6subnets": [
 
        "2a00:1345:37:13a::/64"
 
      ],
 
      "usage": {
 
        "disk": {
 
          "total": 10,
 
          "used": 0,
 
          "free": 10,
 
          "percent": "0"
 
        },
 
        "memory": {
 
          "total": 0,
 
          "used": 0,
 
          "free": 0,
 
          "percent": "0"
 
        },
 
        "bandwidth": {
 
          "total": 100,
 
          "used": 0,
 
          "free": 100,
 
          "percent": "0"
 
        }
 
      },
 
      "label": "xd002",
 
      "ip": [
 
        "34.34.56.56",
 
        " 2a00:1345:37:13a::a246"
 
      ],
 
      "cpus": "1"
 
    }
 
  }
 
}

As should be obvious you have the doled out open IP and the secret phrase to validate

the VPS in the reaction. Presently you can mechanize conveying your application and downsizing rationale at this stage or give these

certifications to your clients on the off chance that you are an affiliate.

Presently you can perform stop and start the activity on your VPS as underneath,

To stop the server,

const url = 'https://secure.veesp.com/api/service/29618/vms/17228/stop';
 
const options = {
   method: 'POST',
   url: url,
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
 
request(options, function (err, res, body) {
   console.log(body);
});

To begin the server,

const url = 'https://secure.veesp.com/api/service/29618/vms/17228/start';
 
const options = {
   method: 'POST',
   url: url,
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
 
request(options, function (err, res, body) {
   console.log(body);
});

Other than that you can modify your VPS, restart your VPS, change the boot request of your VPS utilizing this API.

So this is the nuts and bolts of how you can utilize robotization and application scaling utilizing HTTP API.

Expectation you will locate this helpful to robotize and scale up your business as your client base develops,

without expecting to login to the dashboard until the end of time.

Cheerful scaling!

Thanks for putting your time in this article!

Leave a Comment

error: Alert: Content is protected!!