Added database management for HTTP client

This commit is contained in:
Walter Dal Mut
2014-09-13 08:19:51 +02:00
parent e9a487ae2a
commit b5cb30a50d
9 changed files with 133 additions and 32 deletions
+1 -1
View File
@@ -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]
@@ -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);
}
}
+33 -5
View File
@@ -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();
}
}
@@ -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);
}
+1 -1
View File
@@ -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);
}
+24
View File
@@ -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) {
+16
View File
@@ -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;
}
}
+27 -23
View File
@@ -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");
}
}
+2 -2
View File
@@ -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"
],