Skip to content

Commit dbaa83e

Browse files
committed
extract sql server name text field and mysql name text field as a common one.
1 parent bbb19d0 commit dbaa83e

File tree

12 files changed

+105
-96
lines changed

12 files changed

+105
-96
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/sqlserver/common/SqlServerNameTextField.java renamed to PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/database/ServerNameTextField.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*/
5-
package com.microsoft.azure.toolkit.intellij.sqlserver.common;
5+
package com.microsoft.azure.toolkit.intellij.database;
66

77
import com.microsoft.azure.toolkit.intellij.common.ValidationDebouncedTextInput;
8-
import com.microsoft.azure.toolkit.lib.Azure;
9-
import com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity;
108
import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo;
11-
import com.microsoft.azure.toolkit.lib.sqlserver.service.AzureSqlServer;
129
import com.microsoft.azuretools.azurecommons.helpers.NotNull;
10+
import lombok.Getter;
11+
import lombok.Setter;
1312
import org.apache.commons.lang3.StringUtils;
1413

14+
import java.util.Objects;
15+
import java.util.function.Function;
1516
import java.util.regex.Pattern;
1617

17-
public class SqlServerNameTextField extends ValidationDebouncedTextInput {
18+
public class ServerNameTextField extends ValidationDebouncedTextInput {
1819

1920
private static final Pattern PATTERN = Pattern.compile("^[a-z0-9][a-z0-9-]+[a-z0-9]$");
21+
@Setter
22+
private int minLength = 3;
23+
@Setter
24+
private int maxLength = 63;
25+
@Getter
2026
private String subscriptionId;
27+
@Setter
28+
private Function<ServerNameTextField, AzureValidationInfo> validateFunction;
2129

2230
public void setSubscriptionId(String subscriptionId) {
2331
if (!StringUtils.equals(subscriptionId, this.subscriptionId)) {
2432
this.subscriptionId = subscriptionId;
2533
}
2634
}
2735

28-
/**
29-
* Server name should not contain reserved words.
30-
* Your server name can contain only lowercase letters, numbers, and '-', but can't start or end with '-' or have more than 63 characters.
31-
* The specified server name is already in use.
32-
*/
3336
@Override
3437
@NotNull
3538
public AzureValidationInfo doValidateValue() {
@@ -39,24 +42,20 @@ public AzureValidationInfo doValidateValue() {
3942
}
4043
final String value = this.getValue();
4144
// validate length
42-
if (StringUtils.length(value) < 1 || StringUtils.length(value) > 63) {
43-
return AzureValidationInfo.builder().input(this).message("Server name must be at least 1 characters and at most 63 characters.")
45+
if (StringUtils.length(value) < minLength || StringUtils.length(value) > maxLength) {
46+
return AzureValidationInfo.builder().input(this)
47+
.message(String.format("Server name must be at least %s characters and at most %s characters.", minLength, maxLength))
4448
.type(AzureValidationInfo.Type.ERROR).build();
4549
}
4650
// validate special character
4751
if (!PATTERN.matcher(value).matches()) {
4852
return AzureValidationInfo.builder().input(this)
49-
.message("Your server name can contain only lowercase letters, numbers, and '-', but can't start or end with '-' or have more than 63 characters.")
53+
.message("Your server name can contain only lowercase letters, numbers, and '-', but can't start or end with '-'.")
5054
.type(AzureValidationInfo.Type.ERROR).build();
5155
}
5256
// validate availability
53-
CheckNameAvailabilityResultEntity resultEntity = Azure.az(AzureSqlServer.class).checkNameAvailability(subscriptionId, value);
54-
if (!resultEntity.isAvailable()) {
55-
String message = resultEntity.getUnavailabilityReason();
56-
if ("AlreadyExists".equalsIgnoreCase(resultEntity.getUnavailabilityReason())) {
57-
message = "The specified server name is already in use.";
58-
}
59-
return AzureValidationInfo.builder().input(this).message(message).type(AzureValidationInfo.Type.ERROR).build();
57+
if (Objects.nonNull(validateFunction)) {
58+
return validateFunction.apply(this);
6059
}
6160
return AzureValidationInfo.OK;
6261
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
package com.microsoft.azure.toolkit.intellij.mysql;
6+
7+
import com.microsoft.azure.toolkit.intellij.database.ServerNameTextField;
8+
import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo;
9+
import com.microsoft.azuretools.core.mvp.model.mysql.MySQLMvpModel;
10+
11+
import java.util.function.Function;
12+
13+
public class MySQLNameValidator implements Function<ServerNameTextField, AzureValidationInfo> {
14+
15+
@Override
16+
public AzureValidationInfo apply(ServerNameTextField textField) {
17+
final String value = textField.getValue();
18+
// validate availability
19+
if (!MySQLMvpModel.checkNameAvailabilitys(textField.getSubscriptionId(), value)) {
20+
return AzureValidationInfo.builder().input(textField).message(value + " already existed.").type(AzureValidationInfo.Type.ERROR).build();
21+
}
22+
return AzureValidationInfo.OK;
23+
}
24+
}

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

Lines changed: 0 additions & 64 deletions
This file was deleted.

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/mysql/creation/MySQLCreationAdvancedPanel.form

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<text value="Server name:"/>
6868
</properties>
6969
</component>
70-
<component id="a5dd0" class="com.microsoft.azure.toolkit.intellij.mysql.ServerNameTextField" binding="serverNameTextField">
70+
<component id="a5dd0" class="com.microsoft.azure.toolkit.intellij.database.ServerNameTextField" binding="serverNameTextField">
7171
<constraints>
7272
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="1" use-parent-layout="false">
7373
<preferred-size width="150" height="-1"/>

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/mysql/creation/MySQLCreationAdvancedPanel.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
import com.microsoft.azure.toolkit.intellij.database.AdminUsernameTextField;
1515
import com.microsoft.azure.toolkit.intellij.database.PasswordUtils;
1616
import com.microsoft.azure.toolkit.intellij.database.RegionComboBox;
17+
import com.microsoft.azure.toolkit.intellij.database.ServerNameTextField;
1718
import com.microsoft.azure.toolkit.intellij.database.ui.ConnectionSecurityPanel;
1819
import com.microsoft.azure.toolkit.intellij.mysql.MySQLRegionValidator;
19-
import com.microsoft.azure.toolkit.intellij.mysql.ServerNameTextField;
2020
import com.microsoft.azure.toolkit.intellij.mysql.VersionComboBox;
21+
import com.microsoft.azure.toolkit.intellij.sqlserver.common.SqlServerNameValidator;
2122
import com.microsoft.azure.toolkit.lib.common.form.AzureFormInput;
2223
import com.microsoft.azure.toolkit.lib.common.model.Subscription;
2324
import com.microsoft.azure.toolkit.lib.mysql.AzureMySQLConfig;
@@ -69,6 +70,9 @@ private void init() {
6970
passwordFieldInput = PasswordUtils.generatePasswordFieldInput(this.passwordField, this.adminUsernameTextField);
7071
confirmPasswordFieldInput = PasswordUtils.generateConfirmPasswordFieldInput(this.confirmPasswordField, this.passwordField);
7172
regionComboBox.setValidateFunction(new MySQLRegionValidator());
73+
serverNameTextField.setMinLength(3);
74+
serverNameTextField.setMaxLength(63);
75+
serverNameTextField.setValidateFunction(new SqlServerNameValidator());
7276
}
7377

7478
private void initListeners() {
@@ -82,7 +86,7 @@ private void onSubscriptionChanged(final ItemEvent e) {
8286
if (e.getStateChange() == ItemEvent.SELECTED && e.getItem() instanceof Subscription) {
8387
final Subscription subscription = (Subscription) e.getItem();
8488
this.resourceGroupComboBox.setSubscription(subscription);
85-
this.serverNameTextField.setSubscription(subscription);
89+
this.serverNameTextField.setSubscriptionId(subscription.getId());
8690
this.regionComboBox.setSubscription(subscription);
8791
}
8892
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/mysql/creation/MySQLCreationBasicPanel.form

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<text value="Server name:"/>
3030
</properties>
3131
</component>
32-
<component id="c92f8" class="com.microsoft.azure.toolkit.intellij.mysql.ServerNameTextField" binding="serverNameTextField">
32+
<component id="c92f8" class="com.microsoft.azure.toolkit.intellij.database.ServerNameTextField" binding="serverNameTextField">
3333
<constraints>
3434
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="1" use-parent-layout="false">
3535
<preferred-size width="150" height="-1"/>

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/mysql/creation/MySQLCreationBasicPanel.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import com.microsoft.azure.toolkit.intellij.common.TextDocumentListenerAdapter;
1111
import com.microsoft.azure.toolkit.intellij.database.AdminUsernameTextField;
1212
import com.microsoft.azure.toolkit.intellij.database.PasswordUtils;
13-
import com.microsoft.azure.toolkit.intellij.mysql.ServerNameTextField;
13+
import com.microsoft.azure.toolkit.intellij.database.ServerNameTextField;
14+
import com.microsoft.azure.toolkit.intellij.mysql.MySQLNameValidator;
1415
import com.microsoft.azure.toolkit.lib.common.form.AzureFormInput;
1516
import com.microsoft.azure.toolkit.lib.mysql.AzureMySQLConfig;
1617
import lombok.Getter;
@@ -48,9 +49,12 @@ public class MySQLCreationBasicPanel extends JPanel implements AzureFormPanel<Az
4849
}
4950

5051
private void init() {
51-
serverNameTextField.setSubscription(config.getSubscription());
52+
serverNameTextField.setSubscriptionId(config.getSubscription().getId());
5253
passwordFieldInput = PasswordUtils.generatePasswordFieldInput(this.passwordField, this.adminUsernameTextField);
5354
confirmPasswordFieldInput = PasswordUtils.generateConfirmPasswordFieldInput(this.confirmPasswordField, this.passwordField);
55+
serverNameTextField.setMinLength(3);
56+
serverNameTextField.setMaxLength(63);
57+
serverNameTextField.setValidateFunction(new MySQLNameValidator());
5458
}
5559

5660
private void initListeners() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
package com.microsoft.azure.toolkit.intellij.sqlserver.common;
6+
7+
import com.microsoft.azure.toolkit.intellij.database.ServerNameTextField;
8+
import com.microsoft.azure.toolkit.lib.Azure;
9+
import com.microsoft.azure.toolkit.lib.common.entity.CheckNameAvailabilityResultEntity;
10+
import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo;
11+
import com.microsoft.azure.toolkit.lib.sqlserver.service.AzureSqlServer;
12+
13+
import java.util.function.Function;
14+
import java.util.regex.Pattern;
15+
16+
public class SqlServerNameValidator implements Function<ServerNameTextField, AzureValidationInfo> {
17+
18+
private static final Pattern PATTERN = Pattern.compile("^[a-z0-9][a-z0-9-]+[a-z0-9]$");
19+
20+
@Override
21+
public AzureValidationInfo apply(ServerNameTextField textField) {
22+
final String value = textField.getValue();
23+
// validate availability
24+
CheckNameAvailabilityResultEntity resultEntity = Azure.az(AzureSqlServer.class).checkNameAvailability(textField.getSubscriptionId(), value);
25+
if (!resultEntity.isAvailable()) {
26+
String message = resultEntity.getUnavailabilityReason();
27+
if ("AlreadyExists".equalsIgnoreCase(resultEntity.getUnavailabilityReason())) {
28+
message = "The specified server name is already in use.";
29+
}
30+
return AzureValidationInfo.builder().input(textField).message(message).type(AzureValidationInfo.Type.ERROR).build();
31+
}
32+
return AzureValidationInfo.OK;
33+
}
34+
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/sqlserver/creation/SqlServerCreationAdvancedPanel.form

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<text value="Server name:"/>
6666
</properties>
6767
</component>
68-
<component id="a5dd0" class="com.microsoft.azure.toolkit.intellij.sqlserver.common.SqlServerNameTextField" binding="serverNameTextField">
68+
<component id="a5dd0" class="com.microsoft.azure.toolkit.intellij.database.ServerNameTextField" binding="serverNameTextField">
6969
<constraints>
7070
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="1" use-parent-layout="false">
7171
<preferred-size width="150" height="-1"/>

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/sqlserver/creation/SqlServerCreationAdvancedPanel.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import com.microsoft.azure.toolkit.intellij.database.ui.ConnectionSecurityPanel;
1414
import com.microsoft.azure.toolkit.intellij.database.AdminUsernameTextField;
1515
import com.microsoft.azure.toolkit.intellij.database.PasswordUtils;
16-
import com.microsoft.azure.toolkit.intellij.sqlserver.common.SqlServerNameTextField;
16+
import com.microsoft.azure.toolkit.intellij.database.ServerNameTextField;
1717
import com.microsoft.azure.toolkit.intellij.database.RegionComboBox;
18+
import com.microsoft.azure.toolkit.intellij.sqlserver.common.SqlServerNameValidator;
1819
import com.microsoft.azure.toolkit.intellij.sqlserver.common.SqlServerRegionValidator;
1920
import com.microsoft.azure.toolkit.lib.common.form.AzureFormInput;
2021
import com.microsoft.azure.toolkit.lib.common.model.Subscription;
@@ -37,7 +38,7 @@ public class SqlServerCreationAdvancedPanel extends JPanel implements AzureFormP
3738
@Getter
3839
private ResourceGroupComboBox resourceGroupComboBox;
3940
@Getter
40-
private SqlServerNameTextField serverNameTextField;
41+
private ServerNameTextField serverNameTextField;
4142
@Getter
4243
private RegionComboBox regionComboBox;
4344
@Getter
@@ -65,6 +66,9 @@ private void init() {
6566
passwordFieldInput = PasswordUtils.generatePasswordFieldInput(this.passwordField, this.adminUsernameTextField);
6667
confirmPasswordFieldInput = PasswordUtils.generateConfirmPasswordFieldInput(this.confirmPasswordField, this.passwordField);
6768
regionComboBox.setValidateFunction(new SqlServerRegionValidator());
69+
serverNameTextField.setMinLength(1);
70+
serverNameTextField.setMaxLength(63);
71+
serverNameTextField.setValidateFunction(new SqlServerNameValidator());
6872
}
6973

7074
private void initListeners() {

0 commit comments

Comments
 (0)