2c58645c890aac370e0c4d7e835ab862cb2c9062
[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.profile.upload.K8sProfile
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
25 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
26 import org.slf4j.LoggerFactory
27 import org.springframework.http.HttpMethod.GET
28 import org.springframework.http.HttpMethod.POST
29 import java.nio.file.Path
30
31 class K8sPluginApi(
32     private val username: String,
33     private val password: String,
34     private val baseUrl: String
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(username, password, baseUrl, definition, definitionVersion)
41         try {
42             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
43                 GET.name,
44                 "",
45                 ""
46             )
47             log.debug(result.toString())
48             return result.status in 200..299
49         } catch (e: Exception) {
50             log.error("Caught exception trying to get k8s rb definition")
51             throw BlueprintProcessorException("${e.message}")
52         }
53     }
54
55     fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
56         val rbDefinitionService = K8sDefinitionRestClient(username, password, baseUrl, definition, definitionVersion)
57         try {
58             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
59                 GET.name,
60                 "/profile/$profileName",
61                 ""
62             )
63             log.debug(result.toString())
64             return result.status in 200..299
65         } catch (e: Exception) {
66             log.error("Caught exception trying to get k8s rb profile")
67             throw BlueprintProcessorException("${e.message}")
68         }
69     }
70
71     fun createProfile(definition: String, definitionVersion: String, profile: K8sProfile) {
72         val rbDefinitionService = K8sDefinitionRestClient(username, password, baseUrl, definition, definitionVersion)
73         val profileJsonString: String = objectMapper.writeValueAsString(profile)
74         try {
75             val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
76                 POST.name,
77                 "/profile",
78                 profileJsonString
79             )
80             if (result.status !in 200..299) {
81                 throw Exception(result.body)
82             }
83         } catch (e: Exception) {
84             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
85             throw BlueprintProcessorException("${e.message}")
86         }
87     }
88
89     fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
90         val fileUploadService = K8sUploadFileRestClientService(username, password, baseUrl, definition, definitionVersion)
91         try {
92             val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
93                 "/profile/${profile.profileName}/content",
94                 filePath
95             )
96             if (result.status !in 200..299) {
97                 throw Exception(result.body)
98             }
99         } catch (e: Exception) {
100             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
101             throw BlueprintProcessorException("${e.message}")
102         }
103     }
104 }