Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2580 Skip certain storage billing #2584

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 EPAM Systems, Inc. (https://www.epam.com/)
* Copyright 2017-2022 EPAM Systems, Inc. (https://www.epam.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,16 +18,17 @@

import com.epam.pipeline.client.pipeline.CloudPipelineAPI;
import com.epam.pipeline.client.pipeline.CloudPipelineApiBuilder;
import com.epam.pipeline.client.pipeline.RetryingCloudPipelineApiExecutor;
import com.epam.pipeline.entity.cluster.InstanceType;
import com.epam.pipeline.entity.cluster.NodeDisk;
import com.epam.pipeline.entity.datastorage.AbstractDataStorage;
import com.epam.pipeline.entity.metadata.MetadataEntry;
import com.epam.pipeline.entity.pipeline.PipelineRun;

import com.epam.pipeline.entity.region.AbstractCloudRegion;
import com.epam.pipeline.entity.security.acl.AclClass;
import com.epam.pipeline.entity.user.PipelineUser;
import com.epam.pipeline.exception.PipelineResponseException;
import com.epam.pipeline.utils.QueryUtils;
import com.epam.pipeline.vo.EntityVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -39,43 +40,49 @@
public class CloudPipelineAPIClient {

private final CloudPipelineAPI cloudPipelineAPI;
private final RetryingCloudPipelineApiExecutor retryingApiExecutor;

public CloudPipelineAPIClient(@Value("${cloud.pipeline.host}") String cloudPipelineHostUrl,
@Value("${cloud.pipeline.token}") String cloudPipelineToken) {
this.cloudPipelineAPI =
new CloudPipelineApiBuilder(0, 0, cloudPipelineHostUrl, cloudPipelineToken)
.buildClient();
this.retryingApiExecutor = new RetryingCloudPipelineApiExecutor();
}

public List<PipelineUser> loadAllUsers() {
return QueryUtils.execute(cloudPipelineAPI.loadAllUsers());
return retryingApiExecutor.execute(cloudPipelineAPI.loadAllUsers());
}

public List<PipelineRun> loadAllPipelineRunsActiveInPeriod(final String from, final String to) {
return QueryUtils.execute(cloudPipelineAPI.loadRunsActivityStats(from, to));
return retryingApiExecutor.execute(cloudPipelineAPI.loadRunsActivityStats(from, to));
}

public List<AbstractDataStorage> loadAllDataStorages() {
return QueryUtils.execute(cloudPipelineAPI.loadAllDataStorages());
return retryingApiExecutor.execute(cloudPipelineAPI.loadAllDataStorages());
}

public List<InstanceType> loadAllInstanceTypesForRegion(final Long regionId) {
try {
return QueryUtils.execute(cloudPipelineAPI.loadAllInstanceTypesForRegion(regionId));
return retryingApiExecutor.execute(cloudPipelineAPI.loadAllInstanceTypesForRegion(regionId));
} catch (PipelineResponseException e) {
return Collections.emptyList();
}
}

public List<MetadataEntry> loadMetadataEntry(List<EntityVO> entities) {
return QueryUtils.execute(cloudPipelineAPI.loadFolderMetadata(entities));
return retryingApiExecutor.execute(cloudPipelineAPI.loadFolderMetadata(entities));
}

public List<NodeDisk> loadNodeDisks(final String nodeId) {
return QueryUtils.execute(cloudPipelineAPI.loadNodeDisks(nodeId));
return retryingApiExecutor.execute(cloudPipelineAPI.loadNodeDisks(nodeId));
}

public List<AbstractCloudRegion> loadAllCloudRegions() {
return QueryUtils.execute(cloudPipelineAPI.loadAllRegions());
return retryingApiExecutor.execute(cloudPipelineAPI.loadAllRegions());
}

public List<EntityVO> searchEntriesByMetadata(final AclClass entityClass, final String key, final String value) {
return retryingApiExecutor.execute(cloudPipelineAPI.searchMetadata(key, value, entityClass));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 EPAM Systems, Inc. (https://www.epam.com/)
* Copyright 2017-2022 EPAM Systems, Inc. (https://www.epam.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,29 +20,42 @@
import com.epam.pipeline.billingreportagent.service.EntityLoader;
import com.epam.pipeline.billingreportagent.service.impl.CloudPipelineAPIClient;
import com.epam.pipeline.entity.datastorage.AbstractDataStorage;
import com.epam.pipeline.entity.security.acl.AclClass;
import com.epam.pipeline.entity.user.PipelineUser;
import com.epam.pipeline.vo.EntityVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class StorageLoader implements EntityLoader<AbstractDataStorage> {

private final CloudPipelineAPIClient apiClient;
private final String storageExcludeKey;
private final String storageExcludeValue;

public StorageLoader(final CloudPipelineAPIClient apiClient) {
public StorageLoader(final CloudPipelineAPIClient apiClient,
@Value("${sync.storage.billing.exclude.metadata.key:Billing status}")
final String storageExcludeKey,
@Value("${sync.storage.billing.exclude.metadata.value:Exclude}")
final String storageExcludeValue) {
this.apiClient = apiClient;
this.storageExcludeKey = storageExcludeKey;
this.storageExcludeValue = storageExcludeValue;
}

@Override
public List<EntityContainer<AbstractDataStorage>> loadAllEntities() {
final Map<String, EntityWithMetadata<PipelineUser>> usersWithMetadata = prepareUsers(apiClient);

final Set<Long> storageIdsToIgnore = loadStorageIdsToIgnoreByTag();
return apiClient.loadAllDataStorages()
.stream()
.filter(storage -> isStorageBillable(storage, storageIdsToIgnore))
.map(storage -> EntityContainer.<AbstractDataStorage>builder()
.entity(storage)
.owner(usersWithMetadata.get(storage.getOwner()))
Expand All @@ -55,4 +68,15 @@ public List<EntityContainer<AbstractDataStorage>> loadAllEntitiesActiveInPeriod(
final LocalDateTime to) {
return loadAllEntities();
}

private Set<Long> loadStorageIdsToIgnoreByTag() {
return apiClient.searchEntriesByMetadata(AclClass.DATA_STORAGE, storageExcludeKey, storageExcludeValue)
.stream()
.map(EntityVO::getEntityId)
.collect(Collectors.toSet());
}

private boolean isStorageBillable(final AbstractDataStorage storage, final Set<Long> storageIdsToIgnore) {
return !storage.isShared() && !storageIdsToIgnore.contains(storage.getId());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2017-2020 EPAM Systems, Inc. (https://www.epam.com/)
# Copyright 2017-2022 EPAM Systems, Inc. (https://www.epam.com/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,3 +57,5 @@ sync.storage.index.mapping=classpath:/templates/storage_billing.json
sync.storage.index.name=storage-
sync.storage.file.index.pattern=*cp-%s-%s-%d
sync.aws.json.price.endpoint.template=https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/%s/current/index.json
sync.storage.billing.exclude.metadata.key=Billing status
sync.storage.billing.exclude.metadata.value=Exclude
4 changes: 3 additions & 1 deletion deploy/docker/cp-billing-srv/config/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2017-2020 EPAM Systems, Inc. (https://www.epam.com/)
# Copyright 2017-2022 EPAM Systems, Inc. (https://www.epam.com/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,3 +57,5 @@ sync.storage.index.name=storage-
sync.storage.price.load.mode=${CP_BILLING_AWS_PRICE_TYPE:json}
sync.storage.file.index.pattern=${CP_BILLING_STORAGE_INDEX_PATTERN:*cp-%s-%s-%d}
sync.aws.json.price.endpoint.template=${CP_BILLING_AWS_PRICES_ENDPOINT:https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/%s/current/index.json}
sync.storage.billing.exclude.metadata.key=${CP_BILLING_STORAGE_EXCLUDE_METADATA_KEY:Billing status}
sync.storage.billing.exclude.metadata.value=${CP_BILLING_STORAGE_EXCLUDE_METADATA_VALUE:Exclude}