|
| 1 | +use crate::models::list_deployments::{CreationSource, MongoDBPortBinding}; |
| 2 | +use napi_derive::napi; |
| 3 | +use semver::Version; |
| 4 | + |
| 5 | +#[napi(object)] |
| 6 | +pub struct CreateDeploymentOptions { |
| 7 | + // Identifiers |
| 8 | + pub name: Option<String>, |
| 9 | + |
| 10 | + // Image details |
| 11 | + pub image: Option<String>, |
| 12 | + pub mongodb_version: Option<String>, |
| 13 | + |
| 14 | + // Creation source |
| 15 | + pub creation_source: Option<CreationSource>, |
| 16 | + |
| 17 | + // Initial database configuration |
| 18 | + pub local_seed_location: Option<String>, |
| 19 | + pub mongodb_initdb_database: Option<String>, |
| 20 | + pub mongodb_initdb_root_password_file: Option<String>, |
| 21 | + pub mongodb_initdb_root_password: Option<String>, |
| 22 | + pub mongodb_initdb_root_username_file: Option<String>, |
| 23 | + pub mongodb_initdb_root_username: Option<String>, |
| 24 | + |
| 25 | + // Logging |
| 26 | + pub mongot_log_file: Option<String>, |
| 27 | + pub runner_log_file: Option<String>, |
| 28 | + |
| 29 | + // Telemetry |
| 30 | + pub do_not_track: Option<bool>, |
| 31 | + pub telemetry_base_url: Option<String>, |
| 32 | + |
| 33 | + // Port configuration |
| 34 | + pub mongodb_port_binding: Option<MongoDBPortBinding>, |
| 35 | +} |
| 36 | + |
| 37 | +impl From<CreateDeploymentOptions> for atlas_local::models::CreateDeploymentOptions { |
| 38 | + fn from(source: CreateDeploymentOptions) -> Self { |
| 39 | + let version: Option<Version> = match source.mongodb_version.as_deref() { |
| 40 | + Some("latest") => None, |
| 41 | + None => None, |
| 42 | + Some(ver_string) => { |
| 43 | + // If malformed Version if given, it will panic here |
| 44 | + Some(Version::parse(ver_string).expect("Parse version string")) |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + Self { |
| 49 | + name: source.name, |
| 50 | + image: source.image, |
| 51 | + mongodb_version: version, |
| 52 | + creation_source: source |
| 53 | + .creation_source |
| 54 | + .map(atlas_local::models::CreationSource::from), |
| 55 | + local_seed_location: source.local_seed_location, |
| 56 | + mongodb_initdb_database: source.mongodb_initdb_database, |
| 57 | + mongodb_initdb_root_password_file: source.mongodb_initdb_root_password_file, |
| 58 | + mongodb_initdb_root_password: source.mongodb_initdb_root_password, |
| 59 | + mongodb_initdb_root_username_file: source.mongodb_initdb_root_username_file, |
| 60 | + mongodb_initdb_root_username: source.mongodb_initdb_root_username, |
| 61 | + mongot_log_file: source.mongot_log_file, |
| 62 | + runner_log_file: source.runner_log_file, |
| 63 | + do_not_track: source.do_not_track, |
| 64 | + telemetry_base_url: source.telemetry_base_url, |
| 65 | + mongodb_port_binding: source |
| 66 | + .mongodb_port_binding |
| 67 | + .map(atlas_local::models::MongoDBPortBinding::from), |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use crate::models::list_deployments::{BindingType, CreationSourceType}; |
| 75 | + |
| 76 | + use super::*; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_lib_create_deployment_options_from_create_deployment_options() { |
| 80 | + let create_deployment_options = CreateDeploymentOptions { |
| 81 | + name: Some("test_deployment".to_string()), |
| 82 | + image: Some("mongodb/mongodb-atlas-local".to_string()), |
| 83 | + mongodb_version: Some("8.0.0".to_string()), |
| 84 | + creation_source: Some(CreationSource { |
| 85 | + source_type: CreationSourceType::MCPServer, |
| 86 | + source: "MCPSERVER".to_string(), |
| 87 | + }), |
| 88 | + local_seed_location: Some("/host/seed-data".to_string()), |
| 89 | + mongodb_initdb_database: Some("testdb".to_string()), |
| 90 | + mongodb_initdb_root_password_file: Some("/run/secrets/password".to_string()), |
| 91 | + mongodb_initdb_root_password: Some("password123".to_string()), |
| 92 | + mongodb_initdb_root_username_file: Some("/run/secrets/username".to_string()), |
| 93 | + mongodb_initdb_root_username: Some("admin".to_string()), |
| 94 | + mongot_log_file: Some("/tmp/mongot.log".to_string()), |
| 95 | + runner_log_file: Some("/tmp/runner.log".to_string()), |
| 96 | + do_not_track: Some(false), |
| 97 | + telemetry_base_url: Some("https://telemetry.example.com".to_string()), |
| 98 | + mongodb_port_binding: Some(MongoDBPortBinding { |
| 99 | + binding_type: BindingType::Loopback, |
| 100 | + ip: "127.0.0.1".to_string(), |
| 101 | + port: 27017, |
| 102 | + }), |
| 103 | + }; |
| 104 | + let lib_create_deployment_options: atlas_local::models::CreateDeploymentOptions = |
| 105 | + create_deployment_options.into(); |
| 106 | + assert_eq!( |
| 107 | + lib_create_deployment_options.name, |
| 108 | + Some("test_deployment".to_string()) |
| 109 | + ); |
| 110 | + assert_eq!( |
| 111 | + lib_create_deployment_options.image, |
| 112 | + Some("mongodb/mongodb-atlas-local".to_string()) |
| 113 | + ); |
| 114 | + assert_eq!( |
| 115 | + lib_create_deployment_options.mongodb_version, |
| 116 | + Some(Version::new(8, 0, 0)) |
| 117 | + ); |
| 118 | + assert_eq!( |
| 119 | + lib_create_deployment_options.creation_source, |
| 120 | + Some(atlas_local::models::CreationSource::MCPServer) |
| 121 | + ); |
| 122 | + assert_eq!( |
| 123 | + lib_create_deployment_options.local_seed_location, |
| 124 | + Some("/host/seed-data".to_string()) |
| 125 | + ); |
| 126 | + assert_eq!( |
| 127 | + lib_create_deployment_options.mongodb_initdb_database, |
| 128 | + Some("testdb".to_string()) |
| 129 | + ); |
| 130 | + assert_eq!( |
| 131 | + lib_create_deployment_options.mongodb_initdb_root_password_file, |
| 132 | + Some("/run/secrets/password".to_string()) |
| 133 | + ); |
| 134 | + assert_eq!( |
| 135 | + lib_create_deployment_options.mongodb_initdb_root_password, |
| 136 | + Some("password123".to_string()) |
| 137 | + ); |
| 138 | + assert_eq!( |
| 139 | + lib_create_deployment_options.mongodb_initdb_root_username_file, |
| 140 | + Some("/run/secrets/username".to_string()) |
| 141 | + ); |
| 142 | + assert_eq!( |
| 143 | + lib_create_deployment_options.mongodb_initdb_root_username, |
| 144 | + Some("admin".to_string()) |
| 145 | + ); |
| 146 | + assert_eq!( |
| 147 | + lib_create_deployment_options.mongot_log_file, |
| 148 | + Some("/tmp/mongot.log".to_string()) |
| 149 | + ); |
| 150 | + assert_eq!( |
| 151 | + lib_create_deployment_options.runner_log_file, |
| 152 | + Some("/tmp/runner.log".to_string()) |
| 153 | + ); |
| 154 | + assert_eq!(lib_create_deployment_options.do_not_track, Some(false)); |
| 155 | + assert_eq!( |
| 156 | + lib_create_deployment_options.telemetry_base_url, |
| 157 | + Some("https://telemetry.example.com".to_string()) |
| 158 | + ); |
| 159 | + } |
| 160 | +} |
0 commit comments