com.sap.cloud.spring.boot.autoconfig.services.ServiceManagerAutoConfiguration Maven / Gradle / Ivy
/******************************************************************************
* © 2020 SAP SE or an SAP affiliate company. All rights reserved. *
******************************************************************************/
package com.sap.cloud.spring.boot.autoconfig.services;
import com.sap.cds.mtx.impl.XsuaaParams;
import com.sap.cloud.environment.servicebinding.api.DefaultServiceBinding;
import com.sap.cloud.mt.subscription.PollingParameters;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import com.sap.cloud.mt.subscription.ServiceManager;
import com.sap.cloud.mt.subscription.ServiceSpecification;
import com.sap.cloud.mt.subscription.exceptions.InternalError;
import com.sap.cloud.mt.tools.api.ResilienceConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import static com.sap.cds.mtx.impl.XsuaaParams.CERTIFICATE;
import static com.sap.cds.mtx.impl.XsuaaParams.CERTURL;
import static com.sap.cds.mtx.impl.XsuaaParams.CLIENTID;
import static com.sap.cds.mtx.impl.XsuaaParams.CLIENT_SECRET;
import static com.sap.cds.mtx.impl.XsuaaParams.KEY;
import static com.sap.cds.mtx.impl.XsuaaParams.URL;
import static com.sap.cds.mtx.impl.XsuaaParams.XSAPPNAME;
@EnableConfigurationProperties
@Configuration("ComSapMtInstanceManagerForServiceManagerConfig")
@ConfigurationProperties(ServiceManagerAutoConfiguration.COM_SAP_CLOUD_SERVICE_MANAGER)
@ConditionalOnProperty(name = "com.sap.cloud.service-manager.base-url")
public class ServiceManagerAutoConfiguration {
private static final String HANA_OFFERING_NAME = "hana";
private static final String HANA_HDI_PLAN_NAME = "hdi-shared";
private static final Logger logger = LoggerFactory.getLogger(ServiceManagerAutoConfiguration.class);
public static final String COM_SAP_CLOUD_SERVICE_MANAGER = "com.sap.cloud.service-manager";
public static final String SERVICE_MANAGER = "service-manager";
public static final String SM_URL = "sm_url";
private String instanceName;
private String instanceGuid;
private String baseUrl;
private String tokenUrl;
private String clientId;
private String clientSecret;
private String certificate;
private String certUrl;
private String xsappname;
private String key;
private final Map credentials = new HashMap<>();
private PollingParameters polling = PollingParameters.Builder.create().interval(Duration.ofSeconds(10)).timeout(Duration.ofMinutes(20)).build();
private ResilienceConfig resilienceConfig = ResilienceConfig.builder().baseWaitTime(Duration.ofMillis(500)).numOfRetries(3).build();
@Bean("ComSapMtInstanceManagerClientForServiceManager")
public ServiceManager client() {
try {
logger.debug("Create service manager");
ServiceSpecification serviceSpecification = ServiceSpecification.Builder.create()
.resilienceConfig(resilienceConfig)
.polling(polling)
.build();
return new ServiceManager(createBinding(), serviceSpecification, HANA_OFFERING_NAME, HANA_HDI_PLAN_NAME);
} catch (InternalError e) {
throw new RuntimeException(e);
}
}
public ResilienceConfig getResilienceConfig() {
return resilienceConfig;
}
public void setResilienceConfig(ResilienceConfig resilienceConfig) {
this.resilienceConfig = resilienceConfig;
}
public PollingParameters getPolling() {
return polling;
}
public void setPolling(PollingParameters polling) {
this.polling = polling;
}
public String getInstanceName() {
return instanceName;
}
public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}
public String getBaseUrl() {
return baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
public String getTokenUrl() {
return tokenUrl;
}
public void setTokenUrl(String tokenUrl) {
this.tokenUrl = tokenUrl;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public String getInstanceGuid() {
return instanceGuid;
}
public void setInstanceGuid(String instanceGuid) {
this.instanceGuid = instanceGuid;
}
public String getCertificate() {
return certificate;
}
public void setCertificate(String certificate) {
this.certificate = certificate;
}
public String getCertUrl() {
return certUrl;
}
public void setCertUrl(String certUrl) {
this.certUrl = certUrl;
}
public String getXsappname() {
return xsappname;
}
public void setXsappname(String xsappname) {
this.xsappname = xsappname;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Map getCredentials() {
return credentials;
}
public void setCredentials(Map credentials) {
if (!CollectionUtils.isEmpty(credentials)) {
this.credentials.putAll(credentials);
}
}
private XsuaaParams xsuaaParamsForSM() {
transferIntoCredentials();
return new XsuaaParams(credentials);
}
private void transferIntoCredentials() {
if (CollectionUtils.isEmpty(credentials)) {
if (!StringUtils.isBlank(tokenUrl)) {
credentials.put(URL, tokenUrl);
}
if (!StringUtils.isBlank(clientId)) {
credentials.put(CLIENTID, clientId);
}
if (!StringUtils.isBlank(clientSecret)) {
credentials.put(CLIENT_SECRET, clientSecret);
}
if (!StringUtils.isBlank(certificate)) {
credentials.put(CERTIFICATE, certificate);
}
if (!StringUtils.isBlank(certUrl)) {
credentials.put(CERTURL, certUrl);
}
if (!StringUtils.isBlank(xsappname)) {
credentials.put(XSAPPNAME, xsappname);
}
if (!StringUtils.isBlank(key)) {
credentials.put(KEY, key);
}
if (!StringUtils.isBlank(baseUrl)) {
credentials.put(SM_URL, baseUrl);
}
}
}
private ServiceBinding createBinding() {
transferIntoCredentials();
var parameters = new HashMap();
return DefaultServiceBinding.builder()
.copy(parameters)
.withServiceName(SERVICE_MANAGER)
.withName(instanceName)
.withCredentials(credentials).build();
}
}