Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IJMP-1630 Added initial processing of the global Zowe team conf file #186

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class ZOSMFConnectionConfigurable : BoundSearchableConfigurable("z/OSMF Connecti
if (wasModified) {
panel?.updateUI()
}
zoweConfigStates.clear()
}

/** Reset the Connections table changes. Updates UI when the changes were introduced */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import org.zowe.explorer.utils.removeTrailingSlashes
import org.zowe.explorer.utils.validateConnectionName
import org.zowe.explorer.utils.validateForBlank
import org.zowe.explorer.utils.validateZosmfUrl
import org.zowe.explorer.zowe.ZOWE_CONFIG_NAME
import org.zowe.explorer.zowe.service.ZoweConfigServiceImpl
import org.zowe.explorer.zowe.service.ZoweConfigType
import java.awt.Component
import javax.swing.JCheckBox
import javax.swing.JComponent
Expand Down Expand Up @@ -80,8 +81,8 @@ class ZoweTeamConfigDialog(
override fun createCenterPanel(): JComponent {
val sameWidthLabelsGroup = "CONNECTION_DIALOG_LABELS_WIDTH_GROUP"

state.zoweConfigPath = "${project?.basePath}/${ZOWE_CONFIG_NAME}"
val connectionName = "zowe-".plus(project?.name)
state.zoweConfigPath = ZoweConfigServiceImpl.getZoweConfigLocation(project, ZoweConfigType.LOCAL)
val connectionName = ZoweConfigServiceImpl.getZoweConnectionName(project, ZoweConfigType.LOCAL)
return panel {
row {
label("Connection name")
Expand Down
46 changes: 28 additions & 18 deletions src/main/kotlin/org/zowe/explorer/zowe/ZoweStartupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import com.intellij.openapi.ui.Messages
import org.zowe.explorer.config.connect.ConnectionConfig
import org.zowe.explorer.explorer.EXPLORER_NOTIFICATION_GROUP_ID
import org.zowe.explorer.utils.subscribe
import org.zowe.explorer.zowe.service.ZOWE_CONFIG_CHANGED
import org.zowe.explorer.zowe.service.ZoweConfigHandler
import org.zowe.explorer.zowe.service.ZoweConfigService
import org.zowe.explorer.zowe.service.ZoweConfigState
import org.zowe.explorer.zowe.service.*
import org.zowe.kotlinsdk.zowe.config.ZoweConfig
import java.util.regex.Pattern

const val ZOWE_CONFIG_NAME = "zowe.config.json"

Expand All @@ -35,21 +33,29 @@ const val ZOWE_CONFIG_NAME = "zowe.config.json"
* @param project - project instance to check zoweConfig.
* @return Nothing.
*/
fun showNotificationForAddUpdateZoweConfigIfNeeded(project: Project) {
fun showNotificationForAddUpdateZoweConfigIfNeeded(project: Project, type: ZoweConfigType) {
val zoweConfigService = project.service<ZoweConfigService>()
val zoweConfigState = zoweConfigService.getZoweConfigState()
val zoweConfigState = zoweConfigService.getZoweConfigState(type = type)

if (zoweConfigState == ZoweConfigState.NEED_TO_ADD) {
val topic = if (type == ZoweConfigType.LOCAL)
LOCAL_ZOWE_CONFIG_CHANGED
else
GLOBAL_ZOWE_CONFIG_CHANGED
NotificationGroupManager.getInstance().getNotificationGroup(EXPLORER_NOTIFICATION_GROUP_ID)
.createNotification("Zowe config file detected", NotificationType.INFORMATION).apply {
subscribe(ZOWE_CONFIG_CHANGED, object : ZoweConfigHandler {
.createNotification(
"${
Pattern.compile("^.").matcher(type.value).replaceFirst { m -> m.group().uppercase() }
} Zowe config file detected", NotificationType.INFORMATION
).apply {
subscribe(topic, object : ZoweConfigHandler {
override fun onConfigSaved(config: ZoweConfig, connectionConfig: ConnectionConfig) {
hideBalloon()
}
})
addAction(object : DumbAwareAction("Add Zowe Connection") {
addAction(object : DumbAwareAction("Add $type Zowe Connection") {
override fun actionPerformed(e: AnActionEvent) {
project.service<ZoweConfigService>().addOrUpdateZoweConfig(false, true)
project.service<ZoweConfigService>().addOrUpdateZoweConfig(false, true, type)
hideBalloon()
}
}).notify(project)
Expand All @@ -62,13 +68,13 @@ fun showNotificationForAddUpdateZoweConfigIfNeeded(project: Project) {
* @param project - project instance to check zoweConfig.
* @return Nothing.
*/
fun showDialogForDeleteZoweConfigIfNeeded(project: Project) {
fun showDialogForDeleteZoweConfigIfNeeded(project: Project, type: ZoweConfigType) {
val zoweConfigService = project.service<ZoweConfigService>()
val zoweConfigState = zoweConfigService.getZoweConfigState()
if(zoweConfigState != ZoweConfigState.NEED_TO_ADD || zoweConfigState != ZoweConfigState.NOT_EXISTS) {
val zoweConfigState = zoweConfigService.getZoweConfigState(type = type)
if (zoweConfigState != ZoweConfigState.NEED_TO_ADD || zoweConfigState != ZoweConfigState.NOT_EXISTS) {
val choice = Messages.showDialog(
project,
"Zowe config file has been deleted.\n" +
"$type Zowe config file has been deleted.\n" +
"Would you like to delete the corresponding connection?\n" +
"If you decide to leave the connection, it will be converted to a regular connection (username will be visible).",
"Deleting Zowe Config connection",
Expand All @@ -79,11 +85,14 @@ fun showDialogForDeleteZoweConfigIfNeeded(project: Project) {
AllIcons.General.QuestionDialog
)
if (choice == 0) {
zoweConfigService.deleteZoweConfig()
zoweConfigService.deleteZoweConfig(type)
}
}
zoweConfigService.zoweConfig = null
zoweConfigService.checkAndRemoveOldZoweConnection()
if (type == ZoweConfigType.LOCAL)
zoweConfigService.localZoweConfig = null
else
zoweConfigService.globalZoweConfig = null
zoweConfigService.checkAndRemoveOldZoweConnection(type)
}

/**
Expand All @@ -95,6 +104,7 @@ fun showDialogForDeleteZoweConfigIfNeeded(project: Project) {
class ZoweStartupActivity : StartupActivity {

override fun runActivity(project: Project) {
showNotificationForAddUpdateZoweConfigIfNeeded(project)
for (type in ZoweConfigType.entries)
showNotificationForAddUpdateZoweConfigIfNeeded(project, type)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.components.service
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.DumbAwareAction
import org.zowe.explorer.zowe.ZOWE_CONFIG_NAME
import org.zowe.explorer.zowe.service.ZoweConfigService
import org.zowe.explorer.zowe.service.ZoweConfigServiceImpl
import org.zowe.explorer.zowe.service.ZoweConfigState
import org.zowe.explorer.zowe.service.ZoweConfigType
import org.zowe.kotlinsdk.zowe.config.parseConfigJson

/**
Expand All @@ -40,11 +41,17 @@ class UpdateZoweConfigAction : DumbAwareAction() {
e.presentation.isEnabledAndVisible = false
return
}

var type = ZoweConfigType.GLOBAL
val zoweLocalConfigLocation = ZoweConfigServiceImpl.getZoweConfigLocation(project, ZoweConfigType.LOCAL)
if (e.getData(CommonDataKeys.VIRTUAL_FILE)?.path == zoweLocalConfigLocation)
type = ZoweConfigType.LOCAL

FileDocumentManager.getInstance().saveDocument(editor.document)

val zoweConfigService = project.service<ZoweConfigService>()

zoweConfigService.addOrUpdateZoweConfig(true)
zoweConfigService.addOrUpdateZoweConfig(true, type = type)
}

override fun update(e: AnActionEvent) {
Expand All @@ -57,21 +64,38 @@ class UpdateZoweConfigAction : DumbAwareAction() {
return
}
val vFile = e.getData(CommonDataKeys.VIRTUAL_FILE)
if (vFile?.path != "${project.basePath}/$ZOWE_CONFIG_NAME") {
var type = ZoweConfigType.GLOBAL
val zoweLocalConfigLocation = ZoweConfigServiceImpl.getZoweConfigLocation(project, ZoweConfigType.LOCAL)
val zoweGlobalConfigLocation = ZoweConfigServiceImpl.getZoweConfigLocation(project, ZoweConfigType.GLOBAL)
if (vFile?.path != zoweLocalConfigLocation && vFile?.path != zoweGlobalConfigLocation) {
e.presentation.isEnabledAndVisible = false
return
}
if (vFile.path == zoweLocalConfigLocation)
type = ZoweConfigType.LOCAL

val zoweConfigService = project.service<ZoweConfigService>()

val prevZoweConfig = zoweConfigService.zoweConfig
val prevZoweConfig = if (type == ZoweConfigType.LOCAL)
zoweConfigService.localZoweConfig
else
zoweConfigService.globalZoweConfig
runCatching {
zoweConfigService.zoweConfig = parseConfigJson(editor.document.text)
zoweConfigService.zoweConfig?.extractSecureProperties(vFile.path.split("/").toTypedArray())
if (type == ZoweConfigType.LOCAL) {
zoweConfigService.localZoweConfig = parseConfigJson(editor.document.text)
zoweConfigService.localZoweConfig?.extractSecureProperties(vFile.path.split("/").toTypedArray())
} else {
zoweConfigService.globalZoweConfig = parseConfigJson(editor.document.text)
zoweConfigService.globalZoweConfig?.extractSecureProperties(vFile.path.split("/").toTypedArray())
}
}
val zoweState = zoweConfigService.getZoweConfigState(false)
val zoweState = zoweConfigService.getZoweConfigState(false, type = type)
e.presentation.isEnabledAndVisible =
zoweState == ZoweConfigState.NEED_TO_UPDATE || zoweState == ZoweConfigState.NEED_TO_ADD
zoweConfigService.zoweConfig = prevZoweConfig
if (type == ZoweConfigType.LOCAL) {
zoweConfigService.localZoweConfig = prevZoweConfig
} else {
zoweConfigService.globalZoweConfig = prevZoweConfig
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ interface ZoweConfigHandler {
}

/**
* Instance of config changed topic.
* Instance of local config changed topic.
*/
@JvmField
val ZOWE_CONFIG_CHANGED = Topic.create("ZOWE_CONFIG_CHANGED", ZoweConfigHandler::class.java)
val LOCAL_ZOWE_CONFIG_CHANGED = Topic.create("LOCAL_ZOWE_CONFIG_CHANGED", ZoweConfigHandler::class.java)

/**
* Instance of global config changed topic.
*/
@JvmField
val GLOBAL_ZOWE_CONFIG_CHANGED = Topic.create("GLOBAL_ZOWE_CONFIG_CHANGED", ZoweConfigHandler::class.java)

/**
* ZoweConfigService implements ability to interact
Expand All @@ -48,9 +53,14 @@ interface ZoweConfigService {
val myProject: Project

/**
* Instance of zowe config file object model.
* Instance of local zowe config file object model.
*/
var localZoweConfig: ZoweConfig?

/**
* Instance of global zowe config file object model.
*/
var zoweConfig: ZoweConfig?
var globalZoweConfig: ZoweConfig?

/**
* Compares zoweConfig data with related connection config.
Expand All @@ -60,21 +70,21 @@ interface ZoweConfigService {
* SYNCHRONIZED if zowe.config.json and connection config are presented and their data are the same.
* NOT_EXISTS if zowe.config.json file is not presented in project.
*/
fun getZoweConfigState(scanProject: Boolean = true): ZoweConfigState
fun getZoweConfigState(scanProject: Boolean = true, type: ZoweConfigType): ZoweConfigState

/**
* Adds or updates connection config related to zoweConnection
* @param scanProject - will rescan project for zowe.config.json if true.
* @param checkConnection - Verify zowe connection by sending info request if true.
* @return - ConnectionConfig that was added or updated.
*/
fun addOrUpdateZoweConfig(scanProject: Boolean = true, checkConnection: Boolean = true): ConnectionConfig?
fun addOrUpdateZoweConfig(scanProject: Boolean = true, checkConnection: Boolean = true, type: ZoweConfigType): ConnectionConfig?

/**
* Deletes connection config related to zoweConnection
* @return - Nothing.
*/
fun deleteZoweConfig()
fun deleteZoweConfig(type: ZoweConfigType)

/**
* Creates zowe.schema.json for the currrent project and adds credentials to the secret store
Expand All @@ -84,17 +94,20 @@ interface ZoweConfigService {
fun addZoweConfigFile(state: ConnectionDialogState)

/**
* Checks all connections and removes linl to Zowe config file if it exists
* Checks all connections and removes link to Zowe config file if it exists
* renames old connection if it is needed
* @return - Nothing.
*/
fun checkAndRemoveOldZoweConnection()
fun checkAndRemoveOldZoweConnection(type: ZoweConfigType)

companion object {
fun getInstance(project: Project): ZoweConfigService = project.getService(ZoweConfigService::class.java)
}
}

/**
* ZoweConfigState enum class that represents Zowe Team connection states
*/
enum class ZoweConfigState(val value: String) {
NEED_TO_UPDATE("Update"),
NEED_TO_ADD("Add"),
Expand All @@ -106,3 +119,15 @@ enum class ZoweConfigState(val value: String) {
return value
}
}

/**
* ZoweConfigType enum class that represents type of Zowe Team Configuration
*/
enum class ZoweConfigType(val value: String) {
KUGDev marked this conversation as resolved.
Show resolved Hide resolved
GLOBAL("global"),
LOCAL("local");

override fun toString(): String {
return value
}
}
Loading