Search in sources :

Example 1 with Organization

use of org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization 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 2 with Organization

use of org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class AccountManagementSystemRestClientTest method getOrganizationByExternalId.

@Test
public void getOrganizationByExternalId() {
    final Organization org = accountManagementSystemRestClient.getOrganizationByExternalId("12345");
    Assertions.assertNotNull(org);
    Assertions.assertEquals("1pcZDw72EPhdanw4pJEnrudOnyj", org.getId());
}
Also used : Organization(org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization) Test(org.junit.jupiter.api.Test)

Example 3 with Organization

use of org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization 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)

Example 4 with Organization

use of org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class FileQuotaPlansService method init.

@Override
public void init() throws IOException {
    log.debug("Using FileQuotaPlansService implementation of QuotaPlansService");
    if (plansConfigFile.isEmpty()) {
        throw new IllegalArgumentException("Error in static quota plans config: Property 'registry.quota.plans.config.file' is required.");
    }
    log.info("Loading registry quota plans config file from {}", plansConfigFile.get().getAbsolutePath());
    YAMLMapper mapper = SerDesObjectMapperProducer.getYAMLMapper();
    QuotaPlansConfigList quotaPlansConfigList = mapper.readValue(plansConfigFile.get(), QuotaPlansConfigList.class);
    List<QuotaPlan> staticQuotaPlans = quotaPlansConfigList.getPlans();
    Set<String> names = new HashSet<>();
    List<String> duplicatedNames = staticQuotaPlans.stream().map(d -> {
        Set<ConstraintViolation<QuotaPlan>> errors = validator.validate(d);
        if (!errors.isEmpty()) {
            throw new ConstraintViolationException(errors);
        }
        return d;
    }).filter(d -> !names.add(d.getName())).map(d -> d.getName()).collect(Collectors.toList());
    if (!duplicatedNames.isEmpty()) {
        throw new IllegalArgumentException("Error in static quota plans config, duplicated plan name: " + duplicatedNames.toString());
    }
    if (!names.contains(defaultQuotaPlan)) {
        throw new IllegalArgumentException("Error in static quota plans config, default plan does not exist in plans config, default plan name: " + defaultQuotaPlan);
    }
    for (QuotaPlan p : staticQuotaPlans) {
        tmClient.validateConfig(p.getResources());
        plans.put(p.getName(), p);
    }
    List<OrganizationAssignment> staticOrganizationAssignments = quotaPlansConfigList.getOrganizations();
    if (staticOrganizationAssignments == null)
        staticOrganizationAssignments = Collections.emptyList();
    for (OrganizationAssignment assignment : staticOrganizationAssignments) {
        if (!plans.containsKey(assignment.getPlan())) {
            throw new IllegalStateException("Could not find quota plan named '" + assignment.getPlan() + "' intended for organization ID '" + assignment.getOrgId() + "'");
        }
        organizationAssignments.put(assignment.getOrgId(), assignment);
    }
    if (quotaPlansConfigList.getReconcile() != null && quotaPlansConfigList.getReconcile()) {
        reconcile();
    }
}
Also used : TenantManagerService(org.bf2.srs.fleetmanager.spi.tenants.TenantManagerService) UpdateTenantRequest(org.bf2.srs.fleetmanager.spi.tenants.model.UpdateTenantRequest) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Inject(javax.inject.Inject) TenantLimit(org.bf2.srs.fleetmanager.spi.tenants.model.TenantLimit) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) ResourceStorage(org.bf2.srs.fleetmanager.storage.ResourceStorage) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) QuotaPlan(org.bf2.srs.fleetmanager.service.model.QuotaPlan) SerDesObjectMapperProducer(org.bf2.srs.fleetmanager.common.SerDesObjectMapperProducer) NoSuchElementException(java.util.NoSuchElementException) ConstraintViolation(javax.validation.ConstraintViolation) TenantManagerServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantManagerServiceException) QuotaPlansService(org.bf2.srs.fleetmanager.service.QuotaPlansService) QuotaPlansConfigList(org.bf2.srs.fleetmanager.service.model.QuotaPlansConfigList) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Validator(javax.validation.Validator) IOException(java.io.IOException) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) Collectors(java.util.stream.Collectors) File(java.io.File) OrganizationAssignment(org.bf2.srs.fleetmanager.service.model.OrganizationAssignment) List(java.util.List) ConstraintViolationException(javax.validation.ConstraintViolationException) IfBuildProfile(io.quarkus.arc.profile.IfBuildProfile) Optional(java.util.Optional) Utils(org.bf2.srs.fleetmanager.execution.impl.workers.Utils) TenantNotFoundServiceException(org.bf2.srs.fleetmanager.spi.tenants.TenantNotFoundServiceException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) Collections(java.util.Collections) HashSet(java.util.HashSet) Set(java.util.Set) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) QuotaPlan(org.bf2.srs.fleetmanager.service.model.QuotaPlan) OrganizationAssignment(org.bf2.srs.fleetmanager.service.model.OrganizationAssignment) ConstraintViolationException(javax.validation.ConstraintViolationException) QuotaPlansConfigList(org.bf2.srs.fleetmanager.service.model.QuotaPlansConfigList) HashSet(java.util.HashSet)

Example 5 with Organization

use of org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.

the class RegistryServiceImpl method getRegistries.

@Override
public RegistryListDto getRegistries(Integer page, Integer size, String orderBy, String search) {
    // Defaults
    var sort = Sort.by("id", Sort.Direction.Ascending);
    page = (page != null) ? page : 1;
    size = (size != null) ? size : 10;
    if (orderBy != null) {
        var order = orderBy.split(" ");
        if (order.length != 2) {
            throw new ValidationException("invalid orderBy");
        }
        if ("asc".equals(order[1])) {
            sort = Sort.by(order[0], Sort.Direction.Ascending);
        } else {
            sort = Sort.by(order[0], Sort.Direction.Descending);
        }
    }
    List<Pair<String, Object>> conditions = new ArrayList<>();
    if (search != null && !search.isEmpty()) {
        var basicQuery = new BasicQuery(search, Arrays.asList("name", "status"));
        conditions.add(Pair.of(basicQuery.getColumn(), basicQuery.getArgument()));
    }
    // list only registries from your organization or the ones the user owns
    if (isResolvable(securityIdentity)) {
        final AccountInfo accountInfo = authService.extractAccountInfo();
        String orgId = accountInfo.getOrganizationId();
        if (orgId != null) {
            conditions.add(Pair.of("orgId", orgId));
        } else {
            conditions.add(Pair.of("ownerId", accountInfo.getAccountId()));
        }
    } else {
        conditions.add(Pair.of("ownerId", OWNER_ID_PLACEHOLDER));
    }
    var query = new SearchQuery(conditions);
    PanacheQuery<RegistryData> itemsQuery = storage.executeRegistrySearchQuery(query, sort);
    var items = itemsQuery.page(Page.of(page - 1, size)).stream().map(convertRegistry::convert).collect(Collectors.toList());
    return RegistryListDto.builder().items(items).page(page).size(size).total(itemsQuery.count()).build();
}
Also used : SearchQuery(org.bf2.srs.fleetmanager.util.SearchQuery) ValidationException(javax.validation.ValidationException) BasicQuery(org.bf2.srs.fleetmanager.util.BasicQuery) ArrayList(java.util.ArrayList) RegistryData(org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData) AccountInfo(org.bf2.srs.fleetmanager.spi.common.model.AccountInfo) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

HashMap (java.util.HashMap)2 List (java.util.List)2 AccountManagementSystemClientException (org.bf2.srs.fleetmanager.spi.ams.impl.exception.AccountManagementSystemClientException)2 Organization (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.Organization)2 QuotaCostList (org.bf2.srs.fleetmanager.spi.ams.impl.model.response.QuotaCostList)2 RegistryData (org.bf2.srs.fleetmanager.storage.sqlPanacheImpl.model.RegistryData)2 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)1 Timed (io.micrometer.core.annotation.Timed)1 IfBuildProfile (io.quarkus.arc.profile.IfBuildProfile)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1