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

pl.wrzasq.cform.macro.apigateway.ApiModel.kt Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
/**
 * This file is part of the pl.wrzasq.cform.
 *
 * @license http://mit-license.org/ The MIT license
 * @copyright 2021 © by Rafał Wrzeszcz - Wrzasq.pl.
 */

package pl.wrzasq.cform.macro.apigateway

import pl.wrzasq.cform.macro.model.ResourceDefinition
import pl.wrzasq.cform.macro.template.asMap
import pl.wrzasq.cform.macro.template.popProperty

/**
 * API Gateway model definition.
 *
 * @param api Container API.
 * @param id Resource logical ID.
 * @param input Resource specification.
 */
class ApiModel(
    private val api: ApiGateway,
    val id: String,
    input: Map
) : ApiTemplateResource {
    private val properties: Map

    init {
        val computed = mutableMapOf()
        val leftover = input.toMutableMap().apply {
            putIfAbsent("ContentType", "application/json")
        }
            .popProperty("Schema", {
                val schema = asMap(it).toMutableMap()
                if ("\$schema" !in schema) {
                    schema["\$schema"] = "http://json-schema.org/draft-04/schema#"
                }
                if ("title" !in schema) {
                    schema["title"] = id
                }

                computed["Schema"] = schema
            })

        properties = leftover + computed
    }

    override val resourceId
        get() = "${api.resourceId}Model$id"

    /**
     * Builds resource definition.
     *
     * @return Resource model.
     */
    fun generateResource() = ResourceDefinition(
        id = resourceId,
        type = "AWS::ApiGateway::Model",
        properties = properties + mapOf("RestApiId" to api.ref())
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy