-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored the code, regarding the code change suggetions by GutoVero…
…nezi.
- Loading branch information
Sina Kashipazha
committed
Jul 15, 2021
1 parent
317ecbb
commit 2ff1691
Showing
13 changed files
with
108 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/LoadBalancerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.apache.cloudstack.api.command.user.loadbalancer; | ||
|
||
import com.cloud.exception.InvalidParameterValueException; | ||
import com.cloud.network.Network; | ||
import com.cloud.network.rules.FirewallRule; | ||
import com.cloud.network.vpc.Vpc; | ||
import com.cloud.user.OwnedBy; | ||
import com.cloud.utils.db.EntityManager; | ||
import org.apache.cloudstack.api.BaseAsyncCmd; | ||
import org.apache.cloudstack.network.lb.LoadBalancerConfig; | ||
|
||
|
||
public interface LoadBalancerHelper { | ||
|
||
static LoadBalancerConfig findConfigById(EntityManager entityMgr, Long id){ | ||
LoadBalancerConfig config = entityMgr.findById(LoadBalancerConfig.class, id); | ||
if (config == null) { | ||
throw new InvalidParameterValueException("Unable to find load balancer config: " + id); | ||
} | ||
return config; | ||
} | ||
|
||
static String getSyncObjType(EntityManager entityMgr, Long id){ | ||
LoadBalancerConfig config = findConfigById(entityMgr, id); | ||
return getSyncObjType(config.getNetworkId(), config.getVpcId()); | ||
} | ||
|
||
static String getSyncObjType(Long networkId, Long vpcId){ | ||
if (networkId != null) { | ||
return BaseAsyncCmd.networkSyncObject; | ||
} else if (vpcId != null) { | ||
return BaseAsyncCmd.vpcSyncObject; | ||
} | ||
return null; | ||
} | ||
|
||
static Long getSyncObjId(EntityManager entityMgr, Long id){ | ||
LoadBalancerConfig config = findConfigById(entityMgr, id); | ||
return getSyncObjId(config.getNetworkId(), config.getVpcId()); | ||
} | ||
|
||
// Cause vpcId might be null, this method might return null | ||
static Long getSyncObjId(Long networkId, Long vpcId){ | ||
if (networkId != null) | ||
return networkId; | ||
return vpcId; | ||
} | ||
|
||
static <T extends OwnedBy> long getEntityOwnerId(EntityManager entityMgr , Class<T> entityType, Long id){ | ||
T t = entityMgr.findById(entityType, id); | ||
if (t != null) { | ||
return t.getAccountId(); | ||
} | ||
throw new InvalidParameterValueException("Unable to find the entity owner"); | ||
} | ||
|
||
static long getEntityOwnerId(EntityManager entityMgr, Long id) { | ||
LoadBalancerConfig config = findConfigById(entityMgr, id); | ||
if (config.getNetworkId() != null) { | ||
return LoadBalancerHelper.getEntityOwnerId(entityMgr, Network.class, config.getNetworkId()); | ||
} else if (config.getVpcId() != null) { | ||
return LoadBalancerHelper.getEntityOwnerId(entityMgr, Vpc.class, config.getVpcId()); | ||
} else if (config.getLoadBalancerId() != null) { | ||
return LoadBalancerHelper.getEntityOwnerId(entityMgr, FirewallRule.class, config.getLoadBalancerId()); | ||
} | ||
throw new InvalidParameterValueException("Unable to find the entity owner"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.