-
Notifications
You must be signed in to change notification settings - Fork 70
/
createBucket.mjs
executable file
·58 lines (53 loc) · 1.84 KB
/
createBucket.mjs
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
#!/usr/bin/env node
/*
This example creates a new bucket. If a bucket of the same name already exists,
it is deleted and then created again.
*/
import {InfluxDB, HttpError} from '@influxdata/influxdb-client'
import {OrgsAPI, BucketsAPI} from '@influxdata/influxdb-client-apis'
import {url, org, token} from './env.mjs'
const influxDB = new InfluxDB({url, token})
async function recreateBucket(name) {
console.log('*** Get organization by name ***')
const orgsAPI = new OrgsAPI(influxDB)
const organizations = await orgsAPI.getOrgs({org})
if (!organizations || !organizations.orgs || !organizations.orgs.length) {
console.error(`No organization named "${org}" found!`)
}
const orgID = organizations.orgs[0].id
console.log(`Using organization "${org}" identified by "${orgID}"`)
console.log('*** Get buckets by name ***')
const bucketsAPI = new BucketsAPI(influxDB)
try {
const buckets = await bucketsAPI.getBuckets({orgID, name})
if (buckets && buckets.buckets && buckets.buckets.length) {
console.log(`Bucket named "${name}" already exists"`)
const bucketID = buckets.buckets[0].id
console.log(`*** Delete Bucket "${name}" identified by "${bucketID}" ***`)
await bucketsAPI.deleteBucketsID({bucketID})
}
} catch (e) {
if (e instanceof HttpError && e.statusCode == 404) {
// OK, bucket not found
} else {
throw e
}
}
console.log(`*** Create Bucket "${name}" ***`)
// creates a bucket, entity properties are specified in the "body" property
const bucket = await bucketsAPI.postBuckets({body: {orgID, name}})
console.log(
JSON.stringify(
bucket,
(key, value) => (key === 'links' ? undefined : value),
2
)
)
}
try {
await recreateBucket('example-bucket')
console.log('\nFinished SUCCESS')
} catch (e) {
console.error(e)
console.log('\nFinished ERROR')
}