nodejs/influxdb-cli 操作org bucket user APIToken

本文详细介绍了如何使用Node.js操作InfluxDB,包括创建组织、bucket、用户,以及为用户分配APIToken,涉及了InfluxDBAPI的不同部分,如orgs,buckets,users和authorizations的CRUD操作。
摘要由CSDN通过智能技术生成

nodejs 操作influxdb

const { OrgsAPI, BucketsAPI, UsersAPI, AuthorizationsAPI } = require( '@influxdata/influxdb-client-apis');

const { InfluxDB } = require('@influxdata/influxdb-client');

const uuid = require('uuid');

const influxHost = “127.0.0.1”;

const influxPort = 27017;

const url = `http://${influxHost}:${influxPort}`;

const token = 'zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw==';

const influxDB = new InfluxDB({url, token});

const name = `ecmaster`; //orgName

// org create

const orgsAPI = new OrgsAPI(influxDB);

const organization = await orgsAPI.postOrgs({body: {name}});

const orgID = organization.id;

//bucket create

const bucketsAPI = new BucketsAPI(influxDB);

const bucket = await bucketsAPI.postBuckets({body: {orgID, name }});

// user create

const usersAPI = new UsersAPI(influxDB);

const user = await usersAPI.postUsers({body: {name}});

const userID = user.id;

const password = uuid.v4();

await usersAPI.postUsersIDPassword({userID, body: {password}});

await orgsAPI.postOrgsIDOwners({ orgID, body: { id: user.id } }); // 当前orgID的所有者,拥有此org中所有资源的权限,没有org的write权限。 此用户登陆web界面创建的API Token分配的资源权限均是此org的。

//APIToken create

/*

API Token permission字段相关参数解释

[name] 可选: 如果设置了名称,则是该名称的资源的权限。如果未设置,则为该资源类型的所有资源的权限。

[id] 可选: 如果设置了 ID,则是 该 ID 资源的权限。如果未设置,则为该资源类型的所有资源的权限。

[orgID] 可选: 如果设置了 orgID,则这是该orgID组织拥有的所有资源的权限。如果未设置,则为该资源类型的所有资源的权限。

 type 必填: "authorizations" "buckets" "dashboards" "orgs" "sources" "tasks" "telegrafs" "users" "variables" "scrapers" "secrets" "labels" "views" "documents" "notificationRules" "notificationEndpoints" "checks" "dbrp" "notebooks" "annotations" "remotes" "replications"

*/

const authorizationsAPI = new AuthorizationsAPI(influxDB);

const apiToken = await authorizationsAPI.postAuthorizations({ body : {orgID, userID: user.id, permissions: [

  {

    "action": "read",

    "resource": {

      "orgID": `${orgID}`,

      "type": "authorizations"

    }

  },

  {

    "action": "read",

    "resource": {

      "orgID": `${orgID}`,

      "type": "buckets"

    }

  },

  {

    "action": "read",

    "resource": {

      "orgID": `${orgID}`,

      "type": "users"

    }

  },

  {

    "action": "write",

    "resource": {

      "orgID": `${orgID}`,

      "type": "buckets"

    }

  }

]}

});

const influxSetting= {

  org: organization.name,

  bucket: bucket.name,

  url,

  token: apiToken.token,

  username: user.name,

  password: password

}    

console.log(influxSetting);

influxdb-cli

mac安装:

brew install influxdb-cli

influx config create --config-name onboarding \

    --host-url "http://ip:8086" \

    --token "zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw==" \

    --active

influx config list  

influx config delete onboarding

org相关操作

influx org list 查询 默认limit只显示20个。

influx org list --name pppp_ecmaster //超过20个指定name可查询到

influx org delete -i 07668febbebcbbbd //删除org -i 指定id

bucket相关操作

influx bucket list -o undefined_ecmaster //查看指定org的bucket

influx bucket delete -i 6d10e05825be0228 -o undefined_ecmaster

API Token相关操作

influx auth list -o ecmaster //查看指定org的APIToken

user相关操作

influx user list  查询 默认limit只显示20个

influx user list --name pppp_ecmaster //超过20个 指定name才可查询到

influx user delete -i 0ba14fd3b1efe000 //删除用户 指定id

读写数据

influx write --bucket undefined_ecmaster -o undefined_ecmaster  --url https://influx-testdata.s3.amazonaws.com/air-sensor-data-annotated.csv //往bucket中写入数据

influx query -o undefined_ecmaster 'from(bucket:"undefined_ecmaster") |> range(start:-1d,stop:-1ms)' //读取数据

curl方式调用

查询org
curl --request GET "ip:8086/api/v2/orgs" \

    --header "Authorization: Token zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw=="

新增 org

curl --request POST "http://ip:8086/api/v2/orgs" \

  --header "Authorization: Token zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw==" \

  --header "Accept: application/json" \

  --header "Content-Type: application/json" \

  --data '{"name": "personal_ecmaster1" }'

删除org

curl --request DELETE "ip:8086/api/v2/orgs/f70722a047489640" \

    --header "Authorization: Token zTQfDoeRo6C92S_6qjIhvq4KlDp9o-a8wgTMOCBY5Qb-DYVL4WIKr533QtbLgebwLmxtnYscFrR_9fCEEbsvmw=="

接口文档:https://docs.influxdata.com/influxdb/v2/api/#operation/DeleteAuthorizationsID

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李庆政370

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值