What is missing?
Description
Fixes the inability to explicitly set an ICMP Type of 0 in Security Group Rules.
Root Cause
Currently, the PortRangeMin and PortRangeMax fields use the omitempty JSON tag. In Go, setting an integer to 0 triggers this tag, causing the JSON marshaler to completely omit the field from the API payload.
|
// The maximum port number in the range that is matched by the security group |
|
// rule. The PortRangeMin attribute constrains the PortRangeMax attribute. If |
|
// the protocol is ICMP, this value must be an ICMP code. |
|
PortRangeMax int `json:"port_range_max,omitempty"` |
|
|
|
// The minimum port number in the range that is matched by the security group |
|
// rule. If the protocol is TCP or UDP, this value must be less than or equal |
|
// to the value of the PortRangeMax attribute. If the protocol is ICMP, this |
|
// value must be an ICMP type. |
|
PortRangeMin int `json:"port_range_min,omitempty"` |
When creating a security group rule for the icmp protocol, OpenStack Neutron uses port_range_min to define the ICMP Type and port_range_max to define the ICMP Code. Because 0 is a valid ICMP Type (e.g., Echo Reply), the omitempty tag prevents the user from successfully sending {"port_range_min": 0} to the API. Consequently, Neutron receives a request with an ICMP Code but a missing ICMP Type, resulting in a `400 Bad Request.
failed to create security group rule in openstack: Expected HTTP response code [201 202] when accessing [POST http://10.32.50.10:9696/v2.0/security-group-rules], but got 400 instead: {\"NeutronError\": {\"type\": \"SecurityGroupMissingIcmpType\", \"message\": \"ICMP code (port-range-max) 255 is provided but ICMP type (port-range-min) is missing.\", \"detail\": \"\"}}
Solution
By changing the struct fields to *int pointers, we can distinguish 0 from nil. This allows 0 to be safely marshaled without the omitempty tag dropping it.
I noticed this issue has been open since 2019, so I would love to help get it resolved.
What is missing?
Description
Fixes the inability to explicitly set an ICMP Type of
0in Security Group Rules.Root Cause
Currently, the
PortRangeMinandPortRangeMaxfields use theomitemptyJSON tag. In Go, setting an integer to0triggers this tag, causing the JSON marshaler to completely omit the field from the API payload.gophercloud/openstack/networking/v2/extensions/security/rules/requests.go
Lines 126 to 135 in a83babe
When creating a security group rule for the
icmpprotocol, OpenStack Neutron usesport_range_minto define the ICMP Type andport_range_maxto define the ICMP Code. Because0is a valid ICMP Type (e.g., Echo Reply), theomitemptytag prevents the user from successfully sending{"port_range_min": 0}to the API. Consequently, Neutron receives a request with an ICMP Code but a missing ICMP Type, resulting in a `400 Bad Request.Solution
By changing the struct fields to
*intpointers, we can distinguish 0 from nil. This allows 0 to be safely marshaled without the omitempty tag dropping it.I noticed this issue has been open since 2019, so I would love to help get it resolved.