Skip to content

Commit

Permalink
implement connection validation
Browse files Browse the repository at this point in the history
  • Loading branch information
callbacksin committed Jul 26, 2024
1 parent d3a22fe commit 0c07a39
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/org/zowe/zdevops/classic/AbstractBuildStep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.kohsuke.stapler.QueryParameter
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection
import org.zowe.zdevops.Messages
import org.zowe.zdevops.config.ZOSConnectionList
import org.zowe.zdevops.utils.validateConnection
import java.io.IOException
import java.io.PrintWriter
import java.io.StringWriter
Expand Down Expand Up @@ -71,6 +72,8 @@ abstract class AbstractBuildStep(val connectionName: String) : Builder(), Simple
connURL.host, connURL.port.toString(), connection.username, connection.password, connURL.protocol
)

validateConnection(zosConnection)

perform(build, launcher, listener, zosConnection)
return true
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/kotlin/org/zowe/zdevops/config/ZOSConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
* Copyright IBA Group 2022
*/

package org.zowe.zdevops.config;
package org.zowe.zdevops.config

import com.cloudbees.plugins.credentials.CredentialsMatchers
import com.cloudbees.plugins.credentials.CredentialsProvider
import com.cloudbees.plugins.credentials.common.StandardCredentials
import com.cloudbees.plugins.credentials.common.StandardListBoxModel
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder
import org.zowe.kotlinsdk.zowe.client.sdk.zosfiles.ZosDsnList
import org.zowe.kotlinsdk.zowe.client.sdk.zosfiles.input.ListParams
import org.zowe.zdevops.Messages
import org.zowe.zdevops.declarative.jobs.zMessages
import hudson.Extension
import hudson.model.AbstractDescribableImpl
import hudson.model.Descriptor
Expand All @@ -32,6 +28,9 @@ import net.sf.json.JSONObject
import org.kohsuke.stapler.DataBoundConstructor
import org.kohsuke.stapler.QueryParameter
import org.kohsuke.stapler.StaplerRequest
import org.zowe.zdevops.Messages
import org.zowe.zdevops.declarative.jobs.zMessages
import org.zowe.zdevops.utils.getTestDatasetList
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
Expand Down Expand Up @@ -119,9 +118,9 @@ constructor(
val testConnection = org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection(
connURL.host, connURL.port.toString(), credentials.username, credentials.password.plainText, connURL.protocol
)
ZosDsnList(testConnection).listDsn(zMessages.zdevops_config_ZOSConnection_validation_testDS(), ListParams())
getTestDatasetList(testConnection)
}.onFailure {
return FormValidation.error(zMessages.zdevops_config_ZOSConnection_validation_error());
return FormValidation.error("${zMessages.zdevops_config_ZOSConnection_validation_error()}\n(${it.message})");
}
return FormValidation.ok(zMessages.zdevops_config_ZOSConnection_validation_success())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import hudson.tasks.BuildStepDescriptor
import hudson.tasks.Builder
import jenkins.tasks.SimpleBuildStep
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection
import org.zowe.zdevops.Messages
import org.zowe.zdevops.config.ZOSConnectionList
import org.zowe.zdevops.utils.getZoweZosConnection
import java.io.PrintWriter
import java.io.StringWriter
import java.net.URL
import java.nio.charset.StandardCharsets

