Once upon a time when someone needs to publish their own website on the internet, they had to go buy expensive server hoping that performance will be enough for growing user base or buy even more expensive server that they were not going to use at its full capacity. But this is only a 20% of the work, then they had to contact an ISP and buy a public IP for the server and pay the monthly internet bill and power bill and then make sure the server is running 24×7.
But today we have a hosting service to make sure every single thing is handled for us for a reasonable price. Whether you are a blogger, entrepreneur, reseller or a developer you just have to sit back and relax. Whether you are growing 1 million users per day or having a rough patch, you only need to pay for what you have used.
For example, imagine that you have built a rocket to go to mars and you are building a mobile app to sell tickets. You published your app today and when you wake up tomorrow there are millions of people who want to buy a ticket, but your app is broken since your VPS cannot handle the load or you are a VPS reseller who has a website and manually ordering servers from other platforms.
But what if, you can automate this without ever being wanted to log in to your dashboard again. In this article, we are going to see how to develop it using NodeJS. For this article, I will use the Veesp’s API to do that.
Further details about this API can be found in the official documentation at following link https://secure.veesp.com/userapi
So let’s get started!
Install the npm Request Module
In order to perform any kind of HTTP requests, you need an HTTP client library. You are free to use your own NodeJS HTTP client. For this article, we are going to use npm request module.
So first let’s get started by installing the request module using npm,
npm i --save request
Then create a file called app.js and import the request module.
const request = require('request');
Ordering a VPS
Veesp provide a variety of service categories including VPS hosting, domain names, DNS hosting and few other services. Also, VPS hosting is divided into a few types, Linux SSD VPS, Linux HDD VPS, Windows VPS and dedicated servers.
Veesp API uses HTTP basic user authentication for authenticating users. You should send your username and password with the request headers in order to use endpoints.
We will leave out this module import, and username password authentication after this example. But you should send these authentication details in each request as we mentioned before,
So here is the code to get a list of service categories 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); });
When you want to execute the code you can open your terminal and run node app.js
. Your console output 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 list of categories and relevant ID for each category. For this example, we are going to order a new Linux SSD VPS where the ID is 18 as we can see in the response.
Then we can use this category ID to get a list of products under this category as below.
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); });
After executing this code, your console output 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] }, …
Now we can order the product. Pass your hostname as a parameter ‘domain’ in the request body. You can select the ID of the product you want to order, for this example we are going to order 512 Mb Ram, 10 Gb SSD with 100 Gb traffic and 200 Mbps network interface. ID relevant to that product is 212 as you can see in the response.
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); });
If you do not get any error from the server, you VPS should now be up and running.
Listing the Current Services
You may want to get a list of current VPS to analyze your current usage, and also to control stop or start a VPS when needed.
Let’s take a list of services by calling the /service API as below
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 should get a response 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' } ] }
Because we have created a Linux SSD VPS above we will get that and a list of all other services we are using. If you only need VPS servers or a certain category only, you can filter by the category_url or category as you wish.
For example, if you only want to select only Linux SSD VPS category
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
Now we have a list of services and their IDs. Finally, now we can get a list of VMs related to the service as below. You should pass your service ID with the following URL. Please, note that the service ID of the above request 29618 is used 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 should get a response like this from the server with VM IDs and info 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 you can see you have the assigned public IP and the password to authenticate the VPS in the response. Now you can automate deploying your application and scaling logic at this stage or give back these credentials to your customers if you are a reseller.
Now you can perform stop and start operation on your VPS as below,
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 start 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 rebuild your VPS, restart your VPS, change the boot order of your VPS using this API.
So this is the basics of how you can use automation and application scaling using HTTP API. Hope you will find this useful to automate and scale up your business as your customer base grows, without even needing to login to the dashboard ever again.
Happy scaling!
Thanks for spending a few minutes to read this article!
The post PaaS: Managing Your Cloud Infrastructure via RESTful API in Node.js appeared first on The Crazy Programmer.
No comments:
Post a Comment