diff --git a/misk/src/main/kotlin/misk/environment/Deployment.kt b/misk/src/main/kotlin/misk/environment/Deployment.kt index 4077e43e72a..0ccadf847aa 100644 --- a/misk/src/main/kotlin/misk/environment/Deployment.kt +++ b/misk/src/main/kotlin/misk/environment/Deployment.kt @@ -10,51 +10,10 @@ package misk.environment // "wisp.deployment.Deployment" // ) //) -data class Deployment( - /** - * The name of this deployment. This is used for debugging and should not be parsed. - * - * All pods in the same deployment will have this same name. - */ - val name: String, - - /** - * Whether the service is running in a production environment, having an SLA or handling customer data. - */ - val isProduction: Boolean = false, - - /** - * Whether the service is running in a test environment, either locally or in a CI. - */ - val isTest: Boolean = false, - - /** - * Whether the service is running on a local developer machine, including as a Docker image. - */ - val isLocalDevelopment: Boolean = false -) { - init { - if (isProduction) check(!isTest && !isLocalDevelopment) - if (isTest) check(!isLocalDevelopment) - } - - /** - * Returns true if running in a managed cluster, such as a staging or production cluster. Mutually exclusive with isFake. - */ - val isReal: Boolean - get() = !isFake - - /** - * Returns true if running outside of a cluster (CI or local development). Mutually exclusive with isReal. - */ - val isFake: Boolean - get() = isTest || isLocalDevelopment - - val wispDeployment: wisp.deployment.Deployment - get() = wisp.deployment.Deployment( - name, - isProduction = isProduction, - isTest = isTest, - isLocalDevelopment = isLocalDevelopment - ) -} +class Deployment( + name: String, + isProduction: Boolean = false, + isStaging: Boolean = false, + isTest: Boolean = false, + isLocalDevelopment: Boolean = false +) : wisp.deployment.Deployment(name, isProduction, isStaging, isTest, isLocalDevelopment) diff --git a/misk/src/main/kotlin/misk/environment/DeploymentModule.kt b/misk/src/main/kotlin/misk/environment/DeploymentModule.kt index e05ca174340..30652121533 100644 --- a/misk/src/main/kotlin/misk/environment/DeploymentModule.kt +++ b/misk/src/main/kotlin/misk/environment/DeploymentModule.kt @@ -18,16 +18,17 @@ class DeploymentModule( ) : this( Deployment( - deployment.name, - deployment.isProduction, - deployment.isTest, - deployment.isLocalDevelopment + name = deployment.name, + isProduction = deployment.isProduction, + isStaging = deployment.isStaging, + isTest = deployment.isTest, + isLocalDevelopment = deployment.isLocalDevelopment ), env ) override fun configure() { - bind().toInstance(deployment.wispDeployment) + bind().toInstance(deployment) bind().toInstance(deployment) bind().toInstance(env) } @@ -41,7 +42,7 @@ class DeploymentModule( fun forTesting(): Module { return Modules.combine( DeploymentModule( - deployment = TEST_DEPLOYMENT.wispDeployment, + deployment = TEST_DEPLOYMENT, env = Env("TESTING") ), EnvironmentModule(Environment.TESTING) diff --git a/wisp-deployment/src/main/kotlin/wisp/deployment/Deployment.kt b/wisp-deployment/src/main/kotlin/wisp/deployment/Deployment.kt index 9e6141fb8a6..ba704c4825b 100644 --- a/wisp-deployment/src/main/kotlin/wisp/deployment/Deployment.kt +++ b/wisp-deployment/src/main/kotlin/wisp/deployment/Deployment.kt @@ -1,7 +1,9 @@ package wisp.deployment -/** Deployment describes the context in which the application is running */ -data class Deployment( +/** Deployment describes the context in which the application is running + * TODO: convert to data class when misk Deployment removed. + */ +open class Deployment( /** * The name of this deployment. This is used for debugging and should not be parsed. * @@ -29,28 +31,132 @@ data class Deployment( */ val isLocalDevelopment: Boolean = false ) { - init { - when { - isProduction -> check(!isStaging && !isTest && !isLocalDevelopment) - isStaging -> check(!isProduction && !isTest && !isLocalDevelopment) - isTest -> check(!isProduction && !isStaging && !isLocalDevelopment) - isLocalDevelopment -> check(!isProduction && !isStaging && !isTest) - } - } + + val delegateDeployment = + DelegateDeployment(name, isProduction, isStaging, isTest, isLocalDevelopment) /** * Returns true if running in a managed cluster, such as a staging or production cluster. Mutually exclusive with isFake. */ val isReal: Boolean - get() = !isFake + get() = delegateDeployment.isFake /** * Returns true if running outside of a cluster (CI or local development). Mutually exclusive with isReal. */ val isFake: Boolean - get() = isTest || isLocalDevelopment + get() = delegateDeployment.isReal + + /** + * TEMPORARY, move into [Deployment] when misk's Deployment is removed. + */ + data class DelegateDeployment( + /** + * The name of this deployment. This is used for debugging and should not be parsed. + * + * All pods in the same deployment will have this same name. + */ + val name: String, + + /** + * Whether the service is running in a production environment, having an SLA or handling customer data. + */ + val isProduction: Boolean = false, + + /** + * Whether the service is running in a staging environment. + */ + val isStaging: Boolean = false, + + /** + * Whether the service is running in a test environment, either locally or in a CI. + */ + val isTest: Boolean = false, + + /** + * Whether the service is running on a local developer machine, including as a Docker image. + */ + val isLocalDevelopment: Boolean = false + ) { + init { + when { + isProduction -> check(!isStaging && !isTest && !isLocalDevelopment) + isStaging -> check(!isProduction && !isTest && !isLocalDevelopment) + isTest -> check(!isProduction && !isStaging && !isLocalDevelopment) + isLocalDevelopment -> check(!isProduction && !isStaging && !isTest) + } + } + + /** + * Returns true if running in a managed cluster, such as a staging or production cluster. Mutually exclusive with isFake. + */ + val isReal: Boolean + get() = !isFake + + /** + * Returns true if running outside of a cluster (CI or local development). Mutually exclusive with isReal. + */ + val isFake: Boolean + get() = isTest || isLocalDevelopment + + } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is Deployment) return false + + if (delegateDeployment != other.delegateDeployment) return false + + return true + } + + override fun hashCode(): Int { + return delegateDeployment.hashCode() + } + } +val PRODUCTION = Deployment( + "production", + isProduction = true, + isStaging = false, + isTest = false, + isLocalDevelopment = false +) + +val STAGING = Deployment( + "staging", + isProduction = false, + isStaging = true, + isTest = false, + isLocalDevelopment = false +) + +val TESTING = Deployment( + "testing", + isProduction = false, + isStaging = false, + isTest = true, + isLocalDevelopment = false +) + +val DEVELOPMENT = Deployment( + "development", + isProduction = false, + isStaging = false, + isTest = false, + isLocalDevelopment = true +) + +val deployments = mapOf( + "production" to PRODUCTION, + "staging" to STAGING, + "testing" to TESTING, + "test" to TESTING, + "development" to DEVELOPMENT, + "dev" to DEVELOPMENT +) + /** * Determines a Deployment based on the value within the ENVIRONMENT variable, defaulting to * local development if not set (i.e. isLocalDevelopment == true) @@ -74,37 +180,5 @@ fun getDeploymentFromEnvironmentVariable( ) val deploymentName = name ?: environment - return when (environment.toLowerCase()) { - "production" -> Deployment( - deploymentName, - isProduction = true, - isStaging = false, - isTest = false, - isLocalDevelopment = false - ) - - "staging" -> Deployment( - deploymentName, - isProduction = false, - isStaging = true, - isTest = false, - isLocalDevelopment = false - ) - - "testing", "test" -> Deployment( - deploymentName, - isProduction = false, - isStaging = false, - isTest = true, - isLocalDevelopment = false - ) - - else -> Deployment( - deploymentName, - isProduction = false, - isStaging = false, - isTest = false, - isLocalDevelopment = true - ) - } + return deployments[deploymentName.toLowerCase()] ?: DEVELOPMENT } diff --git a/wisp-deployment/src/test/kotlin/wisp/deployment/DeploymentTest.kt b/wisp-deployment/src/test/kotlin/wisp/deployment/DeploymentTest.kt index adec2282a98..b071b932115 100644 --- a/wisp-deployment/src/test/kotlin/wisp/deployment/DeploymentTest.kt +++ b/wisp-deployment/src/test/kotlin/wisp/deployment/DeploymentTest.kt @@ -2,6 +2,7 @@ package wisp.deployment import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -42,7 +43,7 @@ internal class DeploymentTest { for ((envVar, envProperty) in environmentMap) { environmentVariableLoader = FakeEnvironmentVariableLoader(mutableMapOf("ENVIRONMENT" to envVar)) val deployment = getDeploymentFromEnvironmentVariable( - name = "foo", environmentVariableLoader = environmentVariableLoader + environmentVariableLoader = environmentVariableLoader ) assertTrue(envProperty.invoke(deployment)) props @@ -50,4 +51,4 @@ internal class DeploymentTest { .forEach { assertFalse(it.invoke(deployment)) } } } -} \ No newline at end of file +}