@@ -12,11 +12,15 @@ import (
1212)
1313
1414const (
15+ DeleteSuccess = "DELETED"
16+ ErrorStatus = "ERROR"
17+
1518 VolumeAvailableStatus = "AVAILABLE"
16- DeleteSuccess = "DELETED"
17- ErrorStatus = "ERROR"
18- ServerActiveStatus = "ACTIVE"
19- ServerResizingStatus = "RESIZING"
19+
20+ ServerActiveStatus = "ACTIVE"
21+ ServerResizingStatus = "RESIZING"
22+
23+ VirtualIpCreatedStatus = "CREATED"
2024
2125 RequestCreateAction = "CREATE"
2226 RequestUpdateAction = "UPDATE"
@@ -35,6 +39,7 @@ type APIClientInterface interface {
3539 GetServerExecute (ctx context.Context , projectId string , serverId string ) (* iaasalpha.Server , error )
3640 GetProjectRequestExecute (ctx context.Context , projectId string , requestId string ) (* iaasalpha.Request , error )
3741 GetAttachedVolumeExecute (ctx context.Context , projectId string , serverId string , volumeId string ) (* iaasalpha.VolumeAttachment , error )
42+ GetVirtualIPExecute (ctx context.Context , projectId string , networkId string , virtualIpId string ) (* iaasalpha.VirtualIp , error )
3843}
3944
4045// CreateVolumeWaitHandler will wait for volume creation
@@ -291,3 +296,53 @@ func RemoveVolumeFromServerWaitHandler(ctx context.Context, a APIClientInterface
291296 handler .SetTimeout (10 * time .Minute )
292297 return handler
293298}
299+
300+ // CreateVirtualIPWaitHandler will wait for server creation
301+ func CreateVirtualIPWaitHandler (ctx context.Context , a APIClientInterface , projectId , networkId , virtualIpId string ) * wait.AsyncActionHandler [iaasalpha.VirtualIp ] {
302+ handler := wait .New (func () (waitFinished bool , response * iaasalpha.VirtualIp , err error ) {
303+ virtualIp , err := a .GetVirtualIPExecute (ctx , projectId , networkId , virtualIpId )
304+ if err != nil {
305+ return false , virtualIp , err
306+ }
307+ if virtualIp .Id == nil || virtualIp .Status == nil {
308+ return false , virtualIp , fmt .Errorf ("create failed for virtual ip with id %s, the response is not valid: the id or the status are missing" , networkId )
309+ }
310+ if * virtualIp .Id == virtualIpId && * virtualIp .Status == VirtualIpCreatedStatus {
311+ return true , virtualIp , nil
312+ }
313+ if * virtualIp .Id == virtualIpId && * virtualIp .Status == ErrorStatus {
314+ return true , virtualIp , fmt .Errorf ("create failed for virtual ip with id %s" , networkId )
315+ }
316+ return false , virtualIp , nil
317+ })
318+ handler .SetTimeout (15 * time .Minute )
319+ return handler
320+ }
321+
322+ // DeleteVirtualIPWaitHandler will wait for volume deletion
323+ func DeleteVirtualIPWaitHandler (ctx context.Context , a APIClientInterface , projectId , networkId , virtualIpId string ) * wait.AsyncActionHandler [iaasalpha.VirtualIp ] {
324+ handler := wait .New (func () (waitFinished bool , response * iaasalpha.VirtualIp , err error ) {
325+ virtualIp , err := a .GetVirtualIPExecute (ctx , projectId , networkId , virtualIpId )
326+ if err == nil {
327+ if virtualIp != nil {
328+ if virtualIp .Id == nil || virtualIp .Status == nil {
329+ return false , virtualIp , fmt .Errorf ("delete failed for virtual ip with id %s, the response is not valid: the id or the status are missing" , virtualIpId )
330+ }
331+ if * virtualIp .Id == virtualIpId && * virtualIp .Status == DeleteSuccess {
332+ return true , virtualIp , nil
333+ }
334+ }
335+ return false , nil , nil
336+ }
337+ oapiErr , ok := err .(* oapierror.GenericOpenAPIError ) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
338+ if ! ok {
339+ return false , virtualIp , fmt .Errorf ("could not convert error to oapierror.GenericOpenAPIError: %w" , err )
340+ }
341+ if oapiErr .StatusCode != http .StatusNotFound {
342+ return false , virtualIp , err
343+ }
344+ return true , nil , nil
345+ })
346+ handler .SetTimeout (15 * time .Minute )
347+ return handler
348+ }
0 commit comments