Skip to content

Commit

Permalink
Moved SecretsManager code examples into secretsmanager directory (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug-AWS authored Jun 11, 2021
1 parent 1469090 commit c2c39a4
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 113 deletions.
13 changes: 0 additions & 13 deletions aws/sdk/examples/secretsmanager-get-secret-value/Cargo.toml

This file was deleted.

13 changes: 0 additions & 13 deletions aws/sdk/examples/secretsmanager-helloworld/Cargo.toml

This file was deleted.

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

This file was deleted.

13 changes: 0 additions & 13 deletions aws/sdk/examples/secretsmanager-list-secrets/Cargo.toml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
[package]
name = "secretsmanager-create-secret"
name = "secretsmanager-code-examples"
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" }
aws-hyper = { path = "../../build/aws-sdk/aws-hyper" }
aws-types = { path = "../../build/aws-sdk/aws-types" }

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" }

Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,44 @@ struct Opt {

/// The value of the secret
#[structopt(short, long)]
value: String,
secret_value: String,

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

/// Creates a secret.
/// # Arguments
///
/// * `-n NAME` - The name of the secret.
/// * `-s SECRET_VALUE` - The secret value.
/// * `[-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() {
let Opt {
info,
name,
region,
value,
secret_value,
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 info {
if verbose {
println!(
"SecretsManager client version: {}\n",
secretsmanager::PKG_VERSION
);
println!("Region: {:?}", &region);
println!("Secret name: {}", name);
println!("Secret value: {}", value);
println!("Secret value: {}", secret_value);

SubscriberBuilder::default()
.with_env_filter("info")
Expand All @@ -66,7 +76,7 @@ async fn main() {
match client
.create_secret()
.name(name)
.secret_string(value)
.secret_string(secret_value)
.send()
.await
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ struct Opt {
/// The name of the secret
#[structopt(short, long)]
name: String,

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

/// Retrieves the value of a secret.
/// # Arguments
///
/// * `-n NAME` - The name of the secret.
/// * `-s SECRET_VALUE` - The secret value.
/// * `[-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() {
let Opt {
Expand Down
107 changes: 107 additions & 0 deletions aws/sdk/examples/secretsmanager/src/bin/helloworld.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

use secretsmanager::{Client, Config, Region, SdkError};

use aws_types::region::ProvideRegion;

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 secret's name
#[structopt(short, long)]
name: String,

/// Specifies the secret's value
#[structopt(short, long)]
secret_value: String,

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

/// Creates a secret.
/// # Arguments
///
/// * `-n NAME` - The name of the secret.
/// * `-s SECRET_VALUE` - The secret value.
/// * `[-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() {
let Opt {
default_region,
name,
secret_value,
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!(
"SecretsManager client version: {}",
secretsmanager::PKG_VERSION
);
println!("Region: {:?}", &region);
println!("Secret name: {}", name);
println!("Secret value: {}", secret_value);

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);

// attempt to create a secret,
// need to find a better way to handle failure such as ResourceExistsException
let data = match client
.create_secret()
.name(&name)
.secret_string(&secret_value)
.send()
.await
{
Ok(secret) => secret,
Err(SdkError::ServiceError { err, .. }) => match err.kind {
secretsmanager::error::CreateSecretErrorKind::ResourceExistsError(_) => {
panic!("This secret already exists!")
}
_ => panic!("Secretsmanager Error: {}", err),
},
Err(other) => panic!("Failed to create secret: {}", other),
};
println!("Created secret {:?} with ARN {:?}", name, data.arn.unwrap());

// try and retrieve the secret value we just created
let retrieved_secret = client
.get_secret_value()
.secret_id(name)
.send()
.await
.expect("unable to retrieve secret");

assert_eq!(retrieved_secret.secret_string.unwrap(), secret_value);
println!(
"successfully retrieved secret string that matches the original one we created earlier"
);
}

0 comments on commit c2c39a4

Please sign in to comment.