4343import io .netris .model .AclAddItem ;
4444import io .netris .model .AclBodyVpc ;
4545import io .netris .model .AclDeleteItem ;
46+ import io .netris .model .AclEditItem ;
4647import io .netris .model .AclGetBody ;
4748import io .netris .model .AclResponseGetOk ;
4849import io .netris .model .AllocationBody ;
4950import io .netris .model .AllocationBodyVpc ;
5051import io .netris .model .FilterBySites ;
5152import io .netris .model .FilterByVpc ;
5253import io .netris .model .GetSiteBody ;
54+
5355import io .netris .model .InlineResponse20015 ;
5456import io .netris .model .InlineResponse20016 ;
5557import io .netris .model .InlineResponse2003 ;
112114import io .netris .model .response .L4LbEditResponse ;
113115import io .netris .model .response .TenantResponse ;
114116import io .netris .model .response .TenantsResponse ;
115- import org .apache .cloudstack .agent .api .CreateNetrisACLCommand ;
117+ import org .apache .cloudstack .agent .api .CreateOrUpdateNetrisACLCommand ;
116118import org .apache .cloudstack .agent .api .AddOrUpdateNetrisStaticRouteCommand ;
117119import org .apache .cloudstack .agent .api .CreateOrUpdateNetrisLoadBalancerRuleCommand ;
118120import org .apache .cloudstack .agent .api .CreateOrUpdateNetrisNatCommand ;
@@ -386,42 +388,10 @@ public boolean deleteNatRule(DeleteNetrisNatRuleCommand cmd) {
386388 }
387389
388390 @ Override
389- public boolean addAclRule ( CreateNetrisACLCommand cmd , boolean forLb ) {
391+ public boolean addOrUpdateAclRule ( CreateOrUpdateNetrisACLCommand cmd , boolean forLb ) {
390392 String aclName = cmd .getNetrisAclName ();
391393 try {
392394 AclApi aclApi = apiClient .getApiStubForMethod (AclApi .class );
393- AclAddItem aclAddItem = new AclAddItem ();
394- aclAddItem .setAction (cmd .getAction ());
395- aclAddItem .setComment (String .format ("ACL rule: %s. %s" , cmd .getNetrisAclName (), cmd .getReason ()));
396- aclAddItem .setName (aclName );
397- String protocol = cmd .getProtocol ();
398- if ("TCP" .equals (protocol )) {
399- aclAddItem .setEstablished (new BigDecimal (1 ));
400- } else {
401- aclAddItem .setReverse ("yes" );
402- }
403- if (!Arrays .asList (PROTOCOL_LIST ).contains (protocol )) {
404- aclAddItem .setProto ("ip" );
405- aclAddItem .setSrcPortTo (cmd .getIcmpType ());
406- // TODO: set proto number: where should the protocol number be set - API sets the protocol number to Src-from & to and Dest-from & to fields
407- } else if ("ICMP" .equals (protocol )) {
408- aclAddItem .setProto ("icmp" );
409- if (cmd .getIcmpType () != -1 ) {
410- aclAddItem .setIcmpType (cmd .getIcmpType ());
411- }
412- } else {
413- aclAddItem .setProto (protocol .toLowerCase (Locale .ROOT ));
414- }
415-
416- aclAddItem .setDstPortFrom (cmd .getDestPortStart ());
417- aclAddItem .setDstPortTo (cmd .getDestPortEnd ());
418- aclAddItem .setDstPrefix (cmd .getDestPrefix ());
419- aclAddItem .setSrcPrefix (cmd .getSourcePrefix ());
420- aclAddItem .setSrcPortFrom (1 );
421- aclAddItem .setSrcPortTo (65535 );
422- if (NatPutBody .ProtocolEnum .ICMP .name ().equalsIgnoreCase (protocol )) {
423- aclAddItem .setIcmpType (cmd .getIcmpType ());
424- }
425395 VPCListing vpcResource ;
426396 String netrisVpcName ;
427397 if (forLb ) {
@@ -436,20 +406,108 @@ public boolean addAclRule(CreateNetrisACLCommand cmd, boolean forLb) {
436406 }
437407 }
438408 AclBodyVpc vpc = new AclBodyVpc ().id (vpcResource .getId ());
439- aclAddItem .setVpc (vpc );
440409 List <String > aclNames = List .of (aclName );
441410 Pair <Boolean , List <BigDecimal >> resultAndMatchingAclIds = getMatchingAclIds (aclNames , netrisVpcName );
442- if (!resultAndMatchingAclIds .second ().isEmpty ()) {
443- logger .debug ("Netris ACL rule: {} already exists" , aclName );
411+ List <BigDecimal > aclIdList = resultAndMatchingAclIds .second ();
412+ if (!aclIdList .isEmpty ()) {
413+ logger .debug ("Netris ACL rule: {} already exists, updating it..." , aclName );
414+ AclEditItem aclEditItem = getAclEditItem (cmd , aclName , aclIdList .get (0 ));
415+ aclEditItem .setVpc (vpc );
416+ try {
417+ aclApi .apiAclPut (aclEditItem );
418+ } catch (ApiException e ) {
419+ if (e .getResponseBody ().contains ("This kind of acl already exists" )) {
420+ logger .info ("Netris ACL rule: {} already exists and doesn't need to be updated" , aclName );
421+ return true ;
422+ }
423+ throw new CloudRuntimeException ("Error updating Netris ACL rule" , e );
424+ }
444425 return true ;
445426 }
427+ AclAddItem aclAddItem = getAclAddItem (cmd , aclName );
428+ aclAddItem .setVpc (vpc );
446429 aclApi .apiAclPost (aclAddItem );
447430 } catch (ApiException e ) {
448431 logAndThrowException (String .format ("Failed to create Netris ACL: %s" , cmd .getNetrisAclName ()), e );
449432 }
450433 return true ;
451434 }
452435
436+ AclAddItem getAclAddItem (CreateOrUpdateNetrisACLCommand cmd , String aclName ) throws ApiException {
437+ AclAddItem aclAddItem = new AclAddItem ();
438+ aclAddItem .setAction (cmd .getAction ());
439+ aclAddItem .setComment (String .format ("ACL rule: %s. %s" , cmd .getNetrisAclName (), cmd .getReason ()));
440+ aclAddItem .setName (aclName );
441+ String protocol = cmd .getProtocol ();
442+ if ("TCP" .equals (protocol )) {
443+ aclAddItem .setEstablished (new BigDecimal (1 ));
444+ } else {
445+ aclAddItem .setReverse ("yes" );
446+ }
447+ if (!Arrays .asList (PROTOCOL_LIST ).contains (protocol )) {
448+ aclAddItem .setProto ("ip" );
449+ aclAddItem .setSrcPortTo (cmd .getIcmpType ());
450+ // TODO: set proto number: where should the protocol number be set - API sets the protocol number to Src-from & to and Dest-from & to fields
451+ } else if ("ICMP" .equals (protocol )) {
452+ aclAddItem .setProto ("icmp" );
453+ if (cmd .getIcmpType () != -1 ) {
454+ aclAddItem .setIcmpType (cmd .getIcmpType ());
455+ }
456+ } else {
457+ aclAddItem .setProto (protocol .toLowerCase (Locale .ROOT ));
458+ }
459+
460+ aclAddItem .setDstPortFrom (cmd .getDestPortStart ());
461+ aclAddItem .setDstPortTo (cmd .getDestPortEnd ());
462+ aclAddItem .setDstPrefix (cmd .getDestPrefix ());
463+ aclAddItem .setSrcPrefix (cmd .getSourcePrefix ());
464+ aclAddItem .setSrcPortFrom (1 );
465+ aclAddItem .setSrcPortTo (65535 );
466+ if (NatPutBody .ProtocolEnum .ICMP .name ().equalsIgnoreCase (protocol )) {
467+ aclAddItem .setIcmpType (cmd .getIcmpType ());
468+ }
469+
470+ return aclAddItem ;
471+ }
472+
473+ AclEditItem getAclEditItem (CreateOrUpdateNetrisACLCommand cmd , String aclName , BigDecimal aclId ) throws ApiException {
474+ AclEditItem aclEditItem = new AclEditItem ();
475+ aclEditItem .setId (aclId );
476+ aclEditItem .setAction (cmd .getAction ());
477+ aclEditItem .setComment (String .format ("ACL rule: %s. %s" , cmd .getNetrisAclName (), cmd .getReason ()));
478+ aclEditItem .setName (aclName );
479+ String protocol = cmd .getProtocol ();
480+ if ("TCP" .equals (protocol )) {
481+ aclEditItem .setEstablished (new BigDecimal (1 ));
482+ } else {
483+ aclEditItem .setReverse ("yes" );
484+ }
485+ if (!Arrays .asList (PROTOCOL_LIST ).contains (protocol )) {
486+ aclEditItem .setProto ("ip" );
487+ aclEditItem .setSrcPortTo (cmd .getIcmpType ());
488+ // TODO: set proto number: where should the protocol number be set - API sets the protocol number to Src-from & to and Dest-from & to fields
489+ } else if ("ICMP" .equals (protocol )) {
490+ aclEditItem .setProto ("icmp" );
491+ if (cmd .getIcmpType () != -1 ) {
492+ aclEditItem .setIcmpType (cmd .getIcmpType ());
493+ }
494+ } else {
495+ aclEditItem .setProto (protocol .toLowerCase (Locale .ROOT ));
496+ }
497+
498+ aclEditItem .setDstPortFrom (cmd .getDestPortStart ());
499+ aclEditItem .setDstPortTo (cmd .getDestPortEnd ());
500+ aclEditItem .setDstPrefix (cmd .getDestPrefix ());
501+ aclEditItem .setSrcPrefix (cmd .getSourcePrefix ());
502+ aclEditItem .setSrcPortFrom (1 );
503+ aclEditItem .setSrcPortTo (65535 );
504+ if (NatPutBody .ProtocolEnum .ICMP .name ().equalsIgnoreCase (protocol )) {
505+ aclEditItem .setIcmpType (cmd .getIcmpType ());
506+ }
507+
508+ return aclEditItem ;
509+ }
510+
453511 @ Override
454512 public boolean deleteAclRule (DeleteNetrisACLCommand cmd , boolean forLb ) {
455513 List <String > aclNames = cmd .getAclRuleNames ();
@@ -768,12 +826,12 @@ public boolean createOrUpdateLbRule(CreateOrUpdateNetrisLoadBalancerRuleCommand
768826
769827 private void applyAclRulesForLb (CreateOrUpdateNetrisLoadBalancerRuleCommand cmd , String lbName ) {
770828 // Add deny all rule first
771- addAclRule (createNetrisACLRuleCommand (cmd , lbName , "ANY" ,
829+ addOrUpdateAclRule (createNetrisACLRuleCommand (cmd , lbName , "ANY" ,
772830 NetrisNetworkRule .NetrisRuleAction .DENY .name ().toLowerCase (Locale .ROOT ), 0 ), true );
773831 AtomicInteger cidrIndex = new AtomicInteger (1 );
774832 for (String cidr : cmd .getCidrList ().split (" " )) {
775833 try {
776- addAclRule (createNetrisACLRuleCommand (cmd , lbName , cidr ,
834+ addOrUpdateAclRule (createNetrisACLRuleCommand (cmd , lbName , cidr ,
777835 NetrisNetworkRule .NetrisRuleAction .PERMIT .name ().toLowerCase (Locale .ROOT ),
778836 cidrIndex .getAndIncrement ()), true );
779837 } catch (Exception e ) {
@@ -782,7 +840,7 @@ private void applyAclRulesForLb(CreateOrUpdateNetrisLoadBalancerRuleCommand cmd,
782840 }
783841 }
784842
785- private CreateNetrisACLCommand createNetrisACLRuleCommand (CreateOrUpdateNetrisLoadBalancerRuleCommand cmd , String netrisLbName , String cidr , String action , int index ) {
843+ private CreateOrUpdateNetrisACLCommand createNetrisACLRuleCommand (CreateOrUpdateNetrisLoadBalancerRuleCommand cmd , String netrisLbName , String cidr , String action , int index ) {
786844 Long zoneId = cmd .getZoneId ();
787845 Long accountId = cmd .getAccountId ();
788846 Long domainId = cmd .getDomainId ();
@@ -801,7 +859,7 @@ private CreateNetrisACLCommand createNetrisACLRuleCommand(CreateOrUpdateNetrisLo
801859 String destinationPrefix = cmd .getPublicIp () + "/32" ;
802860 String srcPort = cmd .getPublicPort ();
803861 String dstPort = cmd .getPublicPort ();
804- CreateNetrisACLCommand aclCommand = new CreateNetrisACLCommand (zoneId , accountId , domainId , networkName , networkId ,
862+ CreateOrUpdateNetrisACLCommand aclCommand = new CreateOrUpdateNetrisACLCommand (zoneId , accountId , domainId , networkName , networkId ,
805863 vpcName , vpcId , Objects .nonNull (vpcId ), action , NetrisServiceImpl .getPrefix (cidr ), NetrisServiceImpl .getPrefix (destinationPrefix ),
806864 Integer .parseInt (srcPort ), Integer .parseInt (dstPort ), cmd .getProtocol ());
807865 String aclName ;
0 commit comments