Skip to content

Commit 1e3f717

Browse files
Make object store service info creation independent from plan
1 parent 0573010 commit 1e3f717

File tree

4 files changed

+253
-39
lines changed

4 files changed

+253
-39
lines changed

multiapps-controller-web/src/main/java/org/cloudfoundry/multiapps/controller/web/Constants.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,20 @@ private Constants() {
1818
public static final long OAUTH_TOKEN_RETENTION_TIME_IN_SECONDS = TimeUnit.MINUTES.toSeconds(2);
1919
public static final long BASIC_TOKEN_RETENTION_TIME_IN_SECONDS = TimeUnit.MINUTES.toSeconds(6);
2020

21+
// Object Store
22+
public static final String ACCESS_KEY_ID = "access_key_id";
23+
public static final String SECRET_ACCESS_KEY = "secret_access_key";
24+
public static final String BUCKET = "bucket";
25+
public static final String REGION = "region";
26+
public static final String ENDPOINT = "endpoint";
27+
public static final String ACCOUNT_NAME = "account_name";
28+
public static final String SAS_TOKEN = "sas_token";
29+
public static final String CONTAINER_NAME = "container_name";
30+
public static final String CONTAINER_URI = "container_uri";
31+
public static final String BASE_64_ENCODED_PRIVATE_KEY_DATA = "base64EncodedPrivateKeyData";
32+
33+
public static final String AWS_S_3 = "aws-s3";
34+
public static final String AZUREBLOB = "azureblob";
35+
public static final String ALIYUN_OSS = "aliyun-oss";
36+
public static final String GOOGLE_CLOUD_STORAGE_CUSTOM = "google-cloud-storage-custom";
2137
}

multiapps-controller-web/src/main/java/org/cloudfoundry/multiapps/controller/web/Messages.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public final class Messages {
1818
public static final String ERROR_FROM_REMOTE_MTAR_ENDPOINT = "Error from remote MTAR endpoint {0} with status code {1}, message: {2}";
1919
public static final String MTAR_ENDPOINT_NOT_SECURE = "Remote MTAR endpoint is not a secure connection. HTTPS required";
2020
public static final String CANNOT_PARSE_CONTAINER_URI_OF_OBJECT_STORE = "Cannot parse container_uri of object store";
21-
public static final String UNSUPPORTED_SERVICE_PLAN_FOR_OBJECT_STORE = "Unsupported service plan for object store!";
2221
public static final String REQUEST_0_1_FAILED_WITH_2 = "Request \"{0} {1}\" failed with \"{2}\"";
2322
public static final String ERROR_OCCURRED_WHILE_DELETING_JOB_ENTRY = "Error occurred while deleting job entry";
2423
public static final String JOB_0_HAS_NOT_BEEN_UPDATED_FOR_15_MINUTES = "Job {0} has not been updated for 15 minutes.";
@@ -48,6 +47,7 @@ public final class Messages {
4847
public static final String JOB_IS_NOT_BEING_EXECUTED = "Job is not being executed";
4948
public static final String JOB_0_EXISTS_IN_STATE_1_BUT_DOES_NOT_EXISTS_IN_THE_RUNNING_TASKS = "Job \"{0}\" exists in state \"{1}\" but does not exists in the running tasks";
5049
public static final String JOB_THREAD_IS_NOT_RUNNING_BUT_STATE_IS_STILL_IN_PROGRESS_UPLOAD_FAILED = "Job thread is not running but state is still in progress. Upload failed";
50+
public static final String ERROR_BUILDING_OBJECT_STORE_CONFIGURATION = "Error while building object store configuration.";
5151

5252
// WARN log messages
5353
public static final String THE_JOB_EXISTS_BUT_IT_IS_NOT_RUNNING_DELETING = "The job exists but it is not running. Deleting";
@@ -61,6 +61,10 @@ public final class Messages {
6161
public static final String OBJECTSTORE_FOR_BINARIES_STORAGE = "Objectstore will be used for binaries storage";
6262
public static final String CLEARING_LOCK_OWNER = "Clearing lock owner {0}...";
6363
public static final String CLEARED_LOCK_OWNER = "Cleared lock owner {0}";
64+
public static final String CREATING_OBJECT_STORE_FOR_ALI_CLOUD = "Creating object store for Ali Cloud";
65+
public static final String CREATING_OBJECT_STORE_FOR_AWS = "Creating object store for AWS";
66+
public static final String CREATING_OBJECT_STORE_FOR_AZURE = "Creating object store for Azure";
67+
public static final String CREATING_OBJECT_STORE_FOR_GCP = "Creating object store for GCP";
6468

6569
// DEBUG log messages
6670
public static final String RECEIVED_UPLOAD_REQUEST = "Received upload request on URI: {}";

multiapps-controller-web/src/main/java/org/cloudfoundry/multiapps/controller/web/configuration/service/ObjectStoreServiceInfoCreator.java

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,78 @@
66
import java.util.Base64;
77
import java.util.Map;
88

9+
import org.cloudfoundry.multiapps.controller.web.Constants;
910
import org.cloudfoundry.multiapps.controller.web.Messages;
1011
import org.jclouds.domain.Credentials;
1112
import org.jclouds.googlecloud.GoogleCredentialsFromJson;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1215

1316
import com.google.common.base.Supplier;
1417

1518
import io.pivotal.cfenv.core.CfService;
1619

1720
public class ObjectStoreServiceInfoCreator {
1821

19-
private static final String OBJECT_STORE_AWS_PLAN = "s3-standard";
20-
private static final String OBJECT_STORE_AZURE_PLAN = "azure-standard";
21-
private static final String OBJECT_STORE_ALICLOUD_PLAN = "oss-standard";
22-
private static final String OBJECT_STORE_GCP_PLAN = "gcs-standard";
22+
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectStoreServiceInfoCreator.class);
2323

2424
public ObjectStoreServiceInfo createServiceInfo(CfService service) {
25-
String plan = service.getPlan();
2625
Map<String, Object> credentials = service.getCredentials()
2726
.getMap();
28-
switch (plan) {
29-
case OBJECT_STORE_AWS_PLAN:
30-
return createServiceInfoForAws(credentials);
31-
case OBJECT_STORE_AZURE_PLAN:
32-
return createServiceInfoForAzure(credentials);
33-
case OBJECT_STORE_ALICLOUD_PLAN:
34-
return createServiceInfoForAliCloud(credentials);
35-
case OBJECT_STORE_GCP_PLAN:
36-
return createServiceInfoForGcpCloud(credentials);
37-
default:
38-
throw new IllegalStateException(Messages.UNSUPPORTED_SERVICE_PLAN_FOR_OBJECT_STORE);
27+
if (isAliCloudIaas(credentials)) {
28+
LOGGER.info(Messages.CREATING_OBJECT_STORE_FOR_ALI_CLOUD);
29+
return createServiceInfoForAliCloud(credentials);
3930
}
31+
if (isAwsIaas(credentials)) {
32+
LOGGER.info(Messages.CREATING_OBJECT_STORE_FOR_AWS);
33+
return createServiceInfoForAws(credentials);
34+
}
35+
if (isAzureIaas(credentials)) {
36+
LOGGER.info(Messages.CREATING_OBJECT_STORE_FOR_AZURE);
37+
return createServiceInfoForAzure(credentials);
38+
}
39+
if (isGcpIaas(credentials)) {
40+
LOGGER.info(Messages.CREATING_OBJECT_STORE_FOR_GCP);
41+
return createServiceInfoForGcpCloud(credentials);
42+
}
43+
throw new IllegalStateException(Messages.ERROR_BUILDING_OBJECT_STORE_CONFIGURATION);
44+
}
45+
46+
private boolean isAwsIaas(Map<String, Object> credentials) {
47+
// @formatter:off
48+
return credentials.containsKey(Constants.ACCESS_KEY_ID) &&
49+
credentials.containsKey(Constants.SECRET_ACCESS_KEY) &&
50+
credentials.containsKey(Constants.BUCKET);
51+
// @formatter:on
4052
}
4153

4254
private ObjectStoreServiceInfo createServiceInfoForAws(Map<String, Object> credentials) {
43-
String accessKeyId = (String) credentials.get("access_key_id");
44-
String secretAccessKey = (String) credentials.get("secret_access_key");
45-
String bucket = (String) credentials.get("bucket");
55+
String accessKeyId = (String) credentials.get(Constants.ACCESS_KEY_ID);
56+
String secretAccessKey = (String) credentials.get(Constants.SECRET_ACCESS_KEY);
57+
String bucket = (String) credentials.get(Constants.BUCKET);
4658
return ImmutableObjectStoreServiceInfo.builder()
47-
.provider("aws-s3")
59+
.provider(Constants.AWS_S_3)
4860
.identity(accessKeyId)
4961
.credential(secretAccessKey)
5062
.container(bucket)
5163
.build();
5264
}
5365

66+
private boolean isAzureIaas(Map<String, Object> credentials) {
67+
// @formatter:off
68+
return credentials.containsKey(Constants.ACCOUNT_NAME) &&
69+
credentials.containsKey(Constants.SAS_TOKEN) &&
70+
credentials.containsKey(Constants.CONTAINER_NAME) &&
71+
credentials.containsKey(Constants.CONTAINER_URI);
72+
// @formatter:on
73+
}
74+
5475
private ObjectStoreServiceInfo createServiceInfoForAzure(Map<String, Object> credentials) {
55-
String accountName = (String) credentials.get("account_name");
56-
String sasToken = (String) credentials.get("sas_token");
57-
String containerName = (String) credentials.get("container_name");
76+
String accountName = (String) credentials.get(Constants.ACCOUNT_NAME);
77+
String sasToken = (String) credentials.get(Constants.SAS_TOKEN);
78+
String containerName = (String) credentials.get(Constants.CONTAINER_NAME);
5879
return ImmutableObjectStoreServiceInfo.builder()
59-
.provider("azureblob")
80+
.provider(Constants.AZUREBLOB)
6081
.identity(accountName)
6182
.credential(sasToken)
6283
.endpoint(getContainerUriEndpoint(credentials).toString())
@@ -66,21 +87,31 @@ private ObjectStoreServiceInfo createServiceInfoForAzure(Map<String, Object> cre
6687

6788
private URL getContainerUriEndpoint(Map<String, Object> credentials) {
6889
try {
69-
URL containerUri = new URL((String) credentials.get("container_uri"));
90+
URL containerUri = new URL((String) credentials.get(Constants.CONTAINER_URI));
7091
return new URL(containerUri.getProtocol(), containerUri.getHost(), containerUri.getPort(), "");
7192
} catch (MalformedURLException e) {
7293
throw new IllegalStateException(Messages.CANNOT_PARSE_CONTAINER_URI_OF_OBJECT_STORE, e);
7394
}
7495
}
7596

97+
private boolean isAliCloudIaas(Map<String, Object> credentials) {
98+
// @formatter:off
99+
return credentials.containsKey(Constants.ACCESS_KEY_ID) &&
100+
credentials.containsKey(Constants.SECRET_ACCESS_KEY) &&
101+
credentials.containsKey(Constants.BUCKET) &&
102+
credentials.containsKey(Constants.REGION) &&
103+
credentials.containsKey(Constants.ENDPOINT);
104+
// @formatter:on
105+
}
106+
76107
private ObjectStoreServiceInfo createServiceInfoForAliCloud(Map<String, Object> credentials) {
77-
String accessKeyId = (String) credentials.get("access_key_id");
78-
String secretAccessKey = (String) credentials.get("secret_access_key");
79-
String bucket = (String) credentials.get("bucket");
80-
String region = (String) credentials.get("region");
81-
String endpoint = (String) credentials.get("endpoint");
108+
String accessKeyId = (String) credentials.get(Constants.ACCESS_KEY_ID);
109+
String secretAccessKey = (String) credentials.get(Constants.SECRET_ACCESS_KEY);
110+
String bucket = (String) credentials.get(Constants.BUCKET);
111+
String region = (String) credentials.get(Constants.REGION);
112+
String endpoint = (String) credentials.get(Constants.ENDPOINT);
82113
return ImmutableObjectStoreServiceInfo.builder()
83-
.provider("aliyun-oss")
114+
.provider(Constants.ALIYUN_OSS)
84115
.identity(accessKeyId)
85116
.credential(secretAccessKey)
86117
.container(bucket)
@@ -89,19 +120,31 @@ private ObjectStoreServiceInfo createServiceInfoForAliCloud(Map<String, Object>
89120
.build();
90121
}
91122

123+
private boolean isGcpIaas(Map<String, Object> credentials) {
124+
// @formatter:off
125+
return credentials.containsKey(Constants.BUCKET) &&
126+
credentials.containsKey(Constants.REGION) &&
127+
credentials.containsKey(Constants.BASE_64_ENCODED_PRIVATE_KEY_DATA);
128+
// @formatter:on
129+
}
130+
92131
private ObjectStoreServiceInfo createServiceInfoForGcpCloud(Map<String, Object> credentials) {
93-
String bucket = (String) credentials.get("bucket");
94-
String region = (String) credentials.get("region");
95-
byte[] decodedKey = Base64.getDecoder()
96-
.decode((String) credentials.get("base64EncodedPrivateKeyData"));
97-
String decodedCredential = new String(decodedKey, StandardCharsets.UTF_8);
98-
Supplier<Credentials> credentialsSupplier = new GoogleCredentialsFromJson(decodedCredential);
132+
String bucket = (String) credentials.get(Constants.BUCKET);
133+
String region = (String) credentials.get(Constants.REGION);
134+
Supplier<Credentials> credentialsSupplier = getGcpCredentialsSupplier(credentials);
99135
return ImmutableObjectStoreServiceInfo.builder()
100-
.provider("google-cloud-storage-custom")
136+
.provider(Constants.GOOGLE_CLOUD_STORAGE_CUSTOM)
101137
.credentialsSupplier(credentialsSupplier)
102138
.container(bucket)
103139
.region(region)
104140
.build();
105141
}
106142

143+
protected Supplier<Credentials> getGcpCredentialsSupplier(Map<String, Object> credentials) {
144+
byte[] decodedKey = Base64.getDecoder()
145+
.decode((String) credentials.get(Constants.BASE_64_ENCODED_PRIVATE_KEY_DATA));
146+
String decodedCredential = new String(decodedKey, StandardCharsets.UTF_8);
147+
return new GoogleCredentialsFromJson(decodedCredential);
148+
}
149+
107150
}

0 commit comments

Comments
 (0)