Doug secretsmanager examples (#318)

* Added SecretsManager code examples for CreateSecret, GetSecretValue, and ListSecrets

* Fixed lint error in SecretsManager ListSecrets code example
This commit is contained in:
Doug 2021-04-20 14:12:56 -07:00 committed by GitHub
parent c36b5efade
commit a3d88b7a75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 257 additions and 0 deletions

View File

@ -0,0 +1,13 @@
[package]
name = "secretsmanager-create-secret"
version = "0.1.0"
authors = ["AWS Rust SDK Team <aws-sdk-rust@amazon.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"
description = "Example usage of the SecretManager service"
[dependencies]
secretsmanager = { package = "aws-sdk-secretsmanager", path = "../../build/aws-sdk/secretsmanager" }
tokio = { version = "1", features = ["full"]}
structopt = { version = "0.3", default-features = false }
tracing-subscriber = { version = "0.2.16", features = ["fmt"] }
aws-types = { path = "../../build/aws-sdk/aws-types" }

View File

@ -0,0 +1,76 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
use secretsmanager::{Client, Config, Region};
use aws_types::region::{EnvironmentProvider, ProvideRegion};
use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;
#[derive(Debug, StructOpt)]
struct Opt {
/// The region
#[structopt(short, long)]
region: Option<String>,
/// The name of the secret
#[structopt(short, long)]
name: String,
/// The value of the secret
#[structopt(short, long)]
value: String,
/// Whether to display additonal runtime information
#[structopt(short, long)]
info: bool,
}
#[tokio::main]
async fn main() {
let Opt {
info,
name,
region,
value,
} = Opt::from_args();
let region = EnvironmentProvider::new()
.region()
.or_else(|| region.as_ref().map(|region| Region::new(region.clone())))
.unwrap_or_else(|| Region::new("us-west-2"));
if info {
println!(
"SecretsManager client version: {}\n",
secretsmanager::PKG_VERSION
);
println!("Region: {:?}", &region);
println!("Secret name: {}", name);
println!("Secret value: {}", value);
SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
}
let config = Config::builder().region(region).build();
let client = Client::from_conf(config);
match client
.create_secret()
.name(name)
.secret_string(value)
.send()
.await
{
Ok(_) => println!("Created secret"),
Err(e) => panic!("Failed to create secret: {}", e),
};
}

View File

@ -0,0 +1,13 @@
[package]
name = "secretsmanager-get-secret-value"
version = "0.1.0"
authors = ["AWS Rust SDK Team <aws-sdk-rust@amazon.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"
description = "Example usage of the SecretManager service"
[dependencies]
secretsmanager = { package = "aws-sdk-secretsmanager", path = "../../build/aws-sdk/secretsmanager" }
tokio = { version = "1", features = ["full"]}
structopt = { version = "0.3", default-features = false }
tracing-subscriber = { version = "0.2.16", features = ["fmt"] }
aws-types = { path = "../../build/aws-sdk/aws-types" }

View File

@ -0,0 +1,73 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
use std::process;
use secretsmanager::{Client, Config, Region};
use aws_types::region::{EnvironmentProvider, ProvideRegion};
use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;
#[derive(Debug, StructOpt)]
struct Opt {
/// The region
#[structopt(short, long)]
region: Option<String>,
/// The name of the secret
#[structopt(short, long)]
name: String,
/// Whether to display additonal runtime information
#[structopt(short, long)]
verbose: bool,
}
#[tokio::main]
async fn main() {
let Opt {
name,
region,
verbose,
} = Opt::from_args();
let region = EnvironmentProvider::new()
.region()
.or_else(|| region.as_ref().map(|region| Region::new(region.clone())))
.unwrap_or_else(|| Region::new("us-west-2"));
if verbose {
println!(
"SecretsManager client version: {}\n",
secretsmanager::PKG_VERSION
);
println!("Region: {:?}", &region);
println!("Secret name: {}", name);
SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
}
let config = Config::builder().region(region).build();
let client = Client::from_conf(config);
match client.get_secret_value().secret_id(name).send().await {
Ok(resp) => {
println!(
"Value: {}",
resp.secret_string.as_deref().unwrap_or("No value!")
);
}
Err(e) => {
println!("Got an error listing secrets:");
println!("{}", e);
process::exit(1);
}
};
}

View File

@ -0,0 +1,13 @@
[package]
name = "secretsmanager-list-secrets"
version = "0.1.0"
authors = ["AWS Rust SDK Team <aws-sdk-rust@amazon.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"
description = "Example usage of the SecretManager service"
[dependencies]
secretsmanager = { package = "aws-sdk-secretsmanager", path = "../../build/aws-sdk/secretsmanager" }
tokio = { version = "1", features = ["full"]}
structopt = { version = "0.3", default-features = false }
tracing-subscriber = { version = "0.2.16", features = ["fmt"] }
aws-types = { path = "../../build/aws-sdk/aws-types" }

View File

@ -0,0 +1,69 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
use std::process;
use secretsmanager::{Client, Config, Region};
use aws_types::region::{EnvironmentProvider, ProvideRegion};
use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;
#[derive(Debug, StructOpt)]
struct Opt {
/// The region
#[structopt(short, long)]
region: Option<String>,
/// Whether to display additonal runtime information
#[structopt(short, long)]
verbose: bool,
}
#[tokio::main]
async fn main() {
let Opt { region, verbose } = Opt::from_args();
let region = EnvironmentProvider::new()
.region()
.or_else(|| region.as_ref().map(|region| Region::new(region.clone())))
.unwrap_or_else(|| Region::new("us-west-2"));
if verbose {
println!(
"SecretsManager client version: {}",
secretsmanager::PKG_VERSION
);
println!("Region: {:?}", &region);
SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
}
let config = Config::builder().region(region).build();
let client = Client::from_conf(config);
match client.list_secrets().send().await {
Ok(resp) => {
println!("Secret names:");
let secrets = resp.secret_list.unwrap_or_default();
for secret in &secrets {
println!(" {}", secret.name.as_deref().unwrap_or("No name!"));
}
println!("Found {} secrets", secrets.len());
}
Err(e) => {
println!("Got an error listing secrets:");
println!("{}", e);
process::exit(1);
}
};
}