From f8bde51891354083ae389a447ce946c38ea29aa5 Mon Sep 17 00:00:00 2001
From: Takashi Kajinami <kajinamit@oss.nttdata.com>
Date: Fri, 3 Nov 2023 22:58:18 +0900
Subject: [PATCH] pci: Drop support for "broken" json

JSON does not allow usage of single quotes and require double quotes.
We have been converting single quotes to support such invalid usage but
that was deprecated a long ago[1].

This also simplifies the logic to handle aliases. Using undef results
in the value set to service default fact.

[1] 1cd349f893408803fec307f615ae3fe265d54fed

Change-Id: If1328531f33a9fe778091ce38b5c1f8072b473b8
---
 lib/puppet/functions/to_array_of_json_strings.rb | 9 +--------
 manifests/compute/pci.pp                         | 5 ++---
 manifests/pci.pp                                 | 4 +---
 spec/classes/nova_compute_pci_spec.rb            | 2 +-
 spec/classes/nova_pci_spec.rb                    | 4 ++--
 5 files changed, 7 insertions(+), 17 deletions(-)

diff --git a/lib/puppet/functions/to_array_of_json_strings.rb b/lib/puppet/functions/to_array_of_json_strings.rb
index 5f05bb035..58ea7bb5f 100644
--- a/lib/puppet/functions/to_array_of_json_strings.rb
+++ b/lib/puppet/functions/to_array_of_json_strings.rb
@@ -18,14 +18,7 @@ Puppet::Functions.create_function(:to_array_of_json_strings) do
     list = args[0]
     if list.class == String
       begin
-        begin
-          list = JSON.load(list)
-        rescue JSON::ParserError
-          # If parsing failed it could be a legacy format that uses single quotes.
-          # NB This will corrupt valid JSON data, e.g {"foo": "\'"} => {"foo": "\""}
-          list = JSON.load(list.gsub("'","\""))
-          Puppet.warning("#{args[0]} is not valid JSON. Support for this format is deprecated and may be removed in future.")
-        end
+        list = JSON.load(list)
       rescue JSON::ParserError
         raise Puppet::ParseError, "Syntax error: #{args[0]} is not valid"
       end
diff --git a/manifests/compute/pci.pp b/manifests/compute/pci.pp
index 5846fdf17..1375817e9 100644
--- a/manifests/compute/pci.pp
+++ b/manifests/compute/pci.pp
@@ -21,13 +21,12 @@ class nova::compute::pci(
 ) {
   include nova::deps
 
-  if $passthrough and
-      !is_service_default($passthrough) and
-      !empty($passthrough) {
+  if !is_service_default($passthrough) and !empty($passthrough) {
     $passthrough_real = to_array_of_json_strings($passthrough)
   } else {
     $passthrough_real = $facts['os_service_default']
   }
+
   nova_config {
     'pci/device_spec':         value => $passthrough_real;
     'pci/report_in_placement': value => $report_in_placement;
diff --git a/manifests/pci.pp b/manifests/pci.pp
index 89db16162..ea60290cf 100644
--- a/manifests/pci.pp
+++ b/manifests/pci.pp
@@ -16,9 +16,7 @@ class nova::pci(
 ) {
   include nova::deps
 
-  if $aliases and
-      !is_service_default($aliases) and
-      !empty($aliases) {
+  if !is_service_default($aliases) and !empty($aliases) {
     $aliases_real = to_array_of_json_strings($aliases)
   } else {
     $aliases_real = $facts['os_service_default']
diff --git a/spec/classes/nova_compute_pci_spec.rb b/spec/classes/nova_compute_pci_spec.rb
index f3e31cc93..026a846cc 100644
--- a/spec/classes/nova_compute_pci_spec.rb
+++ b/spec/classes/nova_compute_pci_spec.rb
@@ -45,7 +45,7 @@ describe 'nova::compute::pci' do
       end
     end
 
-    context 'with passthrough JSON encoded string (deprecated)' do
+    context 'with passthrough JSON encoded string' do
       let :params do
         {
           :passthrough => "[{\"vendor_id\":\"8086\",\"product_id\":\"0126\"},{\"vendor_id\":\"9096\",\"product_id\":\"1520\",\"physical_network\":\"physnet1\"}]",
diff --git a/spec/classes/nova_pci_spec.rb b/spec/classes/nova_pci_spec.rb
index 88be99e39..707adbffa 100644
--- a/spec/classes/nova_pci_spec.rb
+++ b/spec/classes/nova_pci_spec.rb
@@ -32,7 +32,7 @@ describe 'nova::pci' do
       end
     end
 
-    context 'with aliases JSON encoded string (deprecated)' do
+    context 'with aliases JSON encoded string' do
       let :params do
         {
           :aliases => "[{\"vendor_id\":\"8086\",\"product_id\":\"0126\",\"name\":\"graphic_card\"},{\"vendor_id\":\"9096\",\"product_id\":\"1520\",\"name\":\"network_card\"}]",
@@ -81,4 +81,4 @@ describe 'nova::pci' do
       it_configures 'nova-pci'
     end
   end
-end
\ No newline at end of file
+end