Skip to content

Commit

Permalink
Save
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel committed Mar 7, 2024
1 parent 221b882 commit 4e9ac1a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 78 deletions.
1 change: 0 additions & 1 deletion pulumi_rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod grpc;
pub mod pulumi;
pub mod pulumi_logger;
21 changes: 1 addition & 20 deletions pulumi_rust/src/pulumi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,9 @@ impl server::component::pulumi_wasm::external_world::Host for MyState {
}
}

fn extract_file_name(path: &String) -> String {
let file_name_regex: Regex = Regex::new(r"^.*src\\(.*)").unwrap();
file_name_regex.captures(path).unwrap().get(1).unwrap().as_str().to_string()
}

#[async_trait]
impl crate::pulumi::server::component::pulumi_wasm::log::Host for MyState {
async fn log(&mut self, content: crate::pulumi::server::component::pulumi_wasm::log::Content) -> wasmtime::Result<()> {
let normalized_file_name = content.file.map(|s| extract_file_name(&s));

log::logger().log(&log::Record::builder()
.metadata(log::Metadata::builder()
.level(match content.level {
Expand All @@ -76,7 +69,7 @@ impl crate::pulumi::server::component::pulumi_wasm::log::Host for MyState {
)
.args(format_args!("{}", content.args))
.module_path(content.module_path.as_deref())
.file(normalized_file_name.as_deref())
.file(content.file.as_deref())
.line(content.line)
.key_values(&content.key_values.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect::<Vec<(&str, &str)>>())
.build());
Expand Down Expand Up @@ -188,15 +181,3 @@ impl Pulumi {
Ok(())
}
}

#[cfg(test)]
mod test {
use crate::pulumi::extract_file_name;

#[test]
fn should_extract_file_name() {
assert_eq!(extract_file_name(&"src\\main.rs".to_string()), "main.rs");
assert_eq!(extract_file_name(&"pulumi_wasm\\src\\lib.rs".to_string()), "lib.rs");
}

}
46 changes: 0 additions & 46 deletions pulumi_rust/src/pulumi_logger.rs

This file was deleted.

27 changes: 20 additions & 7 deletions pulumi_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::Formatter;
use std::ops::Deref;
use futures::SinkExt;
use lazy_static::lazy_static;
use log::{info, log};
use log::{error, info, log};
use prost::Message;
use prost_types::value::Kind;
use rmpv::{Utf8String, Value};
Expand Down Expand Up @@ -162,8 +162,12 @@ impl GuestOutput for Output {

fn get_field(&self, field: String) -> WasmOutput {
wasm_common::setup_logger();

info!("Getting field [{field}] from Output [TODO]");

let o = output::map_internal(vec![self.output.clone()], move |v| {
let v = v[0].clone();
info!("Value is [{v}]");

let v = match v {
Value::Map(m) => {
Expand All @@ -185,6 +189,8 @@ impl GuestOutput for Output {
Value::Ext(_, _) => todo!(),
};

info!("Result is [{v}]");

v
});

Expand Down Expand Up @@ -263,15 +269,18 @@ impl function_reverse_callback::Guest for Component {
}

fn messagepack_to_protoc(v: &Value) -> prost_types::Value {
match v {
info!("Converting value [{v}] to protoc value");
let result = match v {
Value::Integer(i) => prost_types::Value {
kind: Option::from(prost_types::value::Kind::NumberValue(i.as_f64().unwrap())),
},
_ => {
eprintln!("Cannot convert [{v}]");
error!("Cannot convert [{v}]");
todo!("Cannot convert [{v}]")
}
}
};
info!("Result: [{result:?}]");
result
}

impl register_interface::Guest for Component {
Expand All @@ -283,6 +292,8 @@ impl register_interface::Guest for Component {

let new_output = output::map_internal(values, move |v| {

info!("Converting values [{v:?}] with names [{names:?}]");

let pairs = names.iter().zip(v.iter()).map(|(name, value)| {
let v = messagepack_to_protoc(value);
(name.clone(), v)
Expand All @@ -292,6 +303,8 @@ impl register_interface::Guest for Component {
fields: BTreeMap::from_iter(pairs)
};

info!("Resulting object: [{object:?}]");

let request = grpc::RegisterResourceRequest {
r#type: request.type_.clone(),
name: request.name.clone(),
Expand Down Expand Up @@ -332,12 +345,12 @@ impl register_interface::Guest for Component {

let result_vec = external_world::register_resource(vec_request.as_slice());

let result = grpc::RegisterResourceResponse::decode(&mut result_vec.as_slice()).unwrap();
let response = grpc::RegisterResourceResponse::decode(&mut result_vec.as_slice()).unwrap();

info!("Result: {result:?}");
info!("Response: [{response:?}]");

let result = if (!is_in_preview()) {
let result = result.object.unwrap().fields.get("result").unwrap().clone().kind.unwrap();
let result = response.object.unwrap().fields.get("result").unwrap().clone().kind.unwrap();

match result {
Kind::NullValue(_) => todo!(),
Expand Down
4 changes: 0 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ async fn main() -> Result<(), Error> {
let _pulumi_engine_url = std::env::var("PULUMI_ENGINE")?;
let pulumi_monitor_url = std::env::var("PULUMI_MONITOR")?;

info!("INFO LOG");
warn!("WARN LOG");
error!("ERROR LOG");

match &args.command {
Command::Run => {
let pulumi = Pulumi::create(&args.global_opts.wasm, &Some(pulumi_monitor_url)).await?;
Expand Down

0 comments on commit 4e9ac1a

Please sign in to comment.