From b8f155167d0715519855b30db683005f91b4f520 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Wed, 3 Feb 2021 12:59:43 -0800 Subject: [PATCH 01/17] Change composer project name to ours --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 33957af..a095605 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "dutchie027/vultr", - "description": "Vultr PHP API Wrapper", + "name": "comp-mc/vultr", + "description": "Vultr PHP API Wrapper by dutchie027. Forked.", "keywords": ["php", "automation", "Vultr", "paas", "headless", "server administration"], "type": "library", "require": { From b1faed32dc169000a7071eb86ece9cd8df349528 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Wed, 3 Feb 2021 12:59:55 -0800 Subject: [PATCH 02/17] Store region data in a way that can be reused later and still links together the region information with identifiers. --- src/Regions.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Regions.php b/src/Regions.php index 48b6c58..1f7e224 100644 --- a/src/Regions.php +++ b/src/Regions.php @@ -87,6 +87,13 @@ class Regions */ public $ddos_flag = "ddos_protection"; + /** + * Cached region data. + * + * @var array + */ + private $region_data = []; + /** * __construct * Main Construct - Loads Regions in to arrays and creates reference @@ -116,6 +123,14 @@ public function listRegions() return $this->api->makeAPICall('GET', $this->api::REGIONS_URL); } + public function getRegions() { + $build_data = array(); + foreach ($this->region_data['regions'] as $line) { + $build_data['id'] = $line; + } + return $build_data; + } + /** * loadRegionArrays * Loads arrays with region information @@ -126,7 +141,7 @@ public function listRegions() */ public function loadRegionArrays() { - $data = json_decode($this->listRegions(), true); + $this->region_data = $data = json_decode($this->listRegions(), true); foreach ($data['regions'] as $line) { $this->ids[] = $line['id']; $this->cities[] = $line['city']; From a5872cfb9822de95998ca1b3894e8387225d5182 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Wed, 3 Feb 2021 13:00:23 -0800 Subject: [PATCH 03/17] Make baremetal plans publicly accessible, and method to get both types of plans all at once. --- src/Plans.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Plans.php b/src/Plans.php index 97779b2..21d361f 100644 --- a/src/Plans.php +++ b/src/Plans.php @@ -58,7 +58,7 @@ class Plans * * @var array */ - protected $metal_plan = []; + public $metal_plan = []; /** * Count of Total Metal Plans @@ -159,4 +159,12 @@ public function getNumberOfPlans() { return $this->total_plans; } + + /** + * getAllPlans + * Returns Bare Metal and normal plans combined together. + */ + public function getAllPlans() { + return array_merge($this->plan, $this->metal_plan); + } } From f9712cd50b3199865f81e8e10649209494e8a064 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Sun, 7 Feb 2021 09:59:19 -0800 Subject: [PATCH 04/17] Correct typo for getRegions function. --- src/Regions.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Regions.php b/src/Regions.php index 1f7e224..96cbadb 100644 --- a/src/Regions.php +++ b/src/Regions.php @@ -123,10 +123,19 @@ public function listRegions() return $this->api->makeAPICall('GET', $this->api::REGIONS_URL); } - public function getRegions() { + /** + * getRegions + * Gets the regions with associated data from API + * + * + * @return array + * + */ + public function getRegions() + { $build_data = array(); foreach ($this->region_data['regions'] as $line) { - $build_data['id'] = $line; + $build_data[$line['id']] = $line; } return $build_data; } From 58dc6a2fd088224928256e90feee3028fd058224 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Mon, 8 Feb 2021 11:55:44 -0800 Subject: [PATCH 05/17] Remove stdout functions --- README.md | 8 +---- src/Applications.php | 30 ---------------- src/BareMetal.php | 15 -------- src/Firewalls.php | 15 -------- src/ISO.php | 15 -------- src/Instances.php | 15 -------- src/LoadBalancers.php | 15 -------- src/OperatingSystems.php | 15 -------- src/PrivateNetworks.php | 15 -------- src/Regions.php | 75 ---------------------------------------- src/ReservedIPs.php | 15 -------- src/SSHKeys.php | 15 -------- src/Snapshots.php | 15 -------- src/StartupScripts.php | 15 -------- 14 files changed, 1 insertion(+), 277 deletions(-) diff --git a/README.md b/README.md index 7730960..b31c235 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,10 @@ # vultr-php -[![Latest Stable Version](https://poser.pugx.org/dutchie027/vultr/v)](//packagist.org/packages/dutchie027/vultr) -[![Total Downloads](https://poser.pugx.org/dutchie027/vultr/downloads)](//packagist.org/packages/dutchie027/vultr) -[![License](https://poser.pugx.org/dutchie027/vultr/license)](//packagist.org/packages/dutchie027/vultr) -[![CodeFactor](https://www.codefactor.io/repository/github/dutchie027/vultr-php/badge)](https://www.codefactor.io/repository/github/dutchie027/vultr-php) - PHP Library Intended to Interact with [Vultr's v2 API](https://www.vultr.com/api/v2) -There are a few other PHP libraries out that do similar things, but I wasn't happy that they were very monolithic and also they used cURL and not Guzzle. Further, there was no logging in any of the libraries, so I wrote this one. ## Installation ```php -composer require dutchie027/vultr +composer require comp-mc/vultr ``` ## Usage diff --git a/src/Applications.php b/src/Applications.php index 96a2199..fa65817 100644 --- a/src/Applications.php +++ b/src/Applications.php @@ -107,36 +107,6 @@ public function loadApplications() $this->app_count = $apps['meta']['total']; } - /** - * printNames - * Prints Names and IDs to stdout - * - * - * @return void - * - */ - public function printNames() - { - foreach ($this->ids as $id) { - print $this->deploy_names[$id] . " ($id)" . PHP_EOL; - } - } - - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * getNumberOfApplications * Returns total number of applications diff --git a/src/BareMetal.php b/src/BareMetal.php index e4fbdc4..901c0da 100644 --- a/src/BareMetal.php +++ b/src/BareMetal.php @@ -486,21 +486,6 @@ public function getVNCURLForABareMetal($id) return $this->api->makeAPICall('GET', $this->api::BARE_METAL_URL . "/" . $id . "/vnc"); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * checkBareMetalId * Checks's if an Metal ID is valid or not diff --git a/src/Firewalls.php b/src/Firewalls.php index bd200c7..04618cc 100644 --- a/src/Firewalls.php +++ b/src/Firewalls.php @@ -357,21 +357,6 @@ public function getInstanceCount($id) } } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * updateFirewallGroup * Updates label of Firewall Group diff --git a/src/ISO.php b/src/ISO.php index 8b3b23e..297b477 100644 --- a/src/ISO.php +++ b/src/ISO.php @@ -131,21 +131,6 @@ public function createISO($url) return $this->api->makeAPICall('POST', $this->api::ISO_URL, $body); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * loadISOs * Loads ISO Information in to arrays diff --git a/src/Instances.php b/src/Instances.php index 3769bf6..100fe68 100644 --- a/src/Instances.php +++ b/src/Instances.php @@ -878,21 +878,6 @@ public function getAvailableInstanceUpgrades($inst) return $this->api->makeAPICall('GET', $this->api::INSTANCES_URL . "/" . $inst . "/upgrades"); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * getIds * Returns Instance IDs as an array diff --git a/src/LoadBalancers.php b/src/LoadBalancers.php index c75bcf0..5a73b08 100644 --- a/src/LoadBalancers.php +++ b/src/LoadBalancers.php @@ -83,21 +83,6 @@ public function __construct(API $api) $this->loadLoadBalancers(); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * listLoadBalancers * List all Reserved IPs in your account. diff --git a/src/OperatingSystems.php b/src/OperatingSystems.php index 7d73ba8..6d1f012 100644 --- a/src/OperatingSystems.php +++ b/src/OperatingSystems.php @@ -92,19 +92,4 @@ public function loadOSArray() } $this->total_os_count = $osa['meta']['total']; } - - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } } diff --git a/src/PrivateNetworks.php b/src/PrivateNetworks.php index 2959118..77d3d15 100644 --- a/src/PrivateNetworks.php +++ b/src/PrivateNetworks.php @@ -68,21 +68,6 @@ public function __construct(API $api) $this->loadPrivateNetworks(); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * listPrivateNetworks * Lists Private Networks diff --git a/src/Regions.php b/src/Regions.php index 96cbadb..be2347b 100644 --- a/src/Regions.php +++ b/src/Regions.php @@ -162,81 +162,6 @@ public function loadRegionArrays() } } - /** - * listCities - * Prints Cities to stdout - * - * - * @return void - * - */ - public function listCities() - { - foreach ($this->cities as $city) { - print $city . PHP_EOL; - } - } - - /** - * listIds - * Prints Ususble IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - - /** - * listCountries - * Prints Countries to stdout - * - * - * @return void - * - */ - public function listCountries() - { - foreach ($this->countries as $country) { - print $country . PHP_EOL; - } - } - - /** - * listContinents - * Prints Continents to stdout - * - * - * @return void - * - */ - public function listContinents() - { - foreach ($this->continents as $continent) { - print $continent . PHP_EOL; - } - } - - /** - * listNames - * Prints Names to stdout - * - * - * @return void - * - */ - public function listNames() - { - foreach ($this->names as $name) { - print $name . PHP_EOL; - } - } - /** * getIds * Returns the array of ususble IDs diff --git a/src/ReservedIPs.php b/src/ReservedIPs.php index f1708eb..ddcd7fc 100644 --- a/src/ReservedIPs.php +++ b/src/ReservedIPs.php @@ -61,21 +61,6 @@ public function __construct(API $api) $this->loadReservedIPs(); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * listReservedIPs * List all Reserved IPs in your account. diff --git a/src/SSHKeys.php b/src/SSHKeys.php index c4bdf47..a705b45 100644 --- a/src/SSHKeys.php +++ b/src/SSHKeys.php @@ -68,21 +68,6 @@ public function __construct(API $api) $this->loadSSHKeys(); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * listSSHKeys * Lists SSH Keys diff --git a/src/Snapshots.php b/src/Snapshots.php index bb48762..66ea65f 100644 --- a/src/Snapshots.php +++ b/src/Snapshots.php @@ -68,21 +68,6 @@ public function __construct(API $api) $this->loadSnapshots(); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * listSnapshots * Lists Snapshots diff --git a/src/StartupScripts.php b/src/StartupScripts.php index 0e61d7a..2b7e240 100644 --- a/src/StartupScripts.php +++ b/src/StartupScripts.php @@ -75,21 +75,6 @@ public function __construct(API $api) $this->loadStartupScripts(); } - /** - * listIds - * Prints Instance IDs to stdout - * - * - * @return void - * - */ - public function listIds() - { - foreach ($this->ids as $id) { - print $id . PHP_EOL; - } - } - /** * listStartupScripts * Lists Startup Scripts From c4e15196b8274d5ce67d3bf5ff483bc301915162 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Mon, 8 Feb 2021 11:56:00 -0800 Subject: [PATCH 06/17] Introduce api exception type --- src/Exceptions/VultrAPIException.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/Exceptions/VultrAPIException.php diff --git a/src/Exceptions/VultrAPIException.php b/src/Exceptions/VultrAPIException.php new file mode 100644 index 0000000..3a4bf05 --- /dev/null +++ b/src/Exceptions/VultrAPIException.php @@ -0,0 +1,13 @@ + Date: Mon, 8 Feb 2021 12:08:28 -0800 Subject: [PATCH 07/17] Add request exception type, and throw them instead of printing to console on error. --- src/API.php | 46 +++++++++++---------- src/Exceptions/VultrAPIRequestException.php | 13 ++++++ 2 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 src/Exceptions/VultrAPIRequestException.php diff --git a/src/API.php b/src/API.php index 787b38f..77497df 100644 --- a/src/API.php +++ b/src/API.php @@ -15,6 +15,7 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\VultrAPIRequestException; use GuzzleHttp\Client as Guzzle; use GuzzleHttp\Psr7; use GuzzleHttp\Exception\RequestException; @@ -336,7 +337,7 @@ private function getAPIToken() * @return object * */ - public function account() + public function account(): Account { $account = new Account($this); return $account; @@ -350,7 +351,7 @@ public function account() * @return object * */ - public function applications() + public function applications(): Applications { $ap = new Applications($this); return $ap; @@ -364,7 +365,7 @@ public function applications() * @return object * */ - public function backups() + public function backups(): Backups { $bu = new Backups($this); return $bu; @@ -378,7 +379,7 @@ public function backups() * @return object * */ - public function bareMetal() + public function bareMetal(): BareMetal { $bm = new BareMetal($this); return $bm; @@ -392,7 +393,7 @@ public function bareMetal() * @return object * */ - public function blockStorage() + public function blockStorage(): BlockStorage { $bs = new BlockStorage($this); return $bs; @@ -406,7 +407,7 @@ public function blockStorage() * @return object * */ - public function dns() + public function dns(): DNS { $dns = new DNS($this); return $dns; @@ -420,7 +421,7 @@ public function dns() * @return object * */ - public function firewalls() + public function firewalls(): Firewalls { $fw = new Firewalls($this); return $fw; @@ -434,7 +435,7 @@ public function firewalls() * @return object * */ - public function instances() + public function instances(): Instances { $instances = new Instances($this); return $instances; @@ -448,7 +449,7 @@ public function instances() * @return object * */ - public function iso() + public function iso(): ISO { $iso = new ISO($this); return $iso; @@ -462,7 +463,7 @@ public function iso() * @return object * */ - public function loadBalancers() + public function loadBalancers(): LoadBalancers { $lb = new LoadBalancers($this); return $lb; @@ -476,7 +477,7 @@ public function loadBalancers() * @return object * */ - public function objectStorage() + public function objectStorage(): ObjectStorage { $os = new ObjectStorage($this); return $os; @@ -490,7 +491,7 @@ public function objectStorage() * @return object * */ - public function operatingSystems() + public function operatingSystems(): OperatingSystems { $os = new OperatingSystems($this); return $os; @@ -504,7 +505,7 @@ public function operatingSystems() * @return object * */ - public function plans() + public function plans(): Plans { $plans = new Plans($this); return $plans; @@ -518,7 +519,7 @@ public function plans() * @return object * */ - public function privateNetworks() + public function privateNetworks(): PrivateNetworks { $pn = new PrivateNetworks($this); return $pn; @@ -532,7 +533,7 @@ public function privateNetworks() * @return object * */ - public function regions() + public function regions(): Regions { $regions = new Regions($this); return $regions; @@ -546,7 +547,7 @@ public function regions() * @return object * */ - public function reservedIPs() + public function reservedIPs(): ReservedIPs { $rip = new ReservedIPs($this); return $rip; @@ -560,7 +561,7 @@ public function reservedIPs() * @return object * */ - public function snapshots() + public function snapshots(): Snapshots { $snap = new Snapshots($this); return $snap; @@ -574,7 +575,7 @@ public function snapshots() * @return object * */ - public function sshKeys() + public function sshKeys(): SSHKeys { $ssh = new SSHKeys($this); return $ssh; @@ -588,7 +589,7 @@ public function sshKeys() * @return object * */ - public function startupScripts() + public function startupScripts(): StartupScripts { $ss = new StartupScripts($this); return $ss; @@ -602,7 +603,7 @@ public function startupScripts() * @return object * */ - public function users() + public function users(): Users { $users = new Users($this); return $users; @@ -683,7 +684,7 @@ public function pGenRandomString($length = 6) * @param $body string - usually passed as JSON * * @return string Body Object - * @throws string error message + * @throws VultrAPIRequestException Exception with details regarding the failed request * */ public function makeAPICall($type, $url, $body = null) @@ -697,8 +698,9 @@ public function makeAPICall($type, $url, $body = null) if ($e->hasResponse()) { $response = $e->getResponse(); $ja = json_decode($response->getBody()->getContents(), true); - print $ja['error']; + throw new VultrAPIRequestException('An error occurred while performing the request to ' . $url . ' -> ' . $ja['error']); } + throw new VultrAPIRequestException(('An unknown error ocurred while performing the request to ' . $url)); } } } diff --git a/src/Exceptions/VultrAPIRequestException.php b/src/Exceptions/VultrAPIRequestException.php new file mode 100644 index 0000000..cab123a --- /dev/null +++ b/src/Exceptions/VultrAPIRequestException.php @@ -0,0 +1,13 @@ + Date: Mon, 8 Feb 2021 12:09:56 -0800 Subject: [PATCH 08/17] Add gitignore --- .idea/.gitignore | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..8c6528a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,11 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ + +*.iml +.idea From 07a68bcb9a7cce9a94fba9fc11ee4d63755516cc Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Mon, 8 Feb 2021 12:11:14 -0800 Subject: [PATCH 09/17] Require ext-json --- composer.json | 3 ++- composer.lock | 32 +++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index a095605..08c95bd 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "require": { "monolog/monolog": "^2.2", "guzzlehttp/guzzle": "^7.2", - "aws/aws-sdk-php": "^3.171" + "aws/aws-sdk-php": "^3.171", + "ext-json": "*" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index f278fd2..049b8a2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "41dac099b3512c19c276ebeeaa958e93", + "content-hash": "918be56a2ec979ecea89a098cbd7474e", "packages": [ { "name": "aws/aws-sdk-php", - "version": "3.171.2", + "version": "3.173.5", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "742663a85ec84647f74dea454d2dc45bba180f9d" + "reference": "45a5150c35ab4ee5e4eb05332179a094476d1dd5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/742663a85ec84647f74dea454d2dc45bba180f9d", - "reference": "742663a85ec84647f74dea454d2dc45bba180f9d", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/45a5150c35ab4ee5e4eb05332179a094476d1dd5", + "reference": "45a5150c35ab4ee5e4eb05332179a094476d1dd5", "shasum": "" }, "require": { @@ -92,9 +92,9 @@ "support": { "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "issues": "https://github.com/aws/aws-sdk-php/issues", - "source": "https://github.com/aws/aws-sdk-php/tree/3.171.2" + "source": "https://github.com/aws/aws-sdk-php/tree/3.173.5" }, - "time": "2020-12-18T19:12:13+00:00" + "time": "2021-02-08T19:13:12+00:00" }, { "name": "guzzlehttp/guzzle", @@ -686,16 +686,16 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.20.0", + "version": "v1.22.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "39d483bdf39be819deabf04ec872eb0b2410b531" + "reference": "f377a3dd1fde44d37b9831d68dc8dea3ffd28e13" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/39d483bdf39be819deabf04ec872eb0b2410b531", - "reference": "39d483bdf39be819deabf04ec872eb0b2410b531", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/f377a3dd1fde44d37b9831d68dc8dea3ffd28e13", + "reference": "f377a3dd1fde44d37b9831d68dc8dea3ffd28e13", "shasum": "" }, "require": { @@ -707,7 +707,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "1.20-dev" + "dev-main": "1.22-dev" }, "thanks": { "name": "symfony/polyfill", @@ -746,7 +746,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.20.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.22.0" }, "funding": [ { @@ -762,7 +762,7 @@ "type": "tidelift" } ], - "time": "2020-10-23T14:02:19+00:00" + "time": "2021-01-07T16:49:33+00:00" } ], "packages-dev": [], @@ -771,7 +771,9 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "ext-json": "*" + }, "platform-dev": [], "plugin-api-version": "2.0.0" } From d8a2b512cf045d022cece3503bfd67a96c88a574 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Mon, 8 Feb 2021 12:13:15 -0800 Subject: [PATCH 10/17] API call function return type clarification --- src/API.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API.php b/src/API.php index 77497df..087ba39 100644 --- a/src/API.php +++ b/src/API.php @@ -683,7 +683,7 @@ public function pGenRandomString($length = 6) * @param $url string endpoint * @param $body string - usually passed as JSON * - * @return string Body Object + * @return Psr7\Stream Object * @throws VultrAPIRequestException Exception with details regarding the failed request * */ From 51994d82a22cfc91662c838e93852add94c02a0f Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Mon, 8 Feb 2021 13:06:13 -0800 Subject: [PATCH 11/17] Convert to exceptions instead of printing to stdout. --- src/Account.php | 1 - src/Backups.php | 5 +- src/BareMetal.php | 23 ++-- src/BlockStorage.php | 43 +++---- src/DNS.php | 76 +++++------- src/Exceptions/InvalidParameterException.php | 13 ++ src/Firewalls.php | 66 ++++------ src/Instances.php | 119 +++++++------------ src/LoadBalancers.php | 17 ++- src/ObjectStorage.php | 18 ++- src/PrivateNetworks.php | 13 +- src/ReservedIPs.php | 20 ++-- src/SSHKeys.php | 17 ++- src/Snapshots.php | 8 +- src/StartupScripts.php | 14 +-- src/Users.php | 40 +++---- 16 files changed, 193 insertions(+), 300 deletions(-) create mode 100644 src/Exceptions/InvalidParameterException.php diff --git a/src/Account.php b/src/Account.php index fee6eb3..98d5544 100644 --- a/src/Account.php +++ b/src/Account.php @@ -43,7 +43,6 @@ public function __construct(API $api) * getAccountInfo * Gets account info * - * * @return string * */ diff --git a/src/Backups.php b/src/Backups.php index 34182d8..619af68 100644 --- a/src/Backups.php +++ b/src/Backups.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class Backups { /** @@ -97,8 +99,7 @@ public function getBackup($id) if (in_array($id, $this->ids)) { return $this->api->makeAPICall('GET', $this->api::BACKUPS_URL . "/" . $id); } else { - print "That Backup ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Backup ID isn't associated with your account"); } } diff --git a/src/BareMetal.php b/src/BareMetal.php index 901c0da..81c1c5f 100644 --- a/src/BareMetal.php +++ b/src/BareMetal.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class BareMetal { @@ -164,14 +166,12 @@ public function createBareMetal($oa) { $hasOS = false; if (!isset($oa['region']) || !in_array($oa['region'], $this->api->regions()->ids)) { - print "Invalid Region"; - exit; + throw new InvalidParameterException("Invalid Region"); } else { $ba['region'] = $oa['region']; } if (!isset($oa['plan']) || !in_array($oa['plan'], $this->api->plans()->metal_ids)) { - print "Invalid Plan"; - exit; + throw new InvalidParameterException("Invalid Plan"); } else { $ba['plan'] = $oa['plan']; } @@ -188,8 +188,7 @@ public function createBareMetal($oa) $ba['app_id'] = $oa['app_id']; } if (!$hasOS) { - print "A Valid OS (os_id, iso_id, snapshot_id or app_id) is missing"; - exit; + throw new InvalidParameterException("A Valid OS (os_id, iso_id, snapshot_id or app_id) is missing"); } (isset($oa['label'])) ? $ba['label'] = $oa['label'] : null; (isset($oa['tag'])) ? $ba['tag'] = $oa['tag'] : null; @@ -206,20 +205,17 @@ public function createBareMetal($oa) if (isset($oa['sshkey_id']) && in_array($oa['sshkey_id'], $this->api->sshKeys()->ids)) { $ba['sshkey_id'] = $oa['sshkey_id']; } elseif (isset($oa['sshkey_id']) && !in_array($oa['sshkey_id'], $this->api->sshKeys()->ids)) { - print "You provided an SSH Key ID and it's not part of your account"; - exit; + throw new InvalidParameterException("You provided an SSH Key ID and it's not part of your account"); } if (isset($oa['script_id']) && in_array($oa['script_id'], $this->api->startupScripts()->ids)) { $ba['script_id'] = $oa['script_id']; } elseif (isset($oa['script_id']) && !in_array($oa['script_id'], $this->api->startupScripts()->ids)) { - print "You provided an Startup Script and it's not part of your account"; - exit; + throw new InvalidParameterException("You provided an Startup Script and it's not part of your account"); } if (isset($oa['reserved_ipv4']) && in_array($oa['reserved_ipv4'], $this->api->reservedIPs()->ids)) { $ba['reserved_ipv4'] = $oa['reserved_ipv4']; } elseif (isset($oa['reserved_ipv4']) && !in_array($oa['reserved_ipv4'], $this->api->reservedIPs()->ids)) { - print "You provided a Reserved IP and it's not part of your account"; - exit; + throw new InvalidParameterException("You provided a Reserved IP and it's not part of your account"); } if (isset($oa['user_data'])) { $ba['user_data'] = $oa['user_data']; @@ -500,8 +496,7 @@ public function checkBareMetalId($id) if (in_array($id, $this->ids)) { return true; } else { - print "Instance Not Found"; - exit; + throw new InvalidParameterException("Instance Not Found"); } } } diff --git a/src/BlockStorage.php b/src/BlockStorage.php index 5af3c55..c23c7d0 100644 --- a/src/BlockStorage.php +++ b/src/BlockStorage.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class BlockStorage { /** @@ -122,8 +124,6 @@ public function createBlockStorage($sa = []) $ba['size_gb'] = $this->d_size; $ba['label'] = $this->d_label; - print_r($block_ids); - (isset($sa['region']) && in_array($sa['region'], $block_ids)) ? $ba['region'] = $sa['region'] : null; if (isset($sa['size']) && is_numeric($sa['size'])) { if ($sa['size'] > 9 && $sa['size'] < 10001) { @@ -149,8 +149,7 @@ public function getBlockStorage($blockid) if (in_array($blockid, $this->block_array)) { return $this->api->makeAPICall('GET', $this->api::BLOCK_STORAGE_URL . "/" . $blockid); } else { - print "That Block ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Block ID isn't associated with your account"); } } @@ -168,8 +167,7 @@ public function deleteBlockStorage($blockid) if (in_array($blockid, $this->block_array)) { return $this->api->makeAPICall('DELETE', $this->api::BLOCK_STORAGE_URL . "/" . $blockid); } else { - print "That Block ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Block ID isn't associated with your account"); } } @@ -186,14 +184,11 @@ public function updateBlockStorage($options) { if (in_array($options['blockid'], $this->block_array)) { if (!isset($options['size'])) { - print "you must set the size"; - exit; + throw new InvalidParameterException("You must set the size"); } elseif (!is_numeric($options['size'])) { - print "size must be numeric"; - exit; + throw new InvalidParameterException("Size must be numeric"); } elseif ($options['size'] < 10 || $options['size'] > 10000) { - print "Size must be a number between 10 and 10000"; - exit; + throw new InvalidParameterException("Size must be a number between 10 and 10000"); } else { $ba['size_gb'] = $options['size']; } @@ -202,8 +197,7 @@ public function updateBlockStorage($options) $body = json_encode($ba); return $this->api->makeAPICall('PATCH', $this->api::BLOCK_STORAGE_URL . "/" . $options['block_id'], $body); } else { - print "That block ID doesn't exist in your account"; - exit; + throw new InvalidParameterException("That block ID doesn't exist in your account"); } } @@ -221,14 +215,11 @@ public function attachBlockStorage($options) $instance_ids = $this->api->instances()->getIds(); if (in_array($options['instance'], $instance_ids)) { if (!in_array($options['blockid'], $this->block_array)) { - print "That block ID doesn't exist in your account"; - exit; + throw new InvalidParameterException("That block ID doesn't exist in your account"); } elseif (!isset($options['live'])) { - print "you must set the live variable"; - exit; + throw new InvalidParameterException("You must set the live variable"); } elseif (!is_bool($options['live'])) { - print "the live setting must be either true or false only."; - exit; + throw new InvalidParameterException("The live setting must be either true or false only."); } $ba['instance_id'] = $options['instance']; $ba['live'] = $options['live']; @@ -238,8 +229,7 @@ public function attachBlockStorage($options) $body = json_encode($ba); return $this->api->makeAPICall('POST', $url, $body); } else { - print "That block ID doesn't exist in your account"; - exit; + throw new InvalidParameterException("That block ID doesn't exist in your account"); } } @@ -256,11 +246,9 @@ public function detatchBlockStorage($options) { if (in_array($options['blockid'], $this->block_array)) { if (!isset($options['live'])) { - print "you must set the live variable"; - exit; + throw new InvalidParameterException("You must set the live variable"); } elseif (!is_bool($options['live'])) { - print "the live setting must be either true or false only."; - exit; + throw new InvalidParameterException("The live setting must be either true or false only."); } else { $ba['live'] = $options['live']; } @@ -270,8 +258,7 @@ public function detatchBlockStorage($options) $body = json_encode($ba); return $this->api->makeAPICall('POST', $url, $body); } else { - print "That block ID doesn't exist in your account"; - exit; + throw new InvalidParameterException("That block ID doesn't exist in your account"); } } diff --git a/src/DNS.php b/src/DNS.php index 3cc2504..c027802 100644 --- a/src/DNS.php +++ b/src/DNS.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class DNS { @@ -190,12 +192,11 @@ public function listRecords($domain) public function getRecord($oa) { if (!isset($oa['domain'])) { - print "Domain Not Set"; + throw new InvalidParameterException("Domain Not Set"); } $this->validateDomain($oa['domain']); if (!isset($oa['id'])) { - print "id not set"; - exit; + throw new InvalidParameterException("ID not set"); } $url = $this->api::DNS_URL . "/" . $oa['domain'] . "/records/" . $oa['id']; return $this->api->makeAPICall('GET', $url); @@ -213,12 +214,11 @@ public function getRecord($oa) public function deleteRecord($oa) { if (!isset($oa['domain'])) { - print "Domain Not Set"; + throw new InvalidParameterException("Domain Not Set"); } $this->validateDomain($oa['domain']); if (!isset($oa['id'])) { - print "id not set"; - exit; + throw new InvalidParameterException("ID not set"); } $url = $this->api::DNS_URL . "/" . $oa['domain'] . "/records/" . $oa['id']; return $this->api->makeAPICall('DELETE', $url); @@ -236,16 +236,14 @@ public function deleteRecord($oa) public function updateDomain($oa) { if (!isset($oa['domain'])) { - print "Domain Not Set"; + throw new InvalidParameterException("Domain Not Set"); } $this->validateDomain($oa['domain']); if (!isset($oa['dns_sec'])) { - print "dns_sec not set"; - exit; + throw new InvalidParameterException("dns_sec not set"); } if (!in_array($oa['dns_sec'], $this->valid_dnssec)) { - print "dns_sec must be enabled/disabled. It's not one of those"; - exit; + throw new InvalidParameterException("dns_sec must be enabled/disabled. It's not one of those"); } $ba['dns_sec'] = $oa['dns_sec']; $body = json_encode($ba); @@ -265,14 +263,13 @@ public function updateSOA($oa) { $execute = false; if (!isset($oa['domain'])) { - print "Domain Not Set"; + throw new InvalidParameterException("Domain Not Set"); } $this->validateDomain($oa['domain']); $url = $this->api::DNS_URL . "/" . $oa['domain'] . "/soa"; if (isset($oa['email'])) { if (!filter_var($oa['email'], FILTER_VALIDATE_EMAIL)) { - print "Email is invalid"; - exit; + throw new InvalidParameterException("Email is invalid"); } else { $execute = true; $ba['email'] = $oa['email']; @@ -281,8 +278,7 @@ public function updateSOA($oa) if (isset($oa['nsprimary'])) { if (!filter_var($oa['nsprimary'], FILTER_VALIDATE_DOMAIN)) { - print "NS Primary Is Invalid"; - exit; + throw new InvalidParameterException("NS Primary Is Invalid"); } else { $execute = true; $ba['nsprimary'] = $oa['nsprimary']; @@ -307,20 +303,17 @@ public function createDomain($oa) { $ba['dns_sec'] = $this->d_dns_sec; if (!isset($oa['domain'])) { - print "Domain is not set"; - exit; + throw new InvalidParameterException("Domain is not set"); } $this->validateDomain($oa['domain']); $ba['domain'] = $oa['domain']; if (isset($oa['ip']) && !filter_var($oa['ip'], FILTER_VALIDATE_IP)) { - print "IP is set but is invalid"; - exit; + throw new InvalidParameterException("IP is set but is invalid"); } elseif (isset($oa['ip']) && filter_var($oa['ip'], FILTER_VALIDATE_IP)) { $ba['ip'] = $oa['ip']; } if (isset($oa['dns_sec']) && !in_array($oa['dns_sec'], $this->valid_dnssec)) { - print "DNS SEC is set but is not a valud option (enabled/disabled)"; - exit; + throw new InvalidParameterException("DNS SEC is set but is not a valud option (enabled/disabled)"); } elseif (isset($oa['dns_sec'])) { $ba['dns_sec'] = $oa['dns_sec']; } @@ -341,43 +334,35 @@ public function createRecord($oa) { $ba['ttl'] = $this->d_ttl; if (!isset($oa['domain'])) { - print "Domain is not set"; - exit; + throw new InvalidParameterException("Domain is not set"); } $this->validateDomain($oa['domain']); $url = $this->api::DNS_URL . "/" . $oa['domain'] . "/records"; if (!isset($oa['type'])) { - print "Type is required"; - exit; + throw new InvalidParameterException("Type is required"); } if (!in_array($oa['type'], $this->valid_types)) { - print "Invalid Type"; - exit; + throw new InvalidParameterException("Invalid Type"); } if (in_array($oa['type'], $this->priority_records)) { if (!isset($oa['priority']) || !is_numeric($oa['priority'])) { - print "Priority must be set and be numeric if you use the type you used"; - exit; + throw new InvalidParameterException("Priority must be set and be numeric if you use the type you used"); } else { $ba['priority'] = $oa['priority']; } } if (!isset($oa['data'])) { - print "Data is required"; - exit; + throw new InvalidParameterException("Data is required"); } if (!isset($oa['name'])) { - print "Name is required"; - exit; + throw new InvalidParameterException("Name is required"); } if ($oa['type'] == "A" && !filter_var($oa['data'], FILTER_VALIDATE_IP)) { - print "Type is A but data is not a valid IP"; - exit; + throw new InvalidParameterException("Type is A but data is not a valid IP"); } if (isset($oa['ttl'])) { if (!is_numeric($oa['ttl'])) { - print "TTL must be numeric"; - exit; + throw new InvalidParameterException("TTL must be numeric"); } $ba['ttl'] = $oa['ttl']; } @@ -401,12 +386,10 @@ public function updateRecord($oa) { $exe = false; if (!isset($oa['domain'])) { - print "Domain is not set"; - exit; + throw new InvalidParameterException("Domain is not set"); } if (!isset($oa['id'])) { - print "ID is not set"; - exit; + throw new InvalidParameterException("ID is not set"); } $this->validateDomain($oa['domain']); $url = $this->api::DNS_URL . "/" . $oa['domain'] . "/records/" . $oa['id']; @@ -420,16 +403,14 @@ public function updateRecord($oa) } if (isset($oa['ttl'])) { if (!is_numeric($oa['ttl'])) { - print "TTL must be numeric"; - exit; + throw new InvalidParameterException("TTL must be numeric"); } $ba['ttl'] = $oa['ttl']; $exe = true; } if (isset($oa['priority'])) { if (!is_numeric($oa['priority'])) { - print "priority must be numeric"; - exit; + throw new InvalidParameterException("Priority must be numeric"); } $ba['priority'] = $oa['priority']; $exe = true; @@ -443,8 +424,7 @@ public function updateRecord($oa) private function validateDomain($domain) { if (!preg_match("/([0-9a-z-]+\.)?[0-9a-z-]+\.[a-z]{2,7}/", $domain)) { - print "Domain is not valid"; - exit; + throw new InvalidParameterException("Domain is not valid"); } return true; } diff --git a/src/Exceptions/InvalidParameterException.php b/src/Exceptions/InvalidParameterException.php new file mode 100644 index 0000000..317b560 --- /dev/null +++ b/src/Exceptions/InvalidParameterException.php @@ -0,0 +1,13 @@ +ids)) { return $this->api->makeAPICall('GET', $this->api::FIREWALLS_URL . "/" . $id); } else { - print "That Firewall Group ID doesn't exist"; - exit; + throw new InvalidParameterException("That Firewall Group ID doesn't exist"); } } @@ -157,8 +158,7 @@ public function listFirewallRules($id) if (in_array($id, $this->ids)) { return $this->api->makeAPICall('GET', $this->api::FIREWALLS_URL . "/" . $id . "/rules"); } else { - print "That Firewall Group ID doesn't exist"; - exit; + throw new InvalidParameterException("That Firewall Group ID doesn't exist"); } } @@ -174,8 +174,7 @@ public function listFirewallRules($id) public function createFirewallGroup($name) { if (strlen($name) < 4) { - print "Name needs to be a minimum of 4 characters"; - exit; + throw new InvalidParameterException("Name needs to be a minimum of 4 characters"); } else { $ba['description'] = $name; } @@ -198,35 +197,29 @@ public function createFirewallRule($fa) if (isset($fa['id']) && in_array($fa['id'], $this->ids)) { $url = $this->api::FIREWALLS_URL . "/" . $fa['id'] . "/rules"; } else { - print "Firewall Group ID doesn't exist or noit defined"; - exit; + throw new InvalidParameterException("Firewall Group ID doesn't exist or noit defined"); } if (isset($fa['ip_type']) && in_array($fa['ip_type'], $this->valid_ip_types)) { $ba['ip_type'] = $fa['ip_type']; } else { - print "Invalid IP Type. Must be 'v4' or 'v6'"; - exit; + throw new InvalidParameterException("Invalid IP Type. Must be 'v4' or 'v6'"); } if (isset($fa['protocol']) && in_array(strtoupper($fa['protocol']), $this->valid_protos)) { $ba['protocol'] = strtoupper($fa['protocol']); } else { - print "Invalid protocol. Must be one of these:" . PHP_EOL; - print_r($this->valid_protos); - exit; + throw new InvalidParameterException("Invalid protocol. Must be one of these: " . $this->valid_protos); } if (isset($fa['subnet']) && filter_var($fa['subnet'], FILTER_VALIDATE_IP)) { $ba['subnet'] = $fa['subnet']; } else { - print "Invalid IP address for the subnet key"; - exit; + throw new InvalidParameterException("Invalid IP address for the subnet key"); } if (isset($fa['port'])) { $prm = "/^(\d+)([\:\-]?)(\d+)?$/"; if (preg_match($prm, $fa['port'], $matches)) { if (count($matches) === 3) { if ($matches[1] < 0 || $matches[1] > 65535) { - print "Invalid Port - Must be between 0 and 65535"; - exit; + throw new InvalidParameterException("Invalid Port - Must be between 0 and 65535"); } else { $port = $matches[1]; } @@ -239,16 +232,13 @@ public function createFirewallRule($fa) $port1 = $matches[1]; $port2 = $matches[3]; if ($port1 < 0 || $port1 > 65535) { - print "Port values must be between 0 and 65535"; - exit; + throw new InvalidParameterException("Port values must be between 0 and 65535"); } if ($port2 < 0 || $port2 > 65535) { - print "Port values must be between 0 and 65535"; - exit; + throw new InvalidParameterException("Port values must be between 0 and 65535"); } if ($port1 > $port2) { - print "The first port can't be lesser than the second port"; - exit; + throw new InvalidParameterException("The first port can't be lesser than the second port"); } if ($port1 == $port2) { $port = $port1; @@ -257,27 +247,22 @@ public function createFirewallRule($fa) } $ba['port'] = $port; } else { - print "Something went wrong"; - exit; + throw new InvalidParameterException("Something went wrong"); } } else { - print "Port Value Invalid"; - exit; + throw new InvalidParameterException("Port Value Invalid"); } } else { - print "Port not set"; - exit; + throw new InvalidParameterException("Port not set"); } if (isset($fa['subnet_size']) && is_numeric($fa['subnet_size'])) { if ($fa['subnet_size'] < 0 || $fa['subnet_size'] > 32) { - print "Subnet size is must between 0 and 32"; - exit; + throw new InvalidParameterException("Subnet size is must between 0 and 32"); } else { $ba['subnet_size'] = $fa['subnet_size']; } } else { - print "Subnet size is not set or is not numeric"; - exit; + throw new InvalidParameterException("Subnet size is not set or is not numeric"); } (isset($fa['notes'])) ? $ba['notes'] = $fa['notes'] : null; $body = json_encode($ba); @@ -312,8 +297,7 @@ public function getNumberOfRules($id) if (in_array($id, $this->ids)) { return $this->fwrga[$id]['rule_count']; } else { - print "That Firewall Group ID doesn't exist"; - exit; + throw new InvalidParameterException("That Firewall Group ID doesn't exist"); } } @@ -332,8 +316,7 @@ public function getRuleName($id) if (in_array($id, $this->ids)) { return $this->fwrga[$id]['desc']; } else { - print "That Firewall Group ID doesn't exist"; - exit; + throw new InvalidParameterException("That Firewall Group ID doesn't exist"); } } @@ -352,8 +335,7 @@ public function getInstanceCount($id) if (in_array($id, $this->ids)) { return $this->fwrga[$id]['instance_count']; } else { - print "That Firewall Group ID doesn't exist"; - exit; + throw new InvalidParameterException("That Firewall Group ID doesn't exist"); } } @@ -371,8 +353,7 @@ public function updateFirewallGroup($options) if (in_array($options['group_id'], $this->ids)) { $url = $this->api::FIREWALLS_URL . "/" . $options['group_id']; } else { - print "That Firewall Group ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Firewall Group ID isn't associated with your account"); } $ba['description'] = $this->d_description; (isset($options['description'])) ? $ba['description'] = $options['description'] : null; @@ -394,8 +375,7 @@ public function deleteFirewallGroup($id) if (in_array($id, $this->ids)) { $url = $this->api::FIREWALLS_URL . "/" . $id; } else { - print "That Firewall Group ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Firewall Group ID isn't associated with your account"); } return $this->api->makeAPICall('DELETE', $url); } diff --git a/src/Instances.php b/src/Instances.php index 100fe68..adc5f7c 100644 --- a/src/Instances.php +++ b/src/Instances.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class Instances { /** @@ -83,14 +85,12 @@ public function createInstance($oa) { $hasOS = false; if (!isset($oa['region']) || !in_array($oa['region'], $this->api->regions()->ids)) { - print "Invalid Region"; - exit; + throw new InvalidParameterException("Invalid Region"); } else { $ba['region'] = $oa['region']; } if (!isset($oa['plan']) || !in_array($oa['plan'], $this->api->plans()->ids)) { - print "Invalid Plan"; - exit; + throw new InvalidParameterException("Invalid Plan"); } else { $ba['plan'] = $oa['plan']; } @@ -111,8 +111,7 @@ public function createInstance($oa) $ba['app_id'] = $oa['app_id']; } if (!$hasOS) { - print "A Valid OS (os_id, iso_id, snapshot_id or app_id) is missing"; - exit; + throw new InvalidParameterException("A Valid OS (os_id, iso_id, snapshot_id or app_id) is missing") } (isset($oa['ipxe_chain_url'])) ? $ba['ipxe_chain_url'] = $oa['ipxe_chain_url'] : null; (isset($oa['label'])) ? $ba['label'] = $oa['label'] : null; @@ -129,8 +128,7 @@ public function createInstance($oa) } if (isset($oa['ddos_protection']) && $oa['ddos_protection'] == true) { if (!in_array($oa['region'], $this->api->regions()->ddos_ids)) { - print "You chose to set DDOS, but the region is not capable of it"; - exit; + throw new InvalidParameterException("You chose to set DDOS, but the region is not capable of it") } $ba['ddos_protection'] = true; } else { @@ -143,13 +141,11 @@ public function createInstance($oa) } if (isset($oa['enable_private_network']) && $oa['enable_private_network'] == true) { if (!isset($oa['attach_private_network']) || !is_array($oa['attach_private_network'])) { - print "You chose to enable private networks but you didn't provide one or it's not an array"; - exit; + throw new InvalidParameterException("You chose to enable private networks but you didn't provide one or it's not an array") } foreach ($oa['attach_private_network'] as $pnet) { if (!in_array($pnet, $this->api->privateNetworks()->ids)) { - print "Private Network Not Found"; - exit; + throw new InvalidParameterException("Private Network Not Found") } } $ba['enable_private_network'] = true; @@ -160,26 +156,22 @@ public function createInstance($oa) if (isset($oa['sshkey_id']) && in_array($oa['sshkey_id'], $this->api->sshKeys()->ids)) { $ba['sshkey_id'] = $oa['sshkey_id']; } elseif (isset($oa['sshkey_id']) && !in_array($oa['sshkey_id'], $this->api->sshKeys()->ids)) { - print "You provided an SSH Key ID and it's not part of your account"; - exit; + throw new InvalidParameterException("You provided an SSH Key ID and it's not part of your account"); } if (isset($oa['script_id']) && in_array($oa['script_id'], $this->api->startupScripts()->ids)) { $ba['script_id'] = $oa['script_id']; } elseif (isset($oa['script_id']) && !in_array($oa['script_id'], $this->api->startupScripts()->ids)) { - print "You provided an Startup Script and it's not part of your account"; - exit; + throw new InvalidParameterException("You provided an Startup Script and it's not part of your account"); } if (isset($oa['firewall_group_id']) && in_array($oa['firewall_group_id'], $this->api->firewalls()->ids)) { $ba['firewall_group_id'] = $oa['firewall_group_id']; } elseif (isset($oa['firewall_group_id']) && !in_array($oa['firewall_group_id'], $this->api->firewalls()->ids)) { - print "You provided a Firewall ID that is not part of your account"; - exit; + throw new InvalidParameterException("You provided a Firewall ID that is not part of your account"); } if (isset($oa['reserved_ipv4']) && in_array($oa['reserved_ipv4'], $this->api->reservedIPs()->ids)) { $ba['reserved_ipv4'] = $oa['reserved_ipv4']; } elseif (isset($oa['reserved_ipv4']) && !in_array($oa['reserved_ipv4'], $this->api->reservedIPs()->ids)) { - print "You provided a Reserved IP and it's not part of your account"; - exit; + throw new InvalidParameterException("You provided a Reserved IP and it's not part of your account"); } if (isset($oa['user_data'])) { $ba['user_data'] = $oa['user_data']; @@ -242,8 +234,7 @@ public function updateInstance($oa) if (isset($oa['firewall_group_id']) && in_array($oa['firewall_group_id'], $this->api->firewalls()->ids)) { $ba['firewall_group_id'] = $oa['firewall_group_id']; } elseif (isset($oa['firewall_group_id']) && !in_array($oa['firewall_group_id'], $this->api->firewalls()->ids)) { - print "You provided a Firewall ID that is not part of your account"; - exit; + throw new InvalidParameterException("You provided a Firewall ID that is not part of your account"); } if (isset($oa['user_data'])) { $ba['user_data'] = $oa['user_data']; @@ -252,13 +243,11 @@ public function updateInstance($oa) if (isset($oa['plan']) && in_array($oa['plan'], $this->api->plans()->ids)) { $ba['plan'] = $oa['plan']; } elseif (isset($oa['plan'])) { - print "Invalid Plan"; - exit; + throw new InvalidParameterException("Invalid Plan"); } if (isset($oa['ddos_protection']) && $oa['ddos_protection'] == true) { if (!in_array($oa['region'], $this->api->regions()->ddos_ids)) { - print "You chose to set DDOS, but the region is not capable of it"; - exit; + throw new InvalidParameterException("You chose to set DDOS, but the region is not capable of it"); } $ba['ddos_protection'] = true; } elseif (isset($oa['ddos_protection']) && $oa['ddos_protection'] == false) { @@ -266,13 +255,11 @@ public function updateInstance($oa) } if (isset($oa['enable_private_network']) && $oa['enable_private_network'] == true) { if (!isset($oa['attach_private_network']) || !is_array($oa['attach_private_network'])) { - print "You chose to enable private networks but you didn't provide one or it's not an array"; - exit; + throw new InvalidParameterException("You chose to enable private networks but you didn't provide one or it's not an array"); } foreach ($oa['attach_private_network'] as $pnet) { if (!in_array($pnet, $this->api->privateNetworks()->ids)) { - print "Private Network Not Found"; - exit; + throw new InvalidParameterException("Private Network Not Found"); } } $ba['enable_private_network'] = true; @@ -281,8 +268,7 @@ public function updateInstance($oa) if (isset($oa['detach_private_network']) && is_array($oa['detach_private_network'])) { $ba['detach_private_network'] = $oa['detach_private_network']; } elseif (isset($oa['detach_private_network']) && !is_array($oa['detach_private_network'])) { - print "Detatch Networks Is Not Valid"; - exit; + throw new InvalidParameterException("Detatch Networks Is Not Valid"); } $body = json_encode($ba); return $this->api->makeAPICall('PATCH', $this->api::INSTANCES_URL . "/" . $oa['id'], $body); @@ -469,8 +455,7 @@ public function attachISOToInstance($oa) { $this->checkInstanceId($oa['id']); if (!isset($oa['iso_id']) || !in_array($oa['iso_id'], $this->api->iso()->ids)) { - print "ISO Not Found"; - exit; + throw new InvalidParameterException("ISO Not Found"); } $ba['iso_id'] = $oa['iso_id']; $body = json_encode($ba); @@ -505,8 +490,7 @@ public function attachPrivateNetworkToInstance($oa) { $this->checkInstanceId($oa['id']); if (!isset($oa['network_id']) || !in_array($oa['network_id'], $this->api->privateNetworks()->ids)) { - print "Network ID Not Found"; - exit; + throw new InvalidParameterException("Network ID Not Found"); } $url = $this->api::INSTANCES_URL . "/" . $oa['id'] . "/private-networks/attach"; $ba['network_id'] = $oa['network_id']; @@ -527,8 +511,7 @@ public function detachPrivateNetworkFromInstance($oa) { $this->checkInstanceId($oa['id']); if (!isset($oa['network_id']) || !in_array($oa['network_id'], $this->api->privateNetworks()->ids)) { - print "Network ID Not Found"; - exit; + throw new InvalidParameterException("Network ID Not Found"); } $url = $this->api::INSTANCES_URL . "/" . $oa['id'] . "/private-networks/detach"; $ba['network_id'] = $oa['network_id']; @@ -549,50 +532,42 @@ public function setInstanceBackupSchedule($oa) { $this->checkInstanceId($oa['id']); if (!isset($oa['type'])) { - print "Backup Type Missing"; - exit; + throw new InvalidParameterException("Backup Type Missing"); } if ($oa['type'] == "daily") { if (!isset($oa['hour']) || !is_numeric($oa['hour']) || $oa['hour'] > 24 || $oa['hour'] < 0) { - print "Hour is invalid"; - exit; + throw new InvalidParameterException("Hour is invalid"); } $ba['type'] = $oa['type']; $ba['hour'] = $oa['hour']; } elseif ($oa['type'] == "weekly") { if (!isset($oa['hour']) || !is_numeric($oa['hour']) || $oa['hour'] > 24 || $oa['hour'] < 0) { - print "Hour is invalid"; - exit; + throw new InvalidParameterException("Hour is invalid"); } if (!isset($oa['dow']) || !is_numeric($oa['dow']) || $oa['dow'] > 7 || $oa['dow'] < 0) { - print "Day of Week (dow) is invalid"; - exit; + throw new InvalidParameterException("Day of Week (dow) is invalid"); } $ba['type'] = $oa['type']; $ba['hour'] = $oa['hour']; $ba['dow'] = $oa['dow']; } elseif ($oa['type'] == "monthly") { if (!isset($oa['hour']) || !is_numeric($oa['hour']) || $oa['hour'] > 24 || $oa['hour'] < 0) { - print "Hour is invalid"; - exit; + throw new InvalidParameterException("Hour is invalid"); } if (!isset($oa['dom']) || !is_numeric($oa['dom']) || $oa['dom'] > 28 || $oa['dom'] < 1) { - print "Day of Month (dom) is invalid"; - exit; + throw new InvalidParameterException("Day of Month (dom) is invalid"); } $ba['type'] = $oa['type']; $ba['hour'] = $oa['hour']; $ba['dom'] = $oa['dom']; } elseif ($oa['type'] == "daily_alt_even" || $oa['type'] == "daily_alt_odd") { if (!isset($oa['hour']) || !is_numeric($oa['hour']) || $oa['hour'] > 24 || $oa['hour'] < 0) { - print "Hour is invalid"; - exit; + throw new InvalidParameterException("Hour is invalid"); } $ba['type'] = $oa['type']; $ba['hour'] = $oa['hour']; } else { - print "Type is invalid"; - exit; + throw new InvalidParameterException("Type is invalid"); } $body = json_encode($ba); return $this->api->makeAPICall('POST', $this->api::INSTANCES_URL . "/" . $oa['id'] . "/backup-schedule", $body); @@ -635,8 +610,7 @@ public function restoreInstance($oa) $ba['backup_id'] = $oa['backup_id']; } if (!$hasOS) { - print "A Valid OS (snapshot_id or backup_id) is missing"; - exit; + throw new InvalidParameterException("A Valid OS (snapshot_id or backup_id) is missing"); } $body = json_encode($ba); return $this->api->makeAPICall('POST', $this->api::INSTANCES_URL . "/" . $oa['id'] . "/restore", $body); @@ -707,12 +681,10 @@ public function createInstanceReverseIPv6($oa) { $this->checkInstanceId($oa['id']); if (!isset($oa['ip']) || !filter_var($oa['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - print "IP Address missing or invalid"; - exit; + throw new InvalidParameterException("IP Address missing or invalid"); } if (!isset($oa['reverse']) || !filter_var($oa['reverse'], FILTER_VALIDATE_DOMAIN)) { - print "Reverse missing or it's not a valid domain"; - exit; + throw new InvalidParameterException("Reverse missing or it's not a valid domain"); } $ba['ip'] = $oa['ip']; $ba['reverse'] = $oa['reverse']; @@ -748,12 +720,10 @@ public function createInstanceReverseIPv4($oa) { $this->checkInstanceId($oa['id']); if (!isset($oa['ip']) || !filter_var($oa['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - print "IP Address missing or invalid"; - exit; + throw new InvalidParameterException("IP Address missing or invalid"); } if (!isset($oa['reverse']) || !filter_var($oa['reverse'], FILTER_VALIDATE_DOMAIN)) { - print "Reverse missing or it's not a valid domain"; - exit; + throw new InvalidParameterException("Reverse missing or it's not a valid domain"); } $ba['ip'] = $oa['ip']; $ba['reverse'] = $oa['reverse']; @@ -804,12 +774,10 @@ public function haltInstance($inst) public function setDefaultReverseDNSEntry($oa) { if (!in_array($oa['id'], $this->ids)) { - print "Instance ID Not Found"; - exit; + throw new InvalidParameterException("Instance ID Not Found"); } if (!isset($oa['ip']) || !filter_var($oa['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - print "IP Address missing or invalid"; - exit; + throw new InvalidParameterException("IP Address missing or invalid"); } $ba['ip'] = $oa['ip']; $url = $this->api::INSTANCES_URL . "/" . $oa['id'] . "/ipv4/reverse/default"; @@ -829,12 +797,10 @@ public function setDefaultReverseDNSEntry($oa) public function deleteIPv4Address($oa) { if (!in_array($oa['id'], $this->ids)) { - print "Instance ID Not Found"; - exit; + throw new InvalidParameterException("Instance ID Not Found"); } if (!isset($oa['ip']) || !filter_var($oa['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - print "IP Address missing or invalid"; - exit; + throw new InvalidParameterException("IP Address missing or invalid"); } $url = $this->api::INSTANCES_URL . "/" . $oa['id'] . "/ipv4/" . $oa['ip']; return $this->api->makeAPICall('DELETE', $url); @@ -852,12 +818,10 @@ public function deleteIPv4Address($oa) public function deleteInstanceReverseIPv6($oa) { if (!in_array($oa['id'], $this->ids)) { - print "Instance ID Not Found"; - exit; + throw new InvalidParameterException("Instance ID Not Found"); } if (!isset($oa['ip']) || !filter_var($oa['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - print "IP Address missing or invalid"; - exit; + throw new InvalidParameterException("IP Address missing or invalid"); } $url = $this->api::INSTANCES_URL . "/" . $oa['id'] . "/ipv6/reverse/" . $oa['ip']; return $this->api->makeAPICall('DELETE', $url); @@ -933,8 +897,7 @@ public function checkInstanceId($id) if (in_array($id, $this->ids)) { return true; } else { - print "Instance Not Found"; - exit; + throw new InvalidParameterException("Instance Not Found"); } } } diff --git a/src/LoadBalancers.php b/src/LoadBalancers.php index 5a73b08..2de7372 100644 --- a/src/LoadBalancers.php +++ b/src/LoadBalancers.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class LoadBalancers { @@ -178,20 +180,16 @@ public function createForwardingRule($oa) { $this->checkLoadBalancer($oa['load-balancer-id']); if (!isset($oa['frontend_protocol']) || !in_array($oa['frontend_protocol'], $this->frontend_proto)) { - print "Front End Protocol Missing or Invalid"; - exit; + throw new InvalidParameterException("Front End Protocol Missing or Invalid"); } if (!isset($oa['backend_protocol']) || !in_array($oa['backend_protocol'], $this->backend_proto)) { - print "Front End Protocol Missing or Invalid"; - exit; + throw new InvalidParameterException("Front End Protocol Missing or Invalid"); } if (!isset($oa['frontend_port']) || $oa['frontend_port'] > 65535 || $oa['frontend_port'] < 1) { - print "frontend port invalid"; - exit; + throw new InvalidParameterException("Frontend port invalid"); } if (!isset($oa['backend_port']) || $oa['backend_port'] > 65535 || $oa['backend_port'] < 1) { - print "backend port invalid"; - exit; + throw new InvalidParameterException("Backend port invalid"); } $ba['frontend_protocol'] = $oa['frontend_protocol']; $ba['backend_protocol'] = $oa['backend_protocol']; @@ -258,8 +256,7 @@ public function checkLoadBalancer($id) if (in_array($id, $this->ids)) { return true; } else { - print "Load Balancer ID Not Found"; - exit; + throw new InvalidParameterException("Load Balancer ID Not Found"); } } diff --git a/src/ObjectStorage.php b/src/ObjectStorage.php index f4a4c95..56d26b0 100644 --- a/src/ObjectStorage.php +++ b/src/ObjectStorage.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class ObjectStorage { /** @@ -162,13 +164,13 @@ public function createObjectStorage($options) if (in_array($options['cluster_id'], $this->ids)) { $ba['cluster_id'] = $options['cluster_id']; } else { - print "Bad Cluster ID"; + throw new InvalidParameterException("Bad Cluster ID"); } } elseif (isset($options['region'])) { if (in_array($options['region'], $this->regions)) { $ba['cluster_id'] = $this->region_map[$options['region']]; } else { - print "Bad Region"; + throw new InvalidParameterException("Bad Region"); } } @@ -191,8 +193,7 @@ public function getObjectStorage($oid) if (in_array($oid, $this->storage_ids)) { return $this->api->makeAPICall('GET', $this->api::OBJECT_STORAGE_URL . "/" . $oid); } else { - print "That Storage ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Storage ID isn't associated with your account"); } } @@ -210,8 +211,7 @@ public function deleteObjectStorage($oid) if (in_array($oid, $this->storage_ids)) { return $this->api->makeAPICall('DELETE', $this->api::OBJECT_STORAGE_URL . "/" . $oid); } else { - print "That Storage ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Storage ID isn't associated with your account"); } } @@ -229,8 +229,7 @@ public function regenerateKeys($oid) if (in_array($oid, $this->storage_ids)) { $url = $this->api::OBJECT_STORAGE_URL . "/" . $oid . "/regenerate-keys"; } else { - print "That Storage ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Storage ID isn't associated with your account"); } return $this->api->makeAPICall('POST', $url); } @@ -249,8 +248,7 @@ public function updateObjectStorage($options) if (in_array($options['object_id'], $this->storage_ids)) { $url = $this->api::OBJECT_STORAGE_URL . "/" . $options['object_id']; } else { - print "That Storage ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Storage ID isn't associated with your account"); } $ba['label'] = $this->d_label; (isset($options['label'])) ? $ba['label'] = $options['label'] : null; diff --git a/src/PrivateNetworks.php b/src/PrivateNetworks.php index 77d3d15..847f239 100644 --- a/src/PrivateNetworks.php +++ b/src/PrivateNetworks.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class PrivateNetworks { @@ -142,8 +144,7 @@ public function updatePrivateNetwork($oa) if (in_array($oa['id'], $this->ids)) { $url = $this->api::PRIVATE_NETWORKS_URL . "/" . $oa['id']; } else { - print "That Private Network ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Private Network ID isn't associated with your account") } $ba['description'] = $this->d_label; (isset($oa['description'])) ? $ba['description'] = $oa['description'] : null; @@ -163,21 +164,19 @@ public function updatePrivateNetwork($oa) public function createPrivateNetwork($oa) { if (!isset($oa['region']) || !in_array($oa['region'], $this->api->regions()->ids)) { - print "Invalid Region"; - exit; + throw new InvalidParameterException("Invalid Region"); } else { $ba['region'] = $oa['region']; } if (isset($oa['subnet']) && $this->checkPrivateIP($oa['subnet'])) { $ba['v4_subnet'] = $oa['subnet']; } else { - print "Subnet is invalid. Must be an IP address and must meet RFC Standard for Private Networks."; - exit; + throw new InvalidParameterException("Subnet is invalid. Must be an IP address and must meet RFC Standard for Private Networks."); } if (isset($oa['mask']) && $oa['mask'] > 0 && $oa['mask'] < 32) { $ba['v4_subnet_mask'] = $oa['mask']; } else { - print "Subnet mask must be between 1 and 31 (you can't have a /32 private network)"; + throw new InvalidParameterException("Subnet mask must be between 1 and 31 (you can't have a /32 private network)"); } (isset($oa['description'])) ? $ba['description'] = $oa['description'] : null; $body = json_encode($ba); diff --git a/src/ReservedIPs.php b/src/ReservedIPs.php index ddcd7fc..8b70bc5 100644 --- a/src/ReservedIPs.php +++ b/src/ReservedIPs.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class ReservedIPs { @@ -142,14 +144,12 @@ public function getReservedIP($id) public function createReservedIP($oa) { if (!isset($oa['region']) || !in_array($oa['region'], $this->api->regions()->ids)) { - print "Invalid Region"; - exit; + throw new InvalidParameterException("Invalid Region"); } else { $ba['region'] = $oa['region']; } if (!isset($oa['ip_type']) || !preg_match("/v[46]/", $oa['ip_type'])) { - print "Invalid IP Type"; - exit; + throw new InvalidParameterException("Invalid IP Type"); } else { $ba['ip_type'] = $oa['ip_type']; } @@ -172,14 +172,12 @@ public function createReservedIP($oa) public function attachReservedIP($oa) { if (!isset($oa['instance_id']) || !$this->api->instances()->checkInstanceId($oa['instance_id'])) { - print "Invalid or Missing Instance ID"; - exit; + throw new InvalidParameterException("Invalid or Missing Instance ID"); } else { $ba['instance_id'] = $oa['instance_id']; } if (!isset($oa['reserved_ip']) || !$this->checkReservedIP($oa['reserved_ip'])) { - print "Invalid or Missing Instance IP"; - exit; + throw new InvalidParameterException("Invalid or Missing Instance IP"); } else { $ip = $oa['reserved_ip']; } @@ -218,8 +216,7 @@ public function detachReservedIP($ip) public function convertInstanceIPToReservedIP($oa) { if (!isset($oa['ip_address']) || !$this->checkReservedIP($oa['ip_address'])) { - print "Invalid or Missing IP"; - exit; + throw new InvalidParameterException("Invalid or Missing IP"); } else { $ba['ip_address'] = $oa['ip_address']; } @@ -243,8 +240,7 @@ public function checkReservedIP($id) if (in_array($id, $this->ids)) { return true; } else { - print "IP Not Found"; - exit; + throw new InvalidParameterException("IP Not Found"); } } } diff --git a/src/SSHKeys.php b/src/SSHKeys.php index a705b45..a0b9e09 100644 --- a/src/SSHKeys.php +++ b/src/SSHKeys.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class SSHKeys { @@ -142,14 +144,12 @@ public function updateSSHKey($oa) if (in_array($oa['id'], $this->ids)) { $url = $this->api::SSH_KEYS_URL . "/" . $oa['id']; } else { - print "That SSH Key ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That SSH Key ID isn't associated with your account"); } (isset($oa['name'])) ? $ba['name'] = $oa['name'] : null; (isset($oa['ssh_key'])) ? $ba['ssh_key'] = $oa['ssh_key'] : null; if (!isset($ba['name']) && !isset($ba['ssh_key'])) { - print "You didn't provide any details to update - either a new key or a new description"; - exit; + throw new InvalidParameterException("You didn't provide any details to update - either a new key or a new description"); } else { $body = json_encode($ba); return $this->api->makeAPICall('PATCH', $url, $body); @@ -168,16 +168,13 @@ public function updateSSHKey($oa) public function createSSHKey($oa) { if (!isset($oa['name'])) { - print "Missing a name for your SSH Key"; - exit; + throw new InvalidParameterException("Missing a name for your SSH Key"); } if (!isset($oa['ssh_key'])) { - print "Missing an SSH Key"; - exit; + throw new InvalidParameterException("Missing an SSH Key"); } if (!$this->validateKey($oa['ssh_key'])) { - print "Key is not a valid SSH Key"; - exit; + throw new InvalidParameterException("Key is not a valid SSH Key"); } $ba['ssh_key'] = $oa['ssh_key']; $ba['name'] = $oa['name']; diff --git a/src/Snapshots.php b/src/Snapshots.php index 66ea65f..2e84d9b 100644 --- a/src/Snapshots.php +++ b/src/Snapshots.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class Snapshots { @@ -142,8 +144,7 @@ public function updateSnapshot($options) if (in_array($options['snapshot_id'], $this->ids)) { $url = $this->api::SNAPSHOTS_URL . "/" . $options['snapshot_id']; } else { - print "That Snapshot ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Snapshot ID isn't associated with your account"); } $ba['description'] = $this->d_label; (isset($options['description'])) ? $ba['description'] = $options['description'] : null; @@ -163,8 +164,7 @@ public function updateSnapshot($options) public function createSnapshot($oa) { if (!isset($oa['instance_id']) || !in_array($oa['instance_id'], $this->api->instances()->ids)) { - print "Missing An Instance ID that is part of your account"; - exit; + throw new InvalidParameterException("Missing An Instance ID that is part of your account"); } $ba['instance_id'] = $oa['instance_id']; $ba['description'] = $this->d_label; diff --git a/src/StartupScripts.php b/src/StartupScripts.php index 2b7e240..d47e94c 100644 --- a/src/StartupScripts.php +++ b/src/StartupScripts.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class StartupScripts { @@ -149,8 +151,7 @@ public function updateStartupScript($oa) if (in_array($oa['id'], $this->ids)) { $url = $this->api::STARTUP_SCRIPTS_URL . "/" . $oa['id']; } else { - print "That Startup Script ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That Startup Script ID isn't associated with your account"); } (isset($oa['name'])) ? $ba['name'] = $oa['name'] : null; (isset($oa['script'])) ? $ba['script'] = $oa['script'] : null; @@ -176,19 +177,16 @@ public function createStartupScript($oa) if (in_array($oa['type'], $this->validStartupTypes)) { $ba['type'] = $oa['type']; } else { - print "Startup Script Type is invalid"; - exit; + throw new InvalidParameterException("Startup Script Type is invalid"); } } if (!isset($oa['name'])) { - print "Startup Script Name Required"; - exit; + throw new InvalidParameterException("Startup Script Name Required"); } else { $ba['name'] = $oa['name']; } if (!isset($oa['script'])) { - print "Startup Script Missing"; - exit; + throw new InvalidParameterException("Startup Script Missing"); } else { $ba['script'] = $oa['script']; } diff --git a/src/Users.php b/src/Users.php index 44dee25..57f76f8 100644 --- a/src/Users.php +++ b/src/Users.php @@ -15,6 +15,8 @@ namespace dutchie027\Vultr; +use dutchie027\Vultr\Exceptions\InvalidParameterException; + class Users { @@ -108,8 +110,7 @@ public function getUser($id) if (in_array($id, $this->ids)) { return $this->api->makeAPICall('GET', $this->api::USERS_URL . "/" . $id); } else { - print "That User ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That User ID isn't associated with your account"); } } @@ -159,8 +160,7 @@ public function updateUser($oa) if (in_array($oa['id'], $this->ids)) { $url = $this->api::USERS_URL . "/" . $oa['id']; } else { - print "That User ID doesn't exist"; - exit; + throw new InvalidParameterException("That User ID doesn't exist"); } $cr = false; if (isset($oa['email'])) { @@ -168,14 +168,12 @@ public function updateUser($oa) $cr = true; $ba['email'] = $oa['email']; } else { - print "Email included but is invalid"; - exit; + throw new InvalidParameterException("Email included but is invalid"); } } if (isset($oa['password'])) { if (strlen($oa['password']) < 8) { - print "Password included but is less than 8 characters"; - exit; + throw new InvalidParameterException("Password included but is less than 8 characters"); } else { $cr = true; $ba['password'] = $oa['password']; @@ -183,8 +181,7 @@ public function updateUser($oa) } if (isset($oa['name'])) { if (strlen($oa['name']) < 4) { - print "Name included but is less than 4 characters"; - exit; + throw new InvalidParameterException("Name included but is less than 4 characters"); } else { $cr = true; $ba['name'] = $oa['name']; @@ -192,8 +189,7 @@ public function updateUser($oa) } if (isset($oa['api_enabled'])) { if (!is_bool($oa['api_enabled'])) { - print "API Enabled Flag Is Invalid"; - exit; + throw new InvalidParameterException("API Enabled Flag Is Invalid"); } else { $cr = true; $ba['api_enabled'] = $oa['api_enabled']; @@ -208,14 +204,13 @@ public function updateUser($oa) } } if (!$acl_valid) { - print "Invalid ACLS"; - exit; + throw new InvalidParameterException("Invalid ACLS"); } else { $ba['acls'] = $oa['acls']; $cr = true; } } else { - print "ACL Value(s) passed invalid. Must be an array"; + throw new InvalidParameterException("ACL Value(s) passed invalid. Must be an array"); } } if ($cr) { @@ -238,20 +233,17 @@ public function createUser($oa) $ba['api_enabled'] = $this->d_api_enabled; $ba['acls'] = $this->d_acl; if (!isset($oa['email']) || !filter_var($oa['email'], FILTER_VALIDATE_EMAIL)) { - print "Email required or is invalid"; - exit; + throw new InvalidParameterException("Email required or is invalid"); } else { $ba['email'] = $oa['email']; } if (!isset($oa['password']) || strlen($oa['password']) < 8) { - print "Password Required or is less than 8 characters"; - exit; + throw new InvalidParameterException("Password Required or is less than 8 characters"); } else { $ba['password'] = $oa['password']; } if (!isset($oa['name']) || strlen($oa['name']) < 4) { - print "Name Required or is less than 4 characters"; - exit; + throw new InvalidParameterException("Name Required or is less than 4 characters"); } else { $ba['name'] = $oa['name']; } @@ -263,8 +255,7 @@ public function createUser($oa) } } if (!$acl_valid) { - print "Invalid ACLS"; - exit; + throw new InvalidParameterException("Invalid ACLS"); } else { $ba['acls'] = $oa['acls']; } @@ -287,8 +278,7 @@ public function deleteUser($id) if (in_array($id, $this->ids)) { return $this->api->makeAPICall('DELETE', $this->api::USERS_URL . "/" . $id); } else { - print "That User ID isn't associated with your account"; - exit; + throw new InvalidParameterException("That User ID isn't associated with your account"); } } From cd5054610a44e855174fedeb19809a6382a7321d Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Mon, 8 Feb 2021 13:17:40 -0800 Subject: [PATCH 12/17] Fix overlooked syntax errors. --- src/Instances.php | 8 ++++---- src/PrivateNetworks.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Instances.php b/src/Instances.php index adc5f7c..71bcea3 100644 --- a/src/Instances.php +++ b/src/Instances.php @@ -111,7 +111,7 @@ public function createInstance($oa) $ba['app_id'] = $oa['app_id']; } if (!$hasOS) { - throw new InvalidParameterException("A Valid OS (os_id, iso_id, snapshot_id or app_id) is missing") + throw new InvalidParameterException("A Valid OS (os_id, iso_id, snapshot_id or app_id) is missing"); } (isset($oa['ipxe_chain_url'])) ? $ba['ipxe_chain_url'] = $oa['ipxe_chain_url'] : null; (isset($oa['label'])) ? $ba['label'] = $oa['label'] : null; @@ -128,7 +128,7 @@ public function createInstance($oa) } if (isset($oa['ddos_protection']) && $oa['ddos_protection'] == true) { if (!in_array($oa['region'], $this->api->regions()->ddos_ids)) { - throw new InvalidParameterException("You chose to set DDOS, but the region is not capable of it") + throw new InvalidParameterException("You chose to set DDOS, but the region is not capable of it"); } $ba['ddos_protection'] = true; } else { @@ -141,11 +141,11 @@ public function createInstance($oa) } if (isset($oa['enable_private_network']) && $oa['enable_private_network'] == true) { if (!isset($oa['attach_private_network']) || !is_array($oa['attach_private_network'])) { - throw new InvalidParameterException("You chose to enable private networks but you didn't provide one or it's not an array") + throw new InvalidParameterException("You chose to enable private networks but you didn't provide one or it's not an array"); } foreach ($oa['attach_private_network'] as $pnet) { if (!in_array($pnet, $this->api->privateNetworks()->ids)) { - throw new InvalidParameterException("Private Network Not Found") + throw new InvalidParameterException("Private Network Not Found"); } } $ba['enable_private_network'] = true; diff --git a/src/PrivateNetworks.php b/src/PrivateNetworks.php index 847f239..3aeba35 100644 --- a/src/PrivateNetworks.php +++ b/src/PrivateNetworks.php @@ -144,7 +144,7 @@ public function updatePrivateNetwork($oa) if (in_array($oa['id'], $this->ids)) { $url = $this->api::PRIVATE_NETWORKS_URL . "/" . $oa['id']; } else { - throw new InvalidParameterException("That Private Network ID isn't associated with your account") + throw new InvalidParameterException("That Private Network ID isn't associated with your account"); } $ba['description'] = $this->d_label; (isset($oa['description'])) ? $ba['description'] = $oa['description'] : null; From 5f781ecd5831796da9e00099d1181e5bc8b1984d Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Tue, 9 Feb 2021 18:45:37 -0800 Subject: [PATCH 13/17] Fixes displaying errors that may not exist --- src/API.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API.php b/src/API.php index 087ba39..752da45 100644 --- a/src/API.php +++ b/src/API.php @@ -698,7 +698,7 @@ public function makeAPICall($type, $url, $body = null) if ($e->hasResponse()) { $response = $e->getResponse(); $ja = json_decode($response->getBody()->getContents(), true); - throw new VultrAPIRequestException('An error occurred while performing the request to ' . $url . ' -> ' . $ja['error']); + throw new VultrAPIRequestException('An error occurred while performing the request to ' . $url . ' -> ' . (isset($ja['error']) ? $ja['error'] : json_encode($ja))); } throw new VultrAPIRequestException(('An unknown error ocurred while performing the request to ' . $url)); } From 70e5197e7d434f6c2ddcce803653c4a3523c251c Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Tue, 9 Feb 2021 18:48:06 -0800 Subject: [PATCH 14/17] Fixes phpdoc for instances method return type --- src/Instances.php | 73 ++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/Instances.php b/src/Instances.php index 71bcea3..b541ada 100644 --- a/src/Instances.php +++ b/src/Instances.php @@ -16,6 +16,7 @@ namespace dutchie027\Vultr; use dutchie027\Vultr\Exceptions\InvalidParameterException; +use GuzzleHttp\Psr7\Stream; class Instances { @@ -78,7 +79,7 @@ public function listInstances() * * @param $oa array * - * @return void + * @return Stream * */ public function createInstance($oa) @@ -94,24 +95,24 @@ public function createInstance($oa) } else { $ba['plan'] = $oa['plan']; } - if (!$hasOS && isset($oa['os_id']) && in_array($oa['os_id'], $this->api->operatingSystems()->ids)) { + if (isset($oa['os_id']) && in_array($oa['os_id'], $this->api->operatingSystems()->ids)) { $hasOS = true; $ba['os_id'] = $oa['os_id']; } - if (!$hasOS && isset($oa['iso_id']) && in_array($oa['iso_id'], $this->api->iso()->ids)) { + if (isset($oa['iso_id']) && in_array($oa['iso_id'], $this->api->iso()->ids)) { $hasOS = true; $ba['iso_id'] = $oa['iso_id']; } - if (!$hasOS && isset($oa['snapshot_id']) && in_array($oa['snapshot_id'], $this->api->snapshots()->ids)) { + if (isset($oa['snapshot_id']) && in_array($oa['snapshot_id'], $this->api->snapshots()->ids)) { $hasOS = true; $ba['snapshot_id'] = $oa['snapshot_id']; } - if (!$hasOS && isset($oa['app_id']) && in_array($oa['app_id'], $this->api->applications()->ids)) { + if (isset($oa['app_id']) && in_array($oa['app_id'], $this->api->applications()->ids)) { $hasOS = true; $ba['app_id'] = $oa['app_id']; } if (!$hasOS) { - throw new InvalidParameterException("A Valid OS (os_id, iso_id, snapshot_id or app_id) is missing"); + throw new InvalidParameterException("At least one OS parameter (os_id, iso_id, snapshot_id or app_id) is missing"); } (isset($oa['ipxe_chain_url'])) ? $ba['ipxe_chain_url'] = $oa['ipxe_chain_url'] : null; (isset($oa['label'])) ? $ba['label'] = $oa['label'] : null; @@ -210,7 +211,7 @@ public function loadInstances() * * @param $oa array * - * @return void + * @return Stream * */ public function updateInstance($oa) @@ -280,7 +281,7 @@ public function updateInstance($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function haltInstances($oa) @@ -299,7 +300,7 @@ public function haltInstances($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function rebootInstances($oa) @@ -318,7 +319,7 @@ public function rebootInstances($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function startInstances($oa) @@ -337,7 +338,7 @@ public function startInstances($oa) * * @param $inst string * - * @return void + * @return Stream * */ public function startInstance($inst) @@ -352,7 +353,7 @@ public function startInstance($inst) * * @param $inst string * - * @return void + * @return Stream * */ public function rebootInstance($inst) @@ -367,7 +368,7 @@ public function rebootInstance($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function reinstallInstance($oa) @@ -388,7 +389,7 @@ public function reinstallInstance($oa) * * @param $inst string * - * @return void + * @return Stream * */ public function instanceBandwidth($inst) @@ -403,7 +404,7 @@ public function instanceBandwidth($inst) * * @param $inst string * - * @return void + * @return Stream * */ public function getInstanceNeighbors($inst) @@ -418,7 +419,7 @@ public function getInstanceNeighbors($inst) * * @param $inst string * - * @return void + * @return Stream * */ public function listInstancePrivateNetworks($inst) @@ -433,7 +434,7 @@ public function listInstancePrivateNetworks($inst) * * @param $inst string * - * @return void + * @return Stream * */ public function getInstanceISOStatus($inst) @@ -448,7 +449,7 @@ public function getInstanceISOStatus($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function attachISOToInstance($oa) @@ -468,7 +469,7 @@ public function attachISOToInstance($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function detachISOFromInstance($inst) @@ -483,7 +484,7 @@ public function detachISOFromInstance($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function attachPrivateNetworkToInstance($oa) @@ -504,7 +505,7 @@ public function attachPrivateNetworkToInstance($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function detachPrivateNetworkFromInstance($oa) @@ -525,7 +526,7 @@ public function detachPrivateNetworkFromInstance($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function setInstanceBackupSchedule($oa) @@ -579,7 +580,7 @@ public function setInstanceBackupSchedule($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function getInstanceBackupSchedule($inst) @@ -594,7 +595,7 @@ public function getInstanceBackupSchedule($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function restoreInstance($oa) @@ -622,7 +623,7 @@ public function restoreInstance($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function listInstanceIPv4Information($inst) @@ -637,7 +638,7 @@ public function listInstanceIPv4Information($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function createIPv4($oa) @@ -659,7 +660,7 @@ public function createIPv4($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function getInstanceIPv6Information($inst) @@ -674,7 +675,7 @@ public function getInstanceIPv6Information($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function createInstanceReverseIPv6($oa) @@ -698,7 +699,7 @@ public function createInstanceReverseIPv6($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function listInstanceIPv6Reverse($inst) @@ -713,7 +714,7 @@ public function listInstanceIPv6Reverse($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function createInstanceReverseIPv4($oa) @@ -737,7 +738,7 @@ public function createInstanceReverseIPv4($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function getInstanceUserData($inst) @@ -753,7 +754,7 @@ public function getInstanceUserData($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function haltInstance($inst) @@ -768,7 +769,7 @@ public function haltInstance($inst) * * @param $oa array * - * @return void + * @return Stream * */ public function setDefaultReverseDNSEntry($oa) @@ -791,7 +792,7 @@ public function setDefaultReverseDNSEntry($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function deleteIPv4Address($oa) @@ -812,7 +813,7 @@ public function deleteIPv4Address($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function deleteInstanceReverseIPv6($oa) @@ -833,7 +834,7 @@ public function deleteInstanceReverseIPv6($oa) * * @param $oa array * - * @return void + * @return Stream * */ public function getAvailableInstanceUpgrades($inst) From c1f7c821c653b0cc7ce278a06e7464d92da8ff5c Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Tue, 9 Feb 2021 18:50:25 -0800 Subject: [PATCH 15/17] Fix startup script creation api location, and use base64 encoding to conform to api requirements. --- src/StartupScripts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/StartupScripts.php b/src/StartupScripts.php index d47e94c..fc100bd 100644 --- a/src/StartupScripts.php +++ b/src/StartupScripts.php @@ -188,9 +188,9 @@ public function createStartupScript($oa) if (!isset($oa['script'])) { throw new InvalidParameterException("Startup Script Missing"); } else { - $ba['script'] = $oa['script']; + $ba['script'] = base64_encode($oa['script']); } $body = json_encode($ba); - return $this->api->makeAPICall('POST', $this->api::SNAPSHOTS_URL, $body); + return $this->api->makeAPICall('POST', $this->api::STARTUP_SCRIPTS_URL, $body); } } From 958b2fd0efc20debcb63e9cb24ea4474cea88636 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Tue, 9 Feb 2021 20:43:19 -0800 Subject: [PATCH 16/17] Fix typo in exception --- src/Instances.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Instances.php b/src/Instances.php index b541ada..4225409 100644 --- a/src/Instances.php +++ b/src/Instances.php @@ -162,7 +162,7 @@ public function createInstance($oa) if (isset($oa['script_id']) && in_array($oa['script_id'], $this->api->startupScripts()->ids)) { $ba['script_id'] = $oa['script_id']; } elseif (isset($oa['script_id']) && !in_array($oa['script_id'], $this->api->startupScripts()->ids)) { - throw new InvalidParameterException("You provided an Startup Script and it's not part of your account"); + throw new InvalidParameterException("You provided a Startup Script and it's not part of your account"); } if (isset($oa['firewall_group_id']) && in_array($oa['firewall_group_id'], $this->api->firewalls()->ids)) { $ba['firewall_group_id'] = $oa['firewall_group_id']; From 64facf1fcd092c28b2c433c4246a292ef16bd4f0 Mon Sep 17 00:00:00 2001 From: Jason Ho Date: Tue, 9 Feb 2021 20:43:37 -0800 Subject: [PATCH 17/17] Fix typo in startup script loading --- src/StartupScripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StartupScripts.php b/src/StartupScripts.php index fc100bd..2712605 100644 --- a/src/StartupScripts.php +++ b/src/StartupScripts.php @@ -132,7 +132,7 @@ public function loadStartupScripts() foreach ($sa['startup_scripts'] as $startup) { $id = $startup['id']; $this->ids[] = $id; - $this->startupScritps[$id] = $startup; + $this->startupScripts[$id] = $startup; } $this->total_startup_scripts = $sa['meta']['total']; }