use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class VpcManagerImpl method restartVpc.
@Override
@ActionEvent(eventType = EventTypes.EVENT_VPC_RESTART, eventDescription = "restarting vpc")
public boolean restartVpc(final long vpcId, final boolean cleanUp) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
final Account callerAccount = CallContext.current().getCallingAccount();
final User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
final ReservationContext context = new ReservationContextImpl(null, null, callerUser, callerAccount);
// Verify input parameters
final Vpc vpc = getActiveVpc(vpcId);
if (vpc == null) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC by id specified");
ex.addProxyObject(String.valueOf(vpcId), "VPC");
throw ex;
}
final VpcVO vo = _vpcDao.findById(vpcId);
_accountMgr.checkAccess(callerAccount, null, false, vpc);
s_logger.debug("Restarting VPC " + vpc);
boolean restartRequired = false;
try {
if (cleanUp) {
List<DomainRouterVO> routers = _routerDao.listByVpcId(vpc.getId());
if (routers != null && !routers.isEmpty()) {
s_logger.debug("Shutting down VPC " + vpc + " as a part of VPC restart process");
// Get rid of any non-Running routers
for (final DomainRouterVO router : routers) {
if (router.getState() != VirtualMachine.State.Running) {
s_logger.debug("Destroying " + router + " as it is not in Running state anyway");
_routerMgr.destroyRouter(router.getId(), context.getAccount(), context.getCaller().getId());
}
}
// Refresh the list of routers
routers = _routerDao.listByVpcId(vpc.getId());
if (routers != null && !routers.isEmpty()) {
if (!rollingRestartVpc(vpc, routers, context)) {
s_logger.warn("Failed to execute a rolling restart as a part of VPC " + vpc + " restart process");
restartRequired = true;
return false;
}
}
}
// Reset VPC compliance state if it required a restart
if (vpc.getComplianceStatus() == ComplianceStatus.VPCNeedsRestart) {
vo.setComplianceStatus(ComplianceStatus.Compliant);
}
} else {
s_logger.info("Will not shutdown vpc as a part of VPC " + vpc + " restart process.");
}
s_logger.debug("Starting VPC " + vpc + " as a part of VPC restart process");
if (!startVpc(vpcId, false)) {
s_logger.warn("Failed to start vpc as a part of VPC " + vpc + " restart process");
restartRequired = true;
return false;
}
s_logger.debug("VPC " + vpc + " was restarted successfully");
return true;
} finally {
s_logger.debug("Updating VPC " + vpc + " with restartRequired=" + restartRequired);
vo.setRestartRequired(restartRequired);
_vpcDao.update(vpc.getId(), vo);
}
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class VpcManagerImpl method listVpcs.
@Override
public Pair<List<? extends Vpc>, Integer> listVpcs(final Long id, final String vpcName, final String displayText, final List<String> supportedServicesStr, final String cidr, final Long vpcOffId, final String state, final String accountName, Long domainId, final String keyword, final Long startIndex, final Long pageSizeVal, final Long zoneId, Boolean isRecursive, final Boolean listAll, final Boolean restartRequired, final Map<String, String> tags, final Long projectId, final Boolean display, final String complianceStatus) {
final Account caller = CallContext.current().getCallingAccount();
final List<Long> permittedAccounts = new ArrayList<>();
final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(domainId, isRecursive, null);
_accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false);
domainId = domainIdRecursiveListProject.first();
isRecursive = domainIdRecursiveListProject.second();
final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
final Filter searchFilter = new Filter(VpcVO.class, "created", false, null, null);
final SearchBuilder<VpcVO> sb = _vpcDao.createSearchBuilder();
_accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("displayText", sb.entity().getDisplayText(), SearchCriteria.Op.LIKE);
sb.and("vpcOfferingId", sb.entity().getVpcOfferingId(), SearchCriteria.Op.EQ);
sb.and("zoneId", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
sb.and("restartRequired", sb.entity().isRestartRequired(), SearchCriteria.Op.EQ);
sb.and("cidr", sb.entity().getCidr(), SearchCriteria.Op.EQ);
sb.and("display", sb.entity().isDisplay(), SearchCriteria.Op.EQ);
sb.and("complianceStatus", sb.entity().getComplianceStatus(), SearchCriteria.Op.EQ);
if (tags != null && !tags.isEmpty()) {
final SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
for (int count = 0; count < tags.size(); count++) {
tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
tagSearch.cp();
}
tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
sb.groupBy(sb.entity().getId());
sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
}
// now set the SC criteria...
final SearchCriteria<VpcVO> sc = sb.create();
_accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
if (keyword != null) {
final SearchCriteria<VpcVO> ssc = _vpcDao.createSearchCriteria();
ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
sc.addAnd("name", SearchCriteria.Op.SC, ssc);
}
if (vpcName != null) {
sc.addAnd("name", SearchCriteria.Op.LIKE, "%" + vpcName + "%");
}
if (displayText != null) {
sc.addAnd("displayText", SearchCriteria.Op.LIKE, "%" + displayText + "%");
}
if (tags != null && !tags.isEmpty()) {
int count = 0;
sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.Vpc.toString());
for (final Map.Entry<String, String> entry : tags.entrySet()) {
sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), entry.getKey());
sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), entry.getValue());
count++;
}
}
if (display != null) {
sc.setParameters("display", display);
}
if (id != null) {
sc.addAnd("id", SearchCriteria.Op.EQ, id);
}
if (vpcOffId != null) {
sc.addAnd("vpcOfferingId", SearchCriteria.Op.EQ, vpcOffId);
}
if (zoneId != null) {
sc.addAnd("zoneId", SearchCriteria.Op.EQ, zoneId);
}
if (state != null) {
sc.addAnd("state", SearchCriteria.Op.EQ, state);
}
if (cidr != null) {
sc.addAnd("cidr", SearchCriteria.Op.EQ, cidr);
}
if (restartRequired != null) {
sc.addAnd("restartRequired", SearchCriteria.Op.EQ, restartRequired);
}
if (complianceStatus != null) {
sc.addAnd("complianceStatus", SearchCriteria.Op.EQ, complianceStatus);
}
final List<VpcVO> vpcs = _vpcDao.search(sc, searchFilter);
// filter by supported services
final boolean listBySupportedServices = supportedServicesStr != null && !supportedServicesStr.isEmpty() && !vpcs.isEmpty();
if (listBySupportedServices) {
final List<VpcVO> supportedVpcs = new ArrayList<>();
Service[] supportedServices = null;
if (listBySupportedServices) {
supportedServices = getServices(supportedServicesStr);
}
for (final VpcVO vpc : vpcs) {
if (areServicesSupportedByVpcOffering(vpc.getVpcOfferingId(), supportedServices)) {
supportedVpcs.add(vpc);
}
}
final List<? extends Vpc> wPagination = StringUtils.applyPagination(supportedVpcs, startIndex, pageSizeVal);
if (wPagination != null) {
final Pair<List<? extends Vpc>, Integer> listWPagination = new Pair<>(wPagination, supportedVpcs.size());
return listWPagination;
}
return new Pair<>(supportedVpcs, supportedVpcs.size());
} else {
final List<? extends Vpc> wPagination = StringUtils.applyPagination(vpcs, startIndex, pageSizeVal);
if (wPagination != null) {
final Pair<List<? extends Vpc>, Integer> listWPagination = new Pair<>(wPagination, vpcs.size());
return listWPagination;
}
return new Pair<>(vpcs, vpcs.size());
}
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class VpcManagerImpl method updateVpc.
@Override
@ActionEvent(eventType = EventTypes.EVENT_VPC_UPDATE, eventDescription = "updating vpc")
public Vpc updateVpc(final long vpcId, final String vpcName, final String displayText, final String customId, final Boolean displayVpc, final Long vpcOfferingId, final String sourceNatList, final String syslogServerList, Long advertInterval, AdvertMethod advertMethod, final ComplianceStatus complianceStatus) {
CallContext.current().setEventDetails(" Id: " + vpcId);
final Account caller = CallContext.current().getCallingAccount();
// Verify input parameters
final VpcVO vpcToUpdate = _vpcDao.findById(vpcId);
if (vpcToUpdate == null) {
throw new InvalidParameterValueException("Unable to find vpc by id " + vpcId);
}
_accountMgr.checkAccess(caller, null, false, vpcToUpdate);
final VpcVO vpc = _vpcDao.createForUpdate(vpcId);
boolean restartWithCleanupRequired = false;
if (vpcName != null) {
vpc.setName(vpcName);
}
if (displayText != null) {
vpc.setDisplayText(displayText);
}
if (customId != null) {
vpc.setUuid(customId);
}
if (displayVpc != null) {
vpc.setDisplay(displayVpc);
}
if (syslogServerList != null) {
vpc.setSyslogServerList(syslogServerList);
}
if (advertInterval != null) {
vpc.setAdvertInterval(advertInterval);
restartWithCleanupRequired = true;
}
if (advertMethod != null) {
vpc.setAdvertMethod(advertMethod);
restartWithCleanupRequired = true;
}
if (complianceStatus != null) {
vpc.setComplianceStatus(complianceStatus);
}
if (vpcOfferingId != null) {
final VpcOfferingVO newVpcOffering = _vpcOffDao.findById(vpcOfferingId);
if (newVpcOffering == null) {
throw new InvalidParameterValueException("Unable to find vpc offering by id " + vpcOfferingId);
}
if (vpcOfferingId == vpcToUpdate.getVpcOfferingId()) {
throw new InvalidParameterValueException("The vpc already has the specified offering, so not upgrading. Use restart+cleanup to rebuild.");
}
// check if the new VPC offering matches the network offerings in use
checkVpcOfferingServicesWithCurrentNetworkOfferings(vpcOfferingId, vpcToUpdate);
vpc.setVpcOfferingId(vpcOfferingId);
vpc.setRedundant(newVpcOffering.getRedundantRouter());
restartWithCleanupRequired = true;
// disassociate the public IPs if not required anymore
if (!hasSourceNatService(vpc)) {
boolean success = true;
final List<IPAddressVO> ipsToRelease = _ipAddressDao.listByVpc(vpcId, null);
s_logger.debug("Releasing ips for vpc id=" + vpcId + " as a part of vpc cleanup");
for (final IPAddressVO ipToRelease : ipsToRelease) {
success = success && _ipAddrMgr.disassociatePublicIpAddress(ipToRelease.getId(), CallContext.current().getCallingUserId(), caller);
if (!success) {
s_logger.warn("Failed to cleanup ip " + ipToRelease + " as a part of vpc id=" + vpcId + " cleanup");
}
}
}
}
vpc.setRestartRequired(restartWithCleanupRequired);
if (sourceNatList != null && !sourceNatList.isEmpty() && vpcOfferingId != null && !hasSourceNatService(vpc)) {
throw new InvalidParameterValueException("Source NAT is not enabled on the VPC, so source NAT list is not allowed!");
}
if (sourceNatList != null) {
vpc.setSourceNatList(sourceNatList);
}
if (vpcOfferingId != null && !hasSourceNatService(vpc) && hasSourceNatService(vpcToUpdate)) {
s_logger.warn("SourceNat service not available on VPC " + vpc.getName() + " so setting SourceNatList to null!");
vpc.setSourceNatList(null);
}
// Save the new config
if (_vpcDao.update(vpcId, vpc)) {
s_logger.debug("Updated VPC id=" + vpcId);
} else {
return null;
}
final Account callerAccount = CallContext.current().getCallingAccount();
final User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
final ReservationContext context = new ReservationContextImpl(null, null, callerUser, callerAccount);
if (vpcOfferingId != null && !vpc.isRedundant()) {
final List<DomainRouterVO> routers = _routerDao.listByVpcId(vpc.getId());
for (final DomainRouterVO router : routers) {
// Delete any non-MASTER router since we are supposed to run a single setup according to the new VPC offering
if (router.getRedundantState() != VirtualRouter.RedundantState.MASTER) {
try {
s_logger.warn("Deleting router " + router.getInstanceName() + " as we don't need it any more");
_routerMgr.destroyRouter(router.getId(), context.getAccount(), context.getCaller().getId());
} catch (final ResourceUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
}
}
}
return _vpcDao.findById(vpcId);
}
// Restart the VPC when required
if (restartWithCleanupRequired) {
s_logger.debug("Will now restart+cleanup VPC id=" + vpcId);
try {
final boolean result = restartVpc(vpcId, true);
if (!result) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to restart VPC");
}
} catch (final ResourceUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
} catch (final ConcurrentOperationException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
} catch (final InsufficientCapacityException ex) {
s_logger.info(ex.toString());
throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, ex.getMessage());
}
} else {
// LOOP over all routers
final List<DomainRouterVO> routers = _routerDao.listByVpcId(vpc.getId());
if (routers != null && !routers.isEmpty()) {
s_logger.debug("Updating routers of VPC " + vpc + " as a part of VPC update process");
for (final DomainRouterVO router : routers) {
// Validate that the router is running
if (router.getState() == VirtualMachine.State.Running) {
if (!_routerMgr.updateVR(vpc, router)) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update VPC config");
}
}
}
}
}
return _vpcDao.findById(vpcId);
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class VpcManagerImpl method startVpc.
@Override
public boolean startVpc(final long vpcId, final boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
final CallContext ctx = CallContext.current();
final Account caller = ctx.getCallingAccount();
final User callerUser = _accountMgr.getActiveUser(ctx.getCallingUserId());
// check if vpc exists
final Vpc vpc = getActiveVpc(vpcId);
if (vpc == null) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC by id specified");
ex.addProxyObject(String.valueOf(vpcId), "VPC");
throw ex;
}
// permission check
_accountMgr.checkAccess(caller, null, false, vpc);
final Zone zone = zoneRepository.findById(vpc.getZoneId()).orElse(null);
final DeployDestination dest = new DeployDestination(zone, null, null, null);
final ReservationContext context = new ReservationContextImpl(null, null, callerUser, _accountMgr.getAccount(vpc.getAccountId()));
boolean result = true;
try {
if (!startVpc(vpc, dest, context)) {
s_logger.warn("Failed to start vpc " + vpc);
result = false;
}
} catch (final Exception ex) {
s_logger.warn("Failed to start vpc " + vpc + " due to ", ex);
result = false;
} finally {
// do cleanup
if (!result && destroyOnFailure) {
s_logger.debug("Destroying vpc " + vpc + " that failed to start");
if (destroyVpc(vpc, caller, callerUser.getId())) {
s_logger.warn("Successfully destroyed vpc " + vpc + " that failed to start");
} else {
s_logger.warn("Failed to destroy vpc " + vpc + " that failed to start");
}
}
}
return result;
}
use of com.cloud.legacymodel.user.Account in project cosmic by MissionCriticalCloud.
the class VpcManagerImpl method revokeStaticRoute.
@Override
@ActionEvent(eventType = EventTypes.EVENT_STATIC_ROUTE_DELETE, eventDescription = "deleting static route")
public boolean revokeStaticRoute(final long routeId) throws ResourceUnavailableException {
final Account caller = CallContext.current().getCallingAccount();
final StaticRouteVO route = _staticRouteDao.findById(routeId);
if (route == null) {
throw new InvalidParameterValueException("Unable to find static route by id");
}
_accountMgr.checkAccess(caller, null, false, route);
markStaticRouteForRevoke(route, caller);
return applyStaticRoutesForVpc(route.getVpcId());
}
Aggregations