From c876c18961f67594b1d6eda8748324cf6a4a09fa Mon Sep 17 00:00:00 2001 From: Aakash Hemadri Date: Sat, 13 Mar 2021 21:45:15 +0530 Subject: [PATCH] feat: Add support for setup API to influxdb_client - Check if database has default user, org, bucket - Set up initial user, org and bucket - Set up a new user, org and bucket - Add examples and test - Add necessary models Signed-off-by: Aakash Hemadri --- influxdb2_client/Cargo.toml | 2 +- influxdb2_client/examples/setup.rs | 36 ++++ influxdb2_client/src/api/mod.rs | 3 + influxdb2_client/src/{ => api}/ready.rs | 2 +- influxdb2_client/src/api/setup.rs | 194 ++++++++++++++++++ influxdb2_client/src/lib.rs | 3 +- influxdb2_client/src/models/authorization.rs | 89 ++++++++ influxdb2_client/src/models/bucket.rs | 110 ++++++++++ influxdb2_client/src/models/label.rs | 26 +++ influxdb2_client/src/models/links.rs | 24 +++ influxdb2_client/src/models/mod.rs | 24 +++ influxdb2_client/src/models/onboarding.rs | 73 +++++++ influxdb2_client/src/models/organization.rs | 103 ++++++++++ influxdb2_client/src/models/permission.rs | 23 +++ influxdb2_client/src/models/resource.rs | 74 +++++++ influxdb2_client/src/models/retention_rule.rs | 33 +++ influxdb2_client/src/models/user.rs | 82 ++++++++ 17 files changed, 898 insertions(+), 3 deletions(-) create mode 100644 influxdb2_client/examples/setup.rs create mode 100644 influxdb2_client/src/api/mod.rs rename influxdb2_client/src/{ => api}/ready.rs (95%) create mode 100644 influxdb2_client/src/api/setup.rs create mode 100644 influxdb2_client/src/models/authorization.rs create mode 100644 influxdb2_client/src/models/bucket.rs create mode 100644 influxdb2_client/src/models/label.rs create mode 100644 influxdb2_client/src/models/links.rs create mode 100644 influxdb2_client/src/models/mod.rs create mode 100644 influxdb2_client/src/models/onboarding.rs create mode 100644 influxdb2_client/src/models/organization.rs create mode 100644 influxdb2_client/src/models/permission.rs create mode 100644 influxdb2_client/src/models/resource.rs create mode 100644 influxdb2_client/src/models/retention_rule.rs create mode 100644 influxdb2_client/src/models/user.rs diff --git a/influxdb2_client/Cargo.toml b/influxdb2_client/Cargo.toml index 58e7587167..b4fc4a7354 100644 --- a/influxdb2_client/Cargo.toml +++ b/influxdb2_client/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] # In alphabetical order bytes = { version = "1.0", default-features = false } futures = { version = "0.3.5", default-features = false } -reqwest = { version = "0.11", features = ["stream"] } +reqwest = { version = "0.11", features = ["stream", "json"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.44" snafu = "0.6.6" diff --git a/influxdb2_client/examples/setup.rs b/influxdb2_client/examples/setup.rs new file mode 100644 index 0000000000..bf6d2ee5c5 --- /dev/null +++ b/influxdb2_client/examples/setup.rs @@ -0,0 +1,36 @@ +#[tokio::main] +async fn main() -> Result<(), Box> { + let influx_url = "http://localhost:8888"; + let token = "some-token"; + + let client = influxdb2_client::Client::new(influx_url, token); + + println!("{:?}", client.setup().await?); + println!( + "{:?}", + client + .setup_init( + "some-user", + "some-org", + "some-bucket", + Some("some-password".to_string()), + Some(1), + None + ) + .await? + ); + println!( + "{:?}", + client + .setup_new( + "some-new-user", + "some-new-org", + "some-new-bucket", + Some("some-new-password".to_string()), + Some(1), + None, + ) + .await? + ); + Ok(()) +} diff --git a/influxdb2_client/src/api/mod.rs b/influxdb2_client/src/api/mod.rs new file mode 100644 index 0000000000..23e4d927a1 --- /dev/null +++ b/influxdb2_client/src/api/mod.rs @@ -0,0 +1,3 @@ +//! InfluxDB v2.0 Client API +pub mod setup; +pub mod ready; diff --git a/influxdb2_client/src/ready.rs b/influxdb2_client/src/api/ready.rs similarity index 95% rename from influxdb2_client/src/ready.rs rename to influxdb2_client/src/api/ready.rs index fae9ecce94..1113cd834d 100644 --- a/influxdb2_client/src/ready.rs +++ b/influxdb2_client/src/api/ready.rs @@ -1,7 +1,7 @@ use reqwest::{Method, StatusCode}; use snafu::ResultExt; -use super::{Client, Http, RequestError, ReqwestProcessing}; +use crate::{Client, Http, RequestError, ReqwestProcessing}; impl Client { /// Get the readiness of an instance at startup diff --git a/influxdb2_client/src/api/setup.rs b/influxdb2_client/src/api/setup.rs new file mode 100644 index 0000000000..4f547fe89e --- /dev/null +++ b/influxdb2_client/src/api/setup.rs @@ -0,0 +1,194 @@ +use crate::{Client, Http, RequestError, ReqwestProcessing, Serializing}; +use reqwest::{Method, StatusCode}; +use snafu::ResultExt; + +use crate::models::{IsOnboarding, OnboardingRequest, OnboardingResponse}; + +impl Client { + /// Check if database has default user, org, bucket + pub async fn setup(&self) -> Result { + let setup_url = format!("{}/api/v2/setup", self.url); + let response = self + .request(Method::GET, &setup_url) + .send() + .await + .context(ReqwestProcessing)?; + + match response.status() { + StatusCode::OK => Ok(response + .json::() + .await + .context(ReqwestProcessing)?), + _ => { + let status = response.status(); + let text = response.text().await.context(ReqwestProcessing)?; + Http { status, text }.fail()? + } + } + } + + /// Set up initial user, org and bucket + pub async fn setup_init( + &self, + username: &str, + org: &str, + bucket: &str, + password: Option, + retention_period_hrs: Option, + retention_period_seconds: Option, + ) -> Result { + let setup_init_url = format!("{}/api/v2/setup", self.url); + + let body = OnboardingRequest { + username: username.into(), + org: org.into(), + bucket: bucket.into(), + password: password, + retention_period_hrs, + retention_period_seconds, + }; + + let response = self + .request(Method::POST, &setup_init_url) + .body(serde_json::to_string(&body).context(Serializing)?) + .send() + .await + .context(ReqwestProcessing)?; + + match response.status() { + StatusCode::OK => Ok(response + .json::() + .await + .context(ReqwestProcessing)?), + _ => { + let status = response.status(); + let text = response.text().await.context(ReqwestProcessing)?; + Http { status, text }.fail()? + } + } + } + + /// Set up a new user, org and bucket + pub async fn setup_new( + &self, + username: &str, + org: &str, + bucket: &str, + password: Option, + retention_period_hrs: Option, + retention_period_seconds: Option, + ) -> Result { + let setup_new_url = format!("{}/api/v2/setup/user", self.url); + + let body = OnboardingRequest { + username: username.into(), + org: org.into(), + bucket: bucket.into(), + password: password, + retention_period_hrs, + retention_period_seconds, + }; + + let response = self + .request(Method::POST, &setup_new_url) + .body(serde_json::to_string(&body).context(Serializing)?) + .send() + .await + .context(ReqwestProcessing)?; + + match response.status() { + StatusCode::OK => Ok(response + .json::() + .await + .context(ReqwestProcessing)?), + _ => { + let status = response.status(); + let text = response.text().await.context(ReqwestProcessing)?; + Http { status, text }.fail()? + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use mockito::mock; + + type Error = Box; + type Result = std::result::Result; + + #[tokio::test] + async fn setup() -> Result { + let token = "some-token"; + + let mock_server = mock("GET", "/api/v2/setup") + .match_header("Authorization", format!("Token {}", token).as_str()) + .create(); + + let client = Client::new(&mockito::server_url(), token); + + let _result = client.setup().await; + + mock_server.assert(); + Ok(()) + } + + #[tokio::test] + async fn setup_init() -> Result { + let token = "some-token"; + let username = "some-user"; + let org = "some-org"; + let bucket = "some-bucket"; + let password = "some-password"; + let retention_period_hrs = 1; + + let mock_server = mock("POST", "/api/v2/setup") + .match_header("Authorization", format!("Token {}", token).as_str()) + .match_body( + format!( + r#"{{"username":"{}","org":"{}","bucket":"{}","password":"{}","retentionPeriodHrs":{},}}"#, + username, org, bucket, password, retention_period_hrs + ).as_str(), + ) + .create(); + + let client = Client::new(&mockito::server_url(), token); + + let _result = client + .setup_init(username, org, bucket, password, retention_period_hrs) + .await; + + mock_server.assert(); + Ok(()) + } + + #[tokio::test] + async fn setup_new() -> Result { + let token = "some-token"; + let username = "some-user"; + let org = "some-org"; + let bucket = "some-bucket"; + let password = "some-password"; + let retention_period_hrs = 1; + + let mock_server = mock("POST", "/api/v2/setup/user") + .match_header("Authorization", format!("Token {}", token).as_str()) + .match_body( + format!( + r#"{{"username":"{}","org":"{}","bucket":"{}","password":"{}","retentionPeriodHrs":{}}}"#, + username, org, bucket, password, retention_period_hrs + ).as_str(), + ) + .create(); + + let client = Client::new(&mockito::server_url(), token); + + let _result = client + .setup_new(username, org, bucket, password, retention_period_hrs) + .await; + + mock_server.assert(); + Ok(()) + } +} diff --git a/influxdb2_client/src/lib.rs b/influxdb2_client/src/lib.rs index f332a0a154..314b911bec 100644 --- a/influxdb2_client/src/lib.rs +++ b/influxdb2_client/src/lib.rs @@ -303,4 +303,5 @@ cpu,host=server01,region=us-west usage=0.87 } } -mod ready; +pub mod api; +pub mod models; diff --git a/influxdb2_client/src/models/authorization.rs b/influxdb2_client/src/models/authorization.rs new file mode 100644 index 0000000000..443f9161cc --- /dev/null +++ b/influxdb2_client/src/models/authorization.rs @@ -0,0 +1,89 @@ +use serde::{Deserialize, Serialize}; + +/// Authorization to create +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Authorization { + /// If inactive the token is inactive and requests using the token will be + /// rejected. + #[serde(rename = "status", skip_serializing_if = "Option::is_none")] + pub status: Option, + /// A description of the token. + #[serde(rename = "description", skip_serializing_if = "Option::is_none")] + pub description: Option, + #[serde(rename = "createdAt", skip_serializing_if = "Option::is_none")] + pub created_at: Option, + #[serde(rename = "updatedAt", skip_serializing_if = "Option::is_none")] + pub updated_at: Option, + /// ID of org that authorization is scoped to. + #[serde(rename = "orgID")] + pub org_id: String, + /// List of permissions for an auth. An auth must have at least one + /// Permission. + #[serde(rename = "permissions")] + pub permissions: Vec, + #[serde(rename = "id", skip_serializing_if = "Option::is_none")] + pub id: Option, + /// Passed via the Authorization Header and Token Authentication type. + #[serde(rename = "token", skip_serializing_if = "Option::is_none")] + pub token: Option, + /// ID of user that created and owns the token. + #[serde(rename = "userID", skip_serializing_if = "Option::is_none")] + pub user_id: Option, + /// Name of user that created and owns the token. + #[serde(rename = "user", skip_serializing_if = "Option::is_none")] + pub user: Option, + /// Name of the org token is scoped to. + #[serde(rename = "org", skip_serializing_if = "Option::is_none")] + pub org: Option, + #[serde(rename = "links", skip_serializing_if = "Option::is_none")] + pub links: Option, +} + +impl Authorization { + pub fn new(org_id: String, permissions: Vec) -> Authorization { + Authorization { + status: None, + description: None, + created_at: None, + updated_at: None, + org_id, + permissions, + id: None, + token: None, + user_id: None, + user: None, + org: None, + links: None, + } + } +} + +/// If inactive the token is inactive and requests using the token will be +/// rejected. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum Status { + #[serde(rename = "active")] + Active, + #[serde(rename = "inactive")] + Inactive, +} + +/// +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct AuthorizationAllOfLinks { + /// URI of resource. + #[serde(rename = "self", skip_serializing_if = "Option::is_none")] + pub _self: Option, + /// URI of resource. + #[serde(rename = "user", skip_serializing_if = "Option::is_none")] + pub user: Option, +} + +impl AuthorizationAllOfLinks { + pub fn new() -> AuthorizationAllOfLinks { + AuthorizationAllOfLinks { + _self: None, + user: None, + } + } +} diff --git a/influxdb2_client/src/models/bucket.rs b/influxdb2_client/src/models/bucket.rs new file mode 100644 index 0000000000..210bbd9034 --- /dev/null +++ b/influxdb2_client/src/models/bucket.rs @@ -0,0 +1,110 @@ +use serde::{Deserialize, Serialize}; + +/// Bucket Schema +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Bucket { + #[serde(rename = "links", skip_serializing_if = "Option::is_none")] + pub links: Option, + #[serde(rename = "id", skip_serializing_if = "Option::is_none")] + pub id: Option, + #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + pub _type: Option, + #[serde(rename = "name")] + pub name: String, + #[serde(rename = "description", skip_serializing_if = "Option::is_none")] + pub description: Option, + #[serde(rename = "orgID", skip_serializing_if = "Option::is_none")] + pub org_id: Option, + #[serde(rename = "rp", skip_serializing_if = "Option::is_none")] + pub rp: Option, + #[serde(rename = "createdAt", skip_serializing_if = "Option::is_none")] + pub created_at: Option, + #[serde(rename = "updatedAt", skip_serializing_if = "Option::is_none")] + pub updated_at: Option, + /// Rules to expire or retain data. No rules means data never expires. + #[serde(rename = "retentionRules")] + pub retention_rules: Vec, + #[serde(rename = "labels", skip_serializing_if = "Option::is_none")] + pub labels: Option>, +} + +impl Bucket { + pub fn new(name: String, retention_rules: Vec) -> Bucket { + Bucket { + links: None, + id: None, + _type: None, + name, + description: None, + org_id: None, + rp: None, + created_at: None, + updated_at: None, + retention_rules, + labels: None, + } + } +} + +/// Bucket Type +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum Type { + #[serde(rename = "user")] + User, + #[serde(rename = "system")] + System, +} + +/// +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct BucketLinks { + /// URI of resource. + #[serde(rename = "labels", skip_serializing_if = "Option::is_none")] + pub labels: Option, + /// URI of resource. + #[serde(rename = "members", skip_serializing_if = "Option::is_none")] + pub members: Option, + /// URI of resource. + #[serde(rename = "org", skip_serializing_if = "Option::is_none")] + pub org: Option, + /// URI of resource. + #[serde(rename = "owners", skip_serializing_if = "Option::is_none")] + pub owners: Option, + /// URI of resource. + #[serde(rename = "self", skip_serializing_if = "Option::is_none")] + pub _self: Option, + /// URI of resource. + #[serde(rename = "write", skip_serializing_if = "Option::is_none")] + pub write: Option, +} + +impl BucketLinks { + pub fn new() -> BucketLinks { + BucketLinks { + labels: None, + members: None, + org: None, + owners: None, + _self: None, + write: None, + } + } +} + +/// List all buckets +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Buckets { + #[serde(rename = "links", skip_serializing_if = "Option::is_none")] + pub links: Option, + #[serde(rename = "buckets", skip_serializing_if = "Option::is_none")] + pub buckets: Option>, +} + +impl Buckets { + pub fn new() -> Buckets { + Buckets { + links: None, + buckets: None, + } + } +} diff --git a/influxdb2_client/src/models/label.rs b/influxdb2_client/src/models/label.rs new file mode 100644 index 0000000000..d3ed2c9fce --- /dev/null +++ b/influxdb2_client/src/models/label.rs @@ -0,0 +1,26 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Label { + #[serde(rename = "id", skip_serializing_if = "Option::is_none")] + pub id: Option, + #[serde(rename = "orgID", skip_serializing_if = "Option::is_none")] + pub org_id: Option, + #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + pub name: Option, + /// Key/Value pairs associated with this label. Keys can be removed by + /// sending an update with an empty value. + #[serde(rename = "properties", skip_serializing_if = "Option::is_none")] + pub properties: Option<::std::collections::HashMap>, +} + +impl Label { + pub fn new() -> Label { + Label { + id: None, + org_id: None, + name: None, + properties: None, + } + } +} diff --git a/influxdb2_client/src/models/links.rs b/influxdb2_client/src/models/links.rs new file mode 100644 index 0000000000..76fd174065 --- /dev/null +++ b/influxdb2_client/src/models/links.rs @@ -0,0 +1,24 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Links { + /// URI of resource. + #[serde(rename = "next", skip_serializing_if = "Option::is_none")] + pub next: Option, + /// URI of resource. + #[serde(rename = "self")] + pub _self: String, + /// URI of resource. + #[serde(rename = "prev", skip_serializing_if = "Option::is_none")] + pub prev: Option, +} + +impl Links { + pub fn new(_self: String) -> Links { + Links { + next: None, + _self, + prev: None, + } + } +} diff --git a/influxdb2_client/src/models/mod.rs b/influxdb2_client/src/models/mod.rs new file mode 100644 index 0000000000..dd3acb988a --- /dev/null +++ b/influxdb2_client/src/models/mod.rs @@ -0,0 +1,24 @@ +//! InfluxDB Models +//! +//! Roughly follows the OpenAPI specification + +pub mod user; +pub use self::user::{User, UserLinks, Users, UsersLinks}; +pub mod organization; +pub use self::organization::{Organization, OrganizationLinks, Organizations}; +pub mod bucket; +pub use self::bucket::{Bucket, BucketLinks, Buckets}; +pub mod onboarding; +pub use self::onboarding::{IsOnboarding, OnboardingRequest, OnboardingResponse}; +pub mod links; +pub use self::links::Links; +pub mod permission; +pub use self::permission::Permission; +pub mod label; +pub use self::label::Label; +pub mod authorization; +pub use self::authorization::{Authorization, AuthorizationAllOfLinks}; +pub mod resource; +pub use self::resource::Resource; +pub mod retention_rule; +pub use self::retention_rule::RetentionRule; diff --git a/influxdb2_client/src/models/onboarding.rs b/influxdb2_client/src/models/onboarding.rs new file mode 100644 index 0000000000..54e2c8b8d8 --- /dev/null +++ b/influxdb2_client/src/models/onboarding.rs @@ -0,0 +1,73 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct IsOnboarding { + /// True means that the influxdb instance has NOT had initial setup; false + /// means that the database has been setup. + #[serde(rename = "allowed", skip_serializing_if = "Option::is_none")] + pub allowed: Option, +} + +impl IsOnboarding { + pub fn new() -> IsOnboarding { + IsOnboarding { allowed: None } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct OnboardingRequest { + #[serde(rename = "username")] + pub username: String, + #[serde(rename = "password", skip_serializing_if = "Option::is_none")] + pub password: Option, + #[serde(rename = "org")] + pub org: String, + #[serde(rename = "bucket")] + pub bucket: String, + #[serde( + rename = "retentionPeriodSeconds", + skip_serializing_if = "Option::is_none" + )] + pub retention_period_seconds: Option, + /// Retention period *in nanoseconds* for the new bucket. This key's name + /// has been misleading since OSS 2.0 GA, please transition to use + /// `retentionPeriodSeconds` + #[serde(rename = "retentionPeriodHrs", skip_serializing_if = "Option::is_none")] + pub retention_period_hrs: Option, +} + +impl OnboardingRequest { + pub fn new(username: String, org: String, bucket: String) -> OnboardingRequest { + OnboardingRequest { + username, + password: None, + org, + bucket, + retention_period_seconds: None, + retention_period_hrs: None, + } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct OnboardingResponse { + #[serde(rename = "user", skip_serializing_if = "Option::is_none")] + pub user: Option, + #[serde(rename = "org", skip_serializing_if = "Option::is_none")] + pub org: Option, + #[serde(rename = "bucket", skip_serializing_if = "Option::is_none")] + pub bucket: Option, + #[serde(rename = "auth", skip_serializing_if = "Option::is_none")] + pub auth: Option, +} + +impl OnboardingResponse { + pub fn new() -> OnboardingResponse { + OnboardingResponse { + user: None, + org: None, + bucket: None, + auth: None, + } + } +} diff --git a/influxdb2_client/src/models/organization.rs b/influxdb2_client/src/models/organization.rs new file mode 100644 index 0000000000..4898e69a1f --- /dev/null +++ b/influxdb2_client/src/models/organization.rs @@ -0,0 +1,103 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Organization { + #[serde(rename = "links", skip_serializing_if = "Option::is_none")] + pub links: Option, + #[serde(rename = "id", skip_serializing_if = "Option::is_none")] + pub id: Option, + #[serde(rename = "name")] + pub name: String, + #[serde(rename = "description", skip_serializing_if = "Option::is_none")] + pub description: Option, + #[serde(rename = "createdAt", skip_serializing_if = "Option::is_none")] + pub created_at: Option, + #[serde(rename = "updatedAt", skip_serializing_if = "Option::is_none")] + pub updated_at: Option, + /// If inactive the organization is inactive. + #[serde(rename = "status", skip_serializing_if = "Option::is_none")] + pub status: Option, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct OrganizationLinks { + /// URI of resource. + #[serde(rename = "self", skip_serializing_if = "Option::is_none")] + pub _self: Option, + /// URI of resource. + #[serde(rename = "members", skip_serializing_if = "Option::is_none")] + pub members: Option, + /// URI of resource. + #[serde(rename = "owners", skip_serializing_if = "Option::is_none")] + pub owners: Option, + /// URI of resource. + #[serde(rename = "labels", skip_serializing_if = "Option::is_none")] + pub labels: Option, + /// URI of resource. + #[serde(rename = "secrets", skip_serializing_if = "Option::is_none")] + pub secrets: Option, + /// URI of resource. + #[serde(rename = "buckets", skip_serializing_if = "Option::is_none")] + pub buckets: Option, + /// URI of resource. + #[serde(rename = "tasks", skip_serializing_if = "Option::is_none")] + pub tasks: Option, + /// URI of resource. + #[serde(rename = "dashboards", skip_serializing_if = "Option::is_none")] + pub dashboards: Option, +} + +impl Organization { + pub fn new(name: String) -> Organization { + Organization { + links: None, + id: None, + name, + description: None, + created_at: None, + updated_at: None, + status: None, + } + } +} + +/// If inactive the organization is inactive. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum Status { + #[serde(rename = "active")] + Active, + #[serde(rename = "inactive")] + Inactive, +} + +impl OrganizationLinks { + pub fn new() -> OrganizationLinks { + OrganizationLinks { + _self: None, + members: None, + owners: None, + labels: None, + secrets: None, + buckets: None, + tasks: None, + dashboards: None, + } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Organizations { + #[serde(rename = "links", skip_serializing_if = "Option::is_none")] + pub links: Option, + #[serde(rename = "orgs", skip_serializing_if = "Option::is_none")] + pub orgs: Option>, +} + +impl Organizations { + pub fn new() -> Organizations { + Organizations { + links: None, + orgs: None, + } + } +} diff --git a/influxdb2_client/src/models/permission.rs b/influxdb2_client/src/models/permission.rs new file mode 100644 index 0000000000..16a3e8d702 --- /dev/null +++ b/influxdb2_client/src/models/permission.rs @@ -0,0 +1,23 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Permission { + #[serde(rename = "action")] + pub action: Action, + #[serde(rename = "resource")] + pub resource: crate::models::Resource, +} + +impl Permission { + pub fn new(action: Action, resource: crate::models::Resource) -> Permission { + Permission { action, resource } + } +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum Action { + #[serde(rename = "read")] + Read, + #[serde(rename = "write")] + Write, +} diff --git a/influxdb2_client/src/models/resource.rs b/influxdb2_client/src/models/resource.rs new file mode 100644 index 0000000000..2ac42ed156 --- /dev/null +++ b/influxdb2_client/src/models/resource.rs @@ -0,0 +1,74 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Resource { + #[serde(rename = "type")] + pub _type: Type, + /// If ID is set that is a permission for a specific resource. if it is not + /// set it is a permission for all resources of that resource type. + #[serde(rename = "id", skip_serializing_if = "Option::is_none")] + pub id: Option, + /// Optional name of the resource if the resource has a name field. + #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + pub name: Option, + /// If orgID is set that is a permission for all resources owned my that + /// org. if it is not set it is a permission for all resources of that + /// resource type. + #[serde(rename = "orgID", skip_serializing_if = "Option::is_none")] + pub org_id: Option, + /// Optional name of the organization of the organization with orgID. + #[serde(rename = "org", skip_serializing_if = "Option::is_none")] + pub org: Option, +} + +impl Resource { + pub fn new(_type: Type) -> Resource { + Resource { + _type, + id: None, + name: None, + org_id: None, + org: None, + } + } +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum Type { + #[serde(rename = "authorizations")] + Authorizations, + #[serde(rename = "buckets")] + Buckets, + #[serde(rename = "dashboards")] + Dashboards, + #[serde(rename = "orgs")] + Orgs, + #[serde(rename = "sources")] + Sources, + #[serde(rename = "tasks")] + Tasks, + #[serde(rename = "telegrafs")] + Telegrafs, + #[serde(rename = "users")] + Users, + #[serde(rename = "variables")] + Variables, + #[serde(rename = "scrapers")] + Scrapers, + #[serde(rename = "secrets")] + Secrets, + #[serde(rename = "labels")] + Labels, + #[serde(rename = "views")] + Views, + #[serde(rename = "documents")] + Documents, + #[serde(rename = "notificationRules")] + NotificationRules, + #[serde(rename = "notificationEndpoints")] + NotificationEndpoints, + #[serde(rename = "checks")] + Checks, + #[serde(rename = "dbrp")] + Dbrp, +} diff --git a/influxdb2_client/src/models/retention_rule.rs b/influxdb2_client/src/models/retention_rule.rs new file mode 100644 index 0000000000..a8d32cb27e --- /dev/null +++ b/influxdb2_client/src/models/retention_rule.rs @@ -0,0 +1,33 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct RetentionRule { + #[serde(rename = "type")] + pub _type: Type, + /// Duration in seconds for how long data will be kept in the database. 0 + /// means infinite. + #[serde(rename = "everySeconds")] + pub every_seconds: i32, + /// Shard duration measured in seconds. + #[serde( + rename = "shardGroupDurationSeconds", + skip_serializing_if = "Option::is_none" + )] + pub shard_group_duration_seconds: Option, +} + +impl RetentionRule { + pub fn new(_type: Type, every_seconds: i32) -> RetentionRule { + RetentionRule { + _type, + every_seconds, + shard_group_duration_seconds: None, + } + } +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum Type { + #[serde(rename = "expire")] + Expire, +} diff --git a/influxdb2_client/src/models/user.rs b/influxdb2_client/src/models/user.rs new file mode 100644 index 0000000000..c23f978ec1 --- /dev/null +++ b/influxdb2_client/src/models/user.rs @@ -0,0 +1,82 @@ +use serde::{Deserialize, Serialize}; + +/// User Information +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct User { + #[serde(rename = "id", skip_serializing_if = "Option::is_none")] + pub id: Option, + #[serde(rename = "oauthID", skip_serializing_if = "Option::is_none")] + pub oauth_id: Option, + #[serde(rename = "name")] + pub name: String, + /// If inactive the user is inactive. + #[serde(rename = "status", skip_serializing_if = "Option::is_none")] + pub status: Option, + #[serde(rename = "links", skip_serializing_if = "Option::is_none")] + pub links: Option, +} + +impl User { + pub fn new(name: String) -> User { + User { + id: None, + oauth_id: None, + name, + status: None, + links: None, + } + } +} + +/// If inactive the user is inactive. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] +pub enum Status { + #[serde(rename = "active")] + Active, + #[serde(rename = "inactive")] + Inactive, +} + +/// +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct UserLinks { + #[serde(rename = "self", skip_serializing_if = "Option::is_none")] + pub _self: Option, +} + +impl UserLinks { + pub fn new() -> UserLinks { + UserLinks { _self: None } + } +} + +/// List of Users +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Users { + #[serde(rename = "links", skip_serializing_if = "Option::is_none")] + pub links: Option, + #[serde(rename = "users", skip_serializing_if = "Option::is_none")] + pub users: Option>, +} + +impl Users { + pub fn new() -> Users { + Users { + links: None, + users: None, + } + } +} + +/// +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct UsersLinks { + #[serde(rename = "self", skip_serializing_if = "Option::is_none")] + pub _self: Option, +} + +impl UsersLinks { + pub fn new() -> UsersLinks { + UsersLinks { _self: None } + } +}