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