|
| 1 | +use std::{fmt::Display, net::Ipv4Addr, str::FromStr}; |
| 2 | + |
| 3 | +use reqwest::Client; |
| 4 | +use serde_json::json; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + DeployInput, Error, |
| 8 | + OptionalSupport::{self, Supported}, |
| 9 | + XnodeDeployer, XnodeDeployerError, |
| 10 | + utils::XnodeDeployerErrorInner, |
| 11 | +}; |
| 12 | + |
| 13 | +#[derive(Debug)] |
| 14 | +pub enum HyperstackError { |
| 15 | + ResponseNotObject { |
| 16 | + response: serde_json::Value, |
| 17 | + }, |
| 18 | + ResponseMissingId { |
| 19 | + map: serde_json::Map<String, serde_json::Value>, |
| 20 | + }, |
| 21 | + ResponseMissingInstances { |
| 22 | + map: serde_json::Map<String, serde_json::Value>, |
| 23 | + }, |
| 24 | + ResponseInvalidInstances { |
| 25 | + instances: serde_json::Value, |
| 26 | + }, |
| 27 | + ResponseEmptyInstances {}, |
| 28 | + ResponseInvalidId { |
| 29 | + id: serde_json::Value, |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +impl Display for HyperstackError { |
| 34 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 35 | + f.write_str( |
| 36 | + match self { |
| 37 | + HyperstackError::ResponseNotObject { response } => { |
| 38 | + format!("Hyperstack response not object: {response}") |
| 39 | + } |
| 40 | + HyperstackError::ResponseMissingInstances { map } => { |
| 41 | + format!("Hyperstack response missing instances: {map:?}") |
| 42 | + } |
| 43 | + HyperstackError::ResponseInvalidInstances { instances } => { |
| 44 | + format!("Hyperstack response invalid instances: {instances:?}") |
| 45 | + } |
| 46 | + HyperstackError::ResponseEmptyInstances {} => { |
| 47 | + format!("Hyperstack response empty instances") |
| 48 | + } |
| 49 | + HyperstackError::ResponseMissingId { map } => { |
| 50 | + format!("Hyperstack response missing id: {map:?}") |
| 51 | + } |
| 52 | + HyperstackError::ResponseInvalidId { id } => { |
| 53 | + format!("Hyperstack response invalid id: {id}") |
| 54 | + } |
| 55 | + } |
| 56 | + .as_str(), |
| 57 | + ) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +pub struct HyperstackDeployer { |
| 62 | + client: Client, |
| 63 | + api_key: String, |
| 64 | + hardware: HyperstackHardware, |
| 65 | +} |
| 66 | + |
| 67 | +impl HyperstackDeployer { |
| 68 | + pub fn new(api_key: String, hardware: HyperstackHardware) -> Self { |
| 69 | + Self { |
| 70 | + client: Client::new(), |
| 71 | + api_key, |
| 72 | + hardware, |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl XnodeDeployer for HyperstackDeployer { |
| 78 | + type ProviderOutput = HyperstackOutput; |
| 79 | + |
| 80 | + async fn deploy(&self, input: DeployInput) -> Result<Self::ProviderOutput, Error> { |
| 81 | + log::info!( |
| 82 | + "Hyperstack deployment of {input:?} on {hardware:?} started", |
| 83 | + hardware = self.hardware |
| 84 | + ); |
| 85 | + let response = match &self.hardware { |
| 86 | + HyperstackHardware::VirtualMachine { |
| 87 | + name, |
| 88 | + environment_name, |
| 89 | + flavor_name, |
| 90 | + key_name, |
| 91 | + } => self |
| 92 | + .client |
| 93 | + .post("https://infrahub-api.nexgencloud.com/v1/core/virtual-machines") |
| 94 | + .json(&json!({ |
| 95 | + "name": name, |
| 96 | + "environment_name": environment_name, |
| 97 | + "image_name": "Ubuntu Server 22.04 LTS (Jammy Jellyfish)", |
| 98 | + "flavor_name": flavor_name, |
| 99 | + "key_name": key_name, |
| 100 | + "count": 1, |
| 101 | + "assign_floating_ip": true, |
| 102 | + "user_data": input.cloud_init(), |
| 103 | + "security_rules": [ |
| 104 | + { |
| 105 | + "direction": "ingress", |
| 106 | + "protocol": "tcp", |
| 107 | + "ethertype": "IPv4", |
| 108 | + "remote_ip_prefix": "0.0.0.0/0", |
| 109 | + "port_range_min": 1, |
| 110 | + "port_range_max": 65535 |
| 111 | + }, |
| 112 | + { |
| 113 | + "direction": "ingress", |
| 114 | + "protocol": "udp", |
| 115 | + "ethertype": "IPv4", |
| 116 | + "remote_ip_prefix": "0.0.0.0/0", |
| 117 | + "port_range_min": 1, |
| 118 | + "port_range_max": 65535 |
| 119 | + } |
| 120 | + ] |
| 121 | + })), |
| 122 | + } |
| 123 | + .header("api_key", self.api_key.clone()) |
| 124 | + .send() |
| 125 | + .await |
| 126 | + .and_then(|response| response.error_for_status()) |
| 127 | + .map_err(Error::ReqwestError)? |
| 128 | + .json::<serde_json::Value>() |
| 129 | + .await |
| 130 | + .map_err(Error::ReqwestError)?; |
| 131 | + |
| 132 | + let id = match &response { |
| 133 | + serde_json::Value::Object(map) => map |
| 134 | + .get("instances") |
| 135 | + .ok_or(Error::XnodeDeployerError(XnodeDeployerError::new( |
| 136 | + XnodeDeployerErrorInner::HyperstackError( |
| 137 | + HyperstackError::ResponseMissingInstances { map: map.clone() }, |
| 138 | + ), |
| 139 | + ))) |
| 140 | + .and_then(|instances| match instances { |
| 141 | + serde_json::Value::Array(array) => { |
| 142 | + array |
| 143 | + .first() |
| 144 | + .ok_or(Error::XnodeDeployerError(XnodeDeployerError::new( |
| 145 | + XnodeDeployerErrorInner::HyperstackError( |
| 146 | + HyperstackError::ResponseEmptyInstances {}, |
| 147 | + ), |
| 148 | + ))) |
| 149 | + } |
| 150 | + _ => Err(Error::XnodeDeployerError(XnodeDeployerError::new( |
| 151 | + XnodeDeployerErrorInner::HyperstackError( |
| 152 | + HyperstackError::ResponseInvalidInstances { |
| 153 | + instances: instances.clone(), |
| 154 | + }, |
| 155 | + ), |
| 156 | + ))), |
| 157 | + }) |
| 158 | + .and_then(|instance| match instance { |
| 159 | + serde_json::Value::Object(map) => map |
| 160 | + .get("id") |
| 161 | + .ok_or(Error::XnodeDeployerError(XnodeDeployerError::new( |
| 162 | + XnodeDeployerErrorInner::HyperstackError( |
| 163 | + HyperstackError::ResponseMissingId { map: map.clone() }, |
| 164 | + ), |
| 165 | + ))) |
| 166 | + .and_then(|id| { |
| 167 | + match id { |
| 168 | + serde_json::Value::Number(number) => number.as_u64(), |
| 169 | + _ => None, |
| 170 | + } |
| 171 | + .ok_or(Error::XnodeDeployerError( |
| 172 | + XnodeDeployerError::new(XnodeDeployerErrorInner::HyperstackError( |
| 173 | + HyperstackError::ResponseInvalidId { id: id.clone() }, |
| 174 | + )), |
| 175 | + )) |
| 176 | + }), |
| 177 | + _ => Err(Error::XnodeDeployerError(XnodeDeployerError::new( |
| 178 | + XnodeDeployerErrorInner::HyperstackError( |
| 179 | + HyperstackError::ResponseNotObject { |
| 180 | + response: response.clone(), |
| 181 | + }, |
| 182 | + ), |
| 183 | + ))), |
| 184 | + }), |
| 185 | + _ => Err(Error::XnodeDeployerError(XnodeDeployerError::new( |
| 186 | + XnodeDeployerErrorInner::HyperstackError(HyperstackError::ResponseNotObject { |
| 187 | + response: response.clone(), |
| 188 | + }), |
| 189 | + ))), |
| 190 | + }; |
| 191 | + let id = match id { |
| 192 | + Ok(id) => id, |
| 193 | + Err(e) => return Err(e), |
| 194 | + }; |
| 195 | + |
| 196 | + let output = Self::ProviderOutput { id }; |
| 197 | + log::info!("Hyperstack deployment succeeded: {output:?}"); |
| 198 | + Ok(output) |
| 199 | + } |
| 200 | + |
| 201 | + async fn undeploy(&self, xnode: Self::ProviderOutput) -> Option<Error> { |
| 202 | + let id = xnode.id; |
| 203 | + log::info!("Undeploying hyperstack device {id} started"); |
| 204 | + if let Err(e) = self |
| 205 | + .client |
| 206 | + .delete(format!( |
| 207 | + "https://infrahub-api.nexgencloud.com/v1/core/virtual-machines/{id}" |
| 208 | + )) |
| 209 | + .header("api_key", self.api_key.clone()) |
| 210 | + .send() |
| 211 | + .await |
| 212 | + .and_then(|response| response.error_for_status()) |
| 213 | + { |
| 214 | + return Some(Error::ReqwestError(e)); |
| 215 | + } |
| 216 | + |
| 217 | + log::info!("Undeploying hyperstack device {id} succeeded"); |
| 218 | + None |
| 219 | + } |
| 220 | + |
| 221 | + async fn ipv4( |
| 222 | + &self, |
| 223 | + xnode: &Self::ProviderOutput, |
| 224 | + ) -> Result<OptionalSupport<Option<Ipv4Addr>>, Error> { |
| 225 | + let id = xnode.id; |
| 226 | + let response = self |
| 227 | + .client |
| 228 | + .get(format!( |
| 229 | + "https://infrahub-api.nexgencloud.com/v1/core/virtual-machines/{id}" |
| 230 | + )) |
| 231 | + .header("api_key", self.api_key.clone()) |
| 232 | + .send() |
| 233 | + .await |
| 234 | + .and_then(|response| response.error_for_status()) |
| 235 | + .map_err(Error::ReqwestError)? |
| 236 | + .json::<serde_json::Value>() |
| 237 | + .await |
| 238 | + .map_err(Error::ReqwestError)?; |
| 239 | + |
| 240 | + if let serde_json::Value::Object(map) = &response { |
| 241 | + if let Some(serde_json::Value::Object(instance)) = map.get("instance") { |
| 242 | + if let Some(serde_json::Value::String(floating_ip)) = instance.get("floating_ip") { |
| 243 | + if let Ok(ip) = Ipv4Addr::from_str(floating_ip) { |
| 244 | + return Ok(Supported(Some(ip))); |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + }; |
| 249 | + |
| 250 | + Ok(Supported(None)) |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +#[derive(Debug, Clone)] |
| 255 | +pub struct HyperstackOutput { |
| 256 | + pub id: u64, |
| 257 | +} |
| 258 | + |
| 259 | +#[derive(Debug)] |
| 260 | +pub enum HyperstackHardware { |
| 261 | + // https://docs.hyperstack.cloud/docs/api-reference/core-resources/virtual-machines/vm-core/create-vms |
| 262 | + VirtualMachine { |
| 263 | + name: String, |
| 264 | + environment_name: String, |
| 265 | + flavor_name: String, |
| 266 | + key_name: String, |
| 267 | + }, |
| 268 | +} |
| 269 | + |
| 270 | +#[derive(Debug)] |
| 271 | +pub enum HyperstackUndeployInput { |
| 272 | + VirtualMachine { id: u64 }, |
| 273 | +} |
0 commit comments