29
29
import io .netris .api .v2 .VpcApi ;
30
30
import io .netris .model .AllocationBody ;
31
31
import io .netris .model .AllocationBodyVpc ;
32
+ import io .netris .model .FilterBySites ;
33
+ import io .netris .model .FilterByVpc ;
32
34
import io .netris .model .GetSiteBody ;
33
35
import io .netris .model .InlineResponse2004 ;
34
36
import io .netris .model .IpTreeAllocationTenant ;
37
+ import io .netris .model .IpTreeSubnet ;
35
38
import io .netris .model .IpTreeSubnetSites ;
36
39
import io .netris .model .SitesResponseOK ;
37
40
import io .netris .model .SubnetBody ;
41
+ import io .netris .model .SubnetResBody ;
38
42
import io .netris .model .VPCAdminTenant ;
39
43
import io .netris .model .VPCCreate ;
40
44
import io .netris .model .VPCListing ;
49
53
import io .netris .model .VnetAddBodyGateways ;
50
54
import io .netris .model .VnetAddBodyVpc ;
51
55
import io .netris .model .VnetResAddBody ;
56
+ import io .netris .model .VnetResDeleteBody ;
57
+ import io .netris .model .VnetResListBody ;
58
+ import io .netris .model .VnetsBody ;
52
59
import io .netris .model .response .AuthResponse ;
53
60
import io .netris .model .response .TenantResponse ;
54
61
import io .netris .model .response .TenantsResponse ;
55
62
import org .apache .cloudstack .agent .api .CreateNetrisVnetCommand ;
56
63
import org .apache .cloudstack .agent .api .CreateNetrisVpcCommand ;
64
+ import org .apache .cloudstack .agent .api .DeleteNetrisVnetCommand ;
57
65
import org .apache .cloudstack .agent .api .DeleteNetrisVpcCommand ;
58
66
import org .apache .cloudstack .resource .NetrisResourceObjectUtils ;
59
67
import org .apache .commons .collections .CollectionUtils ;
63
71
import java .math .BigDecimal ;
64
72
import java .util .ArrayList ;
65
73
import java .util .List ;
74
+ import java .util .Objects ;
66
75
import java .util .stream .Collectors ;
67
76
68
77
public class NetrisApiClientImpl implements NetrisApiClient {
@@ -238,6 +247,10 @@ private void deleteVpcIpamAllocationInternal(VPCListing vpcResource, String vpcC
238
247
VpcApi vpcApi = apiClient .getApiStubForMethod (VpcApi .class );
239
248
VPCResponseResourceOK vpcResourcesResponse = vpcApi .apiV2VpcVpcIdResourcesGet (vpcResource .getId ());
240
249
VPCResourceIpam vpcAllocationResource = getVpcAllocationResource (vpcResourcesResponse );
250
+ if (Objects .isNull (vpcAllocationResource )) {
251
+ logger .info ("No VPC IPAM Allocation found for VPC {}" , vpcCidr );
252
+ return ;
253
+ }
241
254
IpamApi ipamApi = apiClient .getApiStubForMethod (IpamApi .class );
242
255
logger .debug ("Removing the IPAM allocation {} with ID {}" , vpcAllocationResource .getName (), vpcAllocationResource .getId ());
243
256
ipamApi .apiV2IpamTypeIdDelete ("allocation" , vpcAllocationResource .getId ());
@@ -304,11 +317,11 @@ public boolean createVnet(CreateNetrisVnetCommand cmd) {
304
317
String vnetCidr = cmd .getCidr ();
305
318
boolean isVpc = cmd .isVpc ();
306
319
307
- String suffix = String . format ( "%s-%s" , vpcId , vpcName );
320
+ String suffix = getNetrisVpcNameSuffix ( vpcId , vpcName , networkId , networkName , isVpc );
308
321
String netrisVpcName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .VPC , suffix );
309
322
VPCListing associatedVpc = getVpcByNameAndTenant (netrisVpcName );
310
323
if (associatedVpc == null ) {
311
- logger .error (String . format ( "Failed to find Netris VPC with name: %s , to create the corresponding vNet for network %s " , vpcName , networkName ) );
324
+ logger .error ("Failed to find Netris VPC with name: {} , to create the corresponding vNet for network {} " , netrisVpcName , networkName );
312
325
return false ;
313
326
}
314
327
@@ -324,20 +337,102 @@ public boolean createVnet(CreateNetrisVnetCommand cmd) {
324
337
InlineResponse2004 subnetResponse = createVpcSubnetInternal (associatedVpc , vNetName , vnetCidr , netrisSubnetName );
325
338
if (subnetResponse == null || !subnetResponse .isIsSuccess ()) {
326
339
String reason = subnetResponse == null ? "Empty response" : "Operation failed on Netris" ;
327
- logger .debug ("The Netris Subnet {} for VPC {} for network {} creation failed: {}" , vnetCidr , vpcName , networkName , reason );
340
+ logger .debug ("The Netris Subnet {} for network {} creation failed: {}" , vnetCidr , networkName , reason );
328
341
return false ;
329
342
}
330
- logger .debug ("Successfully created VPC {} and its IPAM Subnet {} on Netris" , vpcName , vnetCidr );
343
+ logger .debug ("Successfully created IPAM Subnet {} for network {} on Netris" , netrisSubnetName , networkName );
331
344
332
345
VnetResAddBody vnetResponse = createVnetInternal (associatedVpc , netrisVnetName , vnetCidr );
333
346
if (vnetResponse == null || !vnetResponse .isIsSuccess ()) {
334
347
String reason = vnetResponse == null ? "Empty response" : "Operation failed on Netris" ;
335
- logger .debug ("The Netris vNet creation {} for VPC {} failed: {}" , vNetName , vpcName , reason );
348
+ logger .debug ("The Netris vNet creation {} failed: {}" , vNetName , reason );
336
349
return false ;
337
350
}
338
351
return true ;
339
352
}
340
353
354
+ @ Override
355
+ public boolean deleteVnet (DeleteNetrisVnetCommand cmd ) {
356
+ String vpcName = cmd .getVpcName ();
357
+ Long vpcId = cmd .getVpcId ();
358
+ String networkName = cmd .getName ();
359
+ Long networkId = cmd .getId ();
360
+ boolean isVpc = cmd .isVpc ();
361
+ String vnetCidr = cmd .getVNetCidr ();
362
+ try {
363
+ String suffix = getNetrisVpcNameSuffix (vpcId , vpcName , networkId , networkName , isVpc );
364
+ String netrisVpcName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .VPC , suffix );
365
+ VPCListing associatedVpc = getVpcByNameAndTenant (netrisVpcName );
366
+ if (associatedVpc == null ) {
367
+ logger .error ("Failed to find Netris VPC with name: {}, to create the corresponding vNet for network {}" , netrisVpcName , networkName );
368
+ return false ;
369
+ }
370
+
371
+ String vNetName ;
372
+ if (isVpc ) {
373
+ vNetName = String .format ("V%s-N%s-%s" , vpcId , networkId , networkName );
374
+ } else {
375
+ vNetName = String .format ("N%s-%s" , networkId , networkName );
376
+ }
377
+
378
+ String netrisVnetName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .VNET , vNetName ) ;
379
+ String netrisSubnetName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .IPAM_SUBNET , vnetCidr ) ;
380
+ FilterByVpc vpcFilter = new FilterByVpc ();
381
+ vpcFilter .add (associatedVpc .getId ());
382
+ FilterBySites siteFilter = new FilterBySites ();
383
+ siteFilter .add (siteId );
384
+ deleteVnetInternal (associatedVpc , siteFilter , vpcFilter , netrisVnetName , vNetName );
385
+
386
+ logger .debug ("Successfully deleted vNet {}" , vNetName );
387
+ deleteSubnetInternal (vpcFilter , netrisVnetName , netrisSubnetName );
388
+
389
+ } catch (Exception e ) {
390
+ throw new CloudRuntimeException (String .format ("Failed to delete Netris vNet %s" , networkName ), e );
391
+ }
392
+ return true ;
393
+ }
394
+
395
+ private void deleteVnetInternal (VPCListing associatedVpc , FilterBySites siteFilter , FilterByVpc vpcFilter , String netrisVnetName , String vNetName ) {
396
+ try {
397
+ VNetApi vNetApi = apiClient .getApiStubForMethod (VNetApi .class );
398
+ VnetResListBody vnetList = vNetApi .apiV2VnetGet (siteFilter , vpcFilter );
399
+ if (vnetList == null || !vnetList .isIsSuccess ()) {
400
+ throw new CloudRuntimeException (String .format ("Failed to list vNets for the given VPC: %s and site: %s" , associatedVpc .getName (), siteName ));
401
+ }
402
+ List <VnetsBody > vnetsList = vnetList .getData ().stream ().filter (vnet -> vnet .getName ().equals (netrisVnetName )).collect (Collectors .toList ());
403
+ if (CollectionUtils .isEmpty (vnetsList )) {
404
+ logger .debug ("vNet: {} for the given VPC: {} appears to already be deleted on Netris" , vNetName , associatedVpc .getName ());
405
+ return ;
406
+ }
407
+ VnetsBody vnetsBody = vnetsList .get (0 );
408
+
409
+ VnetResDeleteBody deleteVnetResponse = vNetApi .apiV2VnetIdDelete (vnetsBody .getId ().intValue ());
410
+ if (deleteVnetResponse == null || !deleteVnetResponse .isIsSuccess ()) {
411
+ throw new CloudRuntimeException (String .format ("Failed to delete vNet: %s" , vNetName ));
412
+ }
413
+ } catch (ApiException e ) {
414
+ logAndThrowException (String .format ("Failed to delete vNet: %s" , netrisVnetName ), e );
415
+ }
416
+ }
417
+
418
+ private void deleteSubnetInternal (FilterByVpc vpcFilter , String netrisVnetName , String netrisSubnetName ) {
419
+ try {
420
+ logger .debug ("Deleting Netris VPC IPAM Subnet {} for vNet: {}" , netrisSubnetName , netrisVnetName );
421
+ IpamApi ipamApi = apiClient .getApiStubForMethod (IpamApi .class );
422
+ SubnetResBody subnetsResponse = ipamApi .apiV2IpamSubnetsGet (vpcFilter );
423
+ List <IpTreeSubnet > subnets = subnetsResponse .getData ();
424
+ List <IpTreeSubnet > matchedSubnets = subnets .stream ().filter (subnet -> subnet .getName ().equals (netrisSubnetName )).collect (Collectors .toList ());
425
+ if (CollectionUtils .isEmpty (matchedSubnets )) {
426
+ logger .debug ("IPAM subnet: {} for the given vNet: {} appears to already be deleted on Netris" , netrisSubnetName , netrisVnetName );
427
+ return ;
428
+ }
429
+
430
+ ipamApi .apiV2IpamTypeIdDelete ("subnet" , matchedSubnets .get (0 ).getId ().intValue ());
431
+ } catch (ApiException e ) {
432
+ logAndThrowException (String .format ("Failed to delete vNet: %s" , netrisVnetName ), e );
433
+ }
434
+ }
435
+
341
436
private InlineResponse2004 createVpcSubnetInternal (VPCListing associatedVpc , String vNetName , String vNetCidr , String netrisSubnetName ) {
342
437
logger .debug ("Creating Netris VPC Subnet {} for VPC {} for vNet {}" , vNetCidr , associatedVpc .getName (), vNetName );
343
438
try {
@@ -427,4 +522,13 @@ VnetResAddBody createVnetInternal(VPCListing associatedVpc, String netrisVnetNam
427
522
return null ;
428
523
}
429
524
}
525
+
526
+ private String getNetrisVpcNameSuffix (Long vpcId , String vpcName , Long networkId , String networkName , boolean isVpc ) {
527
+ String suffix = null ;
528
+ if (isVpc ) {
529
+ suffix = String .format ("%s-%s" , vpcId , vpcName );
530
+ } else {
531
+ suffix = String .format ("%s-%s" , networkId , networkName );
532
+ }
533
+ }
430
534
}
0 commit comments