diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/digitalocean-api.iml b/.idea/digitalocean-api.iml new file mode 100644 index 0000000..3919d1b --- /dev/null +++ b/.idea/digitalocean-api.iml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0bd959c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..613c06f --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml new file mode 100644 index 0000000..4f8104c --- /dev/null +++ b/.idea/phpunit.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/composer.json b/composer.json index 3db5f4f..5ce1cd5 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,8 @@ "DigitaloceanApi\\Providers\\ConfigServiceProvider" ], "aliases": { - "Domains": "DigitaloceanApi\\Facades\\DomainFacade" + "Domains": "DigitaloceanApi\\Facades\\DomainFacade", + "Droplets": "DigitaloceanApi\\Facades\\DropletFacade" } } }, diff --git a/config/digital-ocean.php b/config/digital-ocean.php index cf8efc3..aee7f97 100644 --- a/config/digital-ocean.php +++ b/config/digital-ocean.php @@ -3,6 +3,7 @@ return [ 'token' => env('TOKEN', ''), 'api-urls' => [ - 'domains' => 'https://api.digitalocean.com/v2/domains' + 'domains' => 'https://api.digitalocean.com/v2/domains', + 'droplets' => 'https://api.digitalocean.com/v2/droplets' ], ]; \ No newline at end of file diff --git a/src/Facades/DropletFacade.php b/src/Facades/DropletFacade.php new file mode 100644 index 0000000..a2d2a3b --- /dev/null +++ b/src/Facades/DropletFacade.php @@ -0,0 +1,14 @@ +getStoreRules()); + + if($validator->fails()) { + return $validator->errors(); + } + return $this->sendRequestService->send('POST', $this->domainsApiUrl, ['json' => $params]); } /** - * @param $name + * @param string $name * * @return mixed|\stdClass + * + * @throws \GuzzleHttp\Exception\GuzzleException */ - public function show($name) + public function show(string $name) { return $this->sendRequestService->send('GET', "{$this->domainsApiUrl}/{$name}"); } /** - * @param $name + * @param string $name * * @return mixed|\stdClass + + * @throws \GuzzleHttp\Exception\GuzzleException */ - public function destroy($name) + public function destroy(string $name) { return $this->sendRequestService->send('DELETE', "{$this->domainsApiUrl}/${name}"); } + + /** + * @return string[] + */ + private function getStoreRules() + { + return [ + 'name' => 'required|string|max:255', + 'ip_address' => 'nullable|string', + ]; + } } \ No newline at end of file diff --git a/src/Services/DropletService.php b/src/Services/DropletService.php new file mode 100644 index 0000000..df4f323 --- /dev/null +++ b/src/Services/DropletService.php @@ -0,0 +1,113 @@ +domainsApiUrl = config('digital-ocean.api-urls.droplets'); + $this->sendRequestService = $sendRequestService; + } + + /** + * @param int $perPage + * @param int $page + * + * @return mixed|\stdClass + * + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function index( + int $perPage = 20, + int $page = 1 + ) { + $repuestParams = [ + 'json' =>[ + 'per_page' => $perPage, + 'page' => $page, + ], + ]; + + return $this->sendRequestService->send('GET', $this->domainsApiUrl, $repuestParams); + } + + /** + * @param array $params + * + * @return \Illuminate\Support\MessageBag|mixed|\stdClass + * + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function store(array $params) + { + $validator = Validator::make($params, $this->getStoreRules()); + + if($validator->fails()) { + return $validator->errors(); + } + + return $this->sendRequestService->send('POST', $this->domainsApiUrl, ['json' => $params]); + } + + /** + * @param int $dropletId + * + * @return mixed|\stdClass + * + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function show(int $dropletId) + { + return $this->sendRequestService->send('GET', "{$this->domainsApiUrl}/{$dropletId}"); + } + + /** + * @param int $dropletId + * + * @return mixed|\stdClass + * + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function destroy(int $dropletId) + { + return $this->sendRequestService->send('DELETE', "{$this->domainsApiUrl}/{$dropletId}"); + } + + /** + * @return string[] + */ + private function getStoreRules() + { + return [ + 'name' => 'required|string', + 'region' => 'required|string', + 'size' => 'required|string', + 'image' => 'required|regex:/^[a-zA-Z0-9\s]+$/', + 'ssh_keys' => 'nullable|array', + 'backups' => 'nullable|boolean', + 'ipv6' => 'nullable|boolean', + 'monitoring' => 'nullable|boolean', + 'tags' => 'nullable|array', + 'user_data' => 'nullable|string', + 'vpc_uuid' => 'nullable|string', + 'with_droplet_agent' => 'nullable|boolean', + ]; + } +} \ No newline at end of file diff --git a/tests/Feature/Domains/TestDomainService.php b/tests/Feature/Domains/TestDomainService.php index 3fb622b..f5f7eb6 100644 --- a/tests/Feature/Domains/TestDomainService.php +++ b/tests/Feature/Domains/TestDomainService.php @@ -4,8 +4,6 @@ use Tests\TestCase; use DigitaloceanApi\Services\DomainService; -use function PHPUnit\Framework\assertTrue; -use Illuminate\Support\Facades\Log; class TestDomainService extends TestCase { diff --git a/tests/Feature/Droplets/TestDropletService.php b/tests/Feature/Droplets/TestDropletService.php new file mode 100644 index 0000000..8686789 --- /dev/null +++ b/tests/Feature/Droplets/TestDropletService.php @@ -0,0 +1,23 @@ +service = $this->app->make(DropletService::class); + } + + public function testIndex() + { + $result = $this->service->index(); + file_put_contents("tests/logs/DropletServiceLog.log", json_encode($result).PHP_EOL, FILE_APPEND | LOCK_EX); + } +} \ No newline at end of file