generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into story/1538/micro-character-encoding-assertion
- Loading branch information
Showing
9 changed files
with
220 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/AzureBlobHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package gov.hhs.cdc.trustedintermediary.rse2e; | ||
|
||
import java.time.LocalDate; | ||
|
||
/* The AzureBlobHelper is a utility class that provides helper methods for working with Azure Blob Storage. */ | ||
public class AzureBlobHelper { | ||
|
||
private AzureBlobHelper() {} | ||
|
||
// Builds a path prefix for a given date in the format "YYYY/MM/DD/". This is meant to make it | ||
// easier for people in the team to find files in the Azure Blob Storage | ||
public static String buildDatePathPrefix(LocalDate date) { | ||
return String.format( | ||
"%d/%02d/%02d/", date.getYear(), date.getMonthValue(), date.getDayOfMonth()); | ||
} | ||
|
||
public static String createDateBasedPath(LocalDate date, String originalName) { | ||
return buildDatePathPrefix(date) + originalName; | ||
} | ||
|
||
public static boolean isInDateFolder(String blobPath, LocalDate creationDate) { | ||
String expectedPath = buildDatePathPrefix(creationDate); | ||
return blobPath.startsWith(expectedPath); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
rs-e2e/src/main/java/gov/hhs/cdc/trustedintermediary/rse2e/AzureBlobOrganizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package gov.hhs.cdc.trustedintermediary.rse2e; | ||
|
||
import com.azure.storage.blob.BlobClient; | ||
import com.azure.storage.blob.BlobContainerClient; | ||
import com.azure.storage.blob.models.BlobItem; | ||
import com.azure.storage.blob.models.BlobProperties; | ||
import com.azure.storage.blob.models.CopyStatusType; | ||
import gov.hhs.cdc.trustedintermediary.context.ApplicationContext; | ||
import gov.hhs.cdc.trustedintermediary.wrappers.Logger; | ||
import java.time.Duration; | ||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
|
||
/* AzureBlobOrganizer is responsible for organizing and cleaning up blobs in an Azure container */ | ||
public class AzureBlobOrganizer { | ||
|
||
private final BlobContainerClient blobContainerClient; | ||
|
||
protected final Logger logger = ApplicationContext.getImplementation(Logger.class); | ||
|
||
public AzureBlobOrganizer(BlobContainerClient blobContainerClient) { | ||
this.blobContainerClient = blobContainerClient; | ||
} | ||
|
||
public void organizeAndCleanupBlobsByDate(int retentionDays, ZoneId timeZone) { | ||
for (BlobItem blobItem : blobContainerClient.listBlobs()) { | ||
String sourceName = blobItem.getName(); | ||
try { | ||
BlobClient sourceBlob = blobContainerClient.getBlobClient(sourceName); | ||
BlobProperties sourceProperties = sourceBlob.getProperties(); | ||
LocalDate sourceCreationDate = | ||
sourceProperties | ||
.getCreationTime() | ||
.toInstant() | ||
.atZone(timeZone) | ||
.toLocalDate(); | ||
|
||
LocalDate retentionDate = LocalDate.now(timeZone).minusDays(retentionDays); | ||
if (sourceCreationDate.isBefore(retentionDate)) { | ||
sourceBlob.delete(); | ||
logger.logInfo("Deleted old blob: {}", sourceName); | ||
continue; | ||
} | ||
|
||
if (AzureBlobHelper.isInDateFolder(sourceName, sourceCreationDate)) { | ||
continue; | ||
} | ||
|
||
String destinationName = | ||
AzureBlobHelper.createDateBasedPath(sourceCreationDate, sourceName); | ||
BlobClient destinationBlob = blobContainerClient.getBlobClient(destinationName); | ||
destinationBlob | ||
.beginCopy(sourceBlob.getBlobUrl(), null) | ||
.waitForCompletion(Duration.ofSeconds(30)); | ||
|
||
CopyStatusType copyStatus = destinationBlob.getProperties().getCopyStatus(); | ||
if (copyStatus == CopyStatusType.SUCCESS) { | ||
sourceBlob.delete(); | ||
logger.logInfo("Moved blob {} to {}", sourceName, destinationName); | ||
} else { | ||
destinationBlob.delete(); | ||
logger.logError("Failed to copy blob: " + sourceName); | ||
} | ||
} catch (Exception e) { | ||
logger.logError("Error processing blob: " + sourceName, e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
rs-e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/rse2e/AzureBlobHelperTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package gov.hhs.cdc.trustedintermediary.rse2e | ||
|
||
import spock.lang.Specification | ||
|
||
import java.time.LocalDate | ||
|
||
class AzureBlobHelperTest extends Specification { | ||
|
||
def "buildDatePathPrefix should create correct path format"() { | ||
given: | ||
def date = LocalDate.of(2024, 3, 15) | ||
|
||
when: | ||
def result = AzureBlobHelper.buildDatePathPrefix(date) | ||
|
||
then: | ||
result == "2024/03/15/" | ||
} | ||
|
||
def "createDateBasedPath should combine date prefix with filename"() { | ||
given: | ||
def date = LocalDate.of(2024, 3, 15) | ||
def fileName = "test.hl7" | ||
|
||
when: | ||
def result = AzureBlobHelper.createDateBasedPath(date, fileName) | ||
|
||
then: | ||
result == "2024/03/15/test.hl7" | ||
} | ||
|
||
def "isInDateFolder should return true for matching date folder"() { | ||
given: | ||
def date = LocalDate.of(2024, 3, 15) | ||
def path = "2024/03/15/test.hl7" | ||
|
||
expect: | ||
AzureBlobHelper.isInDateFolder(path, date) | ||
} | ||
} |
Oops, something went wrong.