From b5cb30a50d07a082533753840441d00452bbbced Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 08:19:51 +0200 Subject: [PATCH] Added database management for HTTP client --- scripts/influxdb_conf.toml | 2 +- spec/InfluxDB/Adapter/GuzzleAdapterSpec.php | 26 +++++++++++ src/InfluxDB/Adapter/GuzzleAdapter.php | 38 +++++++++++++--- src/InfluxDB/Adapter/QueryableInterface.php | 3 ++ src/InfluxDB/Adapter/UdpAdapter.php | 2 +- src/InfluxDB/Client.php | 24 ++++++++++ src/InfluxDB/Options.php | 16 +++++++ tests/InfluxDB/ClientTest.php | 50 +++++++++++---------- tests/bootstrap.php | 4 +- 9 files changed, 133 insertions(+), 32 deletions(-) diff --git a/scripts/influxdb_conf.toml b/scripts/influxdb_conf.toml index e0c505a7b0..4c946e0b38 100644 --- a/scripts/influxdb_conf.toml +++ b/scripts/influxdb_conf.toml @@ -57,7 +57,7 @@ read-timeout = "5s" [[input_plugins.udp_servers]] # array of tables enabled = true port = 5551 - database = "mine" + database = "udp.test" # Raft configuration [raft] diff --git a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php index 5c61ce1bbf..815fa87a7e 100644 --- a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php +++ b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php @@ -13,6 +13,7 @@ class GuzzleAdapterSpec extends ObjectBehavior function let(Client $client, Options $options) { $options->getHttpSeriesEndpoint()->willReturn("localhost"); + $options->getHttpDatabaseEndpoint()->willReturn("localhost"); $options->getUsername()->willReturn("one"); $options->getPassword()->willReturn("two"); $this->beConstructedWith($client, $options); @@ -61,4 +62,29 @@ class GuzzleAdapterSpec extends ObjectBehavior )->willReturn(new Response(200, [], null)); $this->query("select * from tcp.test", "s")->shouldReturn(null); } + + function it_should_list_all_databases(Client $client, Options $options) + { + $client->get( + "localhost", + [ + "auth" => ["one", "two"] + ] + )->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null)); + + $this->getDatabases()->shouldReturn(null); + } + + function it_should_create_a_new_database(Client $client, Options $options) + { + $client->post( + "localhost", + [ + "auth" => ["one", "two"], + "body" => json_encode(["name" => "db_name"]) + ] + )->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null)); + + $this->createDatabase("db_name")->shouldReturn(null); + } } diff --git a/src/InfluxDB/Adapter/GuzzleAdapter.php b/src/InfluxDB/Adapter/GuzzleAdapter.php index bd715057bf..45ad43ea38 100644 --- a/src/InfluxDB/Adapter/GuzzleAdapter.php +++ b/src/InfluxDB/Adapter/GuzzleAdapter.php @@ -42,11 +42,39 @@ class GuzzleAdapter implements AdapterInterface, QueryableInterface } $endpoint = $this->options->getHttpSeriesEndpoint(); - try { + return $this->httpClient->get($endpoint, $options)->json(); - } catch (\Exception $e) { - var_dump((string)$e->getResponse()->getBody(true)); - die(); - } + } + + public function getDatabases() + { + $options = [ + "auth" => [$this->options->getUsername(), $this->options->getPassword()], + ]; + + $endpoint = $this->options->getHttpDatabaseEndpoint(); + + return $this->httpClient->get($endpoint, $options)->json(); + } + + public function createDatabase($name) + { + $httpMessage = [ + "auth" => [$this->options->getUsername(), $this->options->getPassword()], + "body" => json_encode(["name" => $name]) + ]; + + $endpoint = $this->options->getHttpDatabaseEndpoint(); + return $this->httpClient->post($endpoint, $httpMessage)->json(); + } + + public function deleteDatabase($name) + { + $httpMessage = [ + "auth" => [$this->options->getUsername(), $this->options->getPassword()], + ]; + + $endpoint = $this->options->getHttpDatabaseEndpoint($name); + return $this->httpClient->delete($endpoint, $httpMessage)->json(); } } diff --git a/src/InfluxDB/Adapter/QueryableInterface.php b/src/InfluxDB/Adapter/QueryableInterface.php index fa604b069d..164f30bf49 100644 --- a/src/InfluxDB/Adapter/QueryableInterface.php +++ b/src/InfluxDB/Adapter/QueryableInterface.php @@ -4,4 +4,7 @@ namespace InfluxDb\Adapter; interface QueryableInterface { public function query($query, $timePrecision = false); + public function getDatabases(); + public function createDatabase($name); + public function deleteDatabase($name); } diff --git a/src/InfluxDB/Adapter/UdpAdapter.php b/src/InfluxDB/Adapter/UdpAdapter.php index e2dcf8a515..88827173f9 100644 --- a/src/InfluxDB/Adapter/UdpAdapter.php +++ b/src/InfluxDB/Adapter/UdpAdapter.php @@ -14,8 +14,8 @@ class UdpAdapter implements AdapterInterface public function send($message) { - $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $message = json_encode($message); + $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_sendto($socket, $message, strlen($message), 0, $this->options->getHost(), $this->options->getPort()); socket_close($socket); } diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 2b90170250..6971d71ba9 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -41,6 +41,30 @@ class Client return $this->getAdapter()->query($query, $timePrecision); } + public function getDatabases() + { + if (!($this->getAdapter() instanceOf QueryableInterface)) { + throw new \BadMethodCallException("You can query the database only if the adapter supports it!"); + } + return $this->getAdapter()->getDatabases(); + } + + public function createDatabase($name) + { + if (!($this->getAdapter() instanceOf QueryableInterface)) { + throw new \BadMethodCallException("You can query the database only if the adapter supports it!"); + } + return $this->getAdapter()->createDatabase($name); + } + + public function deleteDatabase($name) + { + if (!($this->getAdapter() instanceOf QueryableInterface)) { + throw new \BadMethodCallException("You can query the database only if the adapter supports it!"); + } + return $this->getAdapter()->deleteDatabase($name); + } + private function clearTimePrecision($timePrecision) { switch ($timePrecision) { diff --git a/src/InfluxDB/Options.php b/src/InfluxDB/Options.php index 02ff5ef44a..a9478ba443 100644 --- a/src/InfluxDB/Options.php +++ b/src/InfluxDB/Options.php @@ -97,4 +97,20 @@ class Options $this->getDatabase() ); } + + public function getHttpDatabaseEndpoint($name = false) + { + $url = sprintf( + "%s://%s:%d/db", + $this->getProtocol(), + $this->getHost(), + $this->getPort() + ); + + if ($name !== false) { + $url .= "/{$name}"; + } + + return $url; + } } diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index e4d5b20a17..5d0dc8af12 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -5,7 +5,6 @@ use InfluxDB\Adapter\GuzzleAdapter as InfluxHttpAdapter; use InfluxDB\Options; use InfluxDB\Adapter\UdpAdapter; use GuzzleHttp\Client as GuzzleHttpClient; -use crodas\InfluxPHP\Client as Crodas; class ClientTest extends \PHPUnit_Framework_TestCase { @@ -20,28 +19,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options = include __DIR__ . '/../bootstrap.php'; $this->rawOptions = $options; - $client = new Crodas( - $options["tcp"]["host"], - $options["tcp"]["port"], - $options["tcp"]["username"], - $options["tcp"]["password"] - ); - try { - $client->deleteDatabase($options["tcp"]["database"]); - } catch (\Exception $e) { - // nothing... - } - $client->createDatabase($options["tcp"]["database"]); - - try { - $client->deleteDatabase($options["udp"]["database"]); - } catch (\Exception $e) { - // nothing... - } - $client->createDatabase($options["udp"]["database"]); - - $this->anotherClient = $client; - $tcpOptions = $options["tcp"]; $options = new Options(); @@ -59,6 +36,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase $influx = new Client(); $influx->setAdapter($adapter); $this->object = $influx; + + $databases = $this->object->getDatabases(); + foreach ($databases as $database) { + $this->object->deleteDatabase($database["name"]); + } + + $this->object->createDatabase($this->rawOptions["udp"]["database"]); + $this->object->createDatabase($this->rawOptions["tcp"]["database"]); } /** @@ -122,6 +107,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase { $rawOptions = $this->rawOptions; $options = new Options(); + $options->setHost($rawOptions["udp"]["host"]); $options->setUsername($rawOptions["udp"]["username"]); $options->setPassword($rawOptions["udp"]["password"]); $options->setPort($rawOptions["udp"]["port"]); @@ -138,9 +124,27 @@ class ClientTest extends \PHPUnit_Framework_TestCase // Wait UDP/IP message arrives usleep(200e3); + $this->options->setDatabase("udp.test"); $body = $this->object->query("select * from udp.test"); $this->assertCount(4, $body[0]["points"]); $this->assertEquals("udp.test", $body[0]["name"]); } + + public function testListActiveDatabses() + { + $databases = $this->object->getDatabases(); + + $this->assertCount(2, $databases); + } + + public function testCreateANewDatabase() + { + $this->object->createDatabase("walter"); + $databases = $this->object->getDatabases(); + + $this->assertCount(3, $databases); + + $this->object->deleteDatabase("walter"); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9dd58c2cca..99b776deff 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -4,14 +4,14 @@ return [ "host" => "localhost", "port" => 8086, "protocol" => "http", - "database" => "mine", + "database" => "tcp.test", "username" => "root", "password" => "root", ], "udp" => [ "host" => "localhost", "port" => 5551, - "database" => "mine", + "database" => "udp.test", "username" => "root", "password" => "root" ],