forked from microsoft/azure-tools-for-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request microsoft#106 from maartenba/183-mb-dbexplorer-con…
…nect Add "Connect to server/database" actions
- Loading branch information
Showing
12 changed files
with
582 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...lkit-for-intellij/rider/src/com/microsoft/intellij/serviceexplorer/RiderNodeActionsMap.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) 2018 JetBrains s.r.o. | ||
* <p/> | ||
* All rights reserved. | ||
* <p/> | ||
* MIT License | ||
* <p/> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* <p/> | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* <p/> | ||
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.microsoft.intellij.serviceexplorer | ||
|
||
import com.google.common.collect.ImmutableList | ||
import com.microsoft.intellij.serviceexplorer.azure.database.actions.* | ||
import com.microsoft.tooling.msservices.serviceexplorer.Node | ||
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener | ||
import com.microsoft.tooling.msservices.serviceexplorer.azure.database.sqldatabase.SqlDatabaseNode | ||
import com.microsoft.tooling.msservices.serviceexplorer.azure.database.sqlserver.SqlServerNode | ||
import java.util.* | ||
|
||
class RiderNodeActionsMap : NodeActionsMap() { | ||
override fun getMap(): Map<Class<out Node>, ImmutableList<Class<out NodeActionListener>>> { | ||
return node2Actions | ||
} | ||
|
||
companion object { | ||
private val node2Actions = HashMap<Class<out Node>, ImmutableList<Class<out NodeActionListener>>>() | ||
|
||
init { | ||
node2Actions[SqlServerNode::class.java] = ImmutableList.Builder<Class<out NodeActionListener>>() | ||
.add(ConnectServerAction::class.java) | ||
.build() | ||
|
||
node2Actions[SqlDatabaseNode::class.java] = ImmutableList.Builder<Class<out NodeActionListener>>() | ||
.add(ConnectDatabaseAction::class.java) | ||
.build() | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
.../com/microsoft/intellij/serviceexplorer/azure/database/actions/ConnectDataSourceAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) 2018 JetBrains s.r.o. | ||
* <p/> | ||
* All rights reserved. | ||
* <p/> | ||
* MIT License | ||
* <p/> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* <p/> | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* <p/> | ||
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.microsoft.intellij.serviceexplorer.azure.database.actions | ||
|
||
import com.intellij.database.autoconfig.DataSourceDetector | ||
import com.intellij.database.autoconfig.DataSourceRegistry | ||
import com.intellij.openapi.project.Project | ||
import com.microsoft.azuretools.authmanage.AuthMethodManager | ||
import com.microsoft.azuretools.ijidea.actions.AzureSignInAction | ||
import com.microsoft.intellij.AzurePlugin | ||
import com.microsoft.tooling.msservices.serviceexplorer.Node | ||
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent | ||
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener | ||
|
||
abstract class ConnectDataSourceAction(protected val node: Node) : NodeActionListener() { | ||
|
||
public override fun actionPerformed(e: NodeActionEvent) { | ||
val project = node.project as Project ?: return | ||
|
||
try { | ||
if (!AzureSignInAction.doSignIn(AuthMethodManager.getInstance(), project)) return | ||
|
||
val registry = DataSourceRegistry(project) | ||
|
||
val builder = populateConnectionBuilder(registry.builder) | ||
|
||
if (builder != null) { | ||
builder.commit() | ||
registry.showDialog() | ||
} | ||
} catch (ex: Throwable) { | ||
AzurePlugin.log("Error connecting to database", ex) | ||
throw RuntimeException("Error connecting to database", ex) | ||
} | ||
} | ||
|
||
abstract fun populateConnectionBuilder(builder: DataSourceDetector.Builder): DataSourceDetector.Builder? | ||
|
||
protected class AzureSqlConnectionStringBuilder { | ||
companion object { | ||
const val defaultDriverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver" | ||
|
||
fun build(host: String) : String { | ||
return "jdbc:sqlserver://$host:1433;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;" | ||
} | ||
|
||
fun build(host: String, database: String) : String { | ||
return "jdbc:sqlserver://$host:1433;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;database=$database;" | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...rc/com/microsoft/intellij/serviceexplorer/azure/database/actions/ConnectDatabaseAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) 2018 JetBrains s.r.o. | ||
* <p/> | ||
* All rights reserved. | ||
* <p/> | ||
* MIT License | ||
* <p/> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* <p/> | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* <p/> | ||
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.microsoft.intellij.serviceexplorer.azure.database.actions | ||
|
||
import com.intellij.database.autoconfig.DataSourceDetector | ||
import com.microsoft.intellij.runner.db.AzureDatabaseMvpModel | ||
import com.microsoft.tooling.msservices.helpers.Name | ||
import com.microsoft.tooling.msservices.serviceexplorer.azure.database.sqldatabase.SqlDatabaseNode | ||
import com.microsoft.tooling.msservices.serviceexplorer.azure.database.sqlserver.SqlServerNode | ||
|
||
|
||
@Name("Connect to database") | ||
class ConnectDatabaseAction(private val databaseNode: SqlDatabaseNode) | ||
: ConnectDataSourceAction(databaseNode) { | ||
|
||
override fun populateConnectionBuilder(builder: DataSourceDetector.Builder): DataSourceDetector.Builder? { | ||
val databaseServerNode = databaseNode.parent as SqlServerNode | ||
|
||
val sqlServer = AzureDatabaseMvpModel.getSqlServerById(databaseServerNode.subscriptionId, databaseServerNode.sqlServerId) | ||
|
||
return builder | ||
.withName("Azure SQL Database - " + databaseServerNode.sqlServerName + " - " + databaseNode.sqlDatabaseName) | ||
.withDriverClass(AzureSqlConnectionStringBuilder.defaultDriverClass) | ||
.withUrl(AzureSqlConnectionStringBuilder.build( | ||
sqlServer.fullyQualifiedDomainName(), | ||
databaseNode.sqlDatabaseName | ||
)) | ||
.withUser(sqlServer.administratorLogin()) | ||
} | ||
} | ||
|
44 changes: 44 additions & 0 deletions
44
.../src/com/microsoft/intellij/serviceexplorer/azure/database/actions/ConnectServerAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright (c) 2018 JetBrains s.r.o. | ||
* <p/> | ||
* All rights reserved. | ||
* <p/> | ||
* MIT License | ||
* <p/> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* <p/> | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* <p/> | ||
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.microsoft.intellij.serviceexplorer.azure.database.actions | ||
|
||
import com.intellij.database.autoconfig.DataSourceDetector | ||
import com.microsoft.intellij.runner.db.AzureDatabaseMvpModel | ||
import com.microsoft.tooling.msservices.helpers.Name | ||
import com.microsoft.tooling.msservices.serviceexplorer.azure.database.sqlserver.SqlServerNode | ||
|
||
@Name("Connect to server") | ||
class ConnectServerAction(private val databaseServerNode: SqlServerNode) | ||
: ConnectDataSourceAction(databaseServerNode) { | ||
|
||
override fun populateConnectionBuilder(builder: DataSourceDetector.Builder): DataSourceDetector.Builder? { | ||
val sqlServer = AzureDatabaseMvpModel.getSqlServerById(databaseServerNode.subscriptionId, databaseServerNode.sqlServerId) | ||
|
||
return builder | ||
.withName("Azure SQL Database Server - " + databaseServerNode.sqlServerName) | ||
.withDriverClass(AzureSqlConnectionStringBuilder.defaultDriverClass) | ||
.withUrl(AzureSqlConnectionStringBuilder.build( | ||
sqlServer.fullyQualifiedDomainName() | ||
)) | ||
.withUser(sqlServer.administratorLogin()) | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...s/azure-toolkit-for-intellij/src/com/microsoft/intellij/helpers/base/AzureMvpPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Copyright (c) 2018 JetBrains s.r.o. | ||
* <p/> | ||
* All rights reserved. | ||
* <p/> | ||
* MIT License | ||
* <p/> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* <p/> | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* <p/> | ||
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package com.microsoft.intellij.helpers.base | ||
|
||
import com.intellij.openapi.progress.ProgressIndicator | ||
import com.intellij.openapi.progress.ProgressManager | ||
import com.intellij.openapi.progress.Task | ||
import com.jetbrains.rider.util.idea.application | ||
import com.jetbrains.rider.util.idea.getLogger | ||
import com.jetbrains.rider.util.lifetime.Lifetime | ||
import com.jetbrains.rider.util.reactive.Signal | ||
import com.jetbrains.rider.util.reactive.adviseOnce | ||
import com.microsoft.azuretools.core.mvp.ui.base.MvpPresenter | ||
import com.microsoft.azuretools.core.mvp.ui.base.MvpView | ||
import com.microsoft.tooling.msservices.components.DefaultLoader | ||
|
||
open class AzureMvpPresenter<V : MvpView> : MvpPresenter<V>() { | ||
|
||
companion object { | ||
private val LOG = getLogger(this) | ||
} | ||
|
||
fun <T>subscribe(lifetime: Lifetime, | ||
signal: Signal<T>, | ||
taskName: String, | ||
errorMessage: String, | ||
callableFunc: () -> T, | ||
invokeLaterCallback: (T) -> Unit) { | ||
|
||
signal.adviseOnce(lifetime) { | ||
application.invokeLater { | ||
if (lifetime.isTerminated) return@invokeLater | ||
invokeLaterCallback(it) | ||
} | ||
} | ||
|
||
ProgressManager.getInstance().run(object : Task.Backgroundable(null, taskName, false) { | ||
override fun run(indicator: ProgressIndicator) { | ||
signal.fire(callableFunc()) | ||
} | ||
|
||
override fun onThrowable(e: Throwable) { | ||
LOG.error(e) | ||
errorHandler(errorMessage, e as Exception) | ||
super.onThrowable(e) | ||
} | ||
}) | ||
} | ||
|
||
private fun errorHandler(message: String, e: Exception) { | ||
DefaultLoader.getIdeHelper().invokeLater { | ||
if (isViewDetached) return@invokeLater | ||
mvpView.onErrorWithException(message, e) | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...olkit-for-intellij/src/com/microsoft/intellij/helpers/validator/ConfigurationValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.microsoft.intellij.helpers.validator | ||
|
||
import com.intellij.execution.configurations.RuntimeConfigurationError | ||
|
||
open class ConfigurationValidator { | ||
|
||
/** | ||
* Validate Azure resource name against Azure requirements | ||
* Please see for details - https://docs.microsoft.com/en-us/azure/architecture/best-practices/naming-conventions | ||
* | ||
* @throws [RuntimeConfigurationError] in case name does not match requirements | ||
*/ | ||
@Throws(RuntimeConfigurationError::class) | ||
fun validateResourceName(name: String, | ||
nameLengthMin: Int, | ||
nameLengthMax: Int, | ||
nameLengthErrorMessage: String, | ||
nameRegex: Regex, | ||
nameInvalidCharsMessage: String) { | ||
|
||
validateResourceNameRegex(name, nameRegex, nameInvalidCharsMessage) | ||
|
||
if (name.length !in nameLengthMin..nameLengthMax) | ||
throw RuntimeConfigurationError(nameLengthErrorMessage) | ||
} | ||
|
||
@Throws(RuntimeConfigurationError::class) | ||
fun validateResourceNameRegex(name: String, | ||
nameRegex: Regex, | ||
nameInvalidCharsMessage: String) { | ||
val matches = nameRegex.findAll(name) | ||
if (matches.count() > 0) { | ||
val invalidChars = matches.map { it.value }.distinct().joinToString("', '", "'", "'") | ||
throw RuntimeConfigurationError(String.format(nameInvalidCharsMessage, invalidChars)) | ||
} | ||
} | ||
|
||
/** | ||
* Validate the field is set in a configuration | ||
* | ||
* @param value filed value to validate | ||
* @param message failure message to show to a user | ||
* | ||
* @throws [RuntimeConfigurationError] if field value is not set | ||
*/ | ||
@Throws(RuntimeConfigurationError::class) | ||
fun checkValueIsSet(value: String, message: String) { | ||
if (value.isEmpty()) throw RuntimeConfigurationError(message) | ||
} | ||
|
||
@Throws(RuntimeConfigurationError::class) | ||
fun checkValueIsSet(value: CharArray, message: String) { | ||
if (value.isEmpty()) throw RuntimeConfigurationError(message) | ||
} | ||
} |
Oops, something went wrong.