1- use std:: { fmt:: Display , time :: Duration } ;
1+ use std:: { fmt:: Display , net :: Ipv4Addr , str :: FromStr } ;
22
33use reqwest:: Client ;
44use serde_json:: json;
5- use tokio:: time:: sleep;
65
76use crate :: {
8- DeployInput , DeployOutput , Error , XnodeDeployer , XnodeDeployerError ,
7+ DeployInput , Error ,
8+ OptionalSupport :: { self , Supported } ,
9+ XnodeDeployer , XnodeDeployerError ,
910 utils:: XnodeDeployerErrorInner ,
1011} ;
1112
@@ -60,15 +61,12 @@ impl HivelocityDeployer {
6061impl XnodeDeployer for HivelocityDeployer {
6162 type ProviderOutput = HivelocityOutput ;
6263
63- async fn deploy (
64- & self ,
65- input : DeployInput ,
66- ) -> Result < DeployOutput < Self :: ProviderOutput > , Error > {
64+ async fn deploy ( & self , input : DeployInput ) -> Result < Self :: ProviderOutput , Error > {
6765 log:: info!(
6866 "Hivelocity deployment of {input:?} on {hardware:?} started" ,
6967 hardware = self . hardware
7068 ) ;
71- let mut response = match & self . hardware {
69+ let response = match & self . hardware {
7270 HivelocityHardware :: BareMetal {
7371 location_name,
7472 period,
@@ -147,45 +145,14 @@ impl XnodeDeployer for HivelocityDeployer {
147145 Err ( e) => return Err ( e) ,
148146 } ;
149147
150- let mut ip = "0.0.0.0" . to_string ( ) ;
151- while ip == "0.0.0.0" {
152- log:: info!( "Getting ip address of hivelocity device {device_id}" , ) ;
153- if let serde_json:: Value :: Object ( map) = & response {
154- if let Some ( serde_json:: Value :: String ( primary_ip) ) = map. get ( "primaryIp" ) {
155- ip = primary_ip. clone ( ) ;
156- }
157- } ;
158-
159- sleep ( Duration :: from_secs ( 1 ) ) . await ;
160- let scope = match self . hardware {
161- HivelocityHardware :: BareMetal { .. } => "bare-metal-devices" ,
162- HivelocityHardware :: Compute { .. } => "compute" ,
163- } ;
164- response = self
165- . client
166- . get ( format ! (
167- "https://core.hivelocity.net/api/v2/{scope}/{device_id}"
168- ) )
169- . header ( "X-API-KEY" , self . api_key . clone ( ) )
170- . send ( )
171- . await
172- . map_err ( Error :: ReqwestError ) ?
173- . json :: < serde_json:: Value > ( )
174- . await
175- . map_err ( Error :: ReqwestError ) ?;
176- }
177-
178- let output = DeployOutput :: < Self :: ProviderOutput > {
179- ip,
180- provider : HivelocityOutput { device_id } ,
181- } ;
148+ let output = Self :: ProviderOutput { device_id } ;
182149 log:: info!( "Hivelocity deployment succeeded: {output:?}" ) ;
183150 Ok ( output)
184151 }
185152
186- async fn undeploy ( & self , xnode : DeployOutput < Self :: ProviderOutput > ) -> Option < Error > {
187- let device_id = xnode. provider . device_id ;
188- log:: info!( "Undeploying hivelocity device {device_id} started" , ) ;
153+ async fn undeploy ( & self , xnode : Self :: ProviderOutput ) -> Option < Error > {
154+ let device_id = xnode. device_id ;
155+ log:: info!( "Undeploying hivelocity device {device_id} started" ) ;
189156 let scope = match self . hardware {
190157 HivelocityHardware :: BareMetal { .. } => "bare-metal-devices" ,
191158 HivelocityHardware :: Compute { .. } => "compute" ,
@@ -206,6 +173,40 @@ impl XnodeDeployer for HivelocityDeployer {
206173 log:: info!( "Undeploying hivelocity device {device_id} succeeded" ) ;
207174 None
208175 }
176+
177+ async fn ipv4 (
178+ & self ,
179+ xnode : Self :: ProviderOutput ,
180+ ) -> Result < OptionalSupport < Option < Ipv4Addr > > , Error > {
181+ let device_id = xnode. device_id ;
182+ let scope = match self . hardware {
183+ HivelocityHardware :: BareMetal { .. } => "bare-metal-devices" ,
184+ HivelocityHardware :: Compute { .. } => "compute" ,
185+ } ;
186+ let response = self
187+ . client
188+ . get ( format ! (
189+ "https://core.hivelocity.net/api/v2/{scope}/{device_id}"
190+ ) )
191+ . header ( "X-API-KEY" , self . api_key . clone ( ) )
192+ . send ( )
193+ . await
194+ . and_then ( |response| response. error_for_status ( ) )
195+ . map_err ( Error :: ReqwestError ) ?
196+ . json :: < serde_json:: Value > ( )
197+ . await
198+ . map_err ( Error :: ReqwestError ) ?;
199+
200+ if let serde_json:: Value :: Object ( map) = & response {
201+ if let Some ( serde_json:: Value :: String ( primary_ip) ) = map. get ( "primaryIp" ) {
202+ if let Ok ( ip) = Ipv4Addr :: from_str ( primary_ip) {
203+ return Ok ( Supported ( Some ( ip) ) ) ;
204+ }
205+ }
206+ } ;
207+
208+ Ok ( Supported ( None ) )
209+ }
209210}
210211
211212#[ derive( Debug ) ]
0 commit comments