Skip to content

Commit

Permalink
added api examples for rust
Browse files Browse the repository at this point in the history
Fixes: #566

Adds the missing api examples for rust on how to
use BlueChi's D-Bus API.

Signed-off-by: Michael Engel <mengel@redhat.com>
  • Loading branch information
engelmi committed Oct 18, 2023
1 parent 5680150 commit 2a67ef2
Show file tree
Hide file tree
Showing 15 changed files with 913 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BUILDDIR=builddir

CODESPELL_PARAMS=\
-S Makefile,imgtype,copy,AUTHORS,bin,.git,CHANGELOG.md,changelog.txt,.cirrus.yml,"*.xz,*.gz,*.tar,*.tgz,*ico,*.png,*.1,*.5,*.orig,*.rej,*.xml,*xsl",build.ninja,intro-targets.json,./tests/tests/tier0/proxy-service-fails-on-typo-in-file/systemd/simple.service,tags,./builddir,./subprojects,\
-L keypair,flate,uint,iff,od,ERRO
-L keypair,flate,uint,iff,od,ERRO,crate

build:
meson setup $(BUILDDIR)
Expand Down
1 change: 1 addition & 0 deletions doc/api-examples/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
278 changes: 278 additions & 0 deletions doc/api-examples/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions doc/api-examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "bluechi"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.6", features = ["derive"] }
dbus = "0.9.7"

[[bin]]
name = "enable-unit"
path = "enable-unit.rs"

[[bin]]
name = "get-cpuweight"
path = "get-cpuweight.rs"

[[bin]]
name = "list-node-units"
path = "list-node-units.rs"

[[bin]]
name = "list-nodes"
path = "list-nodes.rs"

[[bin]]
name = "monitor-agent-connection"
path = "monitor-agent-connection.rs"

[[bin]]
name = "monitor-node-connections"
path = "monitor-node-connections.rs"

[[bin]]
name = "monitor-unit"
path = "monitor-unit.rs"

[[bin]]
name = "set-cpuweight"
path = "set-cpuweight.rs"

[[bin]]
name = "start-unit"
path = "start-unit.rs"
57 changes: 57 additions & 0 deletions doc/api-examples/rust/enable-unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: MIT-0

use clap::Parser;
use dbus::blocking::Connection;
use dbus::Path;
use std::time::Duration;

#[derive(Parser)]
struct Cli {
/// The node name to list the units for
#[clap(short, long)]
node_name: String,

/// The names of the units to enable. Names are separated by ','.
#[clap(short, long, value_delimiter = ',')]
unit_names: Vec<String>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Cli::parse();

let conn = Connection::new_system()?;

let bluechi = conn.with_proxy(
"org.eclipse.bluechi",
"/org/eclipse/bluechi",
Duration::from_millis(5000),
);

let (node,): (Path,) =
bluechi.method_call("org.eclipse.bluechi.Manager", "GetNode", (args.node_name,))?;

let node_proxy = conn.with_proxy("org.eclipse.bluechi", node, Duration::from_millis(5000));

let (carries_install_info, changes): (bool, Vec<(String, String, String)>) = node_proxy
.method_call(
"org.eclipse.bluechi.Node",
"EnableUnitFiles",
(args.unit_names, false, false),
)?;

if carries_install_info {
println!("The unit files included enablement information");
} else {
println!("The unit files did not include any enablement information");
}

for (op_type, file_name, file_dest) in changes {
if op_type == "symlink" {
println!("Created symlink {} -> {}", file_name, file_dest);
} else if op_type == "unlink" {
println!("Removed '{}'", file_name);
}
}

Ok(())
}
Loading

0 comments on commit 2a67ef2

Please sign in to comment.