-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathManagementExample.cs
51 lines (41 loc) · 1.78 KB
/
ManagementExample.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using Task = System.Threading.Tasks.Task;
namespace Examples
{
public static class ManagementExample
{
public static async Task Main()
{
const string url = "http://localhost:8086";
const string token = "my-token";
const string org = "my-org";
using var client = new InfluxDBClient(url, token);
// Find ID of Organization with specified name (PermissionAPI requires ID of Organization).
var orgId = (await client.GetOrganizationsApi().FindOrganizationsAsync(org: org)).First().Id;
//
// Create bucket "iot_bucket" with data retention set to 3,600 seconds
//
var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, 3600);
var bucket = await client.GetBucketsApi().CreateBucketAsync("iot_bucket", retention, orgId);
//
// Create access token to "iot_bucket"
//
var resource = new PermissionResource(PermissionResource.TypeBuckets, bucket.Id, null,
orgId);
// Read permission
var read = new Permission(Permission.ActionEnum.Read, resource);
// Write permission
var write = new Permission(Permission.ActionEnum.Write, resource);
var authorization = await client.GetAuthorizationsApi()
.CreateAuthorizationAsync(orgId, new List<Permission> { read, write });
//
// Created token that can be use for writes to "iot_bucket"
//
Console.WriteLine($"Authorized token to write into iot_bucket: {authorization.Token}");
}
}
}