-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcreate_bucket_example.dart
58 lines (46 loc) · 1.85 KB
/
create_bucket_example.dart
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
import 'package:influxdb_client/api.dart';
void main() async {
// Initialize Client and API
var client = InfluxDBClient(
url: 'http://localhost:8086', token: 'my-token', org: 'my-org');
//check server availability
await client.getPingApi().getPing();
var orgs = await client.getOrganizationsApi().getOrgs();
var myOrgId = orgs.orgs!.first.id;
var bucketsApi = client.getBucketsApi();
var bucketName = 'bucket-my-org';
// find and delete bucket 'bucket-my-org'
var buckets = await bucketsApi.getBuckets(name: bucketName);
if (buckets.buckets!.isNotEmpty) {
var bucketID = buckets.buckets!.first.id;
await bucketsApi.deleteBucketsID(bucketID!);
print('Bucket $bucketID was deleted.');
}
// Bucket configuration
var request = PostBucketRequest(
orgID: myOrgId,
name: bucketName,
retentionRules: [
RetentionRule(type: RetentionRuleTypeEnum.expire, everySeconds: 3600)
]);
var bucket = await bucketsApi.postBuckets(request);
// Create Authorization with permission to read/write created bucket
var bucketResource =
Resource(type: ResourceTypeEnum.buckets, id: bucket.id, orgID: myOrgId);
// Authorization configuration
var auth = AuthorizationPostRequest(
description: 'Authorization to read/write bucket:${bucket.name}',
orgID: myOrgId,
permissions: [
Permission(action: PermissionActionEnum.read, resource: bucketResource),
Permission(action: PermissionActionEnum.write, resource: bucketResource)
]);
// Create Authorization
var authorizationsApi = client.getAuthorizationsApi();
var authorization = await authorizationsApi.postAuthorizations(auth);
// Print token
var token = authorization.token;
print('The bucket: \'${bucket.name}\' is successfully created.');
print('The following token can be used to read/write: ${token}');
client.close();
}