This sample describes the steps and configurations to build and deploy microservice-based extensions on SAP BTP, Kyma runtime using SAP Cloud SDK for Java.
The microservice makes API calls to an S/4 System to perform various read/write operations based on the extension logic. The microservice itself can be triggered with an event or an API call. In this example, we will trigger it with an API call by exposing it through Microgateway in Kyma runtime (APIRules).
-
SAP BTP, Kyma runtime instance
-
kubectl configured to use the
KUBECONFIG
file downloaded from the Kyma runtime -
Java 8+
-
Refer to this blog post to learn how to set up API access for an S/4 System from Kyma runtime.
Setting up API access allows you to build a microservice that will call the S/4 System using SAP Cloud SDK for Java.
-
Generate the maven project.
mvn archetype:generate "-DarchetypeGroupId=com.sap.cloud.sdk.archetypes" "-DarchetypeArtifactId=scp-cf-spring" "-DarchetypeVersion=RELEASE"
-
Generate a typed OData client for Java. For the purpose of this sample, generate the code for SAP Marketing Cloud Campaign OData APIs using the metadata file.
-
Implement the code to make API calls using the generated services:
@RestController @RequestMapping("/campaigns") public class CampaignController { private final DefaultErpHttpDestination destination; private final DefaultCampaignsService campaignsService; @Autowired public CampaignController(ApplicationConfig applicationConfig) { this.destination = DestinationAccessor .getDestination(applicationConfig.getTenantName()) .asHttp() .decorate(DefaultErpHttpDestination::new); this.campaignsService = new DefaultCampaignsService() .withServicePath(applicationConfig.getServicePath()); } @RequestMapping(method = RequestMethod.GET) public List<Campaign> getCampaigns() { return this.campaignsService .getAllCampaign() .top(2) .select( Campaign.CAMPAIGN_ID, Campaign.NODE_ID, Campaign.CATEGORY_NAME ) .executeRequest(this.destination); } }
-
Build and push the image
DOCKER_ACCOUNT={your-docker-repo} make push-image
-
Create a ServiceInstance of the
api-access
plan type for your S/4 System. -
Deploy the application on the Kyma runtime.
-
Since the Cloud SDK relies on an environment variable in the form of
destinations:[{ARRAY OF DESTINATIONS}]
, use a Kubernetes Deployment to reference pre-defined variables, such as User, Password and url, to create such a variable. -
These predefined variables will be injected automatically when performing a service binding with this Deployment.
env: - name: destinations value: '[{name: "$(APPLICATION_TENANT_NAME)", url: "$(URL)", username: "$(User)", password: "$(Password)"}]'
For reference, see the full Deployment definition.
# only required once to enable istio sidecar. Ignore if done already kubectl label namespaces {NAMESPACE-TO-DEPLOY} istio-injection=enabled kubectl -n {NAMESPACE-TO-DEPLOY} apply -f k8s/deployment.yaml
-
-
Bind the Deployment with the ServiceInstance. You can either reuse the existing credentials or create new ones.
-
Verify that the Deployment is running by checking the logs:
kubectl -n {NAMESPACE-TO-DEPLOY} logs -l app=sample-cloudsdk-java -c sample-cloudsdk-java
-
Expose the application using an APIRule:
kubectl -n {NAMESPACE-TO-DEPLOY} apply -f k8s/api-rule.yaml
Call the API to get two top campaigns at this address:
<https://sample-cloudsdk-java.{CLUSTER-DOMAIN}/campaigns>