Skip to content

Commit

Permalink
Refactoring Deployment - removing misk's data class; extending Wisp; … (
Browse files Browse the repository at this point in the history
#1951)

* Refactoring Deployment - removing misk's data class; extending Wisp; delegate to internal data class

* Add the missing equals/hashCode overrides
  • Loading branch information
chris-ryan-square authored Jun 1, 2021
1 parent ce63f18 commit 364f226
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 101 deletions.
55 changes: 7 additions & 48 deletions misk/src/main/kotlin/misk/environment/Deployment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 7 additions & 6 deletions misk/src/main/kotlin/misk/environment/DeploymentModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<wisp.deployment.Deployment>().toInstance(deployment.wispDeployment)
bind<wisp.deployment.Deployment>().toInstance(deployment)
bind<Deployment>().toInstance(deployment)
bind<Env>().toInstance(env)
}
Expand All @@ -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)
Expand Down
164 changes: 119 additions & 45 deletions wisp-deployment/src/main/kotlin/wisp/deployment/Deployment.kt
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -42,12 +43,12 @@ 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
.filter { it != envProperty }
.forEach { assertFalse(it.invoke(deployment)) }
}
}
}
}

0 comments on commit 364f226

Please sign in to comment.