Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jesper Dolieslager
committed
Mar 29, 2015
1 parent
549ef4d
commit 375d507
Showing
21 changed files
with
1,317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
namespace Aviogram\InfluxDB\Api; | ||
|
||
use Aviogram\InfluxDB\Entity; | ||
use Aviogram\InfluxDB\ClientRequest; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
|
||
abstract class AbstractApi | ||
{ | ||
/** | ||
* @var ClientRequest | ||
*/ | ||
private $request; | ||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param ClientRequest $request | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct(ClientRequest $request, LoggerInterface $logger = null) | ||
{ | ||
$this->request = $request; | ||
$this->logger = $logger ?: new NullLogger(); | ||
} | ||
|
||
/** | ||
* @return ClientRequest | ||
*/ | ||
protected function getRequest() | ||
{ | ||
return $this->request; | ||
} | ||
|
||
/** | ||
* Abstract method for querying InfluxDB | ||
* | ||
* @param string $query | ||
* | ||
* @return Entity\QueryResult | ||
*/ | ||
protected function query($query) | ||
{ | ||
$result = $this->getRequest() | ||
->request('/query', $this->getRequest()->query()->addQueryPart('q', $query)) | ||
->current(); | ||
|
||
// Log the error | ||
if ($result->hasError() === true) { | ||
$this->getLogger()->error($result->getError()); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @return LoggerInterface | ||
*/ | ||
protected function getLogger() | ||
{ | ||
return $this->logger; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
namespace Aviogram\InfluxDB\Api; | ||
|
||
use Aviogram\InfluxDB\Collection; | ||
use Aviogram\InfluxDB\Entity; | ||
|
||
class Database extends AbstractApi | ||
{ | ||
/** | ||
* Creates a database by InfluxDB | ||
* | ||
* @param $database | ||
* | ||
* @return boolean | ||
*/ | ||
public function create($database) | ||
{ | ||
$result = $this->query('CREATE DATABASE ' . $database); | ||
|
||
return ($result->hasError() !== false); | ||
} | ||
|
||
/** | ||
* Delete a database from the InfluxDB | ||
* | ||
* @param $database | ||
* | ||
* @return bool | ||
*/ | ||
public function delete($database) | ||
{ | ||
$result = $this->query('DROP DATABASE ' . $database); | ||
|
||
return ($result->hasError() !== false); | ||
} | ||
|
||
/** | ||
* Checks if an database exists | ||
* | ||
* @param string $database | ||
* @return bool | ||
*/ | ||
public function hasDatabase($database) | ||
{ | ||
$list = $this->getList(); | ||
|
||
foreach ($list as $item) { | ||
if ($item->getName() === $database) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Return list of databases | ||
* | ||
* @return Collection\Database | ||
*/ | ||
public function getList() | ||
{ | ||
$result = $this->query('SHOW DATABASES'); | ||
$return = new Collection\Database(); | ||
|
||
foreach ($result->getSeries() as $row) { | ||
foreach ($row->getValues() as $values) { | ||
$database = new Entity\Database($values->current()); | ||
$return->append($database); | ||
} | ||
} | ||
|
||
return $return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
namespace Aviogram\InfluxDB\Api; | ||
|
||
class Privilege extends AbstractApi | ||
{ | ||
CONST TYPE_GRANT = 'GRANT', | ||
TYPE_REVOKE = 'REVOKE'; | ||
|
||
CONST PRIVILEGE_ALL = 'ALL', | ||
PRIVILEGE_READ = 'READ', | ||
PRIVILEGE_WRITE = 'WRITE'; | ||
|
||
/** | ||
* Grant a user read rights on the given database | ||
* | ||
* @param $username | ||
* @param $database | ||
* | ||
* @return bool | ||
*/ | ||
public function grantReadRight($username, $database) | ||
{ | ||
return $this->setPrivilege(static::TYPE_GRANT, static::PRIVILEGE_READ, $username, $database); | ||
} | ||
|
||
/** | ||
* Revoke a user read rights on the given database | ||
* | ||
* @param $username | ||
* @param $database | ||
* | ||
* @return bool | ||
*/ | ||
public function revokeReadRight($username, $database) | ||
{ | ||
return $this->setPrivilege(static::TYPE_REVOKE, static::PRIVILEGE_READ, $username, $database); | ||
} | ||
|
||
/** | ||
* Grant a user read write on the given database | ||
* | ||
* @param $username | ||
* @param $database | ||
* | ||
* @return bool | ||
*/ | ||
public function grantWriteRight($username, $database) | ||
{ | ||
return $this->setPrivilege(static::TYPE_GRANT, static::PRIVILEGE_WRITE, $username, $database); | ||
} | ||
|
||
/** | ||
* Revoke a user write rights on the given database | ||
* | ||
* @param $username | ||
* @param $database | ||
* | ||
* @return bool | ||
*/ | ||
public function revokeWriteRight($username, $database) | ||
{ | ||
return $this->setPrivilege(static::TYPE_REVOKE, static::PRIVILEGE_WRITE, $username, $database); | ||
} | ||
|
||
/** | ||
* Grant a user all rights on the given database | ||
* | ||
* @param $username | ||
* @param $database | ||
* | ||
* @return bool | ||
*/ | ||
public function grantAllRights($username, $database) | ||
{ | ||
return $this->setPrivilege(static::TYPE_GRANT, static::PRIVILEGE_ALL, $username, $database); | ||
} | ||
|
||
/** | ||
* Revoke a user all rights on the given database | ||
* | ||
* @param $username | ||
* @param $database | ||
* | ||
* @return bool | ||
*/ | ||
public function revokeAllRights($username, $database) | ||
{ | ||
return $this->setPrivilege(static::TYPE_REVOKE, static::PRIVILEGE_ALL, $username, $database); | ||
} | ||
|
||
/** | ||
* Grant administrator rights for the given user | ||
* | ||
* @param $username | ||
* | ||
* @return bool | ||
*/ | ||
public function grantAdminRights($username) | ||
{ | ||
return $this->setPrivilege(static::TYPE_GRANT, static::PRIVILEGE_ALL, $username); | ||
} | ||
|
||
/** | ||
* Revoke administrator rights for the given user | ||
* | ||
* @param $username | ||
* | ||
* @return bool | ||
*/ | ||
public function revokeAdminRights($username) | ||
{ | ||
return $this->setPrivilege(static::TYPE_REVOKE, static::PRIVILEGE_ALL, $username); | ||
} | ||
|
||
/** | ||
* Generic method for the privileges | ||
* | ||
* @param string $type | ||
* @param string $privilege | ||
* @param string $username | ||
* @param string | null $database | ||
* | ||
* @return boolean | ||
*/ | ||
protected function setPrivilege($type, $privilege, $username, $database = null) | ||
{ | ||
$parts = array($type, $privilege); | ||
|
||
if ($database !== null) { | ||
$parts[] = "ON {$database}"; | ||
} | ||
|
||
if ($type === static::TYPE_GRANT) { | ||
$parts[] = "TO {$username}"; | ||
} else if ($type === static::TYPE_REVOKE) { | ||
$parts[] = "FROM {$username}"; | ||
} | ||
|
||
$query = implode(' ', $parts); | ||
|
||
echo $query . PHP_EOL; | ||
|
||
return ($this->query($query)->hasError() === false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
namespace Aviogram\InfluxDB\Api; | ||
|
||
class Retention extends AbstractApi | ||
{ | ||
const DURATION_INFINITE = 'INF'; | ||
|
||
/** | ||
* Create a new retention policy for the given database | ||
* | ||
* @param string $name | ||
* @param string $database | ||
* @param string $duration Syntax: (90m, 1h, 1d, 1w) means (90 minutes, 1 hour, 1 day, 1 week) | ||
* @param bool $useAsDefault | ||
* | ||
* @return bool | ||
*/ | ||
public function createPolicy( | ||
$name, | ||
$database, | ||
$duration = self::DURATION_INFINITE, | ||
$useAsDefault = false | ||
) { | ||
if (preg_match('/^([0-9]+(m|h|d|w)|INF)$/', $duration) === 0) { | ||
$this->getLogger()->error("Duration syntax is invalid. Given {$duration}, should be [0-9]+(m|h|d|w) or INF."); | ||
|
||
return false; | ||
} | ||
|
||
$query = "CREATE RETENTION POLICY {$name} ON {$database} DURATION {$duration} REPLICATION 1"; | ||
if ($useAsDefault === true) { | ||
$query .= ' DEFAULT'; | ||
} | ||
|
||
return ($this->query($query)->hasError() === false); | ||
} | ||
|
||
public function changePolicy() | ||
{ | ||
|
||
} | ||
|
||
public function getPolicies($database) | ||
{ | ||
$query = "SHOW RETENTION POLICIES {$database}"; | ||
|
||
var_dump($this->query($query)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
namespace Aviogram\InfluxDB\Api; | ||
|
||
use Aviogram\InfluxDB\Collection; | ||
use Aviogram\InfluxDB\Entity; | ||
|
||
class User extends AbstractApi | ||
{ | ||
/** | ||
* Create a new user on the InfluxDB server | ||
* | ||
* @param string $username | ||
* @param string $password | ||
* @return bool | ||
*/ | ||
public function create($username, $password) | ||
{ | ||
$result = $this->query("CREATE USER {$username} WITH PASSWORD '{$password}'"); | ||
|
||
return ($result->hasError() === false); | ||
} | ||
|
||
/** | ||
* Delete an user from the InfluxDB server | ||
* | ||
* @param string $username | ||
* @return bool | ||
*/ | ||
public function delete($username) | ||
{ | ||
$result = $this->query("DROP USER {$username}"); | ||
|
||
return ($result->hasError() === false); | ||
} | ||
|
||
/** | ||
* Get a list of users in the InfluxDB Server | ||
* | ||
* @return Collection\User | ||
*/ | ||
public function getList() | ||
{ | ||
$result = $this->query("SHOW USERS"); | ||
$return = new Collection\User(); | ||
|
||
foreach ($result->getSeries() as $serie) { | ||
foreach ($serie->getValues() as $value) { | ||
$username = $value->offsetGet(0); | ||
$isAdmin = (boolean) $value->offsetGet(1); | ||
|
||
$user = new Entity\User($username, $isAdmin); | ||
|
||
$return->append($user); | ||
} | ||
} | ||
|
||
return $return; | ||
} | ||
} |
Oops, something went wrong.