Last active
January 30, 2024 14:43
-
-
Save sasaken555/8c5db325e17e5e9eca15b0ad6fda7ba0 to your computer and use it in GitHub Desktop.
Find value from AWS SSM ParameterStore with Scala
This file contains hidden or 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
package net.ponzmild.aws | |
import com.amazonaws.services.simplesystemsmanagement.model.{GetParameterRequest} | |
import com.amazonaws.services.simplesystemsmanagement.{AWSSimpleSystemsManagement, AWSSimpleSystemsManagementClientBuilder} | |
import scala.util.{Failure, Success, Try} | |
/** | |
* Find value from AWS SSM ParameterStore | |
*/ | |
class ParamStoreUtil { | |
private def getClient: AWSSimpleSystemsManagement = AWSSimpleSystemsManagementClientBuilder.standard().build() | |
/** | |
* Find value by a key. | |
* | |
* @param key Key Name of SSM ParameterStore | |
* @return the value stored with the specified key | |
* @exception ParameterNotFoundException the value for key not found... | |
*/ | |
def getParam(key: String): String = { | |
val request = new GetParameterRequest() | |
request.withName(key) | |
val ssmRequest = Try(getClient.getParameter(request)) | |
ssmRequest match { | |
case Success(value) => value.getParameter.getValue | |
case Failure(exception) => throw exception | |
} | |
} | |
} |
This file contains hidden or 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
package net.ponzmild.aws | |
import com.amazonaws.services.simplesystemsmanagement.model.ParameterNotFoundException | |
import org.scalatest.FunSuite | |
/** | |
* Test ParamStoreUtil with ScalaTest | |
*/ | |
class ParamStoreUtilTest extends FunSuite { | |
test(".getParam should return specified value") { | |
val testKey ="/Test/Common/Common/Test-Value" // Register value with this key before! | |
val util = new ParamStoreUtil | |
assert(util.getParam(testKey) == "test") | |
} | |
test(".getParam should return NotFound Exception") { | |
val failureKey ="/No/Such/Key/Exists" | |
val util = new ParamStoreUtil | |
assertThrows[ParameterNotFoundException] { | |
util.getParam(failureKey) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment