211bc5abe542d3bb4753fa2b811de1b6354369a1
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  * Modifications Copyright © 2020 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
21
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.K8sDefinitionRestClient
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.K8sUploadFileRestClientService
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s.definition.profile.K8sProfile
26 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
28 import org.slf4j.LoggerFactory
29 import org.springframework.http.HttpMethod.GET
30 import org.springframework.http.HttpMethod.POST
31 import java.nio.file.Path
32
33 class K8sPluginApi(
34     private val k8sConfiguration: K8sConnectionPluginConfiguration
35 ) {
36     private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
37     private val objectMapper = ObjectMapper()
38
39     fun hasDefinition(definition: String, definitionVersion: String): Boolean {
40         val rbDefinitionService = K8sDefinitionRestClient(
41             k8sConfiguration,
42             definition,
43             definitionVersion
44         )
45         try {
46             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
47                 GET.name,
48                 "",
49                 ""
50             )
51             log.debug(result.toString())
52             return result.status in 200..299
53         } catch (e: Exception) {
54             log.error("Caught exception trying to get k8s rb definition")
55             throw BlueprintProcessorException("${e.message}")
56         }
57     }
58
59     fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
60         val rbDefinitionService = K8sDefinitionRestClient(
61             k8sConfiguration,
62             definition,
63             definitionVersion
64         )
65         try {
66             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
67                 GET.name,
68                 "/profile/$profileName",
69                 ""
70             )
71             log.debug(result.toString())
72             return result.status in 200..299
73         } catch (e: Exception) {
74             log.error("Caught exception trying to get k8s rb profile")
75             throw BlueprintProcessorException("${e.message}")
76         }
77     }
78
79     fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
80         val rbDefinitionService = K8sDefinitionRestClient(
81             k8sConfiguration,
82             definition,
83             definitionVersion
84         )
85         val profileJsonString: String = objectMapper.writeValueAsString(profile)
86         try {
87             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
88                 POST.name,
89                 "/profile",
90                 profileJsonString
91             )
92             if (result.status !in 200..299) {
93                 throw Exception(result.body)
94             }
95         } catch (e: Exception) {
96             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
97             throw BlueprintProcessorException("${e.message}")
98         }
99     }
100
101     fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
102         val fileUploadService = K8sUploadFileRestClientService(
103             k8sConfiguration,
104             definition,
105             definitionVersion
106         )
107         try {
108             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
109                 "/profile/${profile.profileName}/content",
110                 filePath
111             )
112             if (result.status !in 200..299) {
113                 throw Exception(result.body)
114             }
115         } catch (e: Exception) {
116             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
117             throw BlueprintProcessorException("${e.message}")
118         }
119     }
120 }