Search in sources :

Example 1 with TermsRequiredException

use of org.bf2.srs.fleetmanager.spi.ams.TermsRequiredException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class AccountManagementServiceImpl method createResource.

@Timed(value = Constants.AMS_CREATE_TIMER, description = Constants.AMS_TIMER_DESCRIPTION)
@Audited(extractResult = KEY_AMS_SUBSCRIPTION_ID)
// but AMS still performs the reservation.
@Override
public String createResource(AccountInfo accountInfo, ResourceType resourceType) throws TermsRequiredException, ResourceLimitReachedException, AccountManagementServiceException {
    try {
        boolean termsAccepted = false;
        String siteCode = amsProperties.termsSiteCode;
        List<String> eventCodes = amsProperties.termsEventCode;
        for (String eventCode : eventCodes) {
            final TermsReview termsReview = new TermsReview();
            termsReview.setAccountUsername(accountInfo.getAccountUsername());
            termsReview.setSiteCode(siteCode);
            termsReview.setEventCode(eventCode);
            // Check if the user has accepted the Terms & Conditions
            final ResponseTermsReview responseTermsReview = restClient.termsReview(termsReview);
            boolean accepted = !responseTermsReview.getTermsRequired();
            // Terms are accepted if *any* of the T&C checks come back as "accepted"
            termsAccepted = termsAccepted || accepted;
        }
        if (!termsAccepted) {
            throw new TermsRequiredException(accountInfo.getAccountUsername());
        }
        // TODO Workaround: Remove this once we have RHOSRTrial working.
        if (resourceType == ResourceType.REGISTRY_INSTANCE_EVAL) {
            log.debug("Creating an eval instance for '{}' in org '{}' without calling AMS.", accountInfo.getAccountUsername(), accountInfo.getOrganizationId());
            return null;
        }
        // Set the productId and resourceName based on if it's an Eval or Standard instance
        String productId = amsProperties.standardProductId;
        String resourceName = amsProperties.standardResourceName;
        if (resourceType == ResourceType.REGISTRY_INSTANCE_EVAL) {
            productId = amsProperties.evalProductId;
            resourceName = amsProperties.evalResourceName;
        }
        // Build a quota resource ID to pass to AMS
        final var quotaResource = ReservedResource.builder().resourceType(amsProperties.resourceType).byoc(false).resourceName(resourceName).billingModel("marketplace").availabilityZone("single").count(1).build();
        // Create the cluster authorization REST operation input
        final ClusterAuthorization clusterAuthorization = ClusterAuthorization.builder().accountUsername(accountInfo.getAccountUsername()).productId(productId).managed(true).byoc(false).cloudProviderId("aws").reserve(true).availabilityZone("single").clusterId(UUID.randomUUID().toString()).resources(Collections.singletonList(quotaResource)).build();
        // Consume quota from AMS via the AMS REST API
        final ClusterAuthorizationResponse clusterAuthorizationResponse = restClient.clusterAuthorization(clusterAuthorization);
        if (clusterAuthorizationResponse.getAllowed()) {
            return clusterAuthorizationResponse.getSubscription().getId();
        } else {
            // User not allowed to create resource
            throw new ResourceLimitReachedException();
        }
    } catch (AccountManagementSystemClientException ex) {
        ExceptionConvert.convert(ex);
        // Never returns
        return null;
    }
}
Also used : ResponseTermsReview(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.ResponseTermsReview) TermsReview(org.bf2.srs.fleetmanager.spi.ams.impl.model.request.TermsReview) ClusterAuthorization(org.bf2.srs.fleetmanager.spi.ams.impl.model.request.ClusterAuthorization) ClusterAuthorizationResponse(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.ClusterAuthorizationResponse) TermsRequiredException(org.bf2.srs.fleetmanager.spi.ams.TermsRequiredException) AccountManagementSystemClientException(org.bf2.srs.fleetmanager.spi.ams.impl.exception.AccountManagementSystemClientException) ResourceLimitReachedException(org.bf2.srs.fleetmanager.spi.ams.ResourceLimitReachedException) ResponseTermsReview(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.ResponseTermsReview) Audited(org.bf2.srs.fleetmanager.common.operation.auditing.Audited) Timed(io.micrometer.core.annotation.Timed)

Example 2 with TermsRequiredException

use of org.bf2.srs.fleetmanager.spi.ams.TermsRequiredException in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class RegistryServiceImpl method createRegistry.

@Audited
@Override
public RegistryDto createRegistry(RegistryCreateDto registryCreate) throws RegistryStorageConflictException, TermsRequiredException, ResourceLimitReachedException, EvalInstancesNotAllowedException, TooManyEvalInstancesForUserException, TooManyInstancesException, AccountManagementServiceException {
    final AccountInfo accountInfo = authService.extractAccountInfo();
    // Make sure we have more instances available (max capacity not yet reached).
    long instanceCount = storage.getRegistryCountTotal();
    if (instanceCount >= maxInstances) {
        throw new TooManyInstancesException();
    }
    // Figure out if we are going to create a standard or eval instance.
    ResourceType resourceType = evalInstancesOnlyEnabled ? ResourceType.REGISTRY_INSTANCE_EVAL : accountManagementService.determineAllowedResourceType(accountInfo);
    if (resourceType == ResourceType.REGISTRY_INSTANCE_EVAL) {
        // Are eval instances allowed?
        if (!evalInstancesEnabled) {
            throw new EvalInstancesNotAllowedException();
        }
        // Limit the # of eval instances per user.  Need to check storage for list of eval registry instances.
        List<RegistryData> registriesByOwner = storage.getRegistriesByOwner(accountInfo.getAccountUsername());
        int evalInstanceCount = 0;
        for (RegistryData registryData : registriesByOwner) {
            // TODO Perform a dedicated query
            if (RegistryInstanceTypeValueDto.EVAL.value().equals(registryData.getInstanceType())) {
                evalInstanceCount++;
            }
        }
        if (evalInstanceCount >= maxEvalInstancesPerUser) {
            throw new TooManyEvalInstancesForUserException();
        }
    }
    // Try to consume some quota from AMS for the appropriate resource type (standard or eval).  If successful
    // we'll get back a subscriptionId - if not we'll throw an exception.
    String subscriptionId = accountManagementService.createResource(accountInfo, resourceType);
    // Convert to registry data and persist it in the DB.
    RegistryInstanceTypeValueDto instanceType = resourceTypeToInstanceType(resourceType);
    RegistryData registryData = convertRegistry.convert(registryCreate, subscriptionId, accountInfo.getAccountUsername(), accountInfo.getOrganizationId(), accountInfo.getAccountId(), instanceType);
    // Generate the ID
    registryData.setId(UUID.randomUUID().toString());
    storage.createOrUpdateRegistry(registryData);
    tasks.submit(ScheduleRegistryTask.builder().registryId(registryData.getId()).build());
    return convertRegistry.convert(registryData);
}
Also used : TooManyInstancesException(org.bf2.srs.fleetmanager.spi.common.TooManyInstancesException) EvalInstancesNotAllowedException(org.bf2.srs.fleetmanager.spi.common.EvalInstancesNotAllowedException) ResourceType(org.bf2.srs.fleetmanager.spi.common.model.ResourceType) TooManyEvalInstancesForUserException(org.bf2.srs.fleetmanager.spi.common.TooManyEvalInstancesForUserException) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) RegistryInstanceTypeValueDto(org.bf2.srs.fleetmanager.rest.service.model.RegistryInstanceTypeValueDto) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) Audited(org.bf2.srs.fleetmanager.common.operation.auditing.Audited)

Aggregations

Audited (org.bf2.srs.fleetmanager.common.operation.auditing.Audited)2 Timed (io.micrometer.core.annotation.Timed)1 RegistryInstanceTypeValueDto (org.bf2.srs.fleetmanager.rest.service.model.RegistryInstanceTypeValueDto)1 ResourceLimitReachedException (org.bf2.srs.fleetmanager.spi.ams.ResourceLimitReachedException)1 TermsRequiredException (org.bf2.srs.fleetmanager.spi.ams.TermsRequiredException)1 AccountManagementSystemClientException (org.bf2.srs.fleetmanager.spi.ams.impl.exception.AccountManagementSystemClientException)1 ClusterAuthorization (org.bf2.srs.fleetmanager.spi.ams.impl.model.request.ClusterAuthorization)1 TermsReview (org.bf2.srs.fleetmanager.spi.ams.impl.model.request.TermsReview)1 ClusterAuthorizationResponse (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.ClusterAuthorizationResponse)1 ResponseTermsReview (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.ResponseTermsReview)1 EvalInstancesNotAllowedException (org.bf2.srs.fleetmanager.spi.common.EvalInstancesNotAllowedException)1 TooManyEvalInstancesForUserException (org.bf2.srs.fleetmanager.spi.common.TooManyEvalInstancesForUserException)1 TooManyInstancesException (org.bf2.srs.fleetmanager.spi.common.TooManyInstancesException)1 AccountInfo (org.bf2.srs.fleetmanager.spi.common.model.AccountInfo)1 ResourceType (org.bf2.srs.fleetmanager.spi.common.model.ResourceType)1 RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)1