Skip to content

Commit b92d233

Browse files
author
Alena Prokharchyk
committed
VPC: CS-15798 - added vpc resource limit. Check vpc functional spec for more details:
http://wiki.cloudstack.org/display/RelOps/Inter-VLAN+Routing+functional+spec Conflicts: server/src/com/cloud/api/ApiResponseHelper.java server/src/com/cloud/network/vpc/VpcManagerImpl.java server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
1 parent 5cbe3d9 commit b92d233

9 files changed

Lines changed: 92 additions & 23 deletions

File tree

‎api/src/com/cloud/api/response/AccountResponse.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
import java.util.Map;
2121

2222
import com.cloud.api.ApiConstants;
23-
import com.cloud.utils.IdentityProxy;
2423
import com.cloud.serializer.Param;
24+
import com.cloud.utils.IdentityProxy;
2525
import com.google.gson.annotations.SerializedName;
2626

27+
@SuppressWarnings("unused")
2728
public class AccountResponse extends BaseResponse {
2829
@SerializedName(ApiConstants.ID) @Param(description="the id of the account")
2930
private IdentityProxy id = new IdentityProxy("account");
@@ -118,6 +119,15 @@ public class AccountResponse extends BaseResponse {
118119
@SerializedName("networkavailable") @Param(description="the total number of networks available to be created for this account", since="3.0.1")
119120
private String networkAvailable;
120121

122+
@SerializedName("vpclimit") @Param(description="the total number of vpcs the account can own", since="3.0.5")
123+
private String vpcLimit;
124+
125+
@SerializedName("vpctotal") @Param(description="the total number of vpcs owned by account", since="3.0.5")
126+
private Long vpcTotal;
127+
128+
@SerializedName("vpcavailable") @Param(description="the total number of vpcs available to be created for this account", since="3.0.5")
129+
private String vpcAvailable;
130+
121131

122132
@SerializedName(ApiConstants.STATE) @Param(description="the state of the account")
123133
private String state;

‎api/src/com/cloud/configuration/Resource.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public enum ResourceType {
2727
snapshot("snapshot", 3, ResourceOwnerType.Account, ResourceOwnerType.Domain),
2828
template("template", 4, ResourceOwnerType.Account, ResourceOwnerType.Domain),
2929
project("project", 5, ResourceOwnerType.Account, ResourceOwnerType.Domain),
30-
network("network", 6, ResourceOwnerType.Account, ResourceOwnerType.Domain);
30+
network("network", 6, ResourceOwnerType.Account, ResourceOwnerType.Domain),
31+
vpc("vpc", 7, ResourceOwnerType.Account, ResourceOwnerType.Domain);
3132

3233
private String name;
3334
private ResourceOwnerType[] supportedOwners;

‎api/src/com/cloud/network/vpc/VpcService.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ List<? extends VpcOffering> listVpcOfferings(Long id, String name, String displa
7676
* @param cidr
7777
* @param networkDomain TODO
7878
* @return
79+
* @throws ResourceAllocationException TODO
7980
*/
80-
public Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr, String networkDomain);
81+
public Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr, String networkDomain) throws ResourceAllocationException;
8182

8283
/**
8384
* @param vpcId

‎server/src/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public AccountResponse createAccountResponse(Account account) {
265265
accountResponse.setDomainName(ApiDBUtils.findDomainById(account.getDomainId()).getName());
266266
accountResponse.setState(account.getState().toString());
267267
accountResponse.setNetworkDomain(account.getNetworkDomain());
268+
accountResponse.setDefaultZone(account.getDefaultZoneId());
268269

269270
// get network stat
270271
List<UserStatisticsVO> stats = ApiDBUtils.listUserStatsBy(account.getId());
@@ -361,7 +362,7 @@ public AccountResponse createAccountResponse(Account account) {
361362
accountResponse.setVmStopped(vmStopped);
362363
accountResponse.setVmRunning(vmRunning);
363364
accountResponse.setObjectName("account");
364-
365+
365366
//get resource limits for projects
366367
Long projectLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.project, account.getId());
367368
String projectLimitDisplay = (accountIsAdmin || projectLimit == -1) ? "Unlimited" : String.valueOf(projectLimit);
@@ -370,7 +371,7 @@ public AccountResponse createAccountResponse(Account account) {
370371
accountResponse.setProjectLimit(projectLimitDisplay);
371372
accountResponse.setProjectTotal(projectTotal);
372373
accountResponse.setProjectAvailable(projectAvail);
373-
374+
374375
//get resource limits for networks
375376
Long networkLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.network, account.getId());
376377
String networkLimitDisplay = (accountIsAdmin || networkLimit == -1) ? "Unlimited" : String.valueOf(networkLimit);
@@ -379,7 +380,16 @@ public AccountResponse createAccountResponse(Account account) {
379380
accountResponse.setNetworkLimit(networkLimitDisplay);
380381
accountResponse.setNetworkTotal(networkTotal);
381382
accountResponse.setNetworkAvailable(networkAvail);
382-
383+
384+
//get resource limits for vpcs
385+
Long vpcLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.vpc, account.getId());
386+
String vpcLimitDisplay = (accountIsAdmin || vpcLimit == -1) ? "Unlimited" : String.valueOf(vpcLimit);
387+
Long vpcTotal = ApiDBUtils.getResourceCount(ResourceType.vpc, account.getId());
388+
String vpcAvail = (accountIsAdmin || vpcLimit == -1) ? "Unlimited" : String.valueOf(vpcLimit - vpcTotal);
389+
accountResponse.setNetworkLimit(vpcLimitDisplay);
390+
accountResponse.setNetworkTotal(vpcTotal);
391+
accountResponse.setNetworkAvailable(vpcAvail);
392+
383393
// adding all the users for an account as part of the response obj
384394
List<UserVO> usersForAccount = ApiDBUtils.listUsersByAccount(account.getAccountId());
385395
List<UserResponse> userResponseList = new ArrayList<UserResponse>();
@@ -392,7 +402,8 @@ public AccountResponse createAccountResponse(Account account) {
392402
accountResponse.setDetails(ApiDBUtils.getAccountDetails(account.getId()));
393403
return accountResponse;
394404
}
395-
405+
406+
396407
@Override
397408
public UserResponse createUserResponse(UserAccount user) {
398409
UserResponse userResponse = new UserResponse();
@@ -3876,4 +3887,4 @@ public Site2SiteVpnConnectionResponse createSite2SiteVpnConnectionResponse(Site2
38763887
response.setObjectName("vpnconnection");
38773888
return response;
38783889
}
3879-
}
3890+
}

‎server/src/com/cloud/configuration/Config.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ public enum Config {
299299
DefaultMaxAccountSnapshots("Account Defaults", ManagementServer.class, Long.class, "max.account.snapshots", "20", "The default maximum number of snapshots that can be created for an account", null),
300300
DefaultMaxAccountVolumes("Account Defaults", ManagementServer.class, Long.class, "max.account.volumes", "20", "The default maximum number of volumes that can be created for an account", null),
301301
DefaultMaxAccountNetworks("Account Defaults", ManagementServer.class, Long.class, "max.account.networks", "20", "The default maximum number of networks that can be created for an account", null),
302+
DefaultMaxAccountVpcs("Account Defaults", ManagementServer.class, Long.class, "max.account.vpcs", "20", "The default maximum number of vpcs that can be created for an account", null),
303+
302304

303305
ResourceCountCheckInterval("Advanced", ManagementServer.class, Long.class, "resourcecount.check.interval", "0", "Time (in seconds) to wait before retrying resource count check task. Default is 0 which is to never run the task", "Seconds"),
304306
DirectAgentLoadSize("Advanced", ManagementServer.class, Integer.class, "direct.agent.load.size", "16", "The number of direct agents to load each time", null),
@@ -321,6 +323,7 @@ public enum Config {
321323
DefaultMaxProjectSnapshots("Project Defaults", ManagementServer.class, Long.class, "max.project.snapshots", "20", "The default maximum number of snapshots that can be created for a project", null),
322324
DefaultMaxProjectVolumes("Project Defaults", ManagementServer.class, Long.class, "max.project.volumes", "20", "The default maximum number of volumes that can be created for a project", null),
323325
DefaultMaxProjectNetworks("Project Defaults", ManagementServer.class, Long.class, "max.project.networks", "20", "The default maximum number of networks that can be created for a project", null),
326+
DefaultMaxProjectVpcs("Project Defaults", ManagementServer.class, Long.class, "max.project.vpcs", "20", "The default maximum number of vpcs that can be created for a project", null),
324327

325328
ProjectInviteRequired("Project Defaults", ManagementServer.class, Boolean.class, "project.invite.required", "false", "If invitation confirmation is required when add account to project. Default value is false", null),
326329
ProjectInvitationExpirationTime("Project Defaults", ManagementServer.class, Long.class, "project.invite.timeout", "86400", "Invitation expiration time (in seconds). Default is 1 day - 86400 seconds", null),

‎server/src/com/cloud/network/vpc/Dao/VpcDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ public interface VpcDao extends GenericDao<VpcVO, Long>{
3636
List<? extends Vpc> listByAccountId(long accountId);
3737

3838
List<VpcVO> listInactiveVpcs();
39+
40+
long countByAccountId(long accountId);
3941

4042
}

‎server/src/com/cloud/network/vpc/Dao/VpcDaoImpl.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
public class VpcDaoImpl extends GenericDaoBase<VpcVO, Long> implements VpcDao{
4141
final GenericSearchBuilder<VpcVO, Integer> CountByOfferingId;
4242
final SearchBuilder<VpcVO> AllFieldsSearch;
43+
final GenericSearchBuilder<VpcVO, Long> CountByAccountId;
4344
ResourceTagsDaoImpl _tagsDao = ComponentLocator.inject(ResourceTagsDaoImpl.class);
4445

4546
protected VpcDaoImpl() {
@@ -56,6 +57,12 @@ protected VpcDaoImpl() {
5657
AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ);
5758
AllFieldsSearch.and("accountId", AllFieldsSearch.entity().getAccountId(), Op.EQ);
5859
AllFieldsSearch.done();
60+
61+
CountByAccountId = createSearchBuilder(Long.class);
62+
CountByAccountId.select(null, Func.COUNT, CountByAccountId.entity().getId());
63+
CountByAccountId.and("offeringId", CountByAccountId.entity().getAccountId(), Op.EQ);
64+
CountByAccountId.and("removed", CountByAccountId.entity().getRemoved(), Op.NULL);
65+
CountByAccountId.done();
5966
}
6067

6168

@@ -102,5 +109,13 @@ public boolean remove(Long id) {
102109
txn.commit();
103110
return result;
104111
}
112+
113+
@Override
114+
public long countByAccountId(long accountId) {
115+
SearchCriteria<Long> sc = CountByAccountId.create();
116+
sc.setParameters("accountId", accountId);
117+
List<Long> results = customSearch(sc, null);
118+
return results.get(0);
119+
}
105120
}
106121

‎server/src/com/cloud/network/vpc/VpcManagerImpl.java‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.cloud.api.commands.ListStaticRoutesCmd;
3737
import com.cloud.configuration.Config;
3838
import com.cloud.configuration.ConfigurationManager;
39+
import com.cloud.configuration.Resource.ResourceType;
3940
import com.cloud.configuration.dao.ConfigurationDao;
4041
import com.cloud.dc.DataCenter;
4142
import com.cloud.dc.Vlan.VlanType;
@@ -89,6 +90,7 @@
8990
import com.cloud.tags.dao.ResourceTagDao;
9091
import com.cloud.user.Account;
9192
import com.cloud.user.AccountManager;
93+
import com.cloud.user.ResourceLimitService;
9294
import com.cloud.user.User;
9395
import com.cloud.user.UserContext;
9496
import com.cloud.utils.NumbersUtil;
@@ -158,6 +160,8 @@ public class VpcManagerImpl implements VpcManager, Manager{
158160
Site2SiteVpnManager _s2sVpnMgr;
159161
@Inject
160162
VlanDao _vlanDao = null;
163+
@Inject
164+
ResourceLimitService _resourceLimitMgr;
161165

162166

163167
private final ScheduledExecutorService _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("VpcChecker"));
@@ -510,13 +514,16 @@ public VpcOffering updateVpcOffering(long vpcOffId, String vpcOfferingName, Stri
510514
@Override
511515
@ActionEvent(eventType = EventTypes.EVENT_VPC_CREATE, eventDescription = "creating vpc", create=true)
512516
public Vpc createVpc(long zoneId, long vpcOffId, long vpcOwnerId, String vpcName, String displayText, String cidr,
513-
String networkDomain) {
517+
String networkDomain) throws ResourceAllocationException {
514518
Account caller = UserContext.current().getCaller();
515519
Account owner = _accountMgr.getAccount(vpcOwnerId);
516520

517521
//Verify that caller can perform actions in behalf of vpc owner
518522
_accountMgr.checkAccess(caller, null, false, owner);
519523

524+
//check resource limit
525+
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.vpc);
526+
520527
// Validate vpc offering
521528
VpcOfferingVO vpcOff = _vpcOffDao.findById(vpcOffId);
522529
if (vpcOff == null) {
@@ -561,7 +568,8 @@ public boolean vpcProviderEnabledInZone(long zoneId)
561568
return false;
562569
}
563570

564-
571+
572+
@DB
565573
protected Vpc createVpc(long zoneId, long vpcOffId, Account vpcOwner, String vpcName, String displayText, String cidr,
566574
String networkDomain) {
567575

@@ -593,16 +601,17 @@ protected Vpc createVpc(long zoneId, long vpcOffId, Account vpcOwner, String vpc
593601
" that overlaps the cidr specified: " + cidr);
594602
}
595603
}
596-
604+
605+
Transaction txn = Transaction.currentTxn();
606+
txn.start();
597607
VpcVO vpc = new VpcVO (zoneId, vpcName, displayText, vpcOwner.getId(), vpcOwner.getDomainId(), vpcOffId, cidr,
598608
networkDomain);
599609
vpc = _vpcDao.persist(vpc);
600-
601-
if (vpc != null) {
602-
s_logger.debug("Created VPC " + vpc);
603-
} else {
604-
s_logger.debug("Failed to create VPC");
605-
}
610+
_resourceLimitMgr.incrementResourceCount(vpcOwner.getId(), ResourceType.vpc);
611+
txn.commit();
612+
613+
s_logger.debug("Created VPC " + vpc);
614+
606615
return vpc;
607616
}
608617

@@ -625,6 +634,7 @@ public boolean deleteVpc(long vpcId) throws ConcurrentOperationException, Resour
625634
}
626635

627636
@Override
637+
@DB
628638
public boolean destroyVpc(Vpc vpc) throws ConcurrentOperationException, ResourceUnavailableException {
629639
UserContext ctx = UserContext.current();
630640

@@ -633,13 +643,22 @@ public boolean destroyVpc(Vpc vpc) throws ConcurrentOperationException, Resource
633643
if (networksCount > 0) {
634644
throw new InvalidParameterValueException("Can't delete VPC " + vpc + " as its used by " + networksCount + " networks");
635645
}
636-
637-
//mark VPC as disabled
638-
s_logger.debug("Updating VPC " + vpc + " with state " + Vpc.State.Inactive + " as a part of vpc delete");
639-
VpcVO vpcVO = _vpcDao.findById(vpc.getId());
640-
vpcVO.setState(Vpc.State.Inactive);
641-
_vpcDao.update(vpc.getId(), vpcVO);
642646

647+
//mark VPC as inactive
648+
if (vpc.getState() != Vpc.State.Inactive) {
649+
s_logger.debug("Updating VPC " + vpc + " with state " + Vpc.State.Inactive + " as a part of vpc delete");
650+
VpcVO vpcVO = _vpcDao.findById(vpc.getId());
651+
vpcVO.setState(Vpc.State.Inactive);
652+
653+
Transaction txn = Transaction.currentTxn();
654+
txn.start();
655+
_vpcDao.update(vpc.getId(), vpcVO);
656+
657+
//decrement resource count
658+
_resourceLimitMgr.decrementResourceCount(vpc.getAccountId(), ResourceType.vpc);
659+
txn.commit();
660+
}
661+
643662
//shutdown VPC
644663
if (!shutdownVpc(vpc.getId())) {
645664
s_logger.warn("Failed to shutdown vpc " + vpc + " as a part of vpc destroy process");

‎server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.cloud.exception.ResourceAllocationException;
5353
import com.cloud.network.dao.IPAddressDao;
5454
import com.cloud.network.dao.NetworkDao;
55+
import com.cloud.network.vpc.Dao.VpcDao;
5556
import com.cloud.projects.Project;
5657
import com.cloud.projects.ProjectAccount.Role;
5758
import com.cloud.projects.dao.ProjectAccountDao;
@@ -119,6 +120,8 @@ public class ResourceLimitManagerImpl implements ResourceLimitService, Manager {
119120
private ProjectAccountDao _projectAccountDao;
120121
@Inject
121122
private NetworkDao _networkDao;
123+
@Inject
124+
private VpcDao _vpcDao;
122125

123126
protected SearchBuilder<ResourceCountVO> ResourceCountSearch;
124127
ScheduledExecutorService _rcExecutor;
@@ -165,13 +168,15 @@ public boolean configure(final String name, final Map<String, Object> params) th
165168
projectResourceLimitMap.put(Resource.ResourceType.user_vm, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectUserVms.key())));
166169
projectResourceLimitMap.put(Resource.ResourceType.volume, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectVolumes.key())));
167170
projectResourceLimitMap.put(Resource.ResourceType.network, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectNetworks.key())));
171+
projectResourceLimitMap.put(Resource.ResourceType.vpc, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectVpcs.key())));
168172

169173
accountResourceLimitMap.put(Resource.ResourceType.public_ip, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountPublicIPs.key())));
170174
accountResourceLimitMap.put(Resource.ResourceType.snapshot, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountSnapshots.key())));
171175
accountResourceLimitMap.put(Resource.ResourceType.template, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountTemplates.key())));
172176
accountResourceLimitMap.put(Resource.ResourceType.user_vm, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountUserVms.key())));
173177
accountResourceLimitMap.put(Resource.ResourceType.volume, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountVolumes.key())));
174178
accountResourceLimitMap.put(Resource.ResourceType.network, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountNetworks.key())));
179+
accountResourceLimitMap.put(Resource.ResourceType.vpc, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountVpcs.key())));
175180

176181
return true;
177182
}
@@ -737,6 +742,8 @@ protected long recalculateAccountResourceCount(long accountId, ResourceType type
737742
newCount = _projectAccountDao.countByAccountIdAndRole(accountId, Role.Admin);
738743
} else if (type == Resource.ResourceType.network) {
739744
newCount = _networkDao.countNetworksUserCanCreate(accountId);
745+
} else if (type == Resource.ResourceType.vpc) {
746+
newCount = _vpcDao.countByAccountId(accountId);
740747
} else {
741748
throw new InvalidParameterValueException("Unsupported resource type " + type);
742749
}

0 commit comments

Comments
 (0)