abstract class AbstractZosmfAction : Builder(), SimpleBuildStep {
Expand All @@ -38,18 +36,8 @@ abstract class AbstractZosmfAction : Builder(), SimpleBuildStep {

override fun perform(run: Run<*, *>, workspace: FilePath, env: EnvVars, launcher: Launcher, listener: TaskListener) {
val connectionName = workspace.read().readBytes().toString(StandardCharsets.UTF_8)
val connection = ZOSConnectionList.resolve(connectionName) ?: let {
val zoweConnection = getZoweZosConnection(connectionName, listener)

val exception = IllegalArgumentException(Messages.zdevops_config_ZOSConnection_resolve_unknown(connectionName))
val sw = StringWriter()
exception.printStackTrace(PrintWriter(sw))
listener.logger.println(sw.toString())
throw exception
}
val connURL = URL(connection.url)
val zoweConnection = ZOSConnection(
connURL.host, connURL.port.toString(), connection.username, connection.password, connURL.protocol
)
runCatching {
perform(run, workspace, env, launcher, listener, zoweConnection)
}.onFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@ package org.zowe.zdevops.declarative
import hudson.EnvVars
import hudson.Extension
import hudson.FilePath
import hudson.model.*
import hudson.model.Run
import hudson.model.TaskListener
import org.jenkinsci.plugins.workflow.steps.Step
import org.jenkinsci.plugins.workflow.steps.StepContext
import org.jenkinsci.plugins.workflow.steps.StepDescriptor
import org.jenkinsci.plugins.workflow.steps.StepExecution
import org.kohsuke.stapler.DataBoundConstructor
import org.zowe.zdevops.utils.getZoweZosConnection
import org.zowe.zdevops.utils.validateConnection


class ZosmfStepDeclarative @DataBoundConstructor constructor(private val connectionName: String) : Step() {
override fun start(context: StepContext): StepExecution {
val listener: TaskListener? = context.get(TaskListener::class.java)
val zosConnection = getZoweZosConnection(connectionName, listener)

validateConnection(zosConnection)

return ZosmfExecution(connectionName, context)
}

Expand Down
73 changes: 73 additions & 0 deletions src/main/kotlin/org/zowe/zdevops/utils/ConnectionValidation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright IBA Group 2024
*/

package org.zowe.zdevops.utils

import hudson.AbortException
import hudson.model.TaskListener
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection
import org.zowe.kotlinsdk.zowe.client.sdk.zosfiles.ZosDsnList
import org.zowe.kotlinsdk.zowe.client.sdk.zosfiles.input.ListParams
import org.zowe.zdevops.Messages
import org.zowe.zdevops.config.ZOSConnectionList
import java.io.PrintWriter
import java.io.StringWriter
import java.net.URL

/**
* Gets a list of datasets
* Calls the listDsn function of ZosDsnList to list data set names.
* Passes a test data set name ('HELLO.THERE').
*
* @param zosConnection The ZOSConnection object representing the connection to the z/OS system.
*/
fun getTestDatasetList(zosConnection: ZOSConnection) {
ZosDsnList(zosConnection).listDsn(Messages.zdevops_config_ZOSConnection_validation_testDS(), ListParams())
}

/**
* Validates a z/OS connection.
*
* @param zosConnection The ZOSConnection object representing the connection to the z/OS system.
*/
fun validateConnection(zosConnection: ZOSConnection) {
try {
getTestDatasetList(zosConnection)
} catch (connectException: Exception) {
val connExMessage = "Failed to connect to z/OS (${zosConnection.user}@${zosConnection.host}:${zosConnection.zosmfPort}): ${connectException.message}"
throw AbortException(connExMessage)
}
}

/**
* Retrieves zOS connection by its name from the zOS connection list in Jenkins configuration.
*
* This function attempts to resolve the connection from the ZOSConnectionList
* using the provided connection name. If the connection is not found in the list, an
* IllegalArgumentException is thrown with a detailed error message, and the stack trace
* is logged using the provided TaskListener.
*
* @param connectionName The name of the connection to resolve.
* @param listener The TaskListener used to log messages and exceptions.
* @return A ZOSConnection object containing the resolved connection details.
* @throws IllegalArgumentException If the connection configuration cannot be resolved.
*/
fun getZoweZosConnection(connectionName: String, listener: TaskListener?): ZOSConnection {
val connection = ZOSConnectionList.resolve(connectionName) ?: run {
val exception = IllegalArgumentException(Messages.zdevops_config_ZOSConnection_resolve_unknown(connectionName))
val sw = StringWriter()
exception.printStackTrace(PrintWriter(sw))
listener?.logger?.println(sw.toString())
throw exception
}

val connURL = URL(connection.url)
return ZOSConnection(connURL.host, connURL.port.toString(), connection.username, connection.password, connURL.protocol)
}

0 comments on commit 0c07a39

Please sign in to comment.