...
 
Commits (4)
/target
**/*.rs.bk
Cargo.lock
template.json
template.yml
......@@ -3,17 +3,24 @@ use cumulus::parameter::Parameter;
use cumulus::Template;
pub fn template() -> Template {
let proxy1 = Instance::new("proxy1").instance_type("t2.micro");
let proxy2 = Instance::new("proxy2").instance_type("t2.micro");
let env = Parameter::image_id("ImageId")
.description("AMI image id for proxy instance")
.default("ami-0ff8a91507f77f868");
let proxy1 = Instance::new("proxy1")
.instance_type("t2.micro")
.image_id(env.r#ref());
let proxy2 = Instance::new("proxy2")
.instance_type("t2.micro")
.image_id("ami-0ff8a91507f77f867");
let eip1 = EIp::vpc("proxyip1");
let eip2 = EIp::vpc("proxyip2");
let scale = Parameter::string("scale");
let mut template = Template::new();
template
.description("Proxy Farm")
.parameter(scale)
.parameter(env)
.resource(proxy1)
.resource(eip1)
.resource(proxy2)
......
......@@ -2,6 +2,7 @@ use serde::Serialize;
use crate::resource::ToResource;
use crate::skip_serializing_if::SkipSerializingIf;
use crate::ValueOrRef;
use super::Affinity;
use super::BlockDeviceMapping;
......@@ -72,7 +73,7 @@ pub struct Instance {
// "ElasticInferenceAccelerators" : [ ElasticInferenceAccelerator, ... ],
// "HostId" : String,
// "IamInstanceProfile" : String,
image_id: String,
image_id: ValueOrRef,
// "InstanceInitiatedShutdownBehavior" : String,
instance_type: InstanceType,
// "Ipv6AddressCount" : Integer,
......@@ -123,8 +124,8 @@ impl Instance {
}
}
pub fn image_id(self, image: impl AsRef<str>) -> Self {
let image_id = image.as_ref().to_string();
pub fn image_id(self, image: impl Into<crate::ValueOrRef>) -> Self {
let image_id = image.into();
Self { image_id, ..self }
}
}
......
......@@ -21,7 +21,9 @@ pub mod parameter;
pub mod resource;
pub mod transform;
mod r#ref;
mod skip_serializing_if;
mod template;
pub use r#ref::{Ref, ValueOrRef};
pub use template::Template;
use serde::Serialize;
use crate::skip_serializing_if::SkipSerializingIf;
use crate::Ref;
/// CloudFormation Template Parameter
/// See [AWS CloudFormation Documenation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html)
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Parameter {
......@@ -31,26 +34,57 @@ pub struct Parameter {
}
impl Parameter {
pub fn string(name: impl ToString) -> Self {
let name = name.to_string();
/// Create new `Parameter` of `string` type
pub fn string(name: impl Into<String>) -> Self {
let name = name.into();
let r#type = DataType::String;
Self {
name,
r#type,
..Self::default()
..<Self as Default>::default()
}
}
pub fn number(name: impl ToString) -> Self {
let name = name.to_string();
/// Create new `Parameter` of `number` type
pub fn number(name: impl Into<String>) -> Self {
let name = name.into();
let r#type = DataType::Number;
Self {
name,
r#type,
..Self::default()
..<Self as Default>::default()
}
}
pub fn image_id(name: impl Into<String>) -> Self {
let name = name.into();
let r#type = DataType::AwsEc2ImageId;
Self {
name,
r#type,
..<Self as Default>::default()
}
}
/// Add parameter description
pub fn description(self, description: impl Into<String>) -> Self {
let description = description.into();
Self {
description,
..self
}
}
/// Add parameter default value
pub fn default(self, default: impl Into<String>) -> Self {
let default = default.into();
Self { default, ..self }
}
pub fn r#ref(&self) -> Ref {
Ref::new(&self.name)
}
pub(crate) fn name(&self) -> String {
self.name.clone()
}
......@@ -60,6 +94,8 @@ impl Parameter {
pub enum DataType {
String,
Number,
#[serde(rename = "AWS::EC2::Image::Id")]
AwsEc2ImageId,
}
impl Default for DataType {
......
use serde::Serialize;
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Ref {
r#ref: String,
}
impl Ref {
pub(crate) fn new(r#ref: &str) -> Self {
let r#ref = r#ref.to_string();
Self { r#ref }
}
}
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum ValueOrRef {
Value(String),
Ref(Ref),
}
impl From<Ref> for ValueOrRef {
fn from(r#ref: Ref) -> Self {
Self::Ref(r#ref)
}
}
impl<T> From<T> for ValueOrRef
where
T: Into<String>,
{
fn from(value: T) -> Self {
let value = value.into();
Self::Value(value)
}
}
impl Default for ValueOrRef {
fn default() -> Self {
Self::Value(String::new())
}
}