Skip to content

Commit da440a3

Browse files
committed
Merge branch 'release'
2 parents 652d3ba + 16c4981 commit da440a3

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/MySQLConnectionUtils.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package com.microsoft.azure.toolkit.intellij.link.mysql;
77

88
import com.microsoft.azuretools.ActionConstants;
9-
import com.microsoft.azuretools.telemetry.TelemetryConstants;
109
import com.microsoft.azuretools.telemetrywrapper.EventType;
1110
import com.microsoft.azuretools.telemetrywrapper.EventUtil;
1211
import com.mysql.cj.jdbc.ConnectionImpl;
@@ -21,55 +20,63 @@ public class MySQLConnectionUtils {
2120
private static final String CONNECTION_ISSUE_MESSAGE = "%s Please follow https://docs.microsoft.com/en-us/azure/mysql/howto-manage-firewall-using-portal "
2221
+ "to create a firewall rule to unblock your local access.";
2322
private static final int CONNECTION_ERROR_CODE = 9000;
23+
private static final int CLASS_NOT_FOUND_ERROR_CODE = -1000;
24+
private static final int UNKNOWN_EXCEPTION_ERROR_CODE = -1;
2425

2526
public static boolean connect(String url, String username, String password) {
2627
try {
2728
Class.forName("com.mysql.jdbc.Driver");
2829
DriverManager.getConnection(url, username, password);
2930
return true;
30-
} catch (ClassNotFoundException | SQLException exception) {
31+
} catch (final ClassNotFoundException | SQLException exception) {
32+
return false;
3133
}
32-
return false;
3334
}
3435

3536
public static ConnectResult connectWithPing(String url, String username, String password) {
37+
int errorCode = 0;
3638
boolean connected = false;
3739
String errorMessage = null;
3840
Long pingCost = null;
3941
String serverVersion = null;
4042
// refresh property
4143
try {
4244
Class.forName("com.mysql.jdbc.Driver");
43-
long start = System.currentTimeMillis();
44-
Connection connection = DriverManager.getConnection(url, username, password);
45+
final long start = System.currentTimeMillis();
46+
final Connection connection = DriverManager.getConnection(url, username, password);
4547
connected = true;
46-
Statement statement = connection.createStatement();
47-
ResultSet resultSet = statement.executeQuery("select 'hi'");
48+
final Statement statement = connection.createStatement();
49+
final ResultSet resultSet = statement.executeQuery("select 'hi'");
4850
if (resultSet.next()) {
49-
String result = resultSet.getString(1);
51+
final String result = resultSet.getString(1);
5052
connected = "hi".equals(result);
5153
}
5254
pingCost = System.currentTimeMillis() - start;
5355
serverVersion = ((ConnectionImpl) connection).getServerVersion().toString();
54-
} catch (ClassNotFoundException | SQLException exception) {
56+
} catch (final SQLException exception) {
57+
errorCode = exception.getErrorCode();
5558
errorMessage = isConnectionIssue(exception) ? String.format(CONNECTION_ISSUE_MESSAGE, exception.getMessage()) : exception.getMessage();
59+
} catch (final ClassNotFoundException | RuntimeException exception) {
60+
errorCode = exception instanceof ClassNotFoundException ? CLASS_NOT_FOUND_ERROR_CODE : UNKNOWN_EXCEPTION_ERROR_CODE;
61+
errorMessage = exception.getMessage();
5662
}
5763
EventUtil.logEvent(EventType.info, ActionConstants.parse(ActionConstants.MySQL.TEST_CONNECTION).getServiceName(),
5864
ActionConstants.parse(ActionConstants.MySQL.TEST_CONNECTION).getOperationName(),
5965
Collections.singletonMap("result", String.valueOf(connected)));
60-
return new ConnectResult(connected, errorMessage, pingCost, serverVersion);
66+
return new ConnectResult(connected, errorMessage, pingCost, serverVersion, errorCode);
6167
}
6268

63-
private static boolean isConnectionIssue(final Exception exception){
64-
return exception instanceof SQLException && ((SQLException) exception).getErrorCode() == CONNECTION_ERROR_CODE;
69+
private static boolean isConnectionIssue(final SQLException exception) {
70+
return exception.getErrorCode() == CONNECTION_ERROR_CODE;
6571
}
6672

6773
@Getter
6874
@AllArgsConstructor
6975
public static class ConnectResult {
70-
private boolean connected;
71-
private String message;
72-
private Long pingCost;
73-
private String serverVersion;
76+
private final boolean connected;
77+
private final String message;
78+
private final Long pingCost;
79+
private final String serverVersion;
80+
private final int errorCode;
7481
}
7582
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/lib/link/AzureLinkService.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.microsoft.azuretools.telemetrywrapper.EventUtil;
2222
import com.microsoft.intellij.AzureLinkStorage;
2323
import com.microsoft.intellij.AzureMySQLStorage;
24+
import com.microsoft.intellij.util.PluginUtil;
2425
import org.apache.commons.codec.digest.DigestUtils;
2526
import org.apache.commons.collections4.CollectionUtils;
2627
import org.apache.commons.lang3.ArrayUtils;
@@ -32,6 +33,7 @@
3233

3334
public class AzureLinkService {
3435
private static final AzureLinkService instance = new AzureLinkService();
36+
private static final int ACCESS_DENIED_ERROR_CODE = 1045;
3537

3638
public static AzureLinkService getInstance() {
3739
return instance;
@@ -42,18 +44,18 @@ private AzureLinkService() {
4244
}
4345

4446
public void link(Project project, LinkConfig<MySQLResourceConfig, ModuleResourceConfig> linkComposite, boolean storageResource) {
45-
ModulePO modulePO = createModulePO(linkComposite.getModule());
47+
final ModulePO modulePO = createModulePO(linkComposite.getModule());
4648
// create resource
47-
MySQLResourcePO resource = createResourcePO(linkComposite.getResource());
49+
final MySQLResourcePO resource = createResourcePO(linkComposite.getResource());
4850
// create link
49-
LinkPO linkPO = new LinkPO(resource.getId(), modulePO.getResourceId(), LinkType.SERVICE_WITH_MODULE, linkComposite.getEnvPrefix());
51+
final LinkPO linkPO = new LinkPO(resource.getId(), modulePO.getResourceId(), LinkType.SERVICE_WITH_MODULE, linkComposite.getEnvPrefix());
5052
// storage mysql
5153
if (storageResource) {
5254
AzureMySQLStorage.getStorage().addResource(resource);
5355
}
5456
// storage password
5557
if (ArrayUtils.isNotEmpty(linkComposite.getResource().getPasswordConfig().getPassword())) {
56-
String inputPassword = String.valueOf(linkComposite.getResource().getPasswordConfig().getPassword());
58+
final String inputPassword = String.valueOf(linkComposite.getResource().getPasswordConfig().getPassword());
5759
AzureMySQLStorage.getStorage().savePassword(resource, resource.getPasswordSave(), resource.getUsername(), inputPassword);
5860
}
5961
// storage link
@@ -65,40 +67,39 @@ private ModulePO createModulePO(ModuleResourceConfig config) {
6567
}
6668

6769
private MySQLResourcePO createResourcePO(MySQLResourceConfig config) {
68-
JdbcUrl jdbcUrl = JdbcUrl.from(config.getUrl());
69-
String businessUniqueKey = MySQLResourcePO.getBusinessUniqueKey(config.getServer().id(), jdbcUrl.getDatabase());
70-
MySQLResourcePO existedResourcePO = AzureMySQLStorage.getStorage().getResourceByBusinessUniqueKey(businessUniqueKey);
71-
String id = Objects.nonNull(existedResourcePO) ? existedResourcePO.getId() : DigestUtils.md5Hex(businessUniqueKey);
72-
MySQLResourcePO resourcePO = MySQLResourcePO.builder()
70+
final JdbcUrl jdbcUrl = JdbcUrl.from(config.getUrl());
71+
final String businessUniqueKey = MySQLResourcePO.getBusinessUniqueKey(config.getServer().id(), jdbcUrl.getDatabase());
72+
final MySQLResourcePO existedResourcePO = AzureMySQLStorage.getStorage().getResourceByBusinessUniqueKey(businessUniqueKey);
73+
final String id = Objects.nonNull(existedResourcePO) ? existedResourcePO.getId() : DigestUtils.md5Hex(businessUniqueKey);
74+
return MySQLResourcePO.builder()
7375
.id(id)
7476
.resourceId(config.getServer().id())
7577
.url(config.getUrl())
7678
.username(config.getUsername())
7779
.passwordSave(config.getPasswordConfig().getPasswordSaveType())
7880
.build();
79-
return resourcePO;
8081
}
8182

8283
public Map<String, String> retrieveLinkEnvsByModuleName(Project project, String moduleName) {
83-
Map<String, String> linkedEnvMap = new LinkedHashMap<>();
84-
List<LinkPO> moduleRelatedLinkList = AzureLinkStorage.getProjectStorage(project).getLinkByModuleId(moduleName)
84+
final Map<String, String> linkedEnvMap = new LinkedHashMap<>();
85+
final List<LinkPO> moduleRelatedLinkList = AzureLinkStorage.getProjectStorage(project).getLinkByModuleId(moduleName)
8586
.stream()
8687
.filter(e -> LinkType.SERVICE_WITH_MODULE.equals(e.getType()))
8788
.collect(Collectors.toList());
8889
if (CollectionUtils.isEmpty(moduleRelatedLinkList)) {
8990
return linkedEnvMap;
9091
}
9192
// services in application level
92-
Set<? extends BaseResourcePO> serviceSet = AzureMySQLStorage.getStorage().getResources();
93-
for (BaseResourcePO service : serviceSet) {
94-
for (LinkPO link : moduleRelatedLinkList) {
93+
final Set<? extends BaseResourcePO> serviceSet = AzureMySQLStorage.getStorage().getResources();
94+
for (final BaseResourcePO service : serviceSet) {
95+
for (final LinkPO link : moduleRelatedLinkList) {
9596
if (!StringUtils.equals(link.getResourceId(), service.getId())) {
9697
continue;
9798
}
98-
String envPrefix = link.getEnvPrefix();
99+
final String envPrefix = link.getEnvPrefix();
99100
if (ResourceType.AZURE_DATABASE_FOR_MYSQL.equals(service.getType())) {
100-
MySQLResourcePO mysql = (MySQLResourcePO) service;
101-
String password = readPasswordCredentials(project, mysql);
101+
final MySQLResourcePO mysql = (MySQLResourcePO) service;
102+
final String password = readPasswordCredentials(project, mysql);
102103
linkedEnvMap.put(envPrefix + "URL", mysql.getUrl());
103104
linkedEnvMap.put(envPrefix + "USERNAME", mysql.getUsername());
104105
linkedEnvMap.put(envPrefix + "PASSWORD", password);
@@ -109,19 +110,24 @@ public Map<String, String> retrieveLinkEnvsByModuleName(Project project, String
109110
}
110111

111112
private String readPasswordCredentials(Project project, MySQLResourcePO service) {
112-
String storagedPassword = AzureMySQLStorage.getStorage().loadPassword(service, service.getPasswordSave(), service.getUsername());
113-
if (StringUtils.isNotBlank(storagedPassword)) {
114-
if (MySQLConnectionUtils.connect(service.getUrl(), service.getUsername(), storagedPassword)) {
115-
return storagedPassword;
113+
final String storedPassword = AzureMySQLStorage.getStorage().loadPassword(service, service.getPasswordSave(), service.getUsername());
114+
if (StringUtils.isNotEmpty(storedPassword)) {
115+
final MySQLConnectionUtils.ConnectResult result = MySQLConnectionUtils.connectWithPing(service.getUrl(), service.getUsername(), storedPassword);
116+
if (result.isConnected()) {
117+
return storedPassword;
118+
}
119+
if (result.getErrorCode() != ACCESS_DENIED_ERROR_CODE) {
120+
PluginUtil.showWarnNotification("Failed to connect MySQL", result.getMessage());
121+
return StringUtils.EMPTY;
116122
}
117123
}
118124
// re-input password
119-
AtomicReference<PasswordConfig> passwordConfigReference = new AtomicReference<>();
125+
final AtomicReference<PasswordConfig> passwordConfigReference = new AtomicReference<>();
120126
ApplicationManager.getApplication().invokeAndWait(() -> {
121-
PasswordDialog dialog = new PasswordDialog(project, service.getUsername(), service.getUrl());
127+
final PasswordDialog dialog = new PasswordDialog(project, service.getUsername(), service.getUrl());
122128
dialog.setOkActionListener(data -> {
123129
dialog.close();
124-
String inputPassword = String.valueOf(data.getPassword());
130+
final String inputPassword = String.valueOf(data.getPassword());
125131
if (MySQLConnectionUtils.connect(service.getUrl(), service.getUsername(), inputPassword)) {
126132
AzureMySQLStorage.getStorage().savePassword(service, data.getPasswordSaveType(), service.getUsername(), inputPassword);
127133
if (!Objects.equals(service.getPasswordSave(), data.getPasswordSaveType())) {
@@ -134,7 +140,7 @@ private String readPasswordCredentials(Project project, MySQLResourcePO service)
134140
EventUtil.logEvent(EventType.info, ActionConstants.parse(ActionConstants.MySQL.UPDATE_PASSWORD).getServiceName(),
135141
ActionConstants.parse(ActionConstants.MySQL.UPDATE_PASSWORD).getOperationName(), null);
136142
});
137-
PasswordConfig passwordConfig = passwordConfigReference.get();
143+
final PasswordConfig passwordConfig = passwordConfigReference.get();
138144
if (Objects.nonNull(passwordConfig)) {
139145
return String.valueOf(passwordConfig.getPassword());
140146
} else {

0 commit comments

Comments
 (0)