From 54b1fdc3632474f85d1cc7b8e64d9c59b49903ab Mon Sep 17 00:00:00 2001 From: Andrzej Ressel Date: Sun, 1 Dec 2024 19:17:05 +0100 Subject: [PATCH] Support resources without parameters --- .../src/resource/worker_route.rs | 31 +++++---- .../src/resource/workers_route.rs | 31 +++++---- .../src/resource/network.rs | 14 ++-- .../src/resource/volume.rs | 14 ++-- .../src/code_generation.rs | 9 ++- pulumi_wasm_generator_lib/src/yaml/mod.rs | 67 +++++++++++++++++++ .../src/yaml/yaml_model.rs | 44 ++++++------ 7 files changed, 155 insertions(+), 55 deletions(-) diff --git a/providers/pulumi_wasm_provider_cloudflare_rust/src/resource/worker_route.rs b/providers/pulumi_wasm_provider_cloudflare_rust/src/resource/worker_route.rs index ed5f1cd67..36786f244 100644 --- a/providers/pulumi_wasm_provider_cloudflare_rust/src/resource/worker_route.rs +++ b/providers/pulumi_wasm_provider_cloudflare_rust/src/resource/worker_route.rs @@ -3,19 +3,24 @@ //! ## Example Usage //! //! -//! ```yaml -//! resources: -//! # Runs the specified worker script for all URLs that match `example.com/*` -//! myRoute: -//! type: cloudflare:WorkerRoute -//! name: my_route -//! properties: -//! zoneId: 0da42c8d2132a9ddaf714f9e7c920711 -//! pattern: example.com/* -//! scriptName: ${myScript.name} -//! myScript: -//! type: cloudflare:WorkersScript -//! name: my_script +//! ```rust +//! use pulumi_wasm_rust::Output; +//! use pulumi_wasm_rust::{add_export, pulumi_main}; +//! #[pulumi_main] +//! fn test_main() -> Result<(), Error> { +//! let myRoute = worker_route::create( +//! "myRoute", +//! WorkerRouteArgs::builder() +//! .pattern("example.com/*") +//! .script_name("${myScript.name}") +//! .zone_id("0da42c8d2132a9ddaf714f9e7c920711") +//! .build_struct(), +//! ); +//! let myScript = workers_script::create( +//! "myScript", +//! WorkersScriptArgs::builder().build_struct(), +//! ); +//! } //! ``` //! //! diff --git a/providers/pulumi_wasm_provider_cloudflare_rust/src/resource/workers_route.rs b/providers/pulumi_wasm_provider_cloudflare_rust/src/resource/workers_route.rs index 6def71c50..0c729dce8 100644 --- a/providers/pulumi_wasm_provider_cloudflare_rust/src/resource/workers_route.rs +++ b/providers/pulumi_wasm_provider_cloudflare_rust/src/resource/workers_route.rs @@ -3,19 +3,24 @@ //! ## Example Usage //! //! -//! ```yaml -//! resources: -//! # Runs the specified worker script for all URLs that match `example.com/*` -//! myRoute: -//! type: cloudflare:WorkersRoute -//! name: my_route -//! properties: -//! zoneId: 0da42c8d2132a9ddaf714f9e7c920711 -//! pattern: example.com/* -//! scriptName: ${myScript.name} -//! myScript: -//! type: cloudflare:WorkersScript -//! name: my_script +//! ```rust +//! use pulumi_wasm_rust::Output; +//! use pulumi_wasm_rust::{add_export, pulumi_main}; +//! #[pulumi_main] +//! fn test_main() -> Result<(), Error> { +//! let myRoute = workers_route::create( +//! "myRoute", +//! WorkersRouteArgs::builder() +//! .pattern("example.com/*") +//! .script_name("${myScript.name}") +//! .zone_id("0da42c8d2132a9ddaf714f9e7c920711") +//! .build_struct(), +//! ); +//! let myScript = workers_script::create( +//! "myScript", +//! WorkersScriptArgs::builder().build_struct(), +//! ); +//! } //! ``` //! //! diff --git a/providers/pulumi_wasm_provider_docker_rust/src/resource/network.rs b/providers/pulumi_wasm_provider_docker_rust/src/resource/network.rs index 78684b5d9..93d825ae7 100644 --- a/providers/pulumi_wasm_provider_docker_rust/src/resource/network.rs +++ b/providers/pulumi_wasm_provider_docker_rust/src/resource/network.rs @@ -4,10 +4,16 @@ //! ## Example Usage //! //! -//! ```yaml -//! resources: -//! privateNetwork: -//! type: docker:Network +//! ```rust +//! use pulumi_wasm_rust::Output; +//! use pulumi_wasm_rust::{add_export, pulumi_main}; +//! #[pulumi_main] +//! fn test_main() -> Result<(), Error> { +//! let privateNetwork = network::create( +//! "privateNetwork", +//! NetworkArgs::builder().build_struct(), +//! ); +//! } //! ``` //! //! diff --git a/providers/pulumi_wasm_provider_docker_rust/src/resource/volume.rs b/providers/pulumi_wasm_provider_docker_rust/src/resource/volume.rs index 5ba9ba112..2212de2d8 100644 --- a/providers/pulumi_wasm_provider_docker_rust/src/resource/volume.rs +++ b/providers/pulumi_wasm_provider_docker_rust/src/resource/volume.rs @@ -4,10 +4,16 @@ //! ## Example Usage //! //! -//! ```yaml -//! resources: -//! sharedVolume: -//! type: docker:Volume +//! ```rust +//! use pulumi_wasm_rust::Output; +//! use pulumi_wasm_rust::{add_export, pulumi_main}; +//! #[pulumi_main] +//! fn test_main() -> Result<(), Error> { +//! let sharedVolume = volume::create( +//! "sharedVolume", +//! VolumeArgs::builder().build_struct(), +//! ); +//! } //! ``` //! //! diff --git a/pulumi_wasm_generator_lib/src/code_generation.rs b/pulumi_wasm_generator_lib/src/code_generation.rs index eddce741a..79d34ceb9 100644 --- a/pulumi_wasm_generator_lib/src/code_generation.rs +++ b/pulumi_wasm_generator_lib/src/code_generation.rs @@ -137,7 +137,7 @@ fn generate_expression(expr: Expression) -> String { #[cfg(test)] mod tests { use crate::code_generation::generate_code; - use crate::yaml::tests::{access_rule, example_array}; + use crate::yaml::tests::{access_rule, example_array, example_empty_properties}; #[test] fn test_example_array() { @@ -152,4 +152,11 @@ mod tests { let code = generate_code(model).unwrap(); assert_eq!(access_rule::get_rust_code(), code) } + + #[test] + fn test_without_parameters() { + let model = example_empty_properties::get_model(); + let code = generate_code(model).unwrap(); + assert_eq!(example_empty_properties::get_rust_code(), code) + } } diff --git a/pulumi_wasm_generator_lib/src/yaml/mod.rs b/pulumi_wasm_generator_lib/src/yaml/mod.rs index a168a8f3e..3e921f569 100644 --- a/pulumi_wasm_generator_lib/src/yaml/mod.rs +++ b/pulumi_wasm_generator_lib/src/yaml/mod.rs @@ -549,6 +549,73 @@ resources: } } + pub(crate) mod example_empty_properties { + use super::*; + use crate::model::ElementId; + use crate::yaml::model::{Example, Resource}; + + pub const YAML: &str = r#" +resources: + myCert: + type: cloudflare:AccessMutualTlsCertificate + name: my_cert +"#; + + pub fn get_yaml_file() -> super::YamlFile { + use crate::yaml::yaml_model::{YamlFile, YamlResource}; + + YamlFile { + resources: { + let mut resources = BTreeMap::new(); + resources.insert( + "myCert".to_string(), + YamlResource { + type_: "cloudflare:AccessMutualTlsCertificate".to_string(), + name: Some("my_cert".to_string()), + properties: BTreeMap::new(), + }, + ); + resources + }, + } + } + + pub fn get_model() -> Example { + Example { + resources: { + let mut map = BTreeMap::new(); + map.insert( + "myCert".to_string(), + Resource { + type_: ElementId::new("cloudflare:index/accessMutualTlsCertificate:AccessMutualTlsCertificate").unwrap(), + // type_: "cloudflare:AccessMutualTlsCertificate".to_string(), + name: Some("my_cert".to_string()), + properties: BTreeMap::new() + }, + ); + map + }, + } + } + + pub fn get_rust_code() -> String { + reformat_code( + r#" + use pulumi_wasm_rust::Output; + use pulumi_wasm_rust::{add_export, pulumi_main}; + #[pulumi_main] + fn test_main() -> Result<(), Error> { + let myCert = access_mutual_tls_certificate::create( + "myCert", + AccessMutualTlsCertificateArgs::builder() + .build_struct(), + ); + } + "#, + ) + } + } + fn reformat_code(code: &str) -> String { let syntax_tree = syn::parse_file(code).unwrap(); prettyplease::unparse(&syntax_tree) diff --git a/pulumi_wasm_generator_lib/src/yaml/yaml_model.rs b/pulumi_wasm_generator_lib/src/yaml/yaml_model.rs index 470e35538..ced5174ef 100644 --- a/pulumi_wasm_generator_lib/src/yaml/yaml_model.rs +++ b/pulumi_wasm_generator_lib/src/yaml/yaml_model.rs @@ -11,6 +11,7 @@ pub(crate) struct YamlResource { #[serde(rename = "type")] pub(crate) type_: String, pub(crate) name: Option, + #[serde(default)] pub(crate) properties: BTreeMap, } @@ -36,46 +37,49 @@ mod tests { #[test] fn test_array_yaml_deserialization() { - use super::super::tests::example_array; - let yaml = example_array::YAML; - let yaml_file = YamlFile::from_yaml(yaml).unwrap(); - let expected_yaml_file = example_array::get_yaml_file(); + use super::super::tests::example_array::*; + let yaml_file = YamlFile::from_yaml(YAML).unwrap(); + let expected_yaml_file = get_yaml_file(); assert_eq!(yaml_file, expected_yaml_file); } #[test] fn test_complex_yaml_deserialization() { - use super::super::tests::complex_yaml; - let yaml = complex_yaml::YAML; - let yaml_file = YamlFile::from_yaml(yaml).unwrap(); - let expected_yaml_file = complex_yaml::get_yaml_file(); + use super::super::tests::complex_yaml::*; + let yaml_file = YamlFile::from_yaml(YAML).unwrap(); + let expected_yaml_file = get_yaml_file(); assert_eq!(yaml_file, expected_yaml_file); } #[test] fn test_access_rule_yaml_deserialization() { - use super::super::tests::access_rule; - let yaml = access_rule::YAML; - let yaml_file = YamlFile::from_yaml(yaml).unwrap(); - let expected_yaml_file = access_rule::get_yaml_file(); + use super::super::tests::access_rule::*; + let yaml_file = YamlFile::from_yaml(YAML).unwrap(); + let expected_yaml_file = get_yaml_file(); assert_eq!(yaml_file, expected_yaml_file); } #[test] fn test_access_organization_yaml_deserialization() { - use super::super::tests::example_access_organization; - let yaml = example_access_organization::YAML; - let yaml_file = YamlFile::from_yaml(yaml).unwrap(); - let expected_yaml_file = example_access_organization::get_yaml_file(); + use super::super::tests::example_access_organization::*; + let yaml_file = YamlFile::from_yaml(YAML).unwrap(); + let expected_yaml_file = get_yaml_file(); assert_eq!(yaml_file, expected_yaml_file); } #[test] fn test_example_variable() { - use super::super::tests::example_variable; - let yaml = example_variable::YAML; - let yaml_file = YamlFile::from_yaml(yaml).unwrap(); - let expected_yaml_file = example_variable::get_yaml_file(); + use super::super::tests::example_variable::*; + let yaml_file = YamlFile::from_yaml(YAML).unwrap(); + let expected_yaml_file = get_yaml_file(); + assert_eq!(yaml_file, expected_yaml_file); + } + + #[test] + fn test_without_parameters() { + use super::super::tests::example_empty_properties::*; + let yaml_file = YamlFile::from_yaml(YAML).unwrap(); + let expected_yaml_file = get_yaml_file(); assert_eq!(yaml_file, expected_yaml_file); } }