Unverified Commit 05eace6b authored by Doug's avatar Doug Committed by GitHub
Browse files

Moved RDS code examples into rds directory (#486)



* Moved RDS code examples into rds directory

* Renamed rds_helloword.rs as rds-helloworld.rs; rdsdata_helloworld.rs as rdsdata-helloworld.rs

* Separated RDS code examples into rds and rdsdata directories

* Deleted orphaned RDS Cargo.toml file

Co-authored-by: default avatarRussell Cohen <rcoh@amazon.com>
parent b9df4d6b
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
[package]
authors = ["LMJW <heysuperming@gmail.com>"]
name = "rds-code-examples"
authors = ["LMJW <heysuperming@gmail.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"
name = "rds-helloworld"
version = "0.1.0"

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

[dependencies]
rds = {package = "aws-sdk-rds", path = "../../build/aws-sdk/rds"}
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"] }
 No newline at end of file
+93 −0
Original line number Diff line number Diff line
@@ -3,12 +3,61 @@
 * SPDX-License-Identifier: Apache-2.0.
 */

use rds::{Client, Config, Region};

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>,

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

/// Displays information about your RDS instances.
/// # Arguments
///
/// * `-k KEY` - The KMS key.
/// * `-o OUT` - The name of the file to store the encryped key in.
/// * `-t TEXT` - The string to encrypt.
/// * `[-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<(), rds::Error> {
    let conf = rds::Config::builder()
        .region(rds::Region::new("us-east-1"))
        .build();
    let client = rds::Client::from_conf(conf);
    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!("RDS client version: {}\n", rds::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.describe_db_instances().send().await?;

    for db_instance in result.db_instances.unwrap_or_default() {
+0 −30
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

// Example `RESOURCE_ARN` and `SECRET_ARN`
// const RESOURCE_ARN: &str = "arn:aws:rds:us-west-2:AWS_ACCOUNT:cluster:database-2";
// const SECRET_ARN: &str =
//     "arn:aws:secretsmanager:us-west-2:AWS_ACCOUNT:secret:database2/test/postgres-b8maVb";
const RESOURCE_ARN: &str = "your aurora serverless db cluster resource arn";
const SECRET_ARN: &str = "your secret arn from secret manager";

#[tokio::main]
async fn main() -> Result<(), rdsdata::Error> {
    let conf = rdsdata::Config::builder()
        .region(rdsdata::Region::new("us-west-2"))
        .build();
    let client = rdsdata::Client::from_conf(conf);
    let st = client
        .execute_statement()
        .resource_arn(RESOURCE_ARN)
        .database("postgres") // Do not confuse this with db instance name
        .sql("SELECT * FROM pg_catalog.pg_tables limit 1")
        .secret_arn(SECRET_ARN);

    let result = st.send().await?;

    println!("{:?}", result);
    Ok(())
}
+15 −0
Original line number Diff line number Diff line
[package]
authors = ["LMJW <heysuperming@gmail.com>"]
name = "rdsdata-code-examples"
authors = ["LMJW <heysuperming@gmail.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"
name = "rdsdata-helloworld"
version = "0.1.0"

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

[dependencies]
rdsdata = {package = "aws-sdk-rdsdata", path = "../../build/aws-sdk/rdsdata"}
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"] }
 No newline at end of file
+95 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use rdsdata::{Client, Config, Region};

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>,

    /// The SQL query string
    #[structopt(short, long)]
    query: String,

    /// The ARN of your Aurora serverless DB cluster
    #[structopt(short, long)]
    resource_arn: String,

    /// The ARN of the Secrets Manager secret
    #[structopt(short, long)]
    secret_arn: String,

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

/// Sends a query to an Aurora serverless cluster.
/// # Arguments
///
/// * `-q QUERY` - The SQL query to run against the cluster.
///    It should look something like: __"SELECT * FROM pg_catalog.pg_tables limit 1"__.
///    Don't forget you'll likely have to escape some characters.
/// * `-r RESOURCE_ARN` - The ARN of your Aurora serverless DB cluster.
///    It should look something like __arn:aws:rds:us-west-2:AWS_ACCOUNT:cluster:database-2__.
/// * `-s SECRET_ARN` - The ARN of the Secrets Manager secret.
///    It should look something like: __arn:aws:secretsmanager:us-west-2:AWS_ACCOUNT:secret:database2/test/postgres-b8maVb__.
/// * `[-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<(), rdsdata::Error> {
    let Opt {
        default_region,
        query,
        resource_arn,
        secret_arn,
        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!("RDS data client version: {}\n", rdsdata::PKG_VERSION);
        println!("Region:                  {:?}", &region);
        println!("Resource ARN:            {}", resource_arn);
        println!("Secrets ARN:             {}", secret_arn);
        println!("Query:");
        println!("  {}", query);

        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 st = client
        .execute_statement()
        .resource_arn(resource_arn)
        .database("postgres") // Do not confuse this with db instance name
        .sql(query)
        .secret_arn(secret_arn);

    let result = st.send().await?;

    println!("{:?}", result);
    Ok(())
}
Loading