forked from influxdata/influxdb-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BucketManagementExample.php
102 lines (84 loc) · 2.79 KB
/
BucketManagementExample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* Shows how to create, list and delete Buckets
*/
require __DIR__ . '/../vendor/autoload.php';
use InfluxDB2\Client;
use InfluxDB2\Model\BucketRetentionRules;
use InfluxDB2\Model\Organization;
use InfluxDB2\Model\PostBucketRequest;
use InfluxDB2\Service\BucketsService;
use InfluxDB2\Service\OrganizationsService;
$organization = 'my-org';
$bucket = 'my-bucket';
$token = 'my-token';
//
// Creating client
//
$client = new Client([
"url" => "http://localhost:8086",
"token" => $token,
"bucket" => $bucket,
"org" => $organization,
"precision" => InfluxDB2\Model\WritePrecision::S
]);
//
// Function for getting organization ID
//
function findMyOrg($client): ?Organization
{
/** @var OrganizationsService $orgService */
$orgService = $client->createService(OrganizationsService::class);
$orgs = $orgService->getOrgs()->getOrgs();
foreach ($orgs as $org) {
if ($org->getName() == $client->options["org"]) {
return $org;
}
}
return null;
}
//
// Creating bucket service
//
$bucketsService = $client->createService(BucketsService::class);
//
// Creating bucket
//
print "\n\n----------------------------------------- Bucket create -----------------------------------------\n";
$rule = new BucketRetentionRules();
$rule->setEverySeconds(3600);
$bucketName = "example-bucket-" . microtime();
$bucketRequest = new PostBucketRequest();
$bucketRequest->setName($bucketName)
->setRetentionRules([$rule])
->setOrgId(findMyOrg($client)->getId());
$respBucket = $bucketsService->postBuckets($bucketRequest);
$bucketName = $respBucket->getName();
$bucketId = $respBucket->getID();
$createdAt = $respBucket->getCreatedAt()->format('Y-m-d H:i:s');
print "ID: $bucketId Created: $createdAt Name: $bucketName was created\n";
//
// Get all buckets
//
print "\n\n----------------------------------------- Bucket List -----------------------------------------\n";
$bucketList = $bucketsService->getBuckets();
foreach ($bucketList->getBuckets() as $item) {
$bucketName = $item->getName();
$bucketId = $item->getID();
$createdAt = $item->getCreatedAt()->format('Y-m-d H:i:s');
print "ID: $bucketId Created: $createdAt Name: $bucketName \n";
}
//
// Delete all buckets contains 'example-bucket'
//
print "\n\n----------------------------------------- Bucket delete -----------------------------------------\n";
$bucketList = $bucketsService->getBuckets();
foreach ($bucketList->getBuckets() as $item) {
$bucketName = $item->getName();
if (strpos($bucketName, 'example-bucket') !== false) {
$createdAt = $item->getCreatedAt()->format('Y-m-d H:i:s');
$bucketsService->deleteBucketsID($item->getID());
print "Name: $bucketName Created: $createdAt was deleted \n";
}
}
$client->close();