Skip to content
Snippets Groups Projects
Commit f2698b77 authored by Rostislav Dublin (EPAM)'s avatar Rostislav Dublin (EPAM)
Browse files

Merge branch 'unique_backed_name_across_GCP' into 'master'

Unique bucket name across GCP (GONRG-701)

See merge request !46
parents 2bb3d69c e88b41aa
No related branches found
No related tags found
1 merge request!46Unique bucket name across GCP (GONRG-701)
Pipeline #10301 passed
......@@ -34,4 +34,5 @@ handlers:
env_variables:
SPRING_PROFILES_ACTIVE: "ENVIRONMENT"
ENABLE_FULL_BUCKET_NAME: 'ENABLE_FULL_BUCKET_NAME_VAR'
\ No newline at end of file
......@@ -4,13 +4,17 @@ import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReaderFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class StorageReaderFactoryImpl implements IStorageReaderFactory {
@Value("#{new Boolean('${enable.full.bucket.name:false}')}")
private Boolean isFullBucketName;
@Override
public IStorageReader getReader(TenantInfo tenant, String projectRegion) {
return new StorageReaderImpl(tenant, projectRegion);
return new StorageReaderImpl(tenant, projectRegion, isFullBucketName);
}
}
......@@ -2,6 +2,7 @@ package org.opengroup.osdu.legal.countries;
import com.google.cloud.storage.*;
import java.util.Objects;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.legal.provider.interfaces.IStorageReader;
import org.springframework.http.MediaType;
......@@ -14,16 +15,22 @@ public class StorageReaderImpl implements IStorageReader {
private String projectRegion;
private Storage storage;
private static final String BUCKET_NAME = "legal-service-configuration";
protected static final String BUCKET_NAME = "legal-service-configuration";
private static final String FILE_NAME = "Legal_COO.json";
private Boolean isFullBucketName = false;
public StorageReaderImpl(TenantInfo tenantInfo, String projectRegion) {
new StorageReaderImpl(tenantInfo, projectRegion, false);
}
public StorageReaderImpl(TenantInfo tenantInfo, String projectRegion, Boolean isFullBucketName) {
this.tenantInfo = tenantInfo;
this.projectRegion = projectRegion;
this.storage = getStorage();
}
this.isFullBucketName = isFullBucketName;
}
@Override
@Override
public byte[] readAllBytes() {
BlobId blobId = getBlobId();
byte[] content = null;
......@@ -67,8 +74,11 @@ public class StorageReaderImpl implements IStorageReader {
this.storage.create(blobInfo, "".getBytes(UTF_8));
}
private String getTenantBucketName() {
return this.tenantInfo.getName() + "-" + BUCKET_NAME;
protected String getTenantBucketName() {
if (Objects.nonNull(isFullBucketName) && isFullBucketName) {
return this.tenantInfo.getProjectId() + "-" + this.tenantInfo.getName() + "-" + BUCKET_NAME;
}
return this.tenantInfo.getName() + "-" + BUCKET_NAME;
}
}
......@@ -8,4 +8,4 @@ server.port=8080
JAVA_HEAP_OPTS=-Xms4096M -Xmx4096M
JAVA_GC_OPTS=-XX:+UseG1GC -XX:+UseStringDeduplication -XX:InitiatingHeapOccupancyPercent=45
enable.full.bucket.name=false
\ No newline at end of file
......@@ -13,6 +13,7 @@ import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.opengroup.osdu.legal.countries.StorageReaderImpl.BUCKET_NAME;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(MockitoJUnitRunner.class)
......@@ -74,4 +75,37 @@ public class StorageReaderImplTests {
byte[] bytes = sut.readAllBytes();
assertEquals(expectedBytes, bytes);
}
@Test
public void should_returnFullBucketName_when_IsFullBucketName_is_true() {
when(tenantInfo.getName()).thenReturn("tenant1");
when(tenantInfo.getProjectId()).thenReturn("projectId1");
String bucketName = tenantInfo.getProjectId() + "-" + tenantInfo.getName() + "-" + BUCKET_NAME;
StorageReaderImpl storageReader = new StorageReaderImpl(tenantInfo, null,
true);
String resultBucketName = storageReader.getTenantBucketName();
assertEquals(bucketName, resultBucketName);
}
@Test
public void should_returnBucketName_when_IsFullBucketName_is_false() {
when(tenantInfo.getName()).thenReturn("tenant1");
when(tenantInfo.getProjectId()).thenReturn("projectId1");
String bucketName = tenantInfo.getName() + "-" + BUCKET_NAME;
StorageReaderImpl storageReader = new StorageReaderImpl(tenantInfo, null,
false);
String resultBucketName = storageReader.getTenantBucketName();
assertEquals(bucketName, resultBucketName);
}
@Test
public void should_returnBucketName_when_IsFullBucketName_is_null() {
when(tenantInfo.getName()).thenReturn("tenant1");
when(tenantInfo.getProjectId()).thenReturn("projectId1");
String bucketName = tenantInfo.getName() + "-" + BUCKET_NAME;
StorageReaderImpl storageReader = new StorageReaderImpl(tenantInfo, null,
null);
String resultBucketName = storageReader.getTenantBucketName();
assertEquals(bucketName, resultBucketName);
}
}
\ No newline at end of file
enable.full.bucket.name=false
......@@ -117,6 +117,12 @@
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
<!-- Tests -->
<dependency>
......@@ -124,9 +130,22 @@
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependency>
<!--Logging-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.8.0-beta4</version>
</dependency>
</dependencies>
<!-- can remove this? -->
<repositories>
<repository>
......
package org.opengroup.osdu.legal.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import com.google.api.client.util.Strings;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.BlobId;
......@@ -12,7 +7,13 @@ import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.collect.Lists;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import lombok.extern.java.Log;
@Log
public class GCPLegalTagUtils extends LegalTagUtils {
private static final String BUCKET_NAME = "legal-service-configuration";
private static final String FILE_NAME = "Legal_COO.json";
......@@ -38,11 +39,30 @@ public class GCPLegalTagUtils extends LegalTagUtils {
}
}
private static String getTenantBucketName() {
String tenantName = System.getProperty("MY_TENANT_PROJECT", System.getenv("MY_TENANT_PROJECT")).toLowerCase();
return tenantName + "-" +BUCKET_NAME;
private static String getTenantBucketName() {
String tenantName = System
.getProperty("MY_TENANT_PROJECT", System.getenv("MY_TENANT_PROJECT")).toLowerCase();
String projectName = System.getProperty("GCLOUD_PROJECT", System.getenv("GCLOUD_PROJECT"))
.toLowerCase();
String enableFullBucketName = System.getProperty("ENABLE_FULL_BUCKET_NAME",
System.getenv("ENABLE_FULL_BUCKET_NAME"));
enableFullBucketName = (Strings.isNullOrEmpty(enableFullBucketName) ? "false"
: enableFullBucketName).toLowerCase();
log.info("ENABLE_FULL_BUCKET_NAME = " + enableFullBucketName);
String bucketName;
if (Boolean.parseBoolean(enableFullBucketName)) {
bucketName = projectName + "-" + tenantName + "-" + BUCKET_NAME;
} else {
bucketName = tenantName + "-" + BUCKET_NAME;
}
log.info("bucketName = " + bucketName);
return bucketName;
}
@Override
public synchronized String accessToken() throws Exception {
if (Strings.isNullOrEmpty(token)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment