Skip to content

Commit

Permalink
Refactor azure settings save logic, using jackson to save all configu…
Browse files Browse the repository at this point in the history
…ration instead of save them individual
  • Loading branch information
Flanker-MSFT authored and Flanker-MSFT committed Aug 29, 2023
1 parent 09dec15 commit f70018a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
public class AzureInitializer {
public static void initialize() {
ProxyUtils.initProxy();
initializeAzureConfiguration();
}

private static void initializeAzureConfiguration() {
IntellijConfigInitializer.initialize();
initializeTelemetry();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static void initialize() {

AzureConfigInitializer.initialize(installId, "Azure Toolkit for IntelliJ", AzurePlugin.PLUGIN_VERSION);
CommonSettings.setUserAgent(Azure.az().config().getUserAgent());
Azure.az().setSaveConfigurationHandler(c -> AzureConfigInitializer.saveAzConfig());
if (StringUtils.isNotBlank(Azure.az().config().getCloud())) {
Azure.az(AzureCloud.class).setByName(Azure.az().config().getCloud());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public boolean doOKAction() {
if (StringUtils.isNotBlank(newConfig.getCloud())) {
Azure.az(AzureCloud.class).setByName(newConfig.getCloud());
}
AzureConfigInitializer.saveAzConfig();
Azure.az().saveConfiguration();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package com.microsoft.azure.toolkit.ide.appservice.function.coretools;

import com.microsoft.azure.toolkit.ide.common.store.AzureConfigInitializer;
import com.microsoft.azure.toolkit.lib.Azure;
import com.microsoft.azure.toolkit.lib.common.event.AzureEventBus;
import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException;
Expand All @@ -14,12 +13,18 @@
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nullable;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.IntStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -98,7 +103,7 @@ private void doDownloadReleaseTools(@Nullable ReleaseInfo releaseInfo, String do
throw new AzureToolkitRuntimeException(e);
}
Azure.az().config().setFunctionCoreToolsPath(executionFilePath);
AzureConfigInitializer.saveAzConfig();
Azure.az().saveConfiguration();
AzureEventBus.emit("function.download_func_core_tools_succeed.version", releaseInfo.releaseVersion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.microsoft.azure.toolkit.ide.common.IActionsContributor;
import com.microsoft.azure.toolkit.ide.common.favorite.Favorites;
import com.microsoft.azure.toolkit.ide.common.icon.AzureIcons;
import com.microsoft.azure.toolkit.ide.common.store.AzureConfigInitializer;
import com.microsoft.azure.toolkit.lib.AzService;
import com.microsoft.azure.toolkit.lib.Azure;
import com.microsoft.azure.toolkit.lib.account.IAccount;
Expand Down Expand Up @@ -111,8 +110,7 @@ public void registerActions(AzureActionManager am) {
.withShortcut(shortcuts.delete())
.visibleWhen((s, place) -> s instanceof AzResource && s instanceof Deletable)
.enableWhen(s -> {
if (s instanceof AbstractAzResource) {
final AbstractAzResource<?, ?, ?> r = (AbstractAzResource<?, ?, ?>) s;
if (s instanceof AbstractAzResource<?, ?, ?> r) {
return !r.getFormalStatus().isDeleted() && !r.isDraftForCreating();
}
return true;
Expand Down Expand Up @@ -301,7 +299,7 @@ public void registerActions(AzureActionManager am) {
.visibleWhen(s -> Azure.az().config().isAuthPersistenceEnabled())
.withHandler((s) -> {
Azure.az().config().setAuthPersistenceEnabled(false);
AzureConfigInitializer.saveAzConfig();
Azure.az().saveConfiguration();
final AzureAccount az = Azure.az(AzureAccount.class);
if (az.isLoggedIn()) {
az.logout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package com.microsoft.azure.toolkit.ide.common.dotnet;

import com.microsoft.azure.toolkit.ide.common.action.ResourceCommonActionsContributor;
import com.microsoft.azure.toolkit.ide.common.store.AzureConfigInitializer;
import com.microsoft.azure.toolkit.lib.Azure;
import com.microsoft.azure.toolkit.lib.common.action.Action;
import com.microsoft.azure.toolkit.lib.common.action.AzureActionManager;
Expand Down Expand Up @@ -97,7 +96,7 @@ public static void installDotnet(final String path) {
CommandUtils.exec(INSTALL_COMMAND, path);
}
Azure.az().config().setDotnetRuntimePath(path);
AzureConfigInitializer.saveAzConfig();
Azure.az().saveConfiguration();
AzureEventBus.emit("dotnet_runtime.updated");
final String INSTALL_SUCCEED_MESSAGE = ".NET runtime is installed and configured successfully.";
AzureMessager.getMessager().success(INSTALL_SUCCEED_MESSAGE, null, openSettingsAction, ResourceCommonActionsContributor.RESTART_IDE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

import com.microsoft.azure.toolkit.lib.Azure;
import com.microsoft.azure.toolkit.lib.AzureConfiguration;
import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager;
import com.microsoft.azure.toolkit.lib.common.utils.InstallationIdUtils;
import com.microsoft.azure.toolkit.lib.common.utils.JsonUtils;
import com.microsoft.azure.toolkit.lib.common.utils.Utils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.microsoft.azure.toolkit.ide.common.dotnet.DotnetRuntimeHandler.getDotnetRuntimePath;
Expand Down Expand Up @@ -44,18 +49,39 @@ public class AzureConfigInitializer {
public static final String AZURITE_PATH = "azurite_path";
public static final String AZURITE_WORKSPACE = "azurite_workspace";
public static final String ENABLE_LEASE_MODE = "enable_lease_mode";
public static final String SYSTEM = "system";
public static final String AZURE_CONFIGURATION = "azure_configuration";

public static void initialize(String defaultMachineId, String pluginName, String pluginVersion) {
String machineId = AzureStoreManager.getInstance().getMachineStore().getProperty(TELEMETRY,
TELEMETRY_INSTALLATION_ID);
if (StringUtils.isBlank(machineId) || !InstallationIdUtils.isValidHashMac(machineId)) {
machineId = defaultMachineId;
}

final String machineId = Optional.ofNullable(AzureStoreManager.getInstance().getMachineStore().getProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID))
.filter(StringUtils::isNotBlank)
.filter(InstallationIdUtils::isValidHashMac)
.orElse(defaultMachineId);
final AzureConfiguration config = Azure.az().config();
config.setMachineId(machineId);
config.setProduct(pluginName);
config.setVersion(pluginVersion);
config.setLogLevel("NONE");
final String userAgent = String.format("%s, v%s, machineid:%s", pluginName, pluginVersion,
BooleanUtils.isNotFalse(config.getTelemetryEnabled()) ? config.getMachineId() : StringUtils.EMPTY);
config.setUserAgent(userAgent);

final IIdeStore ideStore = AzureStoreManager.getInstance().getIdeStore();
final String property = ideStore.getProperty(SYSTEM, AZURE_CONFIGURATION);
if (StringUtils.isBlank(property)) {
loadLegacyData(ideStore, config);
} else {
final AzureConfiguration azureConfiguration = JsonUtils.fromJson(property, AzureConfiguration.class);
try {
Utils.copyProperties(config, azureConfiguration, false);
} catch (IllegalAccessException e) {
AzureMessager.getMessager().warning("Failed to load azure configuration from store.", e);
}
}
saveAzConfig();
}

private static void loadLegacyData(final IIdeStore ideStore, final AzureConfiguration config) {
final String allowTelemetry = ideStore.getProperty(TELEMETRY, TELEMETRY_ALLOW_TELEMETRY, "true");
config.setTelemetryEnabled(Boolean.parseBoolean(allowTelemetry));
final String enableAuthPersistence = ideStore.getProperty(OTHER, ENABLE_AUTH_PERSISTENCE, "true");
Expand Down Expand Up @@ -113,38 +139,12 @@ public static void initialize(String defaultMachineId, String pluginName, String

final Boolean enableLeaseMode = Boolean.valueOf(ideStore.getProperty(AZURITE, ENABLE_LEASE_MODE, "false"));
config.setEnableLeaseMode(enableLeaseMode);

ideStore.getProperty(TELEMETRY, TELEMETRY_PLUGIN_VERSION, "");

final String userAgent = String.format("%s, v%s, machineid:%s", pluginName, pluginVersion,
config.getTelemetryEnabled() ? config.getMachineId() : StringUtils.EMPTY);
config.setUserAgent(userAgent);
config.setProduct(pluginName);
config.setLogLevel("NONE");
config.setVersion(pluginVersion);
saveAzConfig();
}

public static void saveAzConfig() {
final AzureConfiguration config = Azure.az().config();
final IIdeStore ideStore = AzureStoreManager.getInstance().getIdeStore();

AzureStoreManager.getInstance().getMachineStore().setProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID,
config.getMachineId());

ideStore.setProperty(TELEMETRY, TELEMETRY_ALLOW_TELEMETRY, Boolean.toString(config.getTelemetryEnabled()));
ideStore.setProperty(OTHER, ENABLE_AUTH_PERSISTENCE, Boolean.toString(config.isAuthPersistenceEnabled()));
ideStore.setProperty(MONITOR, MONITOR_TABLE_ROWS, String.valueOf(config.getMonitorQueryRowNumber()));
ideStore.setProperty(ACCOUNT, AZURE_ENVIRONMENT_KEY, config.getCloud());
ideStore.setProperty(FUNCTION, FUNCTION_CORE_TOOLS_PATH, config.getFunctionCoreToolsPath());
ideStore.setProperty(STORAGE, STORAGE_EXPLORER_PATH, config.getStorageExplorerPath());
ideStore.setProperty(COMMON, PAGE_SIZE, String.valueOf(config.getPageSize()));
ideStore.setProperty(COSMOS, DOCUMENTS_LABEL_FIELDS, String.join(";", config.getDocumentsLabelFields()));
// don't save pluginVersion, it is saved in AzurePlugin class
ideStore.setProperty(BICEP, DOTNET_RUNTIME_PATH, config.getDotnetRuntimePath());
ideStore.setProperty(EVENT_HUBS, CONSUMER_GROUP_NAME, config.getEventHubsConsumerGroup());
ideStore.setProperty(AZURITE, AZURITE_PATH, config.getAzuritePath());
ideStore.setProperty(AZURITE, AZURITE_WORKSPACE, config.getAzuriteWorkspace());
ideStore.setProperty(AZURITE, ENABLE_LEASE_MODE, String.valueOf(config.getEnableLeaseMode()));
final AzureStoreManager storeManager = AzureStoreManager.getInstance();
storeManager.getIdeStore().setProperty(SYSTEM, AZURE_CONFIGURATION, JsonUtils.toJson(config));
storeManager.getMachineStore().setProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID, config.getMachineId());
}
}

0 comments on commit f70018a

Please sign in to comment.