Skip to content

Commit ad02e47

Browse files
authored
Improve fuzzy setting logic for VirtualNetwork, RouteTable, and LoadBalancer extensions (#3009)
1 parent da81aeb commit ad02e47

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

v2/api/network/customizations/load_balancer_extension.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ func fuzzySetInboundNatRules(lb genruntime.ARMResourceSpec, inboundNatRules []ge
146146
return nil
147147
}
148148

149-
func fuzzySetInboundNatRule(InboundNatRule genruntime.ARMResourceSpec, embeddedInboundNatRule reflect.Value) error {
150-
inboundNatRuleJSON, err := json.Marshal(InboundNatRule)
149+
func fuzzySetInboundNatRule(inboundNatRule genruntime.ARMResourceSpec, embeddedInboundNatRule reflect.Value) error {
150+
inboundNatRuleJSON, err := json.Marshal(inboundNatRule)
151151
if err != nil {
152152
return errors.Wrapf(err, "failed to marshal inboundNatRule json")
153153
}
@@ -163,8 +163,10 @@ func fuzzySetInboundNatRule(InboundNatRule genruntime.ARMResourceSpec, embeddedI
163163
if err != nil {
164164
return errors.Wrap(err, "unable to check that embedded inboundNatRule is the same as inboundNatRule")
165165
}
166-
if string(embeddedInboundNatRuleJSON) != string(inboundNatRuleJSON) {
167-
return errors.Errorf("embeddedInboundNatRuleJSON (%s) != inboundNatRuleJSON (%s)", string(embeddedInboundNatRuleJSON), string(inboundNatRuleJSON))
166+
167+
err = fuzzyEqualityComparison(embeddedInboundNatRuleJSON, inboundNatRuleJSON)
168+
if err != nil {
169+
return errors.Wrap(err, "failed during comparison for embeddedInboundNatRuleJSON and inboundNatRuleJSON")
168170
}
169171

170172
return nil

v2/api/network/customizations/route_table_extensions.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ func fuzzySetRoute(route genruntime.ARMResourceSpec, embeddedRoute reflect.Value
167167
if err != nil {
168168
return errors.Wrap(err, "unable to check that embedded route is the same as route")
169169
}
170-
if string(embeddedRouteJSON) != string(routeJSON) {
171-
return errors.Errorf("embeddedRouteJSON (%s) != routeJSON (%s)", string(embeddedRouteJSON), string(routeJSON))
170+
171+
err = fuzzyEqualityComparison(embeddedRouteJSON, routeJSON)
172+
if err != nil {
173+
return errors.Wrap(err, "failed during comparison for embeddedRouteJSON and routeJSON")
172174
}
173175

174176
return nil

v2/api/network/customizations/virtual_network_extensions.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package customizations
55
import (
66
"context"
77
"encoding/json"
8+
"fmt"
89
"reflect"
910

1011
"github.com/go-logr/logr"
@@ -201,8 +202,30 @@ func fuzzySetSubnet(subnet genruntime.ARMResourceSpec, embeddedSubnet reflect.Va
201202
if err != nil {
202203
return errors.Wrap(err, "unable to check that embedded subnet is the same as subnet")
203204
}
204-
if string(embeddedSubnetJSON) != string(subnetJSON) {
205-
return errors.Errorf("embeddedSubnetJSON (%s) != subnetJSON (%s)", string(embeddedSubnetJSON), string(subnetJSON))
205+
206+
err = fuzzyEqualityComparison(embeddedSubnetJSON, subnetJSON)
207+
if err != nil {
208+
return errors.Wrap(err, "failed during comparison for embeddedSubnetJSON and subnetJSON")
209+
}
210+
211+
return nil
212+
}
213+
214+
func fuzzyEqualityComparison(embeddedResourceJSON, resourceJSON []byte) error {
215+
var embeddedResourceJSONMap map[string]interface{}
216+
err := json.Unmarshal(embeddedResourceJSON, &embeddedResourceJSONMap)
217+
if err != nil {
218+
return errors.Wrap(err, fmt.Sprintf("unable to unmarshal (%s)", embeddedResourceJSONMap))
219+
}
220+
221+
var resourceJSONMap map[string]interface{}
222+
err = json.Unmarshal(resourceJSON, &resourceJSONMap)
223+
if err != nil {
224+
return errors.Wrap(err, fmt.Sprintf("unable to unmarshal (%s)", resourceJSONMap))
225+
}
226+
227+
if !reflect.DeepEqual(embeddedResourceJSONMap, resourceJSONMap) {
228+
return errors.Errorf(" (%s) != (%s)", string(embeddedResourceJSON), string(resourceJSON))
206229
}
207230

208231
return nil

0 commit comments

Comments
 (0)