Search in sources :

Example 1 with AccountManagementSystemClientException

use of org.bf2.srs.fleetmanager.spi.ams.impl.exception.AccountManagementSystemClientException 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 AccountManagementSystemClientException

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

the class AccountManagementServiceImpl method determineAllowedResourceType.

@Timed(value = Constants.AMS_DETERMINE_ALLOWED_INSTANCE_TIMER, description = Constants.AMS_TIMER_DESCRIPTION)
@Audited
@Timeout(FaultToleranceConstants.TIMEOUT_MS)
@RetryUnwrap
// 3 retries, 200ms jitter
@Retry(retryOn = { RetryWrapperException.class })
@RetryWrap
@Override
public ResourceType determineAllowedResourceType(AccountInfo accountInfo) throws AccountManagementServiceException {
    try {
        Organization organization = restClient.getOrganizationByExternalId(accountInfo.getOrganizationId());
        String orgId = organization.getId();
        // Check QuotaCostList for a RHOSR entry with "allowed" quota > 0.  If found, then
        // return "Standard" as the resource type to create.
        QuotaCostList quotaCostList = restClient.getQuotaCostList(orgId, true);
        if (quotaCostList.getSize() > 0) {
            for (QuotaCost quotaCost : quotaCostList.getItems()) {
                // We only care about QuotaCost with "allowed" > 0 and with at least one related resource.
                if (quotaCost.getAllowed() != null && quotaCost.getAllowed() > 0 && quotaCost.getRelated_resources() != null && !quotaCost.getRelated_resources().isEmpty() && isRhosrStandardQuota(quotaCost)) {
                    return ResourceType.REGISTRY_INSTANCE_STANDARD;
                }
            }
        }
        // Default to only allow eval.
        return ResourceType.REGISTRY_INSTANCE_EVAL;
    } catch (AccountManagementSystemClientException ex) {
        ExceptionConvert.convert(ex);
        // Never returns
        return null;
    }
}
Also used : Organization(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization) QuotaCostList(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.QuotaCostList) QuotaCost(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.QuotaCost) AccountManagementSystemClientException(org.bf2.srs.fleetmanager.spi.ams.impl.exception.AccountManagementSystemClientException) Audited(org.bf2.srs.fleetmanager.common.operation.auditing.Audited) Timed(io.micrometer.core.annotation.Timed) Timeout(org.eclipse.microprofile.faulttolerance.Timeout) Retry(org.eclipse.microprofile.faulttolerance.Retry) RetryUnwrap(org.bf2.srs.fleetmanager.common.operation.faulttolerance.RetryUnwrap) RetryWrap(org.bf2.srs.fleetmanager.common.operation.faulttolerance.RetryWrap)

Example 3 with AccountManagementSystemClientException

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

the class AccountManagementSystemRestClient method getOrganizationByExternalId.

public Organization getOrganizationByExternalId(String externalOrgId) {
    String search = "external_id='ORG_ID'".replace("ORG_ID", externalOrgId);
    Map<String, List<String>> queryParams = new HashMap<>();
    queryParams.put("search", Collections.singletonList(search));
    OrganizationList rval = this.client.sendRequest(new Request.RequestBuilder<OrganizationList>().operation(Operation.GET).path(Paths.ORGANIZATIONS_PATH).queryParams(queryParams).responseType(new TypeReference<OrganizationList>() {
    }).build());
    if (rval.getTotal() < 1) {
        throw new AccountManagementSystemClientException("Organization not found with external id: " + externalOrgId);
    }
    return rval.getItems().get(0);
}
Also used : HashMap(java.util.HashMap) QuotaCostList(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.QuotaCostList) List(java.util.List) OrganizationList(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.OrganizationList) OrganizationList(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.OrganizationList) AccountManagementSystemClientException(org.bf2.srs.fleetmanager.spi.ams.impl.exception.AccountManagementSystemClientException)

Aggregations

AccountManagementSystemClientException (org.bf2.srs.fleetmanager.spi.ams.impl.exception.AccountManagementSystemClientException)3 Timed (io.micrometer.core.annotation.Timed)2 Audited (org.bf2.srs.fleetmanager.common.operation.auditing.Audited)2 QuotaCostList (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.QuotaCostList)2 HashMap (java.util.HashMap)1 List (java.util.List)1 RetryUnwrap (org.bf2.srs.fleetmanager.common.operation.faulttolerance.RetryUnwrap)1 RetryWrap (org.bf2.srs.fleetmanager.common.operation.faulttolerance.RetryWrap)1 ResourceLimitReachedException (org.bf2.srs.fleetmanager.spi.ams.ResourceLimitReachedException)1 TermsRequiredException (org.bf2.srs.fleetmanager.spi.ams.TermsRequiredException)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 Organization (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization)1 OrganizationList (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.OrganizationList)1 QuotaCost (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.QuotaCost)1 ResponseTermsReview (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.ResponseTermsReview)1 Retry (org.eclipse.microprofile.faulttolerance.Retry)1 Timeout (org.eclipse.microprofile.faulttolerance.Timeout)1