11package com .sap .ai .sdk .app .controllers ;
22
3+ import static com .sap .ai .sdk .core .model .AiDeploymentTargetStatus .STOPPED ;
4+
35import com .sap .ai .sdk .core .client .ConfigurationApi ;
46import com .sap .ai .sdk .core .client .DeploymentApi ;
57import com .sap .ai .sdk .core .model .AiConfigurationBaseData ;
1012import com .sap .ai .sdk .core .model .AiDeploymentDeletionResponse ;
1113import com .sap .ai .sdk .core .model .AiDeploymentList ;
1214import com .sap .ai .sdk .core .model .AiDeploymentModificationRequest ;
13- import com .sap .ai .sdk .core .model .AiDeploymentModificationResponse ;
14- import com .sap .ai .sdk .core .model .AiDeploymentTargetStatus ;
1515import com .sap .ai .sdk .core .model .AiParameterArgumentBinding ;
1616import com .sap .ai .sdk .foundationmodels .openai .OpenAiModel ;
1717import java .util .List ;
18+ import java .util .stream .Collectors ;
1819import javax .annotation .Nonnull ;
1920import javax .annotation .Nullable ;
2021import lombok .extern .slf4j .Slf4j ;
22+ import org .springframework .http .ResponseEntity ;
2123import org .springframework .web .bind .annotation .GetMapping ;
2224import org .springframework .web .bind .annotation .PathVariable ;
25+ import org .springframework .web .bind .annotation .RequestHeader ;
2326import org .springframework .web .bind .annotation .RequestMapping ;
2427import org .springframework .web .bind .annotation .RestController ;
2528
2629/** Endpoints for AI Core AiDeployment operations */
2730@ Slf4j
2831@ RestController
32+ @ SuppressWarnings ("unused" )
2933@ RequestMapping ("/deployments" )
3034class DeploymentController {
3135
3236 private static final DeploymentApi CLIENT = new DeploymentApi ();
3337 private static final String RESOURCE_GROUP = "default" ;
3438
3539 /**
36- * Create and delete a deployment with the Java specific configuration ID
40+ * Create and delete a deployment with the Java specific configuration ID.
3741 *
3842 * @param configId The configuration id.
3943 * @return the deployment deletion response
4044 */
41- @ GetMapping ("/by-config/{id}/createDelete" )
4245 @ Nullable
43- AiDeploymentDeletionResponse createAndDeleteDeploymentByConfigId (
44- @ Nonnull @ PathVariable ("id" ) final String configId ) {
46+ AiDeploymentDeletionResponse createAndDeleteDeploymentByConfigId (final String configId ) {
4547 final var deployment =
4648 CLIENT .create (
4749 RESOURCE_GROUP , AiDeploymentCreationRequest .create ().configurationId (configId ));
@@ -52,62 +54,109 @@ AiDeploymentDeletionResponse createAndDeleteDeploymentByConfigId(
5254 }
5355
5456 /**
55- * Stop all deployments with the Java specific configuration ID
57+ * Create and delete a deployment with the Java specific configuration ID.
58+ *
59+ * @param configId The configuration id.
60+ * @param accept The accept header.
61+ * @return a response entity with a string representation of the deployment creation response
62+ */
63+ @ GetMapping ("/by-config/{id}/createDelete" )
64+ ResponseEntity <Object > createAndDeleteDeploymentByConfigId (
65+ @ Nonnull @ PathVariable ("id" ) final String configId ,
66+ @ Nullable @ RequestHeader (value = "accept" , required = false ) final String accept ) {
67+ final var response = createAndDeleteDeploymentByConfigId (configId );
68+ if ("application/json" .equals (accept )) {
69+ return ResponseEntity .ok ().body (response );
70+ }
71+ return ResponseEntity .ok ("Deployment created and will be deleted." );
72+ }
73+
74+ /**
75+ * Stop all deployments with the Java specific configuration ID.
5676 *
5777 * <p>Only RUNNING deployments can be STOPPED
5878 *
5979 * @param configId The configuration id.
60- * @return the deployment modification response
80+ * @param accept The accept header.
81+ * @return a response entity with a string representation of the deployment modification response
6182 */
6283 @ GetMapping ("/by-config/{id}/stop" )
6384 @ Nonnull
64- @ SuppressWarnings ( "unused" ) // debug method that doesn't need to be tested
65- List < AiDeploymentModificationResponse > stopByConfigId (
66- @ Nonnull @ PathVariable ( "id" ) final String configId ) {
85+ ResponseEntity < Object > stopByConfigId (
86+ @ Nonnull @ PathVariable ( "id" ) final String configId ,
87+ @ Nullable @ RequestHeader ( value = "accept" , required = false ) final String accept ) {
6788 final List <AiDeployment > myDeployments = getAllByConfigId (configId );
6889 log .info ("Found {} deployments to STOP" , myDeployments .size ());
6990
7091 // STOP my deployments
71- return myDeployments .stream ()
72- .map (
73- deployment ->
74- CLIENT .modify (
75- RESOURCE_GROUP ,
76- deployment .getId (),
77- AiDeploymentModificationRequest .create ()
78- .targetStatus (AiDeploymentTargetStatus .STOPPED )))
79- .toList ();
92+ final var modificationRequest = AiDeploymentModificationRequest .create ().targetStatus (STOPPED );
93+ final var stoppedDeployments =
94+ myDeployments .stream ()
95+ .map (AiDeployment ::getId )
96+ .map (id -> CLIENT .modify (RESOURCE_GROUP , id , modificationRequest ))
97+ .toList ();
98+
99+ if ("application/json" .equals (accept )) {
100+ return ResponseEntity .ok ().body (stoppedDeployments );
101+ }
102+ return ResponseEntity .ok ("Deployments under the given config ID stopped." );
80103 }
81104
82105 /**
83- * Delete all deployments with the Java specific configuration ID
106+ * Delete all deployments with the Java specific configuration ID.
84107 *
85108 * <p>Only UNKNOWN and STOPPED deployments can be DELETED
86109 *
87110 * @param configId The configuration id.
88- * @return the deployment deletion response
111+ * @param accept The accept header.
112+ * @return a response entity with a string representation of the deployment deletion response
89113 */
90114 @ GetMapping ("/by-config/{id}/delete" )
91115 @ Nonnull
92- @ SuppressWarnings ( "unused" ) // debug method that doesn't need to be tested
93- List < AiDeploymentDeletionResponse > deleteByConfigId (
94- @ Nonnull @ PathVariable ( "id" ) final String configId ) {
116+ ResponseEntity < Object > deleteByConfigId (
117+ @ Nonnull @ PathVariable ( "id" ) final String configId ,
118+ @ Nullable @ RequestHeader ( value = "accept" , required = false ) final String accept ) {
95119 final List <AiDeployment > myDeployments = getAllByConfigId (configId );
96120 log .info ("Found {} deployments to DELETE" , myDeployments .size ());
97121
98122 // DELETE my deployments
99- return myDeployments .stream ()
100- .map (deployment -> CLIENT .delete (RESOURCE_GROUP , deployment .getId ()))
101- .toList ();
123+ final var responseList =
124+ myDeployments .stream ()
125+ .map (deployment -> CLIENT .delete (RESOURCE_GROUP , deployment .getId ()))
126+ .toList ();
127+ if ("application/json" .equals (accept )) {
128+ return ResponseEntity .ok ().body (responseList );
129+ }
130+ return ResponseEntity .ok ("Deployments under the given config ID deleted." );
102131 }
103132
104133 /**
105- * Get all deployments with the Java specific configuration ID
134+ * Get all deployments with the Java specific configuration ID.
106135 *
107136 * @param configId The configuration id.
108- * @return the Java specific deployments
137+ * @param accept The accept header.
138+ * @return a response entity with a string representation of the Java specific deployments
109139 */
110140 @ GetMapping ("/by-config/{id}/getAll" )
141+ ResponseEntity <Object > getAllByConfigId (
142+ @ Nonnull @ PathVariable ("id" ) final String configId ,
143+ @ Nullable @ RequestHeader (value = "accept" , required = false ) final String accept ) {
144+ final var deployments = getAllByConfigId (configId );
145+ if ("application/json" .equals (accept )) {
146+ return ResponseEntity .ok ().body (deployments );
147+ }
148+ final var items =
149+ deployments .stream ().map (AiDeployment ::getId ).collect (Collectors .joining (", " ));
150+ return ResponseEntity .ok (
151+ "The following Java-specific deployments are available: %s." .formatted (items ));
152+ }
153+
154+ /**
155+ * Get all deployments with the Java specific configuration ID.
156+ *
157+ * @param configId The configuration id.
158+ * @return the Java specific deployments
159+ */
111160 @ Nonnull
112161 List <AiDeployment > getAllByConfigId (@ Nonnull @ PathVariable ("id" ) final String configId ) {
113162 final AiDeploymentList deploymentList = CLIENT .query (RESOURCE_GROUP );
@@ -120,9 +169,31 @@ List<AiDeployment> getAllByConfigId(@Nonnull @PathVariable("id") final String co
120169 /**
121170 * Get all deployments, including non-Java specific deployments
122171 *
123- * @return the Java specific deployments
172+ * @param accept The accept header.
173+ * @return a response entity with a string representation of the Java specific deployments
124174 */
125175 @ GetMapping ("/getAll" )
176+ @ Nonnull
177+ ResponseEntity <Object > getAll (
178+ @ Nullable @ RequestHeader (value = "accept" , required = false ) final String accept ) {
179+ final var deployments = getAll ();
180+ if ("application/json" .equals (accept )) {
181+ return ResponseEntity .ok ().body (deployments );
182+ }
183+ final var items =
184+ deployments != null
185+ ? deployments .getResources ().stream ()
186+ .map (AiDeployment ::getId )
187+ .collect (Collectors .joining (", " ))
188+ : "" ;
189+ return ResponseEntity .ok ("The following deployments are available: %s." .formatted (items ));
190+ }
191+
192+ /**
193+ * Get all deployments
194+ *
195+ * @return all deployments
196+ */
126197 @ Nullable
127198 AiDeploymentList getAll () {
128199 return CLIENT .query (RESOURCE_GROUP );
0 commit comments