Skip to content

Commit

Permalink
#1236 Demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Nov 23, 2024
1 parent 7e8554b commit 0f19288
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ class EnvironmentsMutations(
).apply {
environmentService.save(this)
}
}
},
unitMutation(
name = "deleteEnvironment",
description = "Deletes an existing environment",
input = DeleteEnvironmentInput::class,
) { input ->
environmentsLicense.checkEnvironmentFeatureEnabled()
val env = environmentService.getById(input.id)
environmentService.delete(env)
},
)
}

Expand All @@ -47,3 +56,8 @@ data class CreateEnvironmentInput(
@ListRef
val tags: List<String>?,
)

data class DeleteEnvironmentInput(
@APIDescription("ID of the environment to delete")
val id: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.nemerosa.ontrack.kdsl.acceptance.tests.provisioning

import net.nemerosa.ontrack.kdsl.acceptance.tests.AbstractACCDSLTestSupport
import net.nemerosa.ontrack.kdsl.spec.admin.admin
import net.nemerosa.ontrack.kdsl.spec.extension.environments.environments
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

class ProvisionDemo : AbstractACCDSLTestSupport() {

companion object {
const val PRODUCT_A = "productA"

const val ENV_STAGING = "staging"
const val ENV_PRODUCTION = "production"

const val BRONZE = "BRONZE"
const val SILVER = "SILVER"
const val GOLD = "GOLD"
}

@Test
@Disabled
fun `Provision environments`() {
// Cleanup
ontrack.findProjectByName(PRODUCT_A)?.delete()
ontrack.environments.findEnvironmentByName(ENV_STAGING)?.delete()
ontrack.environments.findEnvironmentByName(ENV_PRODUCTION)?.delete()

// Predefined promotion levels
ontrack.admin.predefinedPromotionLevels.apply {
createPredefinedPromotionLevel(
name = BRONZE,
image = ProvisionDemo::class.java.getResource("/promotions/bronze.png"),
override = true,
)
createPredefinedPromotionLevel(
name = SILVER,
image = ProvisionDemo::class.java.getResource("/promotions/silver.png"),
override = true,
)
createPredefinedPromotionLevel(
name = GOLD,
image = ProvisionDemo::class.java.getResource("/promotions/gold.png"),
override = true,
)
}

// Project provisioning
val project = ontrack.createProject(PRODUCT_A, "Project which can be deployed into an environment")
val branch = project.branch("main") { this }
val bronze = branch.promotion(BRONZE)
val silver = branch.promotion(SILVER)
val gold = branch.promotion(GOLD)

// Creating the environments
val envStaging = ontrack.environments.createEnvironment(ENV_STAGING, 100)
val envProduction = ontrack.environments.createEnvironment(ENV_PRODUCTION, 200)

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
These tests are disabled by default but can be used individually to provision
any environment for demo purposes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation DeleteEnvironment($id: String!) {
deleteEnvironment(input: {
id: $id
}) {
...PayloadUserErrors
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query FindEnvironmentByName($name: String!) {
environmentByName(name: $name) {
...EnvironmentFragment
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8724,21 +8724,17 @@
"inputFields": [
{
"name": "description",
"description": "description field",
"description": "Description for the environment",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
},
{
"name": "name",
"description": "name field",
"description": "Unique name for the environment",
"type": {
"kind": "NON_NULL",
"name": null,
Expand All @@ -8752,7 +8748,7 @@
},
{
"name": "order",
"description": "order field",
"description": "Order to the environment, used to sort them from lower environments to higher ones",
"type": {
"kind": "NON_NULL",
"name": null,
Expand All @@ -8766,20 +8762,17 @@
},
{
"name": "tags",
"description": "tags field",
"description": "Tags for the environment",
"type": {
"kind": "NON_NULL",
"kind": "LIST",
"name": null,
"ofType": {
"kind": "LIST",
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String"
}
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
},
Expand Down Expand Up @@ -12086,6 +12079,64 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "DeleteEnvironmentInput",
"description": "Input type for the deleteEnvironment mutation.",
"fields": null,
"inputFields": [
{
"name": "id",
"description": "ID of the environment to delete",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null
}
],
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "DeleteEnvironmentPayload",
"description": "Output type for the deleteEnvironment mutation.",
"fields": [
{
"name": "errors",
"description": "List of errors",
"args": [],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "UserError",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
{
"kind": "INTERFACE",
"name": "Payload",
"ofType": null
}
],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "DeleteGenericPropertyInput",
Expand Down Expand Up @@ -32171,6 +32222,29 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "deleteEnvironment",
"description": "Deletes an existing environment",
"args": [
{
"name": "input",
"description": "Input for the mutation",
"type": {
"kind": "INPUT_OBJECT",
"name": "DeleteEnvironmentInput",
"ofType": null
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "DeleteEnvironmentPayload",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "saveSlotAdmissionRuleConfig",
"description": "Saves or creates a configured admission rule for a slot",
Expand Down Expand Up @@ -33983,6 +34057,11 @@
"name": "DeleteConfigurationPayload",
"ofType": null
},
{
"kind": "OBJECT",
"name": "DeleteEnvironmentPayload",
"ofType": null
},
{
"kind": "OBJECT",
"name": "DeleteGenericPropertyPayload",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,20 @@ interface Connector {
/**
* Uploading a single file
*/
@Deprecated("Use uploadFile with the file content")
fun uploadFile(
path: String,
headers: Map<String, String> = emptyMap(),
file: Pair<String, ByteArray>,
)

/**
* Uploading a single file using REST
*/
fun uploadFile(
path: String,
headers: Map<String, String> = emptyMap(),
file: FileContent,
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.nemerosa.ontrack.kdsl.connector

data class FileContent(
val name: String,
val content: ByteArray,
val type: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package net.nemerosa.ontrack.kdsl.connector.support
import net.nemerosa.ontrack.kdsl.connector.Connector
import net.nemerosa.ontrack.kdsl.connector.ConnectorResponse
import net.nemerosa.ontrack.kdsl.connector.ConnectorResponseBody
import net.nemerosa.ontrack.kdsl.connector.FileContent
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.core.io.ByteArrayResource
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.util.LinkedMultiValueMap
Expand All @@ -14,7 +16,6 @@ import org.springframework.web.client.RestTemplate
import org.springframework.web.client.getForEntity
import org.springframework.web.client.postForEntity
import org.springframework.web.util.UriComponentsBuilder
import java.net.URLEncoder
import java.nio.charset.Charset


Expand Down Expand Up @@ -80,6 +81,7 @@ class DefaultConnector(
restTemplate(headers).delete(path)
}

@Deprecated("Use uploadFile with the file content")
override fun uploadFile(path: String, headers: Map<String, String>, file: Pair<String, ByteArray>) {
val actualHeaders = headers.toMutableMap()
actualHeaders["Content-Type"] = MediaType.MULTIPART_FORM_DATA.toString()
Expand All @@ -97,6 +99,30 @@ class DefaultConnector(
)
}

override fun uploadFile(path: String, headers: Map<String, String>, file: FileContent) {
val actualHeaders = headers.toMutableMap()
actualHeaders["Content-Type"] = MediaType.MULTIPART_FORM_DATA.toString()

val resource = object : ByteArrayResource(file.content) {
override fun getFilename(): String = file.name
}

// File part
val fileHeaders = HttpHeaders()
fileHeaders.contentType = MediaType.parseMediaType(file.type)
val fileEntity = HttpEntity<ByteArrayResource>(resource, fileHeaders)
val body: MultiValueMap<String, Any> = LinkedMultiValueMap()
body.add(file.name, fileEntity)

val requestEntity: HttpEntity<MultiValueMap<String, Any>> = HttpEntity(body)

restTemplate(actualHeaders).postForEntity(
path,
requestEntity,
String::class.java
)
}

private fun restTemplate(
headers: Map<String, String>,
noAuth: Boolean = false,
Expand Down
Loading

0 comments on commit 0f19288

Please sign in to comment.