public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-staging][PATCH v2 4/5] edk2-staging/RedfishClientPkg: Add ETag support
@ 2022-06-21  1:46 Nickle Wang
  2022-06-21  3:40 ` Abner Chang
  0 siblings, 1 reply; 2+ messages in thread
From: Nickle Wang @ 2022-06-21  1:46 UTC (permalink / raw)
  To: devel; +Cc: Nickle Wang, Abner Chang, Yang Atom, Nick Ramirez

Implement ETag support in HTTP header

Signed-off-by: Nickle Wang <nickle.wang@hpe.com>
Cc: Abner Chang <abner.chang@hpe.com>
Cc: Yang Atom <Atom.Yang@amd.com>
Cc: Nick Ramirez <nramirez@nvidia.com>
---
 .../v1sim/resource.py                         | 10 +++++--
 .../v1sim/systems.py                          | 28 +++++++++++--------
 2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/resource.py b/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/resource.py
index e722d16a0b..0c7a838281 100644
--- a/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/resource.py
+++ b/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/resource.py
@@ -36,6 +36,12 @@ class RfResource:
             self.final_init_processing(base_path, rel_path)
         else:
             self.res_data = {}
+        self.generate_etag(json.dumps(self.res_data))
+
+    def generate_etag(self, context):
+        md5 = hashlib.md5()
+        md5.update(context.encode('utf-8'))
+        self.etag = 'W/"' + md5.hexdigest() + '"'
 
     def create_sub_objects(self, base_path, rel_path):
         pass
@@ -49,8 +55,8 @@ class RfResource:
             # SHA1 should generate well-behaved etags
             response = flask.make_response(self.response)
             response.mimetype = 'application/json'
-            etag = hashlib.sha1(self.response.encode('utf-8')).hexdigest()
-            response.set_etag(etag)
+            response.headers["ETag"] = self.etag
+
             return response
         except KeyError:
             flask.abort(404)
diff --git a/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/systems.py b/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/systems.py
index 6305a51efb..5adf81be5b 100644
--- a/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/systems.py
+++ b/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/systems.py
@@ -92,7 +92,11 @@ class RfSystemObj(RfResource):
             if "BootOrder" in boot_data:
                 self.res_data['Boot']['BootOrder'] = boot_data['BootOrder']
 
-        resp = flask.Response(json.dumps(self.res_data,indent=4), mimetype="application/json")
+        context = json.dumps(self.res_data,indent=4)
+        self.generate_etag(context)
+        resp = flask.Response(context, mimetype="application/json")
+        resp.headers["ETag"] = self.etag
+
         return 0, 200, None, resp
 
     def reset_resource(self, reset_data):
@@ -136,26 +140,28 @@ class RfMemoryCollection(RfCollection):
 
         post_data["@odata.id"] = newMemoryUrl
 
-        md5 = hashlib.md5()
-        md5.update(json.dumps(post_data).encode("utf-8"))
-        etag_str = 'W/"' + md5.hexdigest() + '"'
-        post_data["@odata.etag"] = etag_str
+        self.generate_etag(json.dumps(post_data,indent=4))
+
+        post_data["@odata.etag"] = self.etag
         self.elements[str(newMemoryIdx)] = post_data
 
         resp = flask.Response(json.dumps(post_data,indent=4), mimetype="application/json")
         resp.headers["Location"] = newMemoryUrl
-        resp.headers["ETag"] = etag_str
-
+        resp.headers["ETag"] = self.etag
         return 0, 200, None, resp
 
     def patch_memory(self, Idx, patch_data):
-        md5 = hashlib.md5()
-        md5.update(json.dumps(patch_data).encode("utf-8"))
-        etag_str = 'W/"' + md5.hexdigest() + '"'
-        patch_data["@odata.etag"] = etag_str
 
+
+        self.elements[str(Idx)] = {**self.elements[str(Idx)], **patch_data}
+
+        context = json.dumps(self.elements[str(Idx)],indent=4)
+        self.generate_etag(context)
+        patch_data["@odata.etag"] = self.etag
         self.elements[str(Idx)] = {**self.elements[str(Idx)], **patch_data}
+
         resp = flask.Response(json.dumps(self.elements[str(Idx)],indent=4), mimetype="application/json")
+        resp.headers["ETag"] = self.etag
         return 0, 200, None, resp
 
     def get_memory(self, Idx):
-- 
2.32.0.windows.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [edk2-staging][PATCH v2 4/5] edk2-staging/RedfishClientPkg: Add ETag support
  2022-06-21  1:46 [edk2-staging][PATCH v2 4/5] edk2-staging/RedfishClientPkg: Add ETag support Nickle Wang
@ 2022-06-21  3:40 ` Abner Chang
  0 siblings, 0 replies; 2+ messages in thread
From: Abner Chang @ 2022-06-21  3:40 UTC (permalink / raw)
  To: Wang, Nickle (Server BIOS), devel@edk2.groups.io; +Cc: Yang Atom, Nick Ramirez

Reviewed-by: Abner Chang <abner.chang@hpe.com>

> -----Original Message-----
> From: Wang, Nickle (Server BIOS) <nickle.wang@hpe.com>
> Sent: Tuesday, June 21, 2022 9:47 AM
> To: devel@edk2.groups.io
> Cc: Wang, Nickle (Server BIOS) <nickle.wang@hpe.com>; Chang, Abner (HPS
> SW/FW Technologist) <abner.chang@hpe.com>; Yang Atom
> <Atom.Yang@amd.com>; Nick Ramirez <nramirez@nvidia.com>
> Subject: [edk2-staging][PATCH v2 4/5] edk2-staging/RedfishClientPkg: Add ETag
> support
> 
> Implement ETag support in HTTP header
> 
> Signed-off-by: Nickle Wang <nickle.wang@hpe.com>
> Cc: Abner Chang <abner.chang@hpe.com>
> Cc: Yang Atom <Atom.Yang@amd.com>
> Cc: Nick Ramirez <nramirez@nvidia.com>
> ---
>  .../v1sim/resource.py                         | 10 +++++--
>  .../v1sim/systems.py                          | 28 +++++++++++--------
>  2 files changed, 25 insertions(+), 13 deletions(-)
> 
> diff --git a/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/resource.py
> b/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/resource.py
> index e722d16a0b..0c7a838281 100644
> --- a/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/resource.py
> +++ b/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/resource.py
> @@ -36,6 +36,12 @@ class RfResource:
>              self.final_init_processing(base_path, rel_path)
> 
>          else:
> 
>              self.res_data = {}
> 
> +        self.generate_etag(json.dumps(self.res_data))
> 
> +
> 
> +    def generate_etag(self, context):
> 
> +        md5 = hashlib.md5()
> 
> +        md5.update(context.encode('utf-8'))
> 
> +        self.etag = 'W/"' + md5.hexdigest() + '"'
> 
> 
> 
>      def create_sub_objects(self, base_path, rel_path):
> 
>          pass
> 
> @@ -49,8 +55,8 @@ class RfResource:
>              # SHA1 should generate well-behaved etags
> 
>              response = flask.make_response(self.response)
> 
>              response.mimetype = 'application/json'
> 
> -            etag = hashlib.sha1(self.response.encode('utf-8')).hexdigest()
> 
> -            response.set_etag(etag)
> 
> +            response.headers["ETag"] = self.etag
> 
> +
> 
>              return response
> 
>          except KeyError:
> 
>              flask.abort(404)
> 
> diff --git a/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/systems.py
> b/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/systems.py
> index 6305a51efb..5adf81be5b 100644
> --- a/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/systems.py
> +++ b/RedfishClientPkg/Tools/Redfish-Profile-Simulator/v1sim/systems.py
> @@ -92,7 +92,11 @@ class RfSystemObj(RfResource):
>              if "BootOrder" in boot_data:
> 
>                  self.res_data['Boot']['BootOrder'] = boot_data['BootOrder']
> 
> 
> 
> -        resp = flask.Response(json.dumps(self.res_data,indent=4),
> mimetype="application/json")
> 
> +        context = json.dumps(self.res_data,indent=4)
> 
> +        self.generate_etag(context)
> 
> +        resp = flask.Response(context, mimetype="application/json")
> 
> +        resp.headers["ETag"] = self.etag
> 
> +
> 
>          return 0, 200, None, resp
> 
> 
> 
>      def reset_resource(self, reset_data):
> 
> @@ -136,26 +140,28 @@ class RfMemoryCollection(RfCollection):
> 
> 
>          post_data["@odata.id"] = newMemoryUrl
> 
> 
> 
> -        md5 = hashlib.md5()
> 
> -        md5.update(json.dumps(post_data).encode("utf-8"))
> 
> -        etag_str = 'W/"' + md5.hexdigest() + '"'
> 
> -        post_data["@odata.etag"] = etag_str
> 
> +        self.generate_etag(json.dumps(post_data,indent=4))
> 
> +
> 
> +        post_data["@odata.etag"] = self.etag
> 
>          self.elements[str(newMemoryIdx)] = post_data
> 
> 
> 
>          resp = flask.Response(json.dumps(post_data,indent=4),
> mimetype="application/json")
> 
>          resp.headers["Location"] = newMemoryUrl
> 
> -        resp.headers["ETag"] = etag_str
> 
> -
> 
> +        resp.headers["ETag"] = self.etag
> 
>          return 0, 200, None, resp
> 
> 
> 
>      def patch_memory(self, Idx, patch_data):
> 
> -        md5 = hashlib.md5()
> 
> -        md5.update(json.dumps(patch_data).encode("utf-8"))
> 
> -        etag_str = 'W/"' + md5.hexdigest() + '"'
> 
> -        patch_data["@odata.etag"] = etag_str
> 
> 
> 
> +
> 
> +        self.elements[str(Idx)] = {**self.elements[str(Idx)], **patch_data}
> 
> +
> 
> +        context = json.dumps(self.elements[str(Idx)],indent=4)
> 
> +        self.generate_etag(context)
> 
> +        patch_data["@odata.etag"] = self.etag
> 
>          self.elements[str(Idx)] = {**self.elements[str(Idx)], **patch_data}
> 
> +
> 
>          resp = flask.Response(json.dumps(self.elements[str(Idx)],indent=4),
> mimetype="application/json")
> 
> +        resp.headers["ETag"] = self.etag
> 
>          return 0, 200, None, resp
> 
> 
> 
>      def get_memory(self, Idx):
> 
> --
> 2.32.0.windows.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-21  3:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-21  1:46 [edk2-staging][PATCH v2 4/5] edk2-staging/RedfishClientPkg: Add ETag support Nickle Wang
2022-06-21  3:40 ` Abner Chang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox