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.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org.onap.ccsdk.cds.blueprintsprocessor.functions.k8s
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
32 private val username: String,
33 private val password: String,
34 private val baseUrl: String
36 private val log = LoggerFactory.getLogger(K8sPluginApi::class.java)!!
37 private val objectMapper = ObjectMapper()
39 fun hasDefinition(definition: String, definitionVersion: String): Boolean {
40 val rbDefinitionService = K8sDefinitionRestClient(username, password, baseUrl, definition, definitionVersion)
42 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
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}")
55 fun hasProfile(definition: String, definitionVersion: String, profileName: String): Boolean {
56 val rbDefinitionService = K8sDefinitionRestClient(username, password, baseUrl, definition, definitionVersion)
58 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
60 "/profile/$profileName",
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}")
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)
75 val result: BlueprintWebClientService.WebClientResponse<String> = rbDefinitionService.exchangeResource(
80 if (result.status !in 200..299) {
81 throw Exception(result.body)
83 } catch (e: Exception) {
84 log.error("Caught exception trying to create k8s rb profile ${profile.profileName}")
85 throw BlueprintProcessorException("${e.message}")
89 fun uploadProfileContent(definition: String, definitionVersion: String, profile: K8sProfile, filePath: Path) {
90 val fileUploadService = K8sUploadFileRestClientService(username, password, baseUrl, definition, definitionVersion)
92 val result: BlueprintWebClientService.WebClientResponse<String> = fileUploadService.uploadBinaryFile(
93 "/profile/${profile.profileName}/content",
96 if (result.status !in 200..299) {
97 throw Exception(result.body)
99 } catch (e: Exception) {
100 log.error("Caught exception trying to upload k8s rb profile ${profile.profileName}")
101 throw BlueprintProcessorException("${e.message}")