Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved QLDB code examples into qldb directory; added create-ledger code example #485

Merged
merged 7 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions aws/sdk/examples/qldb-list-ledgers/Cargo.toml

This file was deleted.

2 changes: 0 additions & 2 deletions aws/sdk/examples/qldb-list-ledgers/README.md

This file was deleted.

22 changes: 0 additions & 22 deletions aws/sdk/examples/qldb-list-ledgers/src/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
[package]
name = "qldbsession-helloworld"
name = "qldb-code-examples"
version = "0.1.0"
authors = ["Russell Cohen <rcoh@amazon.com>"]
authors = ["Russell Cohen <rcoh@amazon.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"

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

[dependencies]
qldb = { package = "aws-sdk-qldb", path = "../../build/aws-sdk/qldb" }
qldbsession = { package = "aws-sdk-qldbsession", path = "../../build/aws-sdk/qldbsession" }
### To use native TLS:
# qldbsession = { package = "aws-sdk-qldbsession", path = "../../build/aws-sdk/qldbsession", default-features = false, features = ["native-tls"] }
aws-types = { path = "../../build/aws-sdk/aws-types" }

tokio = { version = "1", features = ["full"] }

# For command-line arguments
structopt = { version = "0.3", default-features = false }

tracing-subscriber = { version = "0.2.16", features = ["fmt"] }

# used only for static endpoint configuration:
http = "0.2.3"

Expand Down
81 changes: 81 additions & 0 deletions aws/sdk/examples/qldb/src/bin/create-ledger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_types::region::ProvideRegion;

//use qldbsession::model::StartSessionRequest;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//use qldbsession::model::StartSessionRequest;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted.

use qldb::model::PermissionsMode;
use qldb::{Client, Config, Error, Region};

use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

#[derive(Debug, StructOpt)]
struct Opt {
/// The region. Overrides environment variable AWS_DEFAULT_REGION.
#[structopt(short, long)]
default_region: Option<String>,

/// The name of the ledger.
#[structopt(short, long)]
ledger: String,

/// Whether to display additional runtime information
#[structopt(short, long)]
verbose: bool,
}

/// Creates an Amazon QLDB ledger.
/// # Arguments
///
/// * `-l LEDGER` - The name of the ledger.
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
let Opt {
default_region,
ledger,
verbose,
} = Opt::from_args();

let region = default_region
.as_ref()
.map(|region| Region::new(region.clone()))
.or_else(|| aws_types::region::default_provider().region())
.unwrap_or_else(|| Region::new("us-west-2"));

if verbose {
println!("OLDB client version: {}\n", qldb::PKG_VERSION);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
println!("OLDB client version: {}\n", qldb::PKG_VERSION);
println!("QLDB client version: {}\n", qldb::PKG_VERSION);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, changed.

println!("Region: {:?}", &region);

SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
}

let conf = Config::builder().region(region).build();
let client = Client::from_conf(conf);

let result = client
.create_ledger()
.name(ledger)
.permissions_mode(PermissionsMode::AllowAll)
.send()
.await?;

match result.arn {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the result will always have an ARN, should probably unwrap here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

None => {}
Some(a) => {
println!("Ledger ARN: {}", a);
}
}

Ok(())
}
79 changes: 79 additions & 0 deletions aws/sdk/examples/qldb/src/bin/helloworld.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs rename

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_types::region::ProvideRegion;

use qldbsession::model::StartSessionRequest;
use qldbsession::{Client, Config, Error, Region};

use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

#[derive(Debug, StructOpt)]
struct Opt {
/// The region. Overrides environment variable AWS_DEFAULT_REGION.
#[structopt(short, long)]
default_region: Option<String>,

/// Specifies the ledger
#[structopt(short, long)]
ledger: String,

/// Whether to display additional runtime information
#[structopt(short, long)]
verbose: bool,
}

/// Creates a low-level Amazon QLDB session.
/// # Arguments
///
/// * `-l LEDGER` - The name of the ledger to start a new session against.
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
let Opt {
ledger,
default_region,
verbose,
} = Opt::from_args();

let region = default_region
.as_ref()
.map(|region| Region::new(region.clone()))
.or_else(|| aws_types::region::default_provider().region())
.unwrap_or_else(|| Region::new("us-west-2"));

if verbose {
println!("OLDB client version: {}\n", qldb::PKG_VERSION);
println!("Region: {:?}", &region);
println!("Ledger: {}", ledger);

SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
}

let conf = Config::builder().region(region).build();
let client = Client::from_conf(conf);
let result = client
.send_command()
.start_session(StartSessionRequest::builder().ledger_name(ledger).build())
.send()
.await?;

match result.start_session {
Some(s) => {
println!("Your session id: {:?}", s.session_token);
}
None => unreachable!("a start session will result in an Err or a start session result"),
}

Ok(())
}
72 changes: 72 additions & 0 deletions aws/sdk/examples/qldb/src/bin/list-ledgers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

use aws_types::region::ProvideRegion;

//use qldbsession::model::StartSessionRequest;
use qldb::{Client, Config, Error, Region};

use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

#[derive(Debug, StructOpt)]
struct Opt {
/// The region. Overrides environment variable AWS_DEFAULT_REGION.
#[structopt(short, long)]
default_region: Option<String>,

/// Whether to display additional runtime information
#[structopt(short, long)]
verbose: bool,
}

/// Lists your Amazon QLDB ledgers.
/// # Arguments
///
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
/// If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
let Opt {
default_region,
verbose,
} = Opt::from_args();

let region = default_region
.as_ref()
.map(|region| Region::new(region.clone()))
.or_else(|| aws_types::region::default_provider().region())
.unwrap_or_else(|| Region::new("us-west-2"));

if verbose {
println!("OLDB client version: {}\n", qldb::PKG_VERSION);
println!("Region: {:?}", &region);

SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
}

let conf = Config::builder().region(region).build();
let client = Client::from_conf(conf);

let result = client.list_ledgers().send().await?;

if let Some(ledgers) = result.ledgers {
for ledger in ledgers {
println!("* {:?}", ledger);
}

if result.next_token.is_some() {
todo!("pagination is not yet demonstrated")
}
}

Ok(())
}
4 changes: 0 additions & 4 deletions aws/sdk/examples/qldbsession-helloworld/README.md

This file was deleted.

31 changes: 0 additions & 31 deletions aws/sdk/examples/qldbsession-helloworld/src/main.rs

This file was deleted.