|
45 | 45 | import java.io.OutputStream; |
46 | 46 | import java.net.URI; |
47 | 47 | import java.net.URISyntaxException; |
48 | | -import java.security.AccessController; |
49 | | -import java.security.PrivilegedActionException; |
50 | | -import java.security.PrivilegedExceptionAction; |
51 | 48 | import java.util.HashMap; |
52 | 49 | import java.util.Map; |
53 | 50 |
|
54 | 51 | public class AzureStorageServiceImpl extends AbstractComponent implements AzureStorageService { |
55 | 52 |
|
56 | | - final AzureStorageSettings primaryStorageSettings; |
57 | | - final Map<String, AzureStorageSettings> secondariesStorageSettings; |
| 53 | + final Map<String, AzureStorageSettings> storageSettings; |
| 54 | + final Map<String, AzureStorageSettings> deprecatedStorageSettings; |
58 | 55 |
|
59 | 56 | final Map<String, CloudBlobClient> clients; |
60 | 57 |
|
61 | | - public AzureStorageServiceImpl(Settings settings) { |
| 58 | + public AzureStorageServiceImpl(Settings settings, Map<String, AzureStorageSettings> regularStorageSettings) { |
62 | 59 | super(settings); |
63 | 60 |
|
64 | | - Tuple<AzureStorageSettings, Map<String, AzureStorageSettings>> storageSettings = AzureStorageSettings.parse(settings); |
65 | | - this.primaryStorageSettings = storageSettings.v1(); |
66 | | - this.secondariesStorageSettings = storageSettings.v2(); |
| 61 | + if (regularStorageSettings.isEmpty()) { |
| 62 | + this.storageSettings = new HashMap<>(); |
| 63 | + // We have deprecated settings so we need to migrate them to the new implementation |
| 64 | + Tuple<AzureStorageSettings, Map<String, AzureStorageSettings>> storageSettingsMapTuple = AzureStorageSettings.loadLegacy(settings); |
| 65 | + deprecatedStorageSettings = storageSettingsMapTuple.v2(); |
| 66 | + if (storageSettingsMapTuple.v1() != null) { |
| 67 | + if (storageSettingsMapTuple.v1().getName().equals("default") == false) { |
| 68 | + // We add the primary configuration to the list of all settings with its deprecated name in case someone is |
| 69 | + // forcing a specific configuration name when creating the repository instance |
| 70 | + deprecatedStorageSettings.put(storageSettingsMapTuple.v1().getName(), storageSettingsMapTuple.v1()); |
| 71 | + } |
| 72 | + // We add the primary configuration to the list of all settings as the "default" one |
| 73 | + deprecatedStorageSettings.put("default", storageSettingsMapTuple.v1()); |
| 74 | + } else { |
| 75 | + // If someone did not register any settings or deprecated settings, they |
| 76 | + // basically can't use the plugin |
| 77 | + throw new IllegalArgumentException("If you want to use an azure repository, you need to define a client configuration."); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + } else { |
| 82 | + this.storageSettings = regularStorageSettings; |
| 83 | + this.deprecatedStorageSettings = new HashMap<>(); |
| 84 | + } |
67 | 85 |
|
68 | 86 | this.clients = new HashMap<>(); |
69 | 87 |
|
70 | 88 | logger.debug("starting azure storage client instance"); |
71 | 89 |
|
72 | | - // We register the primary client if any |
73 | | - if (primaryStorageSettings != null) { |
74 | | - logger.debug("registering primary client for account [{}]", primaryStorageSettings.getAccount()); |
75 | | - createClient(primaryStorageSettings); |
| 90 | + // We register all regular azure clients |
| 91 | + for (Map.Entry<String, AzureStorageSettings> azureStorageSettingsEntry : this.storageSettings.entrySet()) { |
| 92 | + logger.debug("registering regular client for account [{}]", azureStorageSettingsEntry.getKey()); |
| 93 | + createClient(azureStorageSettingsEntry.getValue()); |
76 | 94 | } |
77 | 95 |
|
78 | | - // We register all secondary clients |
79 | | - for (Map.Entry<String, AzureStorageSettings> azureStorageSettingsEntry : secondariesStorageSettings.entrySet()) { |
80 | | - logger.debug("registering secondary client for account [{}]", azureStorageSettingsEntry.getKey()); |
| 96 | + // We register all deprecated azure clients |
| 97 | + for (Map.Entry<String, AzureStorageSettings> azureStorageSettingsEntry : this.deprecatedStorageSettings.entrySet()) { |
| 98 | + logger.debug("registering deprecated client for account [{}]", azureStorageSettingsEntry.getKey()); |
81 | 99 | createClient(azureStorageSettingsEntry.getValue()); |
82 | 100 | } |
83 | 101 | } |
@@ -107,32 +125,25 @@ void createClient(AzureStorageSettings azureStorageSettings) { |
107 | 125 |
|
108 | 126 | CloudBlobClient getSelectedClient(String account, LocationMode mode) { |
109 | 127 | logger.trace("selecting a client for account [{}], mode [{}]", account, mode.name()); |
110 | | - AzureStorageSettings azureStorageSettings = null; |
111 | | - |
112 | | - if (this.primaryStorageSettings == null) { |
113 | | - throw new IllegalArgumentException("No primary azure storage can be found. Check your elasticsearch.yml."); |
114 | | - } |
115 | | - |
116 | | - if (Strings.hasLength(account)) { |
117 | | - azureStorageSettings = this.secondariesStorageSettings.get(account); |
118 | | - } |
119 | | - |
120 | | - // if account is not secondary, it's the primary |
| 128 | + AzureStorageSettings azureStorageSettings = this.storageSettings.get(account); |
121 | 129 | if (azureStorageSettings == null) { |
122 | | - if (Strings.hasLength(account) == false || primaryStorageSettings.getName() == null || account.equals(primaryStorageSettings.getName())) { |
123 | | - azureStorageSettings = primaryStorageSettings; |
| 130 | + // We can't find a client that has been registered using regular settings so we try deprecated client |
| 131 | + azureStorageSettings = this.deprecatedStorageSettings.get(account); |
| 132 | + if (azureStorageSettings == null) { |
| 133 | + // We did not get an account. That's bad. |
| 134 | + if (Strings.hasLength(account)) { |
| 135 | + throw new IllegalArgumentException("Can not find named azure client [" + account + |
| 136 | + "]. Check your elasticsearch.yml."); |
| 137 | + } |
| 138 | + throw new IllegalArgumentException("Can not find primary/secondary client using deprecated settings. " + |
| 139 | + "Check your elasticsearch.yml."); |
124 | 140 | } |
125 | 141 | } |
126 | 142 |
|
127 | | - if (azureStorageSettings == null) { |
128 | | - // We did not get an account. That's bad. |
129 | | - throw new IllegalArgumentException("Can not find azure account [" + account + "]. Check your elasticsearch.yml."); |
130 | | - } |
131 | | - |
132 | 143 | CloudBlobClient client = this.clients.get(azureStorageSettings.getAccount()); |
133 | 144 |
|
134 | 145 | if (client == null) { |
135 | | - throw new IllegalArgumentException("Can not find an azure client for account [" + account + "]"); |
| 146 | + throw new IllegalArgumentException("Can not find an azure client for account [" + azureStorageSettings.getAccount() + "]"); |
136 | 147 | } |
137 | 148 |
|
138 | 149 | // NOTE: for now, just set the location mode in case it is different; |
|
0 commit comments