Search in sources :

Example 1 with Region

use of org.apache.cloudstack.region.Region in project cloudstack by apache.

the class GlobalLoadBalancingRulesServiceImpl method createGlobalLoadBalancerRule.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_GLOBAL_LOAD_BALANCER_CREATE, eventDescription = "creating global load " + "balancer rule", create = true)
public GlobalLoadBalancerRule createGlobalLoadBalancerRule(CreateGlobalLoadBalancerRuleCmd newRule) {
    final Integer regionId = newRule.getRegionId();
    final String algorithm = newRule.getAlgorithm();
    final String stickyMethod = newRule.getStickyMethod();
    final String name = newRule.getName();
    final String description = newRule.getDescription();
    final String domainName = newRule.getServiceDomainName();
    final String serviceType = newRule.getServiceType();
    final Account gslbOwner = _accountMgr.getAccount(newRule.getEntityOwnerId());
    if (!GlobalLoadBalancerRule.Algorithm.isValidAlgorithm(algorithm)) {
        throw new InvalidParameterValueException("Invalid Algorithm: " + algorithm);
    }
    if (!GlobalLoadBalancerRule.Persistence.isValidPersistence(stickyMethod)) {
        throw new InvalidParameterValueException("Invalid persistence: " + stickyMethod);
    }
    if (!GlobalLoadBalancerRule.ServiceType.isValidServiceType(serviceType)) {
        throw new InvalidParameterValueException("Invalid service type: " + serviceType);
    }
    if (!NetUtils.verifyDomainName(domainName)) {
        throw new InvalidParameterValueException("Invalid domain name : " + domainName);
    }
    GlobalLoadBalancerRuleVO gslbRuleWithDomainName = _gslbRuleDao.findByDomainName(domainName);
    if (gslbRuleWithDomainName != null) {
        throw new InvalidParameterValueException("Domain name " + domainName + "is in use");
    }
    Region region = _regionDao.findById(regionId);
    if (region == null) {
        throw new InvalidParameterValueException("Invalid region ID: " + regionId);
    }
    String providerDnsName = _globalConfigDao.getValue(Config.CloudDnsName.key());
    if (!region.checkIfServiceEnabled(Region.Service.Gslb) || (providerDnsName == null)) {
        throw new CloudRuntimeException("GSLB service is not enabled in region : " + region.getName());
    }
    GlobalLoadBalancerRuleVO newGslbRule = Transaction.execute(new TransactionCallback<GlobalLoadBalancerRuleVO>() {

        @Override
        public GlobalLoadBalancerRuleVO doInTransaction(TransactionStatus status) {
            GlobalLoadBalancerRuleVO newGslbRule = new GlobalLoadBalancerRuleVO(name, description, domainName, algorithm, stickyMethod, serviceType, regionId, gslbOwner.getId(), gslbOwner.getDomainId(), GlobalLoadBalancerRule.State.Staged);
            _gslbRuleDao.persist(newGslbRule);
            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_GLOBAL_LOAD_BALANCER_CREATE, newGslbRule.getAccountId(), 0, newGslbRule.getId(), name, GlobalLoadBalancerRule.class.getName(), newGslbRule.getUuid());
            return newGslbRule;
        }
    });
    s_logger.debug("successfully created new global load balancer rule for the account " + gslbOwner.getId());
    return newGslbRule;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Region(org.apache.cloudstack.region.Region) TransactionStatus(com.cloud.utils.db.TransactionStatus) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 2 with Region

use of org.apache.cloudstack.region.Region in project cloudstack by apache.

the class RegionCmdTest method testCreateFailure.

@Test
public void testCreateFailure() {
    RegionService regionService = Mockito.mock(RegionService.class);
    Region region = Mockito.mock(Region.class);
    Mockito.when(regionService.addRegion(anyInt(), anyString(), isNull())).thenReturn(null);
    addRegionCmd._regionService = regionService;
    try {
        addRegionCmd.execute();
    } catch (ServerApiException exception) {
        assertEquals("Failed to add Region", exception.getDescription());
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) Region(org.apache.cloudstack.region.Region) RegionService(org.apache.cloudstack.region.RegionService) Test(org.junit.Test)

Example 3 with Region

use of org.apache.cloudstack.region.Region in project cloudstack by apache.

the class RegionCmdTest method testCreateSuccess.

@Test
public void testCreateSuccess() {
    RegionService regionService = Mockito.mock(RegionService.class);
    Region region = Mockito.mock(Region.class);
    Mockito.when(regionService.addRegion(anyInt(), anyString(), isNull())).thenReturn(region);
    addRegionCmd._regionService = regionService;
    responseGenerator = Mockito.mock(ResponseGenerator.class);
    RegionResponse regionResponse = Mockito.mock(RegionResponse.class);
    Mockito.when(responseGenerator.createRegionResponse(region)).thenReturn(regionResponse);
    addRegionCmd._responseGenerator = responseGenerator;
    addRegionCmd.execute();
}
Also used : RegionResponse(org.apache.cloudstack.api.response.RegionResponse) ResponseGenerator(org.apache.cloudstack.api.ResponseGenerator) Region(org.apache.cloudstack.region.Region) RegionService(org.apache.cloudstack.region.RegionService) Test(org.junit.Test)

Example 4 with Region

use of org.apache.cloudstack.region.Region in project cloudstack by apache.

the class ListRegionsCmd method execute.

@Override
public void execute() {
    List<? extends Region> result = _regionService.listRegions(this);
    ListResponse<RegionResponse> response = new ListResponse<RegionResponse>();
    List<RegionResponse> regionResponses = new ArrayList<RegionResponse>();
    for (Region region : result) {
        RegionResponse regionResponse = _responseGenerator.createRegionResponse(region);
        regionResponse.setObjectName("region");
        regionResponses.add(regionResponse);
    }
    response.setResponses(regionResponses);
    response.setResponseName(getCommandName());
    this.setResponseObject(response);
}
Also used : RegionResponse(org.apache.cloudstack.api.response.RegionResponse) ListResponse(org.apache.cloudstack.api.response.ListResponse) ArrayList(java.util.ArrayList) Region(org.apache.cloudstack.region.Region)

Example 5 with Region

use of org.apache.cloudstack.region.Region in project cloudstack by apache.

the class AddRegionCmd method execute.

@Override
public void execute() {
    Region region = _regionService.addRegion(getId(), getRegionName(), getEndPoint());
    if (region != null) {
        RegionResponse response = _responseGenerator.createRegionResponse(region);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add Region");
    }
}
Also used : RegionResponse(org.apache.cloudstack.api.response.RegionResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) Region(org.apache.cloudstack.region.Region)

Aggregations

Region (org.apache.cloudstack.region.Region)7 RegionResponse (org.apache.cloudstack.api.response.RegionResponse)4 ServerApiException (org.apache.cloudstack.api.ServerApiException)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 ArrayList (java.util.ArrayList)2 RegionService (org.apache.cloudstack.region.RegionService)2 Test (org.junit.Test)2 ActionEvent (com.cloud.event.ActionEvent)1 Account (com.cloud.user.Account)1 DB (com.cloud.utils.db.DB)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ResponseGenerator (org.apache.cloudstack.api.ResponseGenerator)1 ListResponse (org.apache.cloudstack.api.response.ListResponse)1 PortableIpRangeVO (org.apache.cloudstack.region.PortableIpRangeVO)1