<commit_before>package no.skatteetaten.aurora.boober.service

import com.fasterxml.jackson.databind.JsonNode
import no.skatteetaten.aurora.boober.model.AuroraDeployEnvironment
import no.skatteetaten.aurora.boober.model.AuroraDeploymentSpec
import no.skatteetaten.aurora.boober.service.openshift.OpenShiftClient
import no.skatteetaten.aurora.boober.service.openshift.OpenshiftCommand
import no.skatteetaten.aurora.boober.service.openshift.OperationType.*
import no.skatteetaten.aurora.boober.service.openshift.mergeWithExistingResource
import no.skatteetaten.aurora.boober.service.resourceprovisioning.ProvisioningResult
import no.skatteetaten.aurora.boober.utils.openshiftKind
import no.skatteetaten.aurora.boober.utils.openshiftName
import org.springframework.stereotype.Service

@Service
class OpenShiftCommandBuilder(
        val openShiftClient: OpenShiftClient,
        val openShiftObjectGenerator: OpenShiftObjectGenerator
) {

    fun generateProjectRequest(environment: AuroraDeployEnvironment): OpenshiftCommand {

        val projectRequest = openShiftObjectGenerator.generateProjectRequest(environment)
        return createOpenShiftCommand(environment.namespace, projectRequest, false, false)
    }

    fun generateNamespace(environment: AuroraDeployEnvironment): OpenshiftCommand {
        val namespace = openShiftObjectGenerator.generateNamespace(environment)
        return createMergedUpdateCommand(environment.namespace, namespace)
    }

    fun generateRolebindings(environment: AuroraDeployEnvironment): List<OpenshiftCommand> {
        return openShiftObjectGenerator.generateRolebindings(environment.permissions)
                .map { createMergedUpdateCommand(environment.namespace, it) }
    }

    private fun createMergedUpdateCommand(namespace: String, it: JsonNode) =
            createOpenShiftCommand(namespace, it, true, true).copy(operationType = UPDATE)

    fun generateApplicationObjects(deployId: String,
                                   deploymentSpec: AuroraDeploymentSpec,
                                   provisioningResult: ProvisioningResult?,
                                   mergeWithExistingResource: Boolean
    ): List<OpenshiftCommand> {

        val namespace = deploymentSpec.environment.namespace

        return openShiftObjectGenerator.generateApplicationObjects(deployId, deploymentSpec, provisioningResult)
                .map { createOpenShiftCommand(namespace, it, mergeWithExistingResource, false) }
                .flatMap { command ->
                    if (command.isType(UPDATE, "route") && mustRecreateRoute(command.payload, command.previous)) {
                        val deleteCommand = command.copy(operationType = DELETE)
                        val createCommand = command.copy(operationType = CREATE, payload = command.generated!!)<commit_msg>AOS-2564, create view rolebindings (#210)<commit_after>package no.skatteetaten.aurora.boober.service

import com.fasterxml.jackson.databind.JsonNode
import no.skatteetaten.aurora.boober.model.AuroraDeployEnvironment
import no.skatteetaten.aurora.boober.model.AuroraDeploymentSpec
import no.skatteetaten.aurora.boober.service.openshift.OpenShiftClient
import no.skatteetaten.aurora.boober.service.openshift.OpenshiftCommand
import no.skatteetaten.aurora.boober.service.openshift.OperationType.CREATE
import no.skatteetaten.aurora.boober.service.openshift.OperationType.DELETE
import no.skatteetaten.aurora.boober.service.openshift.OperationType.UPDATE
import no.skatteetaten.aurora.boober.service.openshift.mergeWithExistingResource
import no.skatteetaten.aurora.boober.service.resourceprovisioning.ProvisioningResult
import no.skatteetaten.aurora.boober.utils.openshiftKind
import no.skatteetaten.aurora.boober.utils.openshiftName
import org.springframework.stereotype.Service

@Service
class OpenShiftCommandBuilder(
        val openShiftClient: OpenShiftClient,
        val openShiftObjectGenerator: OpenShiftObjectGenerator
) {

    fun generateProjectRequest(environment: AuroraDeployEnvironment): OpenshiftCommand {

        val projectRequest = openShiftObjectGenerator.generateProjectRequest(environment)
        return createOpenShiftCommand(environment.namespace, projectRequest, false, false)
    }

    fun generateNamespace(environment: AuroraDeployEnvironment): OpenshiftCommand {
        val namespace = openShiftObjectGenerator.generateNamespace(environment)
        return createMergedUpdateCommand(environment.namespace, namespace)
    }

    fun generateRolebindings(environment: AuroraDeployEnvironment): List<OpenshiftCommand> {
        return openShiftObjectGenerator.generateRolebindings(environment.permissions)
                .map { createOpenShiftCommand(environment.namespace, it, true, true) }
    }

    private fun createMergedUpdateCommand(namespace: String, it: JsonNode) =
            createOpenShiftCommand(namespace, it, true, true).copy(operationType = UPDATE)

    fun generateApplicationObjects(deployId: String,
                                   deploymentSpec: AuroraDeploymentSpec,
                                   provisioningResult: ProvisioningResult?,
                                   mergeWithExistingResource: Boolean
    ): List<OpenshiftCommand> {

        val namespace = deploymentSpec.environment.namespace

        return openShiftObjectGenerator.generateApplicationObjects(deployId, deploymentSpec, provisioningResult)
                .map { createOpenShiftCommand(namespace, it, mergeWithExistingResource, false) }
                .flatMap { command ->
                    if (command.isType(UPDATE, "route") && mustRecreateRoute(command.payload, command.previous)) {
                        val deleteCommand = command.copy(operationType = DELETE)
                        val createCommand = command.copy(operationType = CREATE, payload = command.generated!!)
{'fraction_non_alphanumeric': 0.07252822442695861, 'fraction_numerical': 0.0011973999315771468, 'mean_word_length': 3.9719387755102042, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7324280', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1456, 'n_words': 237}