-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSkyflow.java
More file actions
113 lines (99 loc) · 4.63 KB
/
Skyflow.java
File metadata and controls
113 lines (99 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.skyflow;
import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
import com.skyflow.enums.LogLevel;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.ErrorMessage;
import com.skyflow.errors.SkyflowException;
import com.skyflow.logs.ErrorLogs;
import com.skyflow.logs.InfoLogs;
import com.skyflow.utils.Constants;
import com.skyflow.utils.SdkVersion;
import com.skyflow.utils.Utils;
import com.skyflow.utils.logger.LogUtil;
import com.skyflow.utils.validations.Validations;
import com.skyflow.vault.controller.VaultController;
import java.util.LinkedHashMap;
public final class Skyflow extends BaseSkyflow {
private final SkyflowClientBuilder builder;
private Skyflow(SkyflowClientBuilder builder) {
super(builder);
this.builder = builder;
com.skyflow.utils.logger.LogUtil.printInfoLog(InfoLogs.CLIENT_INITIALIZED.getLog());
}
public static SkyflowClientBuilder builder() {
SdkVersion.setSdkPrefix(Constants.SDK_PREFIX);
return new SkyflowClientBuilder();
}
public VaultConfig getVaultConfig() {
Object[] array = this.builder.vaultConfigMap.values().toArray();
return (VaultConfig) array[0];
}
public VaultController vault() throws SkyflowException {
Object[] array = this.builder.vaultClientsMap.keySet().toArray();
if (array.length < 1) {
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
}
String vaultId = (String) array[0];
VaultController controller = this.builder.vaultClientsMap.get(vaultId);
if (controller == null) {
LogUtil.printErrorLog(ErrorLogs.VAULT_CONFIG_DOES_NOT_EXIST.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.VaultIdNotInConfigList.getMessage());
}
return controller;
}
public static final class SkyflowClientBuilder extends BaseSkyflowClientBuilder {
private final LinkedHashMap<String, VaultConfig> vaultConfigMap;
private final LinkedHashMap<String, VaultController> vaultClientsMap;
public SkyflowClientBuilder() {
this.vaultConfigMap = new LinkedHashMap<>();
this.vaultClientsMap = new LinkedHashMap<>();
}
public SkyflowClientBuilder addVaultConfig(VaultConfig vaultConfig) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.VALIDATING_VAULT_CONFIG.getLog());
Validations.validateVaultConfiguration(vaultConfig);
VaultConfig vaultConfigCopy;
try {
vaultConfigCopy = (VaultConfig) vaultConfig.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
if (!this.vaultClientsMap.isEmpty()) {
LogUtil.printErrorLog(Utils.parameterizedString(
ErrorLogs.VAULT_CONFIG_EXISTS.getLog(), vaultConfigCopy.getVaultId()
));
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(),
ErrorMessage.VaultIdAlreadyInConfigList.getMessage());
} else {
this.vaultConfigMap.put(vaultConfigCopy.getVaultId(), vaultConfigCopy); // add new config in map
this.vaultClientsMap.put(vaultConfigCopy.getVaultId(), new VaultController(vaultConfigCopy, this.skyflowCredentials)); // add new controller with new config
LogUtil.printInfoLog(Utils.parameterizedString(
InfoLogs.VAULT_CONTROLLER_INITIALIZED.getLog(), vaultConfigCopy.getVaultId()));
}
return this;
}
public SkyflowClientBuilder addSkyflowCredentials(Credentials credentials) throws SkyflowException {
Validations.validateCredentials(credentials);
Credentials credentialsCopy;
try {
credentialsCopy = (Credentials) credentials.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
this.skyflowCredentials = credentialsCopy;
for (VaultController vault : this.vaultClientsMap.values()) {
vault.setCommonCredentials(this.skyflowCredentials);
}
return this;
}
@Override
public SkyflowClientBuilder setLogLevel(LogLevel logLevel) {
super.setLogLevel(logLevel);
return this;
}
public Skyflow build() {
return new Skyflow(this);
}
}
}