Add initial version, readme and license

parents
MIT License
Copyright (c) 2017 Andrew Hall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
## <img width=25px src="https://www.vultr.com/dist/img/brand/logo_v_onwhite.svg">&nbsp;Vultr Manager
### A manager for your Vultr sevices written in Perl.
##### Requirements
* perl >= 5.0 < 6.0
* libwww-perl > 6.0
* libjson-perl > 2.90
Also requires a Vultr API key with the nessesary permissions.
##### Important Note
Currently this script only returns the balance and is very much a WIP.
##### Usage
As an API key is needed, you can either supply it as an enviroment variable (`API-KEY`) or by modifying the `API_KEY` variable in the script.
$ API-KEY="12345678901234567890" vultrmgr.pl ...
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
our $API_KEY = "API_KEY_HERE";
our %API_RESP_CODES = ( 400 => "An Invalid API URL was specified. Check the URL and try again.",
403 => "An Invalid API key was supplied. Make sure the API is present and correct.",
405 => "The wrong method was used for this function. Entire that the correct method is used (GET/POST).",
412 => "Request failed for some reason. Check the response body for more information.",
500 => "An unknown internal server error occured. Try again later.",
503 => "Excessive requests (2/s) has been detected. Slow down and try again later." );
my $KEY_FROM_ENV = $ENV{"API_KEY"};
if (defined $KEY_FROM_ENV and $KEY_FROM_ENV ne "") {
$API_KEY = $KEY_FROM_ENV;
print "API key from env: $API_KEY\n";
}
else {
print "API key from source: $API_KEY\n";
}
my $ua = LWP::UserAgent->new;
my $vultr_api_url = "https://api.vultr.com/v1/account/info";
my $api_request = HTTP::Request->new("GET" => $vultr_api_url);
$api_request->header("API-Key" => $API_KEY);
$api_request->content("");
my $api_response = $ua->request($api_request);
if ($api_response->is_success) {
my $message = $api_response->decoded_content;
my %data = %{decode_json($message)};
my $bal = ($data{"balance"} + $data{"pending_charges"}) * -1;
print "Balance: $bal\n";
} else {
print "Error: ", $api_response->code, " ",$api_response->message, "\n";
print "$API_RESP_CODES{$api_response->code}\n";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment