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

com.exactpro.th2.converter.controllers.ConverterController.kt Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
/*
 * Copyright 2020-2022 Exactpro (Exactpro Systems Limited)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.exactpro.th2.converter.controllers

import com.exactpro.th2.converter.config.ApplicationConfig
import com.exactpro.th2.converter.controllers.errors.BadRequestException
import com.exactpro.th2.converter.controllers.errors.ErrorCode
import com.exactpro.th2.converter.conversion.Converter.convertFromGit
import com.exactpro.th2.converter.conversion.Converter.convertFromRequest
import com.exactpro.th2.converter.util.ProjectConstants
import com.exactpro.th2.converter.util.ProjectConstants.PROPAGATION_DENY
import com.exactpro.th2.converter.util.ProjectConstants.PROPAGATION_RULE
import com.exactpro.th2.converter.util.ProjectConstants.SOURCE_BRANCH
import com.exactpro.th2.converter.util.RepositoryUtils.schemaExists
import com.exactpro.th2.converter.util.RepositoryUtils.updateRepository
import com.exactpro.th2.converter.util.RepositoryUtils.updateSchemaK8sPropagation
import com.exactpro.th2.infrarepo.git.GitterContext
import com.exactpro.th2.infrarepo.repo.Repository
import com.exactpro.th2.infrarepo.repo.RepositoryResource
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Post
import io.micronaut.http.annotation.Produces
import org.eclipse.jgit.errors.EntryExistsException

@Controller
@Produces(MediaType.APPLICATION_JSON)
class ConverterController {

    @Post("convert/{schemaName}/{targetVersion}")
    fun convertInSameBranch(
        schemaName: String,
        targetVersion: String
    ): ConversionSummary {
        checkRequestedVersion(targetVersion)
        val gitterContext = GitterContext.getContext(ApplicationConfig.git)
        checkSourceSchema(schemaName, gitterContext)
        val gitter = gitterContext.getGitter(schemaName)
        val conversionResult = convertFromGit(targetVersion, gitter)
        val currentResponse = conversionResult.summary
        if (currentResponse.hasErrors()) {
            return currentResponse
        }

        try {
            gitter.lock()
            Repository.removeLinkResources(gitter)
            updateRepository(conversionResult.convertedResources, gitter, Repository::update)
            conversionResult.summary.commitRef = gitter.commitAndPush("Schema conversion")
        } finally {
            gitter.unlock()
        }
        return conversionResult.summary
    }

    @Post("convert/{sourceSchemaName}/{newSchemaName}/{targetVersion}")
    fun convertInNewBranch(
        sourceSchemaName: String,
        newSchemaName: String,
        targetVersion: String
    ): ConversionSummary {
        checkRequestedVersion(targetVersion)
        val gitterContext: GitterContext = GitterContext.getContext(ApplicationConfig.git)
        checkSourceSchema(sourceSchemaName, gitterContext)
        val sourceBranchGitter = gitterContext.getGitter(sourceSchemaName)
        val conversionResult = convertFromGit(targetVersion, sourceBranchGitter)
        val currentResponse = conversionResult.summary
        if (currentResponse.hasErrors()) {
            return currentResponse
        }

        try {
            sourceBranchGitter.lock()
            if (updateSchemaK8sPropagation(PROPAGATION_DENY, sourceBranchGitter)) {
                sourceBranchGitter.commitAndPush("set k8s-propagation to deny for the schema '$sourceSchemaName'")
            }
        } finally {
            sourceBranchGitter.unlock()
        }

        val newBranchGitter = gitterContext.getGitter(newSchemaName)
        try {
            newBranchGitter.lock()
            newBranchGitter.createBranch(sourceSchemaName)
            updateSchemaK8sPropagation(PROPAGATION_RULE, newBranchGitter)
            Repository.removeLinkResources(newBranchGitter)
            updateRepository(conversionResult.convertedResources, newBranchGitter, Repository::update)
            conversionResult.summary.commitRef = newBranchGitter.commitAndPush("Schema conversion")
        } catch (e: EntryExistsException) {
            throw BadRequestException(
                ErrorCode.BRANCH_ALREADY_EXISTS,
                "Branch '$newSchemaName' already exists on git"
            )
        } finally {
            newBranchGitter.unlock()
        }
        return conversionResult.summary
    }

    @Get("convert/{targetVersion}")
    fun convertRequestedResources(
        targetVersion: String,
        @Body resources: Set
    ): ConversionResult {
        checkRequestedVersion(targetVersion)
        return convertFromRequest(targetVersion, resources)
    }

    @Get("test")
    fun testApi(): String {
        return "Conversion API is working !"
    }

    private fun checkRequestedVersion(version: String) {
        if (version !in ProjectConstants.ACCEPTED_VERSIONS) {
            throw BadRequestException(
                ErrorCode.VERSION_NOT_ALLOWED,
                "Conversion to specified version: '$version' is not supported"
            )
        }
    }

    private fun checkSourceSchema(schemaName: String, gitterContext: GitterContext) {
        if (!schemaExists(schemaName, gitterContext)) {
            throw BadRequestException(
                ErrorCode.BRANCH_NOT_FOUND,
                "Specified schema doesn't exist"
            )
        }
        if (schemaName == SOURCE_BRANCH) {
            throw BadRequestException(
                ErrorCode.BRANCH_NOT_ALLOWED,
                "Specified schema must not be the same as $SOURCE_BRANCH"
            )
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy