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