Skip to content

Commit 05a53fd

Browse files
committed
add sqlserver show-property action codes.
1 parent b6fef70 commit 05a53fd

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
6+
package com.microsoft.azure.toolkit.intellij.sqlserver;
7+
8+
import com.intellij.openapi.project.Project;
9+
import com.microsoft.azure.toolkit.intellij.common.IntellijDatasourceService;
10+
import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation;
11+
import com.microsoft.azure.toolkit.lib.common.utils.JdbcUrl;
12+
import com.microsoft.azure.toolkit.lib.sqlserver.model.SqlServerEntity;
13+
import com.microsoft.azuretools.ActionConstants;
14+
import com.microsoft.azuretools.authmanage.AuthMethodManager;
15+
import com.microsoft.intellij.AzurePlugin;
16+
import com.microsoft.intellij.actions.AzureSignInAction;
17+
import com.microsoft.intellij.util.AzureLoginHelper;
18+
import com.microsoft.tooling.msservices.components.DefaultLoader;
19+
import com.microsoft.tooling.msservices.helpers.Name;
20+
import com.microsoft.tooling.msservices.serviceexplorer.AzureIconSymbol;
21+
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent;
22+
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener;
23+
import com.microsoft.tooling.msservices.serviceexplorer.azure.sqlserver.SqlServerNode;
24+
25+
import static com.microsoft.intellij.ui.messages.AzureBundle.message;
26+
27+
@Name(OpenSqlServerByToolsAction.ACTION_NAME)
28+
public class OpenSqlServerByToolsAction extends NodeActionListener {
29+
30+
public static final String ACTION_NAME = "Open by Database Tools";
31+
private static final String NAME_PREFIX = "SQL Server - %s";
32+
private static final String DEFAULT_DRIVER_CLASS_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
33+
34+
private final SqlServerNode node;
35+
private final Project project;
36+
37+
public OpenSqlServerByToolsAction(SqlServerNode node) {
38+
super();
39+
this.node = node;
40+
this.project = (Project) node.getProject();
41+
}
42+
43+
@Override
44+
public AzureIconSymbol getIconSymbol() {
45+
return AzureIconSymbol.SqlServer.CONNECT_TO_SERVER;
46+
}
47+
48+
@Override
49+
public void actionPerformed(NodeActionEvent e) {
50+
AzureSignInAction.doSignIn(AuthMethodManager.getInstance(), project).subscribe((isSuccess) -> {
51+
this.doActionPerformed(e, isSuccess, project);
52+
});
53+
}
54+
55+
@Override
56+
protected String getServiceName(NodeActionEvent event) {
57+
return ActionConstants.parse(ActionConstants.SqlServer.CONNECT_TO_SERVER).getServiceName();
58+
}
59+
60+
@Override
61+
protected String getOperationName(NodeActionEvent event) {
62+
return ActionConstants.parse(ActionConstants.SqlServer.CONNECT_TO_SERVER).getOperationName();
63+
}
64+
65+
@AzureOperation(name = ActionConstants.SqlServer.CONNECT_TO_SERVER, type = AzureOperation.Type.ACTION)
66+
private void doActionPerformed(NodeActionEvent e, boolean isLoggedIn, Project project) {
67+
try {
68+
if (!isLoggedIn ||
69+
!AzureLoginHelper.isAzureSubsAvailableOrReportError(message("common.error.signIn"))) {
70+
return;
71+
}
72+
} catch (final Exception ex) {
73+
AzurePlugin.log(message("common.error.signIn"), ex);
74+
DefaultLoader.getUIHelper().showException(message("common.error.signIn"), ex, message("common.error.signIn"), false, true);
75+
}
76+
SqlServerEntity entity = node.getServer().entity();
77+
IntellijDatasourceService.DatasourceProperties properties = IntellijDatasourceService.DatasourceProperties.builder()
78+
.name(String.format(NAME_PREFIX, entity.getName()))
79+
.driverClassName(DEFAULT_DRIVER_CLASS_NAME)
80+
.url(JdbcUrl.sqlserver(entity.getFullyQualifiedDomainName()).toString())
81+
.username(entity.getAdministratorLoginName() + "@" + entity.getName())
82+
.build();
83+
IntellijDatasourceService.getInstance().openDataSourceManagerDialog(project, properties);
84+
}
85+
86+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
6+
package com.microsoft.azure.toolkit.lib.sqlserver;
7+
8+
import com.microsoft.azure.management.Azure;
9+
import com.microsoft.azure.management.resources.ResourceGroup;
10+
import com.microsoft.azure.toolkit.intellij.common.Draft;
11+
import com.microsoft.azure.toolkit.lib.common.model.Region;
12+
import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation;
13+
import com.microsoft.azure.toolkit.lib.sqlserver.model.SqlFirewallRuleEntity;
14+
import com.microsoft.azure.toolkit.lib.sqlserver.model.SqlServerEntity;
15+
import com.microsoft.azure.toolkit.lib.sqlserver.service.AzureSqlServer;
16+
import com.microsoft.azure.toolkit.lib.sqlserver.service.ISqlServer;
17+
import com.microsoft.azuretools.ActionConstants;
18+
import com.microsoft.azuretools.authmanage.AuthMethodManager;
19+
import com.microsoft.azuretools.telemetry.TelemetryConstants;
20+
import com.microsoft.azuretools.telemetrywrapper.ErrorType;
21+
import com.microsoft.azuretools.telemetrywrapper.EventType;
22+
import com.microsoft.azuretools.telemetrywrapper.EventUtil;
23+
import com.microsoft.azuretools.telemetrywrapper.Operation;
24+
import com.microsoft.azuretools.telemetrywrapper.TelemetryManager;
25+
26+
import java.util.Collections;
27+
28+
public class SqlServerService {
29+
private static final SqlServerService instance = new SqlServerService();
30+
31+
public static SqlServerService getInstance() {
32+
return SqlServerService.instance;
33+
}
34+
35+
@AzureOperation(
36+
name = "sqlserver.create",
37+
params = {
38+
"config.getServerName()",
39+
"config.getSubscription().displayName()"
40+
},
41+
type = AzureOperation.Type.SERVICE
42+
)
43+
public ISqlServer create(final SqlServerConfig config) {
44+
final Operation operation = TelemetryManager.createOperation(ActionConstants.MySQL.CREATE);
45+
try {
46+
operation.start();
47+
final String subscriptionId = config.getSubscription().subscriptionId();
48+
EventUtil.logEvent(EventType.info, operation, Collections.singletonMap(TelemetryConstants.SUBSCRIPTIONID, subscriptionId));
49+
// create resource group if necessary.
50+
if (config.getResourceGroup() instanceof Draft) {
51+
Azure azure = AuthMethodManager.getInstance().getAzureClient(subscriptionId);
52+
ResourceGroup newResourceGroup = azure.resourceGroups().define(config.getResourceGroup().name()).withRegion(config.getRegion().getName()).create();
53+
config.setResourceGroup(newResourceGroup);
54+
}
55+
// create sql server
56+
SqlServerEntity entity = SqlServerEntity.builder().name(config.getServerName()).subscriptionId(config.getSubscription().subscriptionId())
57+
.resourceGroup(config.getResourceGroup().name()).region(Region.fromName(config.getRegion().getName())).administratorLoginName(config.getAdminUsername())
58+
.enableAccessFromAzureServices(config.isAllowAccessFromAzureServices()).enableAccessFromLocalMachine(config.isAllowAccessFromLocalMachine())
59+
.build();
60+
return com.microsoft.azure.toolkit.lib.Azure.az(AzureSqlServer.class).sqlServer(entity).create()
61+
.withAdministratorLoginPassword(String.valueOf(config.getPassword()))
62+
.commit();
63+
} catch (final RuntimeException e) {
64+
EventUtil.logError(operation, ErrorType.systemError, e, null, null);
65+
throw e;
66+
} finally {
67+
operation.complete();
68+
}
69+
}
70+
71+
}

0 commit comments

Comments
 (0)