Bug fixes for config template and config value components
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / k8s-connection-plugin / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / k8s / instance / K8sPluginInstanceApi.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  * Modifications Copyright © 2021 Orange.
5  * Modifications Copyright © 2020 Deutsche Telekom AG.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance
21
22 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
23 import com.fasterxml.jackson.module.kotlin.readValue
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.K8sConnectionPluginConfiguration
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.instance.healthcheck.K8sRbInstanceHealthCheck
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.slf4j.LoggerFactory
30 import org.springframework.http.HttpMethod.DELETE
31 import org.springframework.http.HttpMethod.GET
32 import org.springframework.http.HttpMethod.POST
33 import org.springframework.http.HttpMethod.PUT
34
35 class K8sPluginInstanceApi(
36     private val k8sConfiguration: K8sConnectionPluginConfiguration
37 ) {
38     private val log = LoggerFactory.getLogger(K8sPluginInstanceApi::class.java)!!
39
40     fun getInstanceList(): List<K8sRbInstance>? {
41         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration)
42         try {
43             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
44                 GET.name,
45                 "",
46                 ""
47             )
48             log.debug(result.toString())
49             return if (result.status in 200..299) {
50                 val objectMapper = jacksonObjectMapper()
51                 val parsedObject: ArrayList<K8sRbInstance>? = objectMapper.readValue(result.body)
52                 parsedObject
53             } else if (result.status == 500 && result.body.contains("Did not find any objects with tag"))
54                 null
55             else
56                 throw BlueprintProcessorException(result.body)
57         } catch (e: Exception) {
58             log.error("Caught exception trying to get k8s rb instance")
59             throw BlueprintProcessorException("${e.message}")
60         }
61     }
62
63     fun getInstanceById(instanceId: String): K8sRbInstance? {
64         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
65         try {
66             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
67                 GET.name,
68                 "",
69                 ""
70             )
71             log.debug(result.toString())
72             return if (result.status in 200..299) {
73                 val parsedObject: K8sRbInstance? = JacksonUtils.readValue(result.body, K8sRbInstance::class.java)
74                 parsedObject
75             } else if (result.status == 500 && result.body.contains("Error finding master table"))
76                 null
77             else
78                 throw BlueprintProcessorException(result.body)
79         } catch (e: Exception) {
80             log.error("Caught exception trying to get k8s rb instance")
81             throw BlueprintProcessorException("${e.message}")
82         }
83     }
84
85     fun getInstanceByRequestProperties(
86         rbDefinitionName: String,
87         rbDefinitionVersion: String,
88         rbProfileName: String
89     ): K8sRbInstance? {
90         val instances: List<K8sRbInstance>? = this.getInstanceList()
91         instances?.forEach {
92             if (it.request?.rbName == rbDefinitionName && it.request?.rbVersion == rbDefinitionVersion &&
93                 it.request?.profileName == rbProfileName
94             )
95                 return it
96         }
97         return null
98     }
99
100     fun getInstanceStatus(instanceId: String): K8sRbInstanceStatus? {
101         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
102         try {
103             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
104                 GET.name,
105                 "/status",
106                 ""
107             )
108             log.debug(result.toString())
109             return if (result.status in 200..299) {
110                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
111                     result.body, K8sRbInstanceStatus::class.java
112                 )
113                 parsedObject
114             } else if (result.status == 500 && result.body.contains("Error finding master table"))
115                 null
116             else
117                 throw BlueprintProcessorException(result.body)
118         } catch (e: Exception) {
119             log.error("Caught exception trying to get k8s rb instance")
120             throw BlueprintProcessorException("${e.message}")
121         }
122     }
123
124     fun queryInstanceStatus(
125         instanceId: String,
126         kind: String,
127         apiVersion: String,
128         name: String? = null,
129         labels: Map<String, String>? = null
130     ): K8sRbInstanceStatus? {
131         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
132         try {
133             var path: String = "/query?ApiVersion=$apiVersion&Kind=$kind"
134             if (name != null)
135                 path = path.plus("&Name=$name")
136             if (labels != null && labels.isNotEmpty()) {
137                 path = path.plus("&Labels=")
138                 for ((name, value) in labels)
139                     path = path.plus("$name%3D$value,")
140                 path = path.trimEnd(',')
141             }
142             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
143                 GET.name,
144                 path,
145                 ""
146             )
147             log.debug(result.toString())
148             return if (result.status in 200..299) {
149                 val parsedObject: K8sRbInstanceStatus? = JacksonUtils.readValue(
150                     result.body, K8sRbInstanceStatus::class.java
151                 )
152                 parsedObject
153             } else if (result.status == 500 && result.body.contains("Error finding master table"))
154                 null
155             else
156                 throw BlueprintProcessorException(result.body)
157         } catch (e: Exception) {
158             log.error("Caught exception trying to get k8s rb instance")
159             throw BlueprintProcessorException("${e.message}")
160         }
161     }
162
163     fun getInstanceHealthCheckList(instanceId: String): List<K8sRbInstanceHealthCheck>? {
164         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
165         try {
166             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
167                 GET.name,
168                 "/healthcheck",
169                 ""
170             )
171             log.debug(result.toString())
172             return if (result.status in 200..299) {
173                 val objectMapper = jacksonObjectMapper()
174                 val parsedObject: ArrayList<K8sRbInstanceHealthCheck>? = objectMapper.readValue(result.body)
175                 parsedObject
176             } else if (result.status == 500 && result.body.contains("Error finding master table"))
177                 null
178             else
179                 throw BlueprintProcessorException(result.body)
180         } catch (e: Exception) {
181             log.error("Caught exception trying to get k8s rb instance")
182             throw BlueprintProcessorException("${e.message}")
183         }
184     }
185
186     fun getInstanceHealthCheck(instanceId: String, healthCheckId: String): K8sRbInstanceHealthCheck? {
187         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
188         try {
189             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
190                 GET.name,
191                 "/healthcheck/$healthCheckId",
192                 ""
193             )
194             log.debug(result.toString())
195             return if (result.status in 200..299) {
196                 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
197                     result.body,
198                     K8sRbInstanceHealthCheck::class.java
199                 )
200                 parsedObject
201             } else if (result.status == 500 && result.body.contains("Error finding master table"))
202                 null
203             else
204                 throw BlueprintProcessorException(result.body)
205         } catch (e: Exception) {
206             log.error("Caught exception trying to get k8s rb instance")
207             throw BlueprintProcessorException("${e.message}")
208         }
209     }
210
211     fun startInstanceHealthCheck(instanceId: String): K8sRbInstanceHealthCheck? {
212         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
213         try {
214             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
215                 POST.name,
216                 "/healthcheck",
217                 ""
218             )
219             log.debug(result.toString())
220             return if (result.status in 200..299) {
221                 val parsedObject: K8sRbInstanceHealthCheck? = JacksonUtils.readValue(
222                     result.body,
223                     K8sRbInstanceHealthCheck::class.java
224                 )
225                 parsedObject
226             } else if (result.status == 500 && result.body.contains("Error finding master table"))
227                 null
228             else
229                 throw BlueprintProcessorException(result.body)
230         } catch (e: Exception) {
231             log.error("Caught exception trying to get k8s rb instance")
232             throw BlueprintProcessorException("${e.message}")
233         }
234     }
235
236     fun createConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String): K8sConfigValueResponse? {
237         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
238         try {
239             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
240                 POST.name,
241                 "/config",
242                 JacksonUtils.getJson(configValues)
243             )
244             log.debug(result.toString())
245             return if (result.status in 200..299) {
246                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
247                     result.body, K8sConfigValueResponse::class.java
248                 )
249                 parsedObject
250             } else
251                 throw BlueprintProcessorException(result.body)
252         } catch (e: Exception) {
253             log.error("Caught exception trying to create config instance")
254             throw BlueprintProcessorException("${e.message}")
255         }
256     }
257
258     fun editConfigurationValues(configValues: K8sConfigValueRequest, instanceId: String, configName: String): K8sConfigValueResponse? {
259         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
260         try {
261             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
262                 PUT.name,
263                 "/config/$configName",
264                 JacksonUtils.getJson(configValues)
265             )
266             log.debug(result.toString())
267             return if (result.status in 200..299) {
268                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
269                     result.body, K8sConfigValueResponse::class.java
270                 )
271                 parsedObject
272             } else
273                 throw BlueprintProcessorException(result.body)
274         } catch (e: Exception) {
275             log.error("Caught exception trying to edit config instance")
276             throw BlueprintProcessorException("${e.message}")
277         }
278     }
279
280     fun hasConfigurationValues(instanceId: String, configName: String): Boolean {
281         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
282         try {
283             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
284                 GET.name,
285                 "/config/$configName",
286                 ""
287             )
288             log.debug(result.toString())
289             return result.status in 200..299
290         } catch (e: Exception) {
291             log.error("Caught exception trying to get k8s rb instance")
292             throw BlueprintProcessorException("${e.message}")
293         }
294     }
295
296     fun getConfigurationValues(instanceId: String, configName: String): K8sConfigValueResponse? {
297         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
298         try {
299             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
300                 GET.name,
301                 "/config/$configName",
302                 ""
303             )
304             log.debug(result.toString())
305             return if (result.status in 200..299) {
306                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
307                     result.body, K8sConfigValueResponse::class.java
308                 )
309                 parsedObject
310             } else
311                 throw BlueprintProcessorException(result.body)
312         } catch (e: Exception) {
313             log.error("Caught exception trying to get config instance")
314             throw BlueprintProcessorException("${e.message}")
315         }
316     }
317
318     fun deleteConfigurationValues(instanceId: String, configName: String) {
319         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
320         try {
321             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
322                 DELETE.name,
323                 "/config/$configName",
324                 ""
325             )
326             log.debug(result.toString())
327             if (result.status !in 200..299)
328                 throw BlueprintProcessorException(result.body)
329         } catch (e: Exception) {
330             log.error("Caught exception trying to delete config instance")
331             throw BlueprintProcessorException("${e.message}")
332         }
333     }
334
335     fun rollbackConfigurationValues(instanceId: String): K8sConfigValueResponse? {
336         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
337         try {
338             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
339                 POST.name,
340                 "/rollback",
341                 ""
342             )
343             log.debug(result.toString())
344             return if (result.status in 200..299) {
345                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
346                     result.body, K8sConfigValueResponse::class.java
347                 )
348                 parsedObject
349             } else
350                 throw BlueprintProcessorException(result.body)
351         } catch (e: Exception) {
352             log.error("Caught exception trying to get k8s rb instance")
353             throw BlueprintProcessorException("${e.message}")
354         }
355     }
356
357     fun tagConfigurationValues(instanceId: String): K8sConfigValueResponse? {
358         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
359         try {
360             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
361                 POST.name,
362                 "/tagit",
363                 ""
364             )
365             log.debug(result.toString())
366             return if (result.status in 200..299) {
367                 val parsedObject: K8sConfigValueResponse? = JacksonUtils.readValue(
368                     result.body, K8sConfigValueResponse::class.java
369                 )
370                 parsedObject
371             } else
372                 throw BlueprintProcessorException(result.body)
373         } catch (e: Exception) {
374             log.error("Caught exception trying to get k8s rb instance")
375             throw BlueprintProcessorException("${e.message}")
376         }
377     }
378
379     fun deleteInstanceHealthCheck(instanceId: String, healthCheckId: String) {
380         val rbInstanceService = K8sRbInstanceRestClient(k8sConfiguration, instanceId)
381         try {
382             val result: BlueprintWebClientService.WebClientResponse<String> = rbInstanceService.exchangeResource(
383                 DELETE.name,
384                 "/healthcheck/$healthCheckId",
385                 ""
386             )
387             log.debug(result.toString())
388             if (result.status !in 200..299)
389                 throw BlueprintProcessorException(result.body)
390         } catch (e: Exception) {
391             log.error("Caught exception trying to get k8s rb instance")
392             throw BlueprintProcessorException("${e.message}")
393         }
394     }
395 }