aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/k8s-connection-plugin/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/k8s/query/K8sPluginQueryApi.kt
blob: 36ebe1133a363c59c968e74b61039a145af9c656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.query

import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.slf4j.LoggerFactory
import org.springframework.http.HttpMethod.GET

public class K8sPluginQueryApi(
    private val k8sConfiguration: K8sConnectionPluginConfiguration
) {
    private val log = LoggerFactory.getLogger(K8sPluginQueryApi::class.java)!!

    fun queryK8sResources(
        cloudRegion: String,
        kind: String,
        apiVersion: String,
        name: String? = null,
        namespace: String? = null,
        labels: Map<String, String>? = null
    ): K8sResourceStatus? {
        val rbQueryService = K8sQueryRestClient(k8sConfiguration)
        try {
            var path: String = "?CloudRegion=$cloudRegion&ApiVersion=$apiVersion&Kind=$kind"
            if (name != null)
                path = path.plus("&Name=$name")
            if (namespace != null)
                path = path.plus("&Namespace=$namespace")
            if (labels != null && labels.isNotEmpty()) {
                path = path.plus("&Labels=")
                for ((name, value) in labels)
                    path = path.plus("$name%3D$value,")
                path = path.trimEnd(',')
            }
            val result: BlueprintWebClientService.WebClientResponse<String> = rbQueryService.exchangeResource(
                GET.name,
                path,
                ""
            )
            log.debug(result.toString())
            return if (result.status in 200..299) {
                val parsedObject: K8sResourceStatus? = JacksonUtils.readValue(
                    result.body, K8sResourceStatus::class.java
                )
                parsedObject
            } else if (result.status == 500 && result.body.contains("Error finding master table"))
                null
            else
                throw BluePrintProcessorException(result.body)
        } catch (e: Exception) {
            log.error("Caught exception trying to get k8s rb instance")
            throw BluePrintProcessorException("${e.message}")
        }
    }
}