Unverified Commit 5979b6da authored by Doug's avatar Doug Committed by GitHub
Browse files

Moved SSM code examples to ssm directory; add a describe-parameters code example (#501)



* Moved SSM code examples to ssm directory; add a describe-parameters code example

* Renamed ssm-helloworld.rs to create-parameter.rs, as that's what it does

* Refactored response to remove unused parameter

* Updated SSM code examples to use tracing_subscriber::fmt::init()

Co-authored-by: default avatarRussell Cohen <rcoh@amazon.com>
parent 04dfa496
Loading
Loading
Loading
Loading
+0 −43
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 std::process;

use ssm::model::ParameterType;
use ssm::{Client, Config, Region};

use aws_types::region::{self, ProvideRegion};

#[tokio::main]
async fn main() {
    // Determine the region from environment variables or default to us-east-1
    let region = region::default_provider()
        .region()
        .unwrap_or_else(|| Region::new("us-east-1"));

    // Construct a client
    let config = Config::builder().region(region).build();
    let client = Client::from_conf(config);

    // Put an SSM application parameter named `test_parameter_name`
    match client
        .put_parameter()
        .overwrite(true)
        .r#type(ParameterType::String)
        .name("test_parameter_name")
        .value("some_value")
        .description("some description")
        .send()
        .await
    {
        Ok(response) => {
            println!("Success! Parameter now has version: {}", response.version)
        }
        Err(error) => {
            println!("Got an error putting the parameter: {}", error);
            process::exit(1);
        }
    }
}
+5 −2
Original line number Diff line number Diff line
[package]
name = "ssm-put-parameter"
name = "ssm-code-examples"
version = "0.1.0"
authors = ["John DiSanti <jdisanti@amazon.com>"]
authors = ["John DiSanti <jdisanti@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
@@ -10,3 +10,6 @@ edition = "2018"
ssm = { package = "aws-sdk-ssm", path = "../../build/aws-sdk/ssm" }
tokio = { version = "1", features = ["full"] }
aws-types = { path = "../../build/aws-sdk/aws-types" }

structopt = { version = "0.3", default-features = false }
tracing-subscriber = { version = "0.2.16", features = ["fmt"] }
+94 −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 std::process;

use ssm::model::ParameterType;
use ssm::{Client, Config, Region};

use aws_types::region::{EnvironmentProvider, ProvideRegion};

use structopt::StructOpt;

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

    /// The parameter name
    #[structopt(short, long)]
    name: String,

    /// The parameter value
    #[structopt(short, long)]
    parameter_value: String,

    /// The parameter description
    #[structopt(short, long)]
    description: String,

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

/// Creates a new AWS Systems Manager parameter.
/// # Arguments
///
/// * `-n NAME` - The name of the parameter.
/// * `-p PARAMETER_VALUE` - The value of the parameter.
/// * `-d DESCRIPTION` - The description of the parameter.
/// * `[-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 {
        name,
        parameter_value,
        description,
        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!("SSM client version:   {}", ssm::PKG_VERSION);
        println!("Region:               {:?}", &region);
        println!("Parameter name:       {}", name);
        println!("Paramter value:       {}", parameter_value);
        println!("Paramter description: {}", description);

        tracing_subscriber::fmt::init();
    }

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

    match client
        .put_parameter()
        .overwrite(true)
        .r#type(ParameterType::String)
        .name(name)
        .value(parameter_value)
        .description(description)
        .send()
        .await
    {
        Ok(response) => {
            println!("Success! Parameter now has version: {}", response.version)
        }
        Err(error) => {
            println!("Got an error putting the parameter: {}", error);
            process::exit(1);
        }
    }
}
+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 std::process;

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

use aws_types::region::{EnvironmentProvider, ProvideRegion};

use structopt::StructOpt;

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

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

/// Lists the names of your AWS Systems Manager parameters.
/// # 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() {
    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!("SSM client version:   {}", ssm::PKG_VERSION);
        println!("Region:               {:?}", &region);

        tracing_subscriber::fmt::init();
    }

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

    println!("Parameter names:");

    match client.describe_parameters().send().await {
        Ok(response) => {
            for param in response.parameters.unwrap().iter() {
                match &param.name {
                    None => {}
                    Some(n) => {
                        println!("  {}", n);
                    }
                }
            }
        }
        Err(error) => {
            println!("Got an error listing the parameter names: {}", error);
            process::exit(1);
        }
    }

    println!();
}
+94 −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 std::process;

use ssm::model::ParameterType;
use ssm::{Client, Config, Region};

use aws_types::region::{EnvironmentProvider, ProvideRegion};

use structopt::StructOpt;

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

    /// The parameter name
    #[structopt(short, long)]
    name: String,

    /// The parameter value
    #[structopt(short, long)]
    parameter_value: String,

    /// The parameter description
    #[structopt(short, long)]
    description: String,

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

/// Creates a new AWS Systems Manager parameter.
/// # Arguments
///
/// * `-n NAME` - The name of the parameter.
/// * `-p PARAMETER_VALUE` - The value of the parameter.
/// * `-d DESCRIPTION` - The description of the parameter.
/// * `[-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 {
        name,
        parameter_value,
        description,
        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!("SSM client version:   {}", ssm::PKG_VERSION);
        println!("Region:               {:?}", &region);
        println!("Parameter name:       {}", name);
        println!("Paramter value:       {}", parameter_value);
        println!("Paramter description: {}", description);

        tracing_subscriber::fmt::init();
    }

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

    match client
        .put_parameter()
        .overwrite(true)
        .r#type(ParameterType::String)
        .name(name)
        .value(parameter_value)
        .description(description)
        .send()
        .await
    {
        Ok(response) => {
            println!("Success! Parameter now has version: {}", response.version)
        }
        Err(error) => {
            println!("Got an error putting the parameter: {}", error);
            process::exit(1);
        }
    }
}