Skip to content

Commit

Permalink
fix: fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikRevich committed Dec 9, 2024
1 parent ebd1873 commit 166143d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;

/**
Expand All @@ -24,7 +22,7 @@ public class RepositoryConfigurationHelper {
* @return packed external credentials signature.
*/
private String packExternalCredentials(String ...values) {
StringJoiner result = new StringJoiner(":");
StringJoiner result = new StringJoiner("|");

for (String value : values) {
result.add(value);
Expand All @@ -40,7 +38,7 @@ private String packExternalCredentials(String ...values) {
* @return unpacked external credentials signature.
*/
private List<String> unpackExternalCredentials(String credentials) {
return List.of(credentials.split(":"));
return List.of(credentials.split("\\|"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.google.auth.oauth2.UserCredentials;
import com.google.cloud.WriteChannel;
import com.google.cloud.resourcemanager.ResourceManager;
import com.google.cloud.resourcemanager.ResourceManagerException;
import com.google.cloud.resourcemanager.ResourceManagerOptions;
import com.google.cloud.resourcemanager.Project;
import com.google.cloud.storage.*;
import com.objectstorage.dto.VendorObjectListingDto;
import com.objectstorage.exception.GCPCredentialsInitializationFailureException;
import com.objectstorage.exception.GCSBucketObjectUploadFailureException;
import com.objectstorage.exception.VendorOperationFailureException;
import jakarta.enterprise.context.ApplicationScoped;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -50,52 +52,73 @@ public Credentials getCredentials(String secrets) throws GCPCredentialsInitializ
* @param name given name of the GCS bucket.
* @param credentials given credentials to be used for client configuration.
* @return result of the check.
* @throws VendorOperationFailureException if vendor operation fails.
*/
public Boolean isGCSBucketPresent(
Credentials credentials,
String name) {
String name) throws VendorOperationFailureException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();

Bucket bucket = storage.get(name);
Bucket bucket;

return Objects.nonNull(bucket) && bucket.exists();
try {
bucket = storage.get(name);
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}

try {
return Objects.nonNull(bucket) && bucket.exists();
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}
}

/**
* Creates GCS bucket with the given name.
*
* @param credentials given credentials to be used for client configuration.
* @param name given name of the GCS bucket.
* @throws VendorOperationFailureException if vendor operation fails.
*/
public void createGCSBucket(
Credentials credentials,
String name) {
String name) throws VendorOperationFailureException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();

storage.create(BucketInfo.newBuilder(name).build());
try {
storage.create(BucketInfo.newBuilder(name).build());
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}
}

/**
* Removes GCS bucket with the given name.
*
* @param credentials given credentials to be used for client configuration.
* @param name given name of the GCS bucket.
* @throws VendorOperationFailureException if vendor operation fails.
*/
public void removeGCSBucket(
Credentials credentials,
String name) {
String name) throws VendorOperationFailureException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();

storage.delete(name);
try {
storage.delete(name);
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}
}

/**
Expand Down Expand Up @@ -133,19 +156,30 @@ public void uploadObjectToGCSBucket(
* @param bucketName given name of the GCS bucket.
* @param fileName given name of the file to be retrieved.
* @return result of the check.
* @throws VendorOperationFailureException if vendor operation fails.
*/
public Boolean isObjectPresentInBucket(
Credentials credentials,
String bucketName,
String fileName) {
String fileName) throws VendorOperationFailureException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();

Blob blob = storage.get(BlobId.of(bucketName, fileName));
Blob blob;

try {
blob = storage.get(BlobId.of(bucketName, fileName));
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}

return Objects.nonNull(blob) && blob.exists();
try {
return Objects.nonNull(blob) && blob.exists();
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}
}

/**
Expand All @@ -155,19 +189,30 @@ public Boolean isObjectPresentInBucket(
* @param bucketName given name of the GCS bucket.
* @param fileName given name of the file to be retrieved.
* @return retrieved object content.
* @throws VendorOperationFailureException if vendor operation fails.
*/
public byte[] retrieveObjectFromGCSBucket(
Credentials credentials,
String bucketName,
String fileName) {
String fileName) throws VendorOperationFailureException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();

Blob blob = storage.get(BlobId.of(bucketName, fileName));
Blob blob;

try {
blob = storage.get(BlobId.of(bucketName, fileName));
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}

return blob.getContent();
try {
return blob.getContent();
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}
}

/**
Expand All @@ -176,22 +221,27 @@ public byte[] retrieveObjectFromGCSBucket(
* @param credentials given credentials to be used for client configuration.
* @param bucketName given name of the GCS bucket.
* @return listed objects.
* @throws VendorOperationFailureException if vendor operation fails.
*/
public List<VendorObjectListingDto> listObjectsFromGCSBucket(
Credentials credentials,
String bucketName) {
String bucketName) throws VendorOperationFailureException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();

Page<Blob> blobs = storage.list(bucketName);
try {
Page<Blob> blobs = storage.list(bucketName);

return StreamSupport.stream(blobs.iterateAll().spliterator(), false)
.map(element -> VendorObjectListingDto.of(
element.getBlobId().getName(),
element.getUpdateTimeOffsetDateTime().toEpochSecond()))
.toList();
return StreamSupport.stream(blobs.iterateAll().spliterator(), false)
.map(element -> VendorObjectListingDto.of(
element.getBlobId().getName(),
element.getUpdateTimeOffsetDateTime().toEpochSecond()))
.toList();
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}
}

/**
Expand All @@ -200,37 +250,47 @@ public List<VendorObjectListingDto> listObjectsFromGCSBucket(
* @param credentials given credentials to be used for client configuration.
* @param bucketName given name of the GCS bucket.
* @param fileName given name of the file to be removed.
* @throws VendorOperationFailureException if vendor operation fails.
*/
public void removeObjectFromGCSBucket(
Credentials credentials,
String bucketName,
String fileName) {
String fileName) throws VendorOperationFailureException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();

storage.delete(BlobId.of(bucketName, fileName));
try {
storage.delete(BlobId.of(bucketName, fileName));
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}
}

/**
* Removes all objects from the GCS bucket with the given name.
*
* @param credentials given credentials to be used for client configuration.
* @param bucketName given name of the GCS bucket.
* @throws VendorOperationFailureException if vendor operation fails.
*/
public void removeAllObjectsFromGCSBucket(
Credentials credentials,
String bucketName) {
String bucketName) throws VendorOperationFailureException {
Storage storage = StorageOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();

Page<Blob> blobs = storage.list(bucketName);
try {
Page<Blob> blobs = storage.list(bucketName);

for (Blob blob : blobs.iterateAll()) {
blob.delete(Blob.BlobSourceOption.generationMatch());
for (Blob blob : blobs.iterateAll()) {
blob.delete(Blob.BlobSourceOption.generationMatch());
}
} catch (StorageException e) {
throw new VendorOperationFailureException(e.getMessage());
}
}

Expand All @@ -246,8 +306,12 @@ public Boolean isCallerValid(Credentials credentials) {
.build()
.getService();

for (Project project : resourceManager.list().iterateAll()) {
return true;
try {
for (Project project : resourceManager.list().iterateAll()) {
return true;
}
} catch (ResourceManagerException e) {
return false;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.*;
import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -36,7 +37,9 @@ public class WorkspaceFacade {
* @return created workspace unit key.
*/
public String createWorkspaceUnitKey(ValidationSecretsApplication validationSecretsApplication) {
return validationSecretsApplication.getSecrets().stream().map(element ->
return validationSecretsApplication.getSecrets().stream()
.sorted(Comparator.comparing(element -> element.getProvider().toString()))
.map(element ->
switch (element.getProvider()) {
case S3 -> workspaceService.createUnitKey(
element.getProvider().toString(),
Expand All @@ -63,7 +66,7 @@ public String createFileUnitKey(String name) {
String fileUnit =
workspaceService.createUnitKey(name, Instant.now().toString());

return String.format("%s-%s-%d", name, fileUnit, timestamp.toEpochMilli());
return String.format("%s-%d", fileUnit, timestamp.toEpochMilli());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ databaseChangeLog:
type: TEXT
constraints:
nullable: false
unique: true
- column:
name: created_at
type: BIGINT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ databaseChangeLog:
type: VARCHAR
constraints:
nullable: false
unique: true
- column:
name: created_at
type: LONG
Expand Down

0 comments on commit 166143d

Please sign in to comment.