Unverified Commit 47286edc authored by Doug's avatar Doug Committed by GitHub
Browse files

Update SNS code examples to use common pattern (#617)

* Updated SNS code examples to use common pattern; cleaned up Cargo.toml

* Added -t TOPIC_ARN flag to sns-hello-world code example; added create-topic and list-topic code examples
parent 986be4b8
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -7,10 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sns = { package = "aws-sdk-sns", path = "../../build/aws-sdk/sns" }
aws-sdk-sns = { package = "aws-sdk-sns", path = "../../build/aws-sdk/sns" }
aws-types = { path = "../../build/aws-sdk/aws-types" }

tokio = { version = "1", features = ["full"] }
tracing-subscriber = "0.2.18"

structopt = { version = "0.3", default-features = false }
tracing-subscriber = "0.2.18"
 No newline at end of file
+71 −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 aws_sdk_sns::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region;
use aws_types::region::ProvideRegion;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

    /// Specifies the topic name.
    #[structopt(short, long)]
    topic: String,

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

/// Creates an Amazon SNS topic.
/// # Arguments
///
/// * `-t TOPIC` - The name of the topic.
/// * `[-r REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_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> {
    tracing_subscriber::fmt::init();

    let Opt {
        region,
        topic,
        verbose,
    } = Opt::from_args();

    let region = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    println!();

    if verbose {
        println!("SNS client version:   {}", PKG_VERSION);
        println!(
            "Region:               {}",
            region.region().unwrap().as_ref()
        );
        println!("Topic:                {}", &topic);
        println!();
    }

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

    let resp = client.create_topic().name(topic).send().await?;

    println!(
        "Created topic with ARN: {}",
        resp.topic_arn.as_deref().unwrap_or_default()
    );

    Ok(())
}
+62 −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 aws_sdk_sns::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region;
use aws_types::region::ProvideRegion;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The AWS Region.
    #[structopt(short, long)]
    region: Option<String>,

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

/// Lists your Amazon SNS topics in the region.
/// # Arguments
///
/// * `[-r REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_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> {
    tracing_subscriber::fmt::init();

    let Opt { region, verbose } = Opt::from_args();

    let region = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    println!();

    if verbose {
        println!("SNS client version:   {}", PKG_VERSION);
        println!(
            "Region:               {}",
            region.region().unwrap().as_ref()
        );

        println!();
    }

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

    let resp = client.list_topics().send().await?;

    println!("Topic ARNs:");

    for topic in resp.topics.unwrap_or_default() {
        println!("{}", topic.topic_arn.as_deref().unwrap_or_default());
    }
    Ok(())
}
+29 −31
Original line number Diff line number Diff line
@@ -2,25 +2,26 @@
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use sns::{Client, Config, Region};
use std::process::exit;

use aws_sdk_sns::{Client, Config, Error, Region, PKG_VERSION};
use aws_types::region;
use aws_types::region::ProvideRegion;

use structopt::StructOpt;

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

    /// Specifies the email address to subscribe to the topic
    /// The email address to subscribe to the topic.
    #[structopt(short, long)]
    email_address: String,

    /// Whether to display additional runtime information
    /// The ARN of the topic.
    #[structopt(short, long)]
    topic_arn: String,

    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}
@@ -31,45 +32,42 @@ struct Opt {
/// # Arguments
///
/// * `-e EMAIL_ADDRESS` - The email address of a user subscribing to the topic.
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
///    If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// * `-t TOPIC_ARN` - The ARN of the topic.
/// * `[-r REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_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<(), sns::Error> {
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        default_region,
        region,
        email_address,
        topic_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"));
    let region = region::ChainProvider::first_try(region.map(Region::new))
        .or_default_provider()
        .or_else(Region::new("us-west-2"));

    println!();

    if verbose {
        println!("SNS client version: {}", sns::PKG_VERSION);
        println!("Region:             {:?}", &region);
        println!("SNS client version:   {}", PKG_VERSION);
        println!(
            "Region:               {}",
            region.region().unwrap().as_ref()
        );
        println!("Email address:        {}", &email_address);
        println!("Topic ARN:            {}", &topic_arn);
        println!();
    }

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

    let topics = client.list_topics().send().await?;
    let mut topics = topics.topics.unwrap_or_default();
    let topic_arn = match topics.pop() {
        Some(topic) => topic.topic_arn.expect("topics have ARNs"),
        None => {
            eprintln!("No topics in this account. Please create a topic to proceed");
            exit(1);
        }
    };

    println!("Receiving on topic with ARN: `{}`", topic_arn);

    let rsp = client