]> git.proxmox.com Git - mirror_edk2.git/commitdiff
RedfishPkg: Redfish functions for REST requests are not fully spec complied
authorAbner Chang <abner.chang@amd.com>
Tue, 23 Aug 2022 02:09:37 +0000 (10:09 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Tue, 23 Aug 2022 03:13:11 +0000 (03:13 +0000)
There is no function to send POST request with the
ContentType different from "application\json".
There is no function to send DELETE request with the body.

Cc: Abner Chang <abner.chang@amd.com>
Cc: Nickle Wang <nickle.wang@hpe.com>
Signed-off-by: Igor Kulchytskyy <igork@ami.com>
Reviewed-by: Abner Chang <abner.chang@amd.com>
RedfishPkg/Include/Library/RedfishLib.h
RedfishPkg/PrivateLibrary/RedfishLib/RedfishLib.c
RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/include/redfishService.h
RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/service.c

index b2488abbd4b513fbfdc80bd4fc133a832af101dd..28c2b290e613affebdad15f395c7ea758d3ce391 100644 (file)
@@ -380,6 +380,47 @@ RedfishPatchToPayload (
   OUT    REDFISH_RESPONSE  *RedResponse\r
   );\r
 \r
+/**\r
+  Use HTTP POST to create new Redfish resource in the Resource Collection.\r
+\r
+  The POST request should be submitted to the Resource Collection in which the new resource\r
+  is to belong. The Resource Collection is addressed by URI. The Redfish may\r
+  ignore any service controlled properties. The corresponding redfish response will returned,\r
+  including HTTP StatusCode, Headers and Payload which record any HTTP response messages.\r
+\r
+  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
+  redfish response data.\r
+\r
+  @param[in]    RedfishService        The Service to access the Redfish resources.\r
+  @param[in]    Uri                   Relative path to address the resource.\r
+  @param[in]    Content               JSON represented properties to be update.\r
+  @param[in]    ContentSize           Size of the Content to be send to Redfish service\r
+  @param[in]    ContentType           Type of the Content to be send to Redfish service\r
+  @param[out]   RedResponse           Pointer to the Redfish response data.\r
+\r
+  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not\r
+                                  NULL and the value is 2XX. The Redfish resource will be returned\r
+                                  in Payload within RedResponse if server send it back in the HTTP\r
+                                  response message body.\r
+  @retval EFI_INVALID_PARAMETER   RedfishService, Uri, Content, or RedResponse is NULL.\r
+  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get\r
+                                  more error info from returned HTTP StatusCode, Headers and Payload\r
+                                  within RedResponse:\r
+                                  1. If the returned StatusCode is NULL, indicates any error happen.\r
+                                  2. If the returned StatusCode is not NULL and the value is not 2XX,\r
+                                     indicates any error happen.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RedfishPostToUri (\r
+  IN     REDFISH_SERVICE   RedfishService,\r
+  IN     CONST CHAR8       *Uri,\r
+  IN     CONST CHAR8       *Content,\r
+  IN     UINTN             ContentSize,\r
+  IN     CONST CHAR8       *ContentType,\r
+  OUT    REDFISH_RESPONSE  *RedResponse\r
+  );\r
+\r
 /**\r
   Use HTTP POST to create a new resource in target payload.\r
 \r
@@ -450,6 +491,43 @@ RedfishDeleteByUri (
   OUT    REDFISH_RESPONSE  *RedResponse\r
   );\r
 \r
+/**\r
+  Use HTTP DELETE to remove a resource.\r
+\r
+  This function uses the RedfishService to remove a Redfish resource which is addressed\r
+  by input Uri (only the relative path is required). The corresponding redfish response will\r
+  returned, including HTTP StatusCode, Headers and Payload which record any HTTP response\r
+  messages.\r
+\r
+  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
+  redfish response data.\r
+\r
+  @param[in]    RedfishService        The Service to access the Redfish resources.\r
+  @param[in]    Uri                   Relative path to address the resource.\r
+  @param[in]    Content               JSON represented properties to be deleted.\r
+  @param[out]   RedResponse           Pointer to the Redfish response data.\r
+\r
+  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not\r
+                                  NULL and the value is 2XX, the Redfish resource has been removed.\r
+                                  If there is any message returned from server, it will be returned\r
+                                  in Payload within RedResponse.\r
+  @retval EFI_INVALID_PARAMETER   RedfishService, Uri, or RedResponse is NULL.\r
+  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get\r
+                                  more error info from returned HTTP StatusCode, Headers and Payload\r
+                                  within RedResponse:\r
+                                  1. If the returned StatusCode is NULL, indicates any error happen.\r
+                                  2. If the returned StatusCode is not NULL and the value is not 2XX,\r
+                                     indicates any error happen.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RedfishDeleteByUriEx (\r
+  IN     REDFISH_SERVICE   RedfishService,\r
+  IN     CONST CHAR8       *Uri,\r
+  IN     CONST CHAR8       *Content,\r
+  OUT    REDFISH_RESPONSE  *RedResponse\r
+  );\r
+\r
 /**\r
   Dump text in fractions.\r
 \r
index 9f9d37790e7927a4871be87988a52760ca0e7536..51a85a73360d0f3cad435dbe6f0cc04dc41344af 100644 (file)
@@ -583,6 +583,102 @@ RedfishPatchToPayload (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Use HTTP POST to create new Redfish resource in the Resource Collection.\r
+\r
+  The POST request should be submitted to the Resource Collection in which the new resource\r
+  is to belong. The Resource Collection is addressed by URI. The Redfish may\r
+  ignore any service controlled properties. The corresponding redfish response will returned,\r
+  including HTTP StatusCode, Headers and Payload which record any HTTP response messages.\r
+\r
+  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
+  redfish response data.\r
+\r
+  @param[in]    RedfishService        The Service to access the Redfish resources.\r
+  @param[in]    Uri                   Relative path to address the resource.\r
+  @param[in]    Content               JSON represented properties to be update.\r
+  @param[in]    ContentSize           Size of the Content to be send to Redfish service\r
+  @param[in]    ContentType           Type of the Content to be send to Redfish service\r
+  @param[out]   RedResponse           Pointer to the Redfish response data.\r
+\r
+  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not\r
+                                  NULL and the value is 2XX. The Redfish resource will be returned\r
+                                  in Payload within RedResponse if server send it back in the HTTP\r
+                                  response message body.\r
+  @retval EFI_INVALID_PARAMETER   RedfishService, Uri, Content, or RedResponse is NULL.\r
+  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get\r
+                                  more error info from returned HTTP StatusCode, Headers and Payload\r
+                                  within RedResponse:\r
+                                  1. If the returned StatusCode is NULL, indicates any error happen.\r
+                                  2. If the returned StatusCode is not NULL and the value is not 2XX,\r
+                                     indicates any error happen.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RedfishPostToUri (\r
+  IN     REDFISH_SERVICE   RedfishService,\r
+  IN     CONST CHAR8       *Uri,\r
+  IN     CONST CHAR8       *Content,\r
+  IN     UINTN             ContentSize,\r
+  IN     CONST CHAR8       *ContentType,\r
+  OUT    REDFISH_RESPONSE  *RedResponse\r
+  )\r
+{\r
+  EFI_STATUS        Status;\r
+  EDKII_JSON_VALUE  JsonValue;\r
+\r
+  Status    = EFI_SUCCESS;\r
+  JsonValue = NULL;\r
+\r
+  if ((RedfishService == NULL) || (Uri == NULL) || (Content == NULL) || (RedResponse == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
+\r
+  JsonValue = (EDKII_JSON_VALUE)postUriFromService (\r
+                                  RedfishService,\r
+                                  Uri,\r
+                                  Content,\r
+                                  ContentSize,\r
+                                  ContentType,\r
+                                  &(RedResponse->StatusCode)\r
+                                  );\r
+\r
+  //\r
+  // 1. If the returned StatusCode is NULL, indicates any error happen.\r
+  //\r
+  if (RedResponse->StatusCode == NULL) {\r
+    Status = EFI_DEVICE_ERROR;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  //\r
+  // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.\r
+  //    NOTE: If there is any error message returned from server, it will be returned in\r
+  //          Payload within RedResponse.\r
+  //\r
+  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
+      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
+  {\r
+    Status = EFI_DEVICE_ERROR;\r
+  }\r
+\r
+ON_EXIT:\r
+  if (JsonValue != NULL) {\r
+    RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);\r
+    if (RedResponse->Payload == NULL) {\r
+      //\r
+      // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what\r
+      // we care about if the returned StatusCode is 2XX.\r
+      //\r
+      JsonValueFree (JsonValue);\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   Use HTTP POST to create a new resource in target payload.\r
 \r
@@ -738,6 +834,96 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
+/**\r
+  Use HTTP DELETE to remove a resource.\r
+\r
+  This function uses the RedfishService to remove a Redfish resource which is addressed\r
+  by input Uri (only the relative path is required). The corresponding redfish response will\r
+  returned, including HTTP StatusCode, Headers and Payload which record any HTTP response\r
+  messages.\r
+\r
+  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
+  redfish response data.\r
+\r
+  @param[in]    RedfishService        The Service to access the Redfish resources.\r
+  @param[in]    Uri                   Relative path to address the resource.\r
+  @param[in]    Content               JSON represented properties to be deleted.\r
+  @param[out]   RedResponse           Pointer to the Redfish response data.\r
+\r
+  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not\r
+                                  NULL and the value is 2XX, the Redfish resource has been removed.\r
+                                  If there is any message returned from server, it will be returned\r
+                                  in Payload within RedResponse.\r
+  @retval EFI_INVALID_PARAMETER   RedfishService, Uri, or RedResponse is NULL.\r
+  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get\r
+                                  more error info from returned HTTP StatusCode, Headers and Payload\r
+                                  within RedResponse:\r
+                                  1. If the returned StatusCode is NULL, indicates any error happen.\r
+                                  2. If the returned StatusCode is not NULL and the value is not 2XX,\r
+                                     indicates any error happen.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RedfishDeleteByUriEx (\r
+  IN     REDFISH_SERVICE   RedfishService,\r
+  IN     CONST CHAR8       *Uri,\r
+  IN     CONST CHAR8       *Content,\r
+  OUT    REDFISH_RESPONSE  *RedResponse\r
+  )\r
+{\r
+  EFI_STATUS        Status;\r
+  EDKII_JSON_VALUE  JsonValue;\r
+\r
+  Status    = EFI_SUCCESS;\r
+  JsonValue = NULL;\r
+\r
+  if ((RedfishService == NULL) || (Content == NULL) || (Uri == NULL) || (RedResponse == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
+\r
+  JsonValue = (EDKII_JSON_VALUE)deleteUriFromServiceEx (\r
+                                  RedfishService,\r
+                                  Uri,\r
+                                  Content,\r
+                                  &(RedResponse->StatusCode)\r
+                                  );\r
+\r
+  //\r
+  // 1. If the returned StatusCode is NULL, indicates any error happen.\r
+  //\r
+  if (RedResponse->StatusCode == NULL) {\r
+    Status = EFI_DEVICE_ERROR;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  //\r
+  // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.\r
+  //    NOTE: If there is any error message returned from server, it will be returned in\r
+  //          Payload within RedResponse.\r
+  //\r
+  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
+      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
+  {\r
+    Status = EFI_DEVICE_ERROR;\r
+  }\r
+\r
+ON_EXIT:\r
+  if (JsonValue != NULL) {\r
+    RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);\r
+    if (RedResponse->Payload == NULL) {\r
+      //\r
+      // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what\r
+      // we care about if the returned StatusCode is 2XX.\r
+      //\r
+      JsonValueFree (JsonValue);\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   Dump text in fractions.\r
 \r
index 5c13b682a3b9e4afae9388b0e46cfe349042fa57..75afadc0cecee915636cd444e1e51d9e591b36bc 100644 (file)
@@ -129,6 +129,14 @@ deleteUriFromService (
   EFI_HTTP_STATUS_CODE  **StatusCode\r
   );\r
 \r
+json_t *\r
+deleteUriFromServiceEx (\r
+  redfishService        *service,\r
+  const char            *uri,\r
+  const char            *content,\r
+  EFI_HTTP_STATUS_CODE  **StatusCode\r
+  );\r
+\r
 redfishPayload *\r
 getRedfishServiceRoot (\r
   redfishService        *service,\r
index afa172b53185c84a49d07273758bb479016a2e32..cbd4330a0ed03ee7028a9da23ed3d209de7e399a 100644 (file)
@@ -924,9 +924,10 @@ ON_EXIT:
 }\r
 \r
 json_t *\r
-deleteUriFromService (\r
+deleteUriFromServiceEx (\r
   redfishService        *service,\r
   const char            *uri,\r
+  const char            *content,\r
   EFI_HTTP_STATUS_CODE  **StatusCode\r
   )\r
 {\r
@@ -937,6 +938,8 @@ deleteUriFromService (
   EFI_HTTP_REQUEST_DATA  *RequestData = NULL;\r
   EFI_HTTP_MESSAGE       *RequestMsg  = NULL;\r
   EFI_HTTP_MESSAGE       ResponseMsg;\r
+  CHAR8                  ContentLengthStr[80];\r
+  size_t                 contentLength;\r
 \r
   ret = NULL;\r
 \r
@@ -956,7 +959,7 @@ deleteUriFromService (
   //\r
   // Step 1: Create HTTP request message with 4 headers:\r
   //\r
-  HttpIoHeader = HttpIoCreateHeader ((service->sessionToken || service->basicAuthStr) ? 5 : 4);\r
+  HttpIoHeader = HttpIoCreateHeader ((service->sessionToken || service->basicAuthStr) ? 8 : 7);\r
   if (HttpIoHeader == NULL) {\r
     ret = NULL;\r
     goto ON_EXIT;\r
@@ -979,6 +982,23 @@ deleteUriFromService (
   Status = HttpIoSetHeader (HttpIoHeader, "Connection", "Keep-Alive");\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  Status = HttpIoSetHeader (HttpIoHeader, "Content-Type", "application/json");\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  if (content != NULL) {\r
+    contentLength = strlen (content);\r
+    AsciiSPrint (\r
+      ContentLengthStr,\r
+      sizeof (ContentLengthStr),\r
+      "%lu",\r
+      (UINT64)contentLength\r
+      );\r
+    Status = HttpIoSetHeader (HttpIoHeader, "Content-Length", ContentLengthStr);\r
+    ASSERT_EFI_ERROR (Status);\r
+    Status = HttpIoSetHeader (HttpIoHeader, "OData-Version", "4.0");\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
   //\r
   // Step 2: build the rest of HTTP request info.\r
   //\r
@@ -1004,6 +1024,11 @@ deleteUriFromService (
   RequestMsg->HeaderCount  = HttpIoHeader->HeaderCount;\r
   RequestMsg->Headers      = HttpIoHeader->Headers;\r
 \r
+  if (content != NULL) {\r
+    RequestMsg->BodyLength = contentLength;\r
+    RequestMsg->Body       = (VOID *)content;\r
+  }\r
+\r
   ZeroMem (&ResponseMsg, sizeof (ResponseMsg));\r
 \r
   //\r
@@ -1057,6 +1082,16 @@ ON_EXIT:
   return ret;\r
 }\r
 \r
+json_t *\r
+deleteUriFromService (\r
+  redfishService        *service,\r
+  const char            *uri,\r
+  EFI_HTTP_STATUS_CODE  **StatusCode\r
+  )\r
+{\r
+  return deleteUriFromServiceEx (service, uri, NULL, StatusCode);\r
+}\r
+\r
 redfishPayload *\r
 getRedfishServiceRoot (\r
   redfishService        *service,\r