|
4 | 4 | import java.net.URL;
|
5 | 5 | import java.nio.charset.StandardCharsets;
|
6 | 6 | import java.util.Base64;
|
| 7 | +import java.util.List; |
7 | 8 | import java.util.Map;
|
8 | 9 |
|
| 10 | +import org.cloudfoundry.multiapps.controller.web.Constants; |
9 | 11 | import org.cloudfoundry.multiapps.controller.web.Messages;
|
10 | 12 | import org.jclouds.domain.Credentials;
|
11 | 13 | import org.jclouds.googlecloud.GoogleCredentialsFromJson;
|
|
16 | 18 |
|
17 | 19 | public class ObjectStoreServiceInfoCreator {
|
18 | 20 |
|
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"; |
23 |
| - |
24 |
| - public ObjectStoreServiceInfo createServiceInfo(CfService service) { |
25 |
| - String plan = service.getPlan(); |
| 21 | + public List<ObjectStoreServiceInfo> getAllProvidersServiceInfo(CfService service) { |
26 | 22 | Map<String, Object> credentials = service.getCredentials()
|
27 | 23 | .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); |
39 |
| - } |
| 24 | + return List.of(createServiceInfoForAliCloud(credentials), createServiceInfoForAzure(credentials), |
| 25 | + createServiceInfoForGcpCloud(credentials), createServiceInfoForAws(credentials)); |
| 26 | + } |
| 27 | + |
| 28 | + private ObjectStoreServiceInfo createServiceInfoForAliCloud(Map<String, Object> credentials) { |
| 29 | + String accessKeyId = (String) credentials.get(Constants.ACCESS_KEY_ID); |
| 30 | + String secretAccessKey = (String) credentials.get(Constants.SECRET_ACCESS_KEY); |
| 31 | + String bucket = (String) credentials.get(Constants.BUCKET); |
| 32 | + String region = (String) credentials.get(Constants.REGION); |
| 33 | + String endpoint = (String) credentials.get(Constants.ENDPOINT); |
| 34 | + return ImmutableObjectStoreServiceInfo.builder() |
| 35 | + .provider(Constants.ALIYUN_OSS) |
| 36 | + .identity(accessKeyId) |
| 37 | + .credential(secretAccessKey) |
| 38 | + .container(bucket) |
| 39 | + .endpoint(endpoint) |
| 40 | + .region(region) |
| 41 | + .build(); |
40 | 42 | }
|
41 | 43 |
|
42 | 44 | 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"); |
| 45 | + String accessKeyId = (String) credentials.get(Constants.ACCESS_KEY_ID); |
| 46 | + String secretAccessKey = (String) credentials.get(Constants.SECRET_ACCESS_KEY); |
| 47 | + String bucket = (String) credentials.get(Constants.BUCKET); |
46 | 48 | return ImmutableObjectStoreServiceInfo.builder()
|
47 |
| - .provider("aws-s3") |
| 49 | + .provider(Constants.AWS_S_3) |
48 | 50 | .identity(accessKeyId)
|
49 | 51 | .credential(secretAccessKey)
|
50 | 52 | .container(bucket)
|
51 | 53 | .build();
|
52 | 54 | }
|
53 | 55 |
|
54 | 56 | 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"); |
| 57 | + String accountName = (String) credentials.get(Constants.ACCOUNT_NAME); |
| 58 | + String sasToken = (String) credentials.get(Constants.SAS_TOKEN); |
| 59 | + String containerName = (String) credentials.get(Constants.CONTAINER_NAME); |
| 60 | + URL containerUrl = getContainerUriEndpoint(credentials); |
58 | 61 | return ImmutableObjectStoreServiceInfo.builder()
|
59 |
| - .provider("azureblob") |
| 62 | + .provider(Constants.AZUREBLOB) |
60 | 63 | .identity(accountName)
|
61 | 64 | .credential(sasToken)
|
62 |
| - .endpoint(getContainerUriEndpoint(credentials).toString()) |
| 65 | + .endpoint(containerUrl == null ? null : containerUrl.toString()) |
63 | 66 | .container(containerName)
|
64 | 67 | .build();
|
65 | 68 | }
|
66 | 69 |
|
67 | 70 | private URL getContainerUriEndpoint(Map<String, Object> credentials) {
|
| 71 | + if (!credentials.containsKey(Constants.CONTAINER_URI)) { |
| 72 | + return null; |
| 73 | + } |
68 | 74 | try {
|
69 |
| - URL containerUri = new URL((String) credentials.get("container_uri")); |
| 75 | + URL containerUri = new URL((String) credentials.get(Constants.CONTAINER_URI)); |
70 | 76 | return new URL(containerUri.getProtocol(), containerUri.getHost(), containerUri.getPort(), "");
|
71 | 77 | } catch (MalformedURLException e) {
|
72 | 78 | throw new IllegalStateException(Messages.CANNOT_PARSE_CONTAINER_URI_OF_OBJECT_STORE, e);
|
73 | 79 | }
|
74 | 80 | }
|
75 | 81 |
|
76 |
| - 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"); |
| 82 | + private ObjectStoreServiceInfo createServiceInfoForGcpCloud(Map<String, Object> credentials) { |
| 83 | + String bucket = (String) credentials.get(Constants.BUCKET); |
| 84 | + String region = (String) credentials.get(Constants.REGION); |
| 85 | + Supplier<Credentials> credentialsSupplier = getGcpCredentialsSupplier(credentials); |
82 | 86 | return ImmutableObjectStoreServiceInfo.builder()
|
83 |
| - .provider("aliyun-oss") |
84 |
| - .identity(accessKeyId) |
85 |
| - .credential(secretAccessKey) |
| 87 | + .provider(Constants.GOOGLE_CLOUD_STORAGE_CUSTOM) |
| 88 | + .credentialsSupplier(credentialsSupplier) |
86 | 89 | .container(bucket)
|
87 |
| - .endpoint(endpoint) |
88 | 90 | .region(region)
|
89 | 91 | .build();
|
90 | 92 | }
|
91 | 93 |
|
92 |
| - private ObjectStoreServiceInfo createServiceInfoForGcpCloud(Map<String, Object> credentials) { |
93 |
| - String bucket = (String) credentials.get("bucket"); |
94 |
| - String region = (String) credentials.get("region"); |
| 94 | + protected Supplier<Credentials> getGcpCredentialsSupplier(Map<String, Object> credentials) { |
| 95 | + if (!credentials.containsKey(Constants.BASE_64_ENCODED_PRIVATE_KEY_DATA)) { |
| 96 | + return () -> null; |
| 97 | + } |
95 | 98 | byte[] decodedKey = Base64.getDecoder()
|
96 |
| - .decode((String) credentials.get("base64EncodedPrivateKeyData")); |
| 99 | + .decode((String) credentials.get(Constants.BASE_64_ENCODED_PRIVATE_KEY_DATA)); |
97 | 100 | String decodedCredential = new String(decodedKey, StandardCharsets.UTF_8);
|
98 |
| - Supplier<Credentials> credentialsSupplier = new GoogleCredentialsFromJson(decodedCredential); |
99 |
| - return ImmutableObjectStoreServiceInfo.builder() |
100 |
| - .provider("google-cloud-storage-custom") |
101 |
| - .credentialsSupplier(credentialsSupplier) |
102 |
| - .container(bucket) |
103 |
| - .region(region) |
104 |
| - .build(); |
| 101 | + return new GoogleCredentialsFromJson(decodedCredential); |
105 | 102 | }
|
106 | 103 |
|
107 | 104 | }
|
0 commit comments