All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.spinnaker.keel.test.Resources.kt Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
package com.netflix.spinnaker.keel.test

import com.netflix.spinnaker.keel.api.AccountAwareLocations
import com.netflix.spinnaker.keel.api.ApiVersion
import com.netflix.spinnaker.keel.api.ArtifactReferenceProvider
import com.netflix.spinnaker.keel.api.ComputeResourceSpec
import com.netflix.spinnaker.keel.api.Dependency
import com.netflix.spinnaker.keel.api.Dependent
import com.netflix.spinnaker.keel.api.ExcludedFromDiff
import com.netflix.spinnaker.keel.api.Locatable
import com.netflix.spinnaker.keel.api.Moniker
import com.netflix.spinnaker.keel.api.Monikered
import com.netflix.spinnaker.keel.api.Resource
import com.netflix.spinnaker.keel.api.ResourceKind
import com.netflix.spinnaker.keel.api.ResourceSpec
import com.netflix.spinnaker.keel.api.SimpleLocations
import com.netflix.spinnaker.keel.api.SimpleRegionSpec
import com.netflix.spinnaker.keel.api.SimpleRegions
import com.netflix.spinnaker.keel.api.VersionedArtifactProvider
import com.netflix.spinnaker.keel.api.artifacts.ArtifactType
import com.netflix.spinnaker.keel.api.artifacts.DEBIAN
import com.netflix.spinnaker.keel.api.generateId
import com.netflix.spinnaker.keel.api.plugins.SimpleResourceHandler
import com.netflix.spinnaker.keel.api.plugins.SupportedKind
import com.netflix.spinnaker.keel.api.support.EventPublisher
import com.netflix.spinnaker.keel.core.api.SubmittedResource
import io.mockk.mockk
import java.time.Duration
import java.util.UUID

val TEST_API_V1 = ApiVersion("test", "1")
val TEST_API_V2 = ApiVersion("test", "2")

fun resource(
  kind: ResourceKind = TEST_API_V1.qualify("whatever"),
  id: String = randomString(),
  application: String = "fnord"
): Resource =
  DummyResourceSpec(id = id, application = application)
    .let { spec ->
      resource(
        kind = kind,
        spec = spec,
        application = application
      )
    }

fun artifactVersionedResource(
  kind: ResourceKind = TEST_API_V1.qualify("whatever"),
  id: String = randomString(),
  application: String = "fnord"
): Resource =
  DummyArtifactVersionedResourceSpec(id = id, application = application)
    .let { spec ->
      resource(
        kind = kind,
        spec = spec,
        application = application
      )
    }

fun submittedResource(
  kind: ResourceKind = TEST_API_V1.qualify("whatever"),
  application: String = "fnord"
): SubmittedResource =
  DummyResourceSpec(application = application)
    .let { spec ->
      submittedResource(
        kind = kind,
        spec = spec
      )
    }

fun locatableResource(
  kind: ResourceKind = TEST_API_V1.qualify("locatable"),
  id: String = randomString(),
  application: String = "fnord",
  locations: SimpleLocations = SimpleLocations(
    account = "test",
    vpc = "vpc0",
    regions = setOf(SimpleRegionSpec("us-west-1"))
  ),
  moniker: Moniker = Moniker("fnord", "locatable", "dummy")
): Resource =
  resource(
    kind = kind,
    spec = DummyLocatableResourceSpec(id = id, application = application, locations = locations, moniker = moniker),
    application = application
  )

fun dependentResource(
  kind: ResourceKind = TEST_API_V1.qualify("dependent"),
  id: String = randomString(),
  application: String = "fnord",
  locations: SimpleLocations = SimpleLocations(
    account = "test",
    vpc = "vpc0",
    regions = setOf(SimpleRegionSpec("us-west-1"))
  ),
  moniker: Moniker = Moniker("fnord", "dependent", "dummy"),
  dependsOn: Set = emptySet()
): Resource =
  resource(
    kind = kind,
    spec = DummyDependentResourceSpec(id = id, application = application, locations = locations, moniker = moniker, dependsOn = dependsOn),
    application = application
  )

fun  resource(
  kind: ResourceKind = TEST_API_V1.qualify("whatever"),
  spec: T
): Resource = resource(
  kind = kind,
  spec = spec,
  application = spec.application
)

fun  resource(
  kind: ResourceKind = TEST_API_V1.qualify("whatever"),
  spec: T,
  application: String = "fnord"
): Resource =
  Resource(
    kind = kind,
    spec = spec,
    metadata = mapOf(
      "id" to generateId(kind, spec),
      "version" to 1,
      "application" to application,
      "serviceAccount" to "keel@spinnaker"
    )
  )

fun  submittedResource(
  kind: ResourceKind = TEST_API_V1.qualify("whatever"),
  spec: T
): SubmittedResource =
  SubmittedResource(
    kind = kind,
    metadata = mapOf(
      "serviceAccount" to "keel@spinnaker"
    ),
    spec = spec
  )

fun versionedArtifactResource(
  kind: ResourceKind = TEST_API_V1.qualify("versionedArtifact"),
  id: String = randomString(),
  application: String = "fnord"
): Resource =
  DummyArtifactVersionedResourceSpec(id = id, application = application)
    .let { spec ->
      resource(
        kind = kind,
        spec = spec,
        application = application
      )
    }

fun artifactReferenceResource(
  kind: ResourceKind = TEST_API_V1.qualify("artifactReference"),
  id: String = randomString(),
  application: String = "fnord",
  artifactReference: String = "fnord",
  artifactType: ArtifactType = DEBIAN
): Resource =
  DummyArtifactReferenceResourceSpec(id = id, application = application, artifactReference = artifactReference, artifactType = artifactType)
    .let { spec ->
      resource(
        kind = kind,
        spec = spec,
        application = application
      )
    }

enum class DummyEnum { VALUE }

data class DummyResourceSpec(
  override val id: String = randomString(),
  val data: String = randomString(),
  override val application: String = "fnord",
  override val displayName: String = "fnord-dummy"
) : ResourceSpec {
  val intData: Int = 1234
  val boolData: Boolean = true
  val timeData: Duration = Duration.ofMinutes(5)
  val enumData: DummyEnum = DummyEnum.VALUE
}

data class DummyLocatableResourceSpec(
  override val id: String = randomString(),
  val data: String = randomString(),
  override val application: String = "fnord",
  override val displayName: String = "fnord-locatable-dummy",
  override val locations: SimpleLocations = SimpleLocations(
    account = "test",
    vpc = "vpc0",
    regions = setOf(SimpleRegionSpec("us-west-1"))
  ),
  override val moniker: Moniker = Moniker("fnord", "locatable", "dummy")
) : ResourceSpec, Locatable, Monikered

data class DummyDependentResourceSpec(
  override val id: String = randomString(),
  val data: String = randomString(),
  override val application: String = "fnord",
  override val displayName: String = "fnord-dependent-dummy",
  override val locations: SimpleLocations = SimpleLocations(
    account = "test",
    vpc = "vpc0",
    regions = setOf(SimpleRegionSpec("us-west-1"))
  ),
  override val moniker: Moniker = Moniker("fnord", "dependent", "dummy"),
  override val dependsOn: Set
) : ResourceSpec, Locatable, Monikered, Dependent

data class DummyArtifactVersionedResourceSpec(
  @get:ExcludedFromDiff
  override val id: String = randomString(),
  val data: String = randomString(),
  override val application: String = "fnord",
  override val artifactVersion: String? = "fnord-42.0",
  override val artifactName: String? = "fnord",
  override val artifactType: ArtifactType? = DEBIAN,
  override val displayName: String = "fnord-artifact-versioned-dummy",
) : ResourceSpec, VersionedArtifactProvider

data class DummyArtifactReferenceResourceSpec(
  @get:ExcludedFromDiff
  override val id: String = randomString(),
  val data: String = randomString(),
  override val application: String = "fnord",
  override val artifactType: ArtifactType? = DEBIAN,
  override val artifactReference: String? = "fnord",
  override val displayName: String = "fnord-artifact-reference-dummy",
  override val moniker: Moniker = Moniker("fnord", "artifactReference", "dummy"),
  override val locations: SimpleRegions = SimpleRegions(setOf(SimpleRegionSpec("us-east-1")))
) : ResourceSpec, ArtifactReferenceProvider, Monikered, Locatable

data class DummyResource(
  val id: String = randomString(),
  val data: String = randomString()
) {
  constructor(spec: DummyResourceSpec) : this(spec.id, spec.data)
}

fun randomString(length: Int = 8) =
  UUID.randomUUID()
    .toString()
    .map { it.toInt().toString(16) }
    .joinToString("")
    .substring(0 until length)

object DummyResourceHandlerV1 : SimpleResourceHandler(emptyList()) {
  override val supportedKind =
    SupportedKind(TEST_API_V1.qualify("whatever"), DummyResourceSpec::class.java)

  override val eventPublisher: EventPublisher = mockk(relaxed = true)

  override suspend fun current(resource: Resource): DummyResourceSpec? {
    TODO("not implemented")
  }
}

object DummyResourceHandlerV2 : SimpleResourceHandler(emptyList()) {
  override val supportedKind =
    SupportedKind(TEST_API_V2.qualify("whatever"), DummyResourceSpec::class.java)

  override val eventPublisher: EventPublisher = mockk(relaxed = true)

  override suspend fun current(resource: Resource): DummyResourceSpec? {
    TODO("not implemented")
  }
}

object DummyLocatableResourceHandler : SimpleResourceHandler(emptyList()) {
  override val supportedKind =
    SupportedKind(TEST_API_V1.qualify("locatable"), DummyLocatableResourceSpec::class.java)

  override val eventPublisher: EventPublisher = mockk(relaxed = true)
}

object DummyDependentResourceHandler : SimpleResourceHandler(emptyList()) {
  override val supportedKind =
    SupportedKind(TEST_API_V1.qualify("dependent"), DummyDependentResourceSpec::class.java)

  override val eventPublisher: EventPublisher = mockk(relaxed = true)
}

object DummyAccountAwareLocations : AccountAwareLocations {
  override val account: String = "test"
  override val regions: Set = setOf(SimpleRegionSpec("us-east-1"))
}

class DummyComputeResourceSpec(
  @get:ExcludedFromDiff
  override val id: String = randomString(),
  val data: String = randomString(),
  override val application: String = "fnord",
  override val artifactType: ArtifactType? = DEBIAN,
  override val artifactReference: String? = "fnord",
  override val artifactName: String? = "fnord",
  override val artifactVersion: String? = "fnord-1.0.0",
  override val displayName: String = "fnord-dummy-compute-resource",
  override val moniker: Moniker = Moniker("fnord", "computeResource", "dummy"),
  override val locations: DummyAccountAwareLocations = DummyAccountAwareLocations
) : ComputeResourceSpec

fun computeResource(
  kind: ResourceKind = TEST_API_V1.qualify("compute"),
  spec: DummyComputeResourceSpec = DummyComputeResourceSpec()
): Resource =
  resource(kind, spec)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy