All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.swisscom.cloud.sb.client.ServiceBrokerClient.groovy Maven / Gradle / Ivy

package com.swisscom.cloud.sb.client

import com.swisscom.cloud.sb.client.model.LastOperationResponse
import com.swisscom.cloud.sb.model.usage.ServiceUsage
import groovy.transform.CompileStatic
import org.apache.commons.codec.binary.Base64
import org.springframework.cloud.servicebroker.model.*
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.ResponseEntity
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory
import org.springframework.web.client.RestTemplate

import java.nio.charset.Charset

@CompileStatic
class ServiceBrokerClient implements IServiceBrokerClient {
    protected final RestTemplate restTemplate
    protected final String baseUrl
    private final String username
    private final String password

    ServiceBrokerClient(RestTemplate restTemplate, String baseUrl, String username, String password) {
        this.restTemplate = restTemplate
        this.baseUrl = baseUrl
        this.username = username
        this.password = password
    }

    ServiceBrokerClient(String baseUrl, String username, String password) {
        this(new RestTemplate(new HttpComponentsClientHttpRequestFactory()), baseUrl, username, password)
    }

    @Override
    ResponseEntity getCatalog() {
        return restTemplate.exchange(appendPath('/v2/catalog'), HttpMethod.GET, new HttpEntity(createSimpleAuthHeaders(username, password)), Catalog.class)
    }

    @Override
    ResponseEntity getServiceInstanceLastOperation(String serviceInstanceId) {
        return restTemplate.exchange(appendPath("/v2/service_instances/{serviceInstanceId}/last_operation?service_id={serviceId}&plan_id={planId}"),
                HttpMethod.GET, new HttpEntity(createSimpleAuthHeaders(username, password)), LastOperationResponse.class,
                serviceInstanceId, "serviceId", "planId")
    }

    @Override
    ResponseEntity createServiceInstance(CreateServiceInstanceRequest request) {
        return restTemplate.exchange(appendPath("/v2/service_instances/{serviceInstanceId}?accepts_incomplete={asyncAccepted}"),
                HttpMethod.PUT, new HttpEntity(request, createSimpleAuthHeaders(username, password)),
                CreateServiceInstanceResponse.class, request.serviceInstanceId, request.asyncAccepted)
    }

    @Override
    ResponseEntity updateServiceInstance(UpdateServiceInstanceRequest request) {
        return restTemplate.exchange(appendPath("/v2/service_instances/{serviceInstanceId}?accepts_incomplete={asyncAccepted}"),
                HttpMethod.PATCH, new HttpEntity(request, createSimpleAuthHeaders(username, password)),
                UpdateServiceInstanceResponse.class, request.serviceInstanceId, request.asyncAccepted)
    }

    @Override
    ResponseEntity deleteServiceInstance(com.swisscom.cloud.sb.client.model.DeleteServiceInstanceRequest request) {
        return restTemplate.exchange(appendPath("/v2/service_instances/{serviceInstanceId}?service_id={serviceId}&plan_id={planId}&accepts_incomplete={asyncAccepted}"),
                HttpMethod.DELETE, new HttpEntity(createSimpleAuthHeaders(username, password)),
                Void.class, request.serviceInstanceId, request.serviceId, request.planId, request.asyncAccepted)
    }

    @Override
    ResponseEntity createServiceInstanceBinding(CreateServiceInstanceBindingRequest request) {
        return restTemplate.exchange(appendPath("/v2/service_instances/{serviceInstanceId}/service_bindings/{bindingId}"),
                HttpMethod.PUT, new HttpEntity(request, createSimpleAuthHeaders(username, password)),
                CreateServiceInstanceAppBindingResponse.class, request.serviceInstanceId, request.bindingId)
    }

    @Override
    ResponseEntity deleteServiceInstanceBinding(com.swisscom.cloud.sb.client.model.DeleteServiceInstanceBindingRequest request) {
        return restTemplate.exchange(appendPath("/v2/service_instances/{serviceInstanceId}/service_bindings/{bindingId}?service_id={serviceId}&plan_id={planId}"),
                HttpMethod.DELETE, new HttpEntity(request, createSimpleAuthHeaders(username, password)),
                Void.class, request.serviceInstanceId, request.bindingId, request.serviceId, request.planId)
    }

    static HttpHeaders createSimpleAuthHeaders(String username, String password) {
        def result = new HttpHeaders()
        if (password) {
            String auth = username + ":" + password
            byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")))
            String authHeader = "Basic " + new String(encodedAuth)
            result.set("Authorization", authHeader)
        }
        return result
    }

    protected String appendPath(String path) {
        return baseUrl + path
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy