Skip to content

Commit 57ec0d9

Browse files
authored
Merge pull request #139 from scality/improvement/OSIS-145-disable-get-usage-API
Improvement/osis 145 disable get usage api
2 parents 4f83bcb + 55c9125 commit 57ec0d9

File tree

7 files changed

+34
-151
lines changed

7 files changed

+34
-151
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @scality/object-lead @XinLiScality
1+
* @scality/object

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
buildscript {
22
ext {
3-
osisVersion = '2.2.1'
3+
osisVersion = '2.2.2'
44
vaultclientVersion = '1.1.2'
55
springBootVersion = '2.7.6'
66
}

osis-app/src/main/java/com/scality/osis/healthcheck/UtapiHealthIndicator.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,18 @@
55

66
package com.scality.osis.healthcheck;
77

8-
import com.scality.osis.ScalityAppEnv;
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
11-
import org.springframework.beans.factory.annotation.Autowired;
128
import org.springframework.boot.actuate.health.Health;
139
import org.springframework.boot.actuate.health.HealthIndicator;
1410
import org.springframework.stereotype.Component;
1511

16-
import java.io.IOException;
17-
import java.net.HttpURLConnection;
18-
import java.net.SocketTimeoutException;
1912

2013
@Component("utapi")
2114
public class UtapiHealthIndicator implements HealthIndicator {
22-
private static final Logger logger = LoggerFactory.getLogger(UtapiHealthIndicator.class);
23-
24-
@Autowired
25-
private ScalityAppEnv appEnv;
2615

2716
@Override
2817
public Health health() {
29-
HttpURLConnection connection = null;
30-
try {
31-
// check if Utapi Endpoint is reachable
32-
connection = (HttpURLConnection) new java.net.URL(appEnv.getUtapiEndpoint()).openConnection();
33-
connection.setConnectTimeout(appEnv.getUtapiHealthCheckTimeout());
34-
connection.connect();
35-
} catch (SocketTimeoutException e) {
36-
logger.warn("Failed to connect to Utapi endpoint {} in timeout {}",appEnv.getUtapiEndpoint(), appEnv.getUtapiHealthCheckTimeout());
37-
return Health.down()
38-
.withDetail("error", e.getMessage())
39-
.build();
40-
} catch (IOException e) {
41-
logger.warn("an I/O error occurs while trying to connect {}.",appEnv.getUtapiEndpoint());
42-
return Health.down()
43-
.withDetail("error", e.getMessage())
44-
.build();
45-
} finally {
46-
if (connection != null) {
47-
connection.disconnect();
48-
}
49-
}
50-
return Health.up().build();
18+
// We want to make sure healthcheck is false even if it is set in application.properties
19+
// This will be enabled and potentially refactored with S3C-8266
20+
return Health.down().build();
5121
}
5222
}

osis-app/src/main/java/com/scality/osis/resource/ScalityOsisController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,13 @@ public OsisTenant getTenant(
339339
},
340340
tags = {"usage", "optional"})
341341
@GetMapping(value = "/api/v1/usage", produces = "application/json")
342+
// getUsage API will be re-enabled in S3C-8035
343+
@NotImplement(name = ScalityOsisConstants.GET_USAGE_API_CODE)
342344
public OsisUsage getUsage(
343345
@Parameter(description = "The ID of the tenant to get its usage.") @Valid @RequestParam(value = "tenant_id", required = false) Optional<String> tenantId,
344346
@Parameter(description = "The ID of the user to get its usage.") @Valid @RequestParam(value = "user_id", required = false) Optional<String> userId) {
345-
if (tenantId.isEmpty() && userId.isPresent()) {
346-
throw new BadRequestException("userId must be specified with associated tenantId!");
347-
}
348-
return osisService.getOsisUsage(tenantId, userId);
347+
logger.info("Get Osis Usage request received:: tenant ID:{}, user ID:{}", tenantId, userId);
348+
throw new NotImplementedException();
349349
}
350350

351351
/*

osis-core/src/main/java/com/scality/osis/model/ScalityOsisConstants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ private ScalityOsisConstants() {
1414
}
1515

1616
public static final String DELETE_TENANT_API_CODE = "deleteTenant";
17-
public static final String GET_BUCKET_LIST_API_CODE = "getBucketList";
17+
public static final String GET_USAGE_API_CODE = "getUsage";
1818
public static final String GET_BUCKET_ID_LOGGING_API_CODE = "getBucketLoggingId";
1919

2020
public static final List<String> API_CODES = Arrays.asList(
2121
DELETE_TENANT_API_CODE,
22-
GET_BUCKET_LIST_API_CODE,
22+
GET_USAGE_API_CODE,
2323
GET_BUCKET_ID_LOGGING_API_CODE
2424
);
2525
}

osis-core/src/main/java/com/scality/osis/service/impl/ScalityOsisServiceImpl.java

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,116 +1229,7 @@ public PageOfOsisBucketMeta getBucketList(String tenantId, long offset, long lim
12291229

12301230
@Override
12311231
public OsisUsage getOsisUsage(Optional<String> tenantId, Optional<String> userId) {
1232-
1233-
if (!appEnv.isUtapiEnabled()) {
1234-
logger.info("Utapi is not enabled, returning empty usage");
1235-
return new OsisUsage();
1236-
}
1237-
1238-
logger.info("Get Osis Usage request received:: tenant ID:{}, user ID:{}", tenantId, userId);
1239-
OsisUsage osisUsage = new OsisUsage();
1240-
1241-
if (tenantId.isEmpty()) {
1242-
logger.info("tenant ID is empty, getting platform usage at global level");
1243-
1244-
int start = 0;
1245-
int limit = 100;
1246-
boolean hasMoreTenants = true;
1247-
while (hasMoreTenants) {
1248-
PageOfTenants pageOfTenants = listTenants(start, limit);
1249-
pageOfTenants.getItems().forEach(tenant -> {
1250-
logger.info("Getting usage for tenant:{}", tenant.getTenantId());
1251-
OsisUsage tenantOsisUsage = getOsisUsage(Optional.of(tenant.getTenantId()), Optional.empty());
1252-
logger.info("Usage for tenant:{} is:{}", tenant.getTenantId(), new Gson().toJson(tenantOsisUsage));
1253-
1254-
logger.debug("Consolidating usage for tenant:{}", tenant.getTenantId());
1255-
osisUsage.consolidateUsage(tenantOsisUsage);
1256-
logger.debug("Consolidated usage:{}", new Gson().toJson(osisUsage));
1257-
});
1258-
start += limit;
1259-
hasMoreTenants = pageOfTenants.getPageInfo().getTotal() >= limit;
1260-
}
1261-
1262-
logger.info("Usage for all tenants is:{}", new Gson().toJson(osisUsage));
1263-
1264-
} else {
1265-
1266-
try {
1267-
// getting utapi client for tenant
1268-
Credentials tempCredentials = getCredentials(tenantId.get());
1269-
final UtapiServiceClient utapiClient = this.utapi.getUtapiServiceClient(tempCredentials,
1270-
appEnv.getRegionInfo().get(0));
1271-
1272-
if (userId.isEmpty()) {
1273-
1274-
logger.info("tenant ID is specified, getting usage at tenant level, tenant ID:{}", tenantId.get());
1275-
1276-
// getting s3 client for tenant
1277-
final AmazonS3 s3Client = this.s3.getS3Client(tempCredentials,
1278-
appEnv.getRegionInfo().get(0));
1279-
1280-
logger.debug("Listing buckets for tenant:{}", tenantId.get());
1281-
List<Bucket> buckets = s3Client.listBuckets();
1282-
1283-
// set bucket count by the size of listing buckets
1284-
osisUsage.setBucketCount((long) buckets.size());
1285-
1286-
logger.debug("[UTAPI] Listing metrics for tenant:{}", tenantId.get());
1287-
ListMetricsRequestDTO listMetricsRequestDTO = ScalityModelConverter.toScalityListMetricsRequest(
1288-
"accounts",
1289-
List.of(tenantId.get()),
1290-
List.of(getHourTime())
1291-
);
1292-
Response<MetricsData[]> listMetricsResponseDTO = utapiClient.listAccountsMetrics(listMetricsRequestDTO);
1293-
MetricsData metricsData = listMetricsResponseDTO.getAwsResponse()[0];
1294-
logger.debug("[UTAPI] List Metrics response:{}", new Gson().toJson(metricsData));
1295-
1296-
// set object count and used bytes by the utapi metrics
1297-
osisUsage.setObjectCount(metricsData.getNumberOfObjects().get(1));
1298-
osisUsage.setUsedBytes(metricsData.getStorageUtilized().get(1));
1299-
1300-
AccountData account = vaultAdmin.getAccount(ScalityModelConverter.toGetAccountRequestWithID(tenantId.get()));
1301-
if (account.getQuota() > 0) {
1302-
osisUsage.setTotalBytes((long) account.getQuota()); // set total bytes by the vault quota
1303-
osisUsage.setAvailableBytes(osisUsage.getTotalBytes() - osisUsage.getUsedBytes()); // set available bytes by the difference between total bytes and used bytes
1304-
}
1305-
logger.info("Usage for tenant:{} is:{}", tenantId.get(), new Gson().toJson(osisUsage));
1306-
1307-
} else {
1308-
1309-
logger.info("tenant ID and user ID are specified, getting usage at user level, tenant ID:{}, user ID:{}", tenantId.get(), userId.get());
1310-
1311-
logger.debug("[UTAPI] Listing metrics for user:{}", userId.get());
1312-
ListMetricsRequestDTO listMetricsRequestDTO = ScalityModelConverter.toScalityListMetricsRequest(
1313-
"users",
1314-
List.of(userId.get()),
1315-
List.of(getHourTime())
1316-
);
1317-
Response<MetricsData[]> listMetricsResponseDTO = utapiClient.listUsersMetrics(listMetricsRequestDTO);
1318-
MetricsData metricsData = listMetricsResponseDTO.getAwsResponse()[0];
1319-
logger.debug("[UTAPI] List Metrics response:{}", new Gson().toJson(metricsData));
1320-
1321-
// set object count and used bytes by the utapi metrics
1322-
osisUsage.setObjectCount(metricsData.getNumberOfObjects().get(1));
1323-
osisUsage.setUsedBytes(metricsData.getStorageUtilized().get(1));
1324-
1325-
logger.info("Usage for user:{} is:{}", userId.get(), new Gson().toJson(osisUsage));
1326-
}
1327-
} catch (Exception err) {
1328-
if (isAdminPolicyError(err)) {
1329-
try {
1330-
generateAdminPolicy(tenantId.get());
1331-
return getOsisUsage(tenantId, userId);
1332-
} catch (Exception ex) {
1333-
err = ex;
1334-
}
1335-
}
1336-
logger.error("GetUsage error. Returning empty list. Error details: ", err);
1337-
// For errors, GetUsage should return empty OsisUsage
1338-
return osisUsage;
1339-
}
1340-
}
1341-
return osisUsage;
1232+
throw new NotImplementedException();
13421233
}
13431234

13441235
@Override

osis-core/src/test/java/com/scality/osis/service/impl/ScalityOsisServiceMiscTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.scality.osis.utapiclient.dto.MetricsData;
1515
import com.scality.osis.vaultadmin.impl.VaultServiceException;
1616
import com.scality.vaultclient.dto.*;
17+
18+
import org.junit.jupiter.api.Disabled;
1719
import org.junit.jupiter.api.Test;
1820
import org.mockito.invocation.InvocationOnMock;
1921
import org.mockito.stubbing.Answer;
@@ -27,6 +29,8 @@
2729
import static com.scality.osis.utils.ScalityConstants.IAM_PREFIX;
2830
import static com.scality.osis.utils.ScalityTestUtils.*;
2931
import static org.junit.jupiter.api.Assertions.*;
32+
import static org.mockito.ArgumentMatchers.any;
33+
import static org.mockito.ArgumentMatchers.anyLong;
3034
import static org.mockito.Mockito.*;
3135

3236
class ScalityOsisServiceMiscTests extends BaseOsisServiceTest {
@@ -173,7 +177,19 @@ void testGetBucketListErr() {
173177
assertEquals(0L, response.getItems().size());
174178
}
175179

180+
181+
// test to check if getUsage API throws an error not implemented
182+
// this will be removed as a part of S3C-8266
183+
@Test
184+
void testGetUsage() {
185+
// Setup
186+
assertThrows(NotImplementedException.class, () -> scalityOsisServiceUnderTest.getOsisUsage(Optional.of(SAMPLE_TENANT_ID), Optional.of(SAMPLE_CD_TENANT_ID)),
187+
NOT_IMPLEMENTED_EXCEPTION_ERR);
188+
}
189+
176190
@Test
191+
@Disabled
192+
// This will be enable with S3C-8266
177193
void testGetOsisUsageForAll() {
178194

179195
// Setup
@@ -224,6 +240,8 @@ void testGetOsisUsageForAll() {
224240
}
225241

226242
@Test
243+
@Disabled
244+
// This will be enable with S3C-8266
227245
void testGetOsisUsageForTenant() {
228246

229247
// Setup
@@ -263,6 +281,8 @@ void testGetOsisUsageForTenant() {
263281
}
264282

265283
@Test
284+
@Disabled
285+
// This will be enable with S3C-8266
266286
void testGetOsisUsageForUser() {
267287

268288
// Setup
@@ -281,6 +301,8 @@ void testGetOsisUsageForUser() {
281301
}
282302

283303
@Test
304+
@Disabled
305+
// This will be enable with S3C-8266
284306
void testGetOsisUsageErr() {
285307

286308
// Setup

0 commit comments

Comments
 (0)