[openstack-dev][CLI] How to set NoneType in openstack CLI's json format string?

Stephen Finucane stephenfin at redhat.com
Mon Feb 27 15:44:44 UTC 2023


On Wed, 2023-02-22 at 09:44 +0800, Simon Jones wrote:
> 
> 
> Hi all,
> 
> I'm using openstack command like this, and got error:
> 
> ```
> gyw at c1:~$ openstack port create --network selfservice --vnic-type remote-
> managed \
> > --binding-profile '{"pci_vendor_info": None, "pci_slot": None,
> > "physical_network": None, "card_serial_number": "AB0123XX0042",
> > "pf_mac_address": "08:c0:eb:8e:bd:f8", "vf_num":1, "vnic_type": "remote-
> > managed"}' \
> > pf0vf0
> Expected '<key>=<value>' or JSON data for option --binding-profile, but
> encountered JSON parsing error: Expecting value: line 1 column 21 (char 20)
> ```
> 
> So how to set "pci_vendor_info" to NoneType of python in openstack CLI's json
> format string?

You need a JSON string, as the error message indicates. None is Python. null is
JSON. 

   >>> import json
   >>> binding_profile = {"pci_vendor_info": None, "pci_slot": None}
   >>> json.dumps(binding_profile)
   '{"pci_vendor_info": null, "pci_slot": null}'

Stephen

> 
> BTW, "NoneType of python" means the NoneType in python code, like these
> comment in
> https://github.com/openstack/neutron/blob/stable/yoga/neutron/common/ovn/utils.py
> 
> ```
>     # With this example param_set:
>     #
>     # param_set = {
>     #     'do_not_check_this_key': None,
>     #     'pci_slot': [str],
>     #     'physical_network': [str, type(None)]
>     # }
>     #
>     # We confirm that each binding_profile key is of one of the listed types,
>     # allowing validation of polymorphic entries.
>     #
>     # 'physical_network' is polymorphic because:  When a VNIC_REMOTE_MANAGED
> or
>     # VNIC_DIRECT with PORT_CAP_SWITCHDEV capability port is attached to a
>     # project network backed by an overlay (tunneled) network the value will
> be
>     # 'None'.  For the case of ports attached to a project network backed by
>     # VLAN the value will be of type ``str``.  This comes from Nova and is
>     # provided in the ``physical_network`` tag in the Nova PCI Passthrough
>     # configuration.
>     #
>     # In the above example the type of the value behind
> 'do_not_check_this_key'
>     # will not be checked, 'pci_slot' must be ``str``, 'physical_network  must
>     # be either ``str`` or ``NoneType``.
> ```
> 
> Thank you~
> 
> ----
> Simon Jones




More information about the openstack-discuss mailing list