dab977db007e13e9bf5ab8b9e09350256efd7609
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / k8s-profile-upload / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / k8s / profile / upload / K8sPluginApi.kt
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.profile.upload
21
22 import org.springframework.http.HttpMethod
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
26 import java.nio.file.Path
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.slf4j.LoggerFactory
29
30 class K8sPluginApi(
31     val username: String,
32     val password: String,
33     val baseUrl: String,
34     val definition: String?,
35     val definitionVersion: String?
36 ) {
37     private val service: K8sUploadFileRestClientService // BasicAuthRestClientService
38     private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
39
40     init {
41         var mapOfHeaders = hashMapOf<String, String>()
42         mapOfHeaders.put("Accept", "application/json")
43         mapOfHeaders.put("Content-Type", "application/json")
44         mapOfHeaders.put("cache-control", " no-cache")
45         mapOfHeaders.put("Accept", "application/json")
46         var basicAuthRestClientProperties: BasicAuthRestClientProperties = BasicAuthRestClientProperties()
47         basicAuthRestClientProperties.username = username
48         basicAuthRestClientProperties.password = password
49         basicAuthRestClientProperties.url = "$baseUrl/v1/rb/definition/$definition/$definitionVersion"
50         basicAuthRestClientProperties.additionalHeaders = mapOfHeaders
51
52         this.service = K8sUploadFileRestClientService(basicAuthRestClientProperties)
53     }
54
55     fun hasDefinition(): Boolean {
56         try {
57             val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource(HttpMethod.GET.name, "", "")
58             log.debug(result.toString())
59             return result.status in 200..299
60         } catch (e: Exception) {
61             log.error("Caught exception trying to get k8s rb definition")
62             throw BluePrintProcessorException("${e.message}")
63         }
64     }
65
66     fun hasProfile(profileName: String): Boolean {
67         try {
68             val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource(
69                 HttpMethod.GET.name,
70                 "/profile/$profileName",
71                 ""
72             )
73             log.debug(result.toString())
74             return result.status in 200..299
75         } catch (e: Exception) {
76             log.error("Caught exception trying to get k8s rb profile")
77             throw BluePrintProcessorException("${e.message}")
78         }
79     }
80
81     fun createProfile(profile: K8sProfile) {
82         val objectMapper = ObjectMapper()
83         val profileJsonString: String = objectMapper.writeValueAsString(profile)
84         try {
85             val result: BlueprintWebClientService.WebClientResponse<String> = service.exchangeResource(
86                 HttpMethod.POST.name,
87                 "/profile",
88                 profileJsonString
89             )
90             if (result.status !in 200..299) {
91                 throw Exception(result.body)
92             }
93         } catch (e: Exception) {
94             log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
95             throw BluePrintProcessorException("${e.message}")
96         }
97     }
98
99     fun uploadProfileContent(profile: K8sProfile, filePath: Path) {
100         try {
101             val result: BlueprintWebClientService.WebClientResponse<String> = service.uploadBinaryFile(
102                 "/profile/${profile.profileName}/content",
103                 filePath
104             )
105             if (result.status !in 200..299) {
106                 throw Exception(result.body)
107             }
108         } catch (e: Exception) {
109             log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
110             throw BluePrintProcessorException("${e.message}")
111         }
112     }
113 }