Skip to content

Commit 9cb856a

Browse files
committed
refactor: simplify az network parsing
Signed-off-by: nikpivkin <[email protected]>
1 parent 47cfe0e commit 9cb856a

File tree

8 files changed

+234
-244
lines changed

8 files changed

+234
-244
lines changed

pkg/iac/adapters/arm/compute/adapt.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,10 @@ func extractNetworkInterfaces(networkProfile azure.Value, metadata iacTypes.Meta
106106
// EnableIPForwarding is not available from the VM's networkProfile, so it defaults to false
107107
// Since we only have a reference to the network interface (not the full resource),
108108
// we mark it as unmanaged so that Rego policies can skip it using isManaged() checks
109-
unmanagedMetadata := iacTypes.NewUnmanagedMetadata()
110-
networkInterface := network.NetworkInterface{
111-
Metadata: unmanagedMetadata,
112-
EnableIPForwarding: iacTypes.BoolDefault(false, unmanagedMetadata),
113-
SubnetID: iacTypes.StringDefault("", unmanagedMetadata),
114-
SecurityGroups: nil,
115-
HasPublicIP: iacTypes.BoolDefault(false, unmanagedMetadata),
116-
PublicIPAddress: iacTypes.StringDefault("", unmanagedMetadata),
117-
IPConfigurations: nil,
118-
}
119-
networkInterfaces = append(networkInterfaces, networkInterface)
109+
networkInterfaces = append(networkInterfaces, network.NetworkInterface{
110+
Metadata: iacTypes.NewUnmanagedMetadata(),
111+
})
120112
}
121113
}
122-
123114
return networkInterfaces
124115
}

pkg/iac/adapters/arm/network/adapt.go

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ func adaptSecurityGroupRules(deployment azure.Deployment) (rules []network.Secur
4343

4444
func adaptSecurityGroupRule(resource azure.Resource) network.SecurityGroupRule {
4545
sourceAddressPrefixes := resource.Properties.GetMapValue("sourceAddressPrefixes").AsStringValuesList("")
46-
sourceAddressPrefixes = append(sourceAddressPrefixes, resource.Properties.GetMapValue("sourceAddressPrefix").AsStringValue("", resource.Metadata))
46+
if prefix := resource.Properties.GetMapValue("sourceAddressPrefix").AsStringValue("", resource.Metadata); prefix.IsNotEmpty() {
47+
sourceAddressPrefixes = append(sourceAddressPrefixes, prefix)
48+
}
4749

4850
var sourcePortRanges []common.PortRange
4951
for _, portRange := range resource.Properties.GetMapValue("sourcePortRanges").AsList() {
@@ -57,7 +59,9 @@ func adaptSecurityGroupRule(resource azure.Resource) network.SecurityGroupRule {
5759
}
5860

5961
destinationAddressPrefixes := resource.Properties.GetMapValue("destinationAddressPrefixes").AsStringValuesList("")
60-
destinationAddressPrefixes = append(destinationAddressPrefixes, resource.Properties.GetMapValue("destinationAddressPrefix").AsStringValue("", resource.Metadata))
62+
if prefix := resource.Properties.GetMapValue("destinationAddressPrefix").AsStringValue("", resource.Metadata); prefix.IsNotEmpty() {
63+
destinationAddressPrefixes = append(destinationAddressPrefixes, prefix)
64+
}
6165

6266
var destinationPortRanges []common.PortRange
6367
for _, portRange := range resource.Properties.GetMapValue("destinationPortRanges").AsList() {
@@ -115,12 +119,10 @@ func adaptNetworkWatcherFlowLog(resource azure.Resource) network.NetworkWatcherF
115119
}
116120

117121
func adaptNetworkInterfaces(deployment azure.Deployment) []network.NetworkInterface {
118-
var networkInterfaces = make([]network.NetworkInterface, 0)
119-
122+
var networkInterfaces []network.NetworkInterface
120123
for _, resource := range deployment.GetResourcesByType("Microsoft.Network/networkInterfaces") {
121124
networkInterfaces = append(networkInterfaces, adaptNetworkInterface(resource, deployment))
122125
}
123-
124126
return networkInterfaces
125127
}
126128

@@ -130,63 +132,29 @@ func adaptNetworkInterface(resource azure.Resource, _ azure.Deployment) network.
130132
EnableIPForwarding: resource.Properties.GetMapValue("enableIPForwarding").AsBoolValue(false, resource.Metadata),
131133
HasPublicIP: iacTypes.BoolDefault(false, resource.Metadata),
132134
PublicIPAddress: iacTypes.StringDefault("", resource.Metadata),
133-
SecurityGroups: nil,
134135
SubnetID: iacTypes.StringDefault("", resource.Metadata),
135136
}
136137

137138
ipConfigs := resource.Properties.GetMapValue("ipConfigurations").AsList()
138139
ni.IPConfigurations = make([]network.IPConfiguration, 0, len(ipConfigs))
139140

140-
var primaryConfigSet bool
141-
142141
for _, ipConfig := range ipConfigs {
143142
if ipConfig.IsNull() {
144143
continue
145144
}
146-
147-
ipConfigMeta := resource.Metadata
148-
subnetID := ipConfig.GetMapValue("subnet").GetMapValue("id").AsStringValue("", resource.Metadata)
149-
publicIP := ipConfig.GetMapValue("publicIPAddress")
150-
hasPublicIP := iacTypes.BoolDefault(false, ipConfigMeta)
151-
publicIPAddress := iacTypes.StringDefault("", ipConfigMeta)
152-
primary := ipConfig.GetMapValue("primary").AsBoolValue(false, ipConfigMeta)
153-
154-
if !publicIP.IsNull() {
155-
hasPublicIP = iacTypes.Bool(true, ipConfigMeta)
156-
if publicIPID := publicIP.GetMapValue("id").AsStringValue("", resource.Metadata); publicIPID.Value() != "" {
157-
publicIPAddress = publicIPID
158-
}
159-
}
160-
161-
ipConfiguration := network.IPConfiguration{
162-
Metadata: ipConfigMeta,
163-
HasPublicIP: hasPublicIP,
164-
PublicIPAddress: publicIPAddress,
165-
SubnetID: subnetID,
166-
Primary: primary,
167-
}
168-
169-
ni.IPConfigurations = append(ni.IPConfigurations, ipConfiguration)
170-
171-
// For backward compatibility, populate the single-value fields with the primary configuration
172-
// If no primary is set, use the first configuration
173-
isPrimary := primary.Value() || (len(ni.IPConfigurations) == 1 && !primaryConfigSet && primary.GetMetadata().IsDefault())
174-
if isPrimary && !primaryConfigSet {
175-
if subnetID.Value() != "" {
176-
ni.SubnetID = subnetID
177-
}
178-
if hasPublicIP.Value() {
179-
ni.HasPublicIP = hasPublicIP
180-
if publicIPAddress.Value() != "" {
181-
ni.PublicIPAddress = publicIPAddress
182-
}
183-
}
184-
primaryConfigSet = true
185-
}
145+
ipConfigProps := ipConfig.GetMapValue("properties")
146+
ni.IPConfigurations = append(ni.IPConfigurations, network.IPConfiguration{
147+
Metadata: resource.Metadata,
148+
PublicIPAddress: ipConfigProps.GetMapValue("publicIPAddress").
149+
GetMapValue("id").AsStringValue("", resource.Metadata),
150+
SubnetID: ipConfigProps.GetMapValue("subnet").
151+
GetMapValue("id").AsStringValue("", resource.Metadata),
152+
Primary: ipConfigProps.GetMapValue("primary").AsBoolValue(false, resource.Metadata),
153+
})
186154
}
155+
ni.Setup()
187156

188157
// Note: SecurityGroups are not resolved for ARM templates as related resource search
189158
// is not yet implemented for ARM (parser cannot evaluate expressions/references)
190-
191159
return ni
192160
}

pkg/iac/adapters/arm/network/adapt_test.go

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,11 @@ func TestAdapt(t *testing.T) {
3535
}`,
3636
expected: network.Network{
3737
NetworkWatcherFlowLogs: []network.NetworkWatcherFlowLog{{
38-
Enabled: types.BoolTest(false),
39-
RetentionPolicy: network.RetentionPolicy{
40-
Days: types.IntTest(0),
41-
Enabled: types.BoolTest(false),
42-
},
38+
RetentionPolicy: network.RetentionPolicy{},
4339
}},
4440
SecurityGroups: []network.SecurityGroup{{
45-
Rules: []network.SecurityGroupRule{{
46-
DestinationAddresses: []types.StringValue{types.StringTest("")},
47-
SourceAddresses: []types.StringValue{types.StringTest("")},
48-
}},
41+
Rules: []network.SecurityGroupRule{{}},
4942
}},
50-
NetworkInterfaces: []network.NetworkInterface{},
5143
},
5244
},
5345
{
@@ -98,7 +90,6 @@ func TestAdapt(t *testing.T) {
9890
}`,
9991
expected: network.Network{
10092
NetworkWatcherFlowLogs: []network.NetworkWatcherFlowLog{{
101-
Enabled: types.BoolTest(false),
10293
RetentionPolicy: network.RetentionPolicy{
10394
Days: types.IntTest(100),
10495
Enabled: types.BoolTest(true),
@@ -148,7 +139,71 @@ func TestAdapt(t *testing.T) {
148139
},
149140
}},
150141
}},
151-
NetworkInterfaces: []network.NetworkInterface{},
142+
},
143+
},
144+
{
145+
name: "network interface with ip configurations",
146+
source: `{
147+
"resources": [
148+
{
149+
"type": "Microsoft.Network/networkInterfaces",
150+
"properties": {
151+
"enableIPForwarding": true,
152+
"ipConfigurations": [
153+
{
154+
"name": "primary-ip",
155+
"properties": {
156+
"primary": true,
157+
"subnet": {
158+
"id": "/subscriptions/abc/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/subnet-primary"
159+
},
160+
"publicIPAddress": {
161+
"id": "/subscriptions/abc/resourceGroups/rg/providers/Microsoft.Network/publicIPAddresses/pip-primary"
162+
}
163+
}
164+
},
165+
{
166+
"name": "secondary-ip",
167+
"properties": {
168+
"primary": false,
169+
"subnet": {
170+
"id": "/subscriptions/abc/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/subnet-secondary"
171+
}
172+
}
173+
}
174+
]
175+
}
176+
}
177+
]
178+
}`,
179+
expected: network.Network{
180+
NetworkInterfaces: []network.NetworkInterface{
181+
{
182+
EnableIPForwarding: types.BoolTest(true),
183+
184+
// backward compatibility — filled from primary config
185+
SubnetID: types.StringTest("/subscriptions/abc/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/subnet-primary"),
186+
HasPublicIP: types.BoolTest(true),
187+
PublicIPAddress: types.StringTest("/subscriptions/abc/resourceGroups/rg/providers/Microsoft.Network/publicIPAddresses/pip-primary"),
188+
189+
IPConfigurations: []network.IPConfiguration{
190+
{
191+
Primary: types.BoolTest(true),
192+
SubnetID: types.StringTest(
193+
"/subscriptions/abc/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/subnet-primary",
194+
),
195+
HasPublicIP: types.BoolTest(true),
196+
PublicIPAddress: types.StringTest("/subscriptions/abc/resourceGroups/rg/providers/Microsoft.Network/publicIPAddresses/pip-primary"),
197+
},
198+
{
199+
Primary: types.BoolTest(false),
200+
SubnetID: types.StringTest("/subscriptions/abc/resourceGroups/rg/providers/Microsoft.Network/virtualNetworks/vnet/subnets/subnet-secondary"),
201+
HasPublicIP: types.BoolTest(false),
202+
PublicIPAddress: types.StringTest(""),
203+
},
204+
},
205+
},
206+
},
152207
},
153208
},
154209
}

pkg/iac/adapters/terraform/azure/compute/adapt.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,12 @@ func adaptWindowsVM(resource *terraform.Block, modules terraform.Modules) comput
143143
}
144144

145145
func resolveNetworkInterfaces(resource *terraform.Block, modules terraform.Modules) []network.NetworkInterface {
146-
var networkInterfaces []network.NetworkInterface
147-
148146
nicIDsAttr := resource.GetAttribute("network_interface_ids")
149147
if nicIDsAttr.IsNil() {
150-
return networkInterfaces
148+
return nil
151149
}
152150

151+
var networkInterfaces []network.NetworkInterface
153152
for _, nicIDVal := range nicIDsAttr.AsStringValues() {
154153
if referencedNIC, err := modules.GetReferencedBlock(nicIDsAttr, resource); err == nil {
155154
ni := anetwork.AdaptNetworkInterface(referencedNIC, modules)
@@ -161,10 +160,8 @@ func resolveNetworkInterfaces(resource *terraform.Block, modules terraform.Modul
161160
Metadata: iacTypes.NewUnmanagedMetadata(),
162161
EnableIPForwarding: iacTypes.BoolDefault(false, nicIDVal.GetMetadata()),
163162
SubnetID: iacTypes.StringDefault("", nicIDVal.GetMetadata()),
164-
SecurityGroups: nil,
165163
HasPublicIP: iacTypes.BoolDefault(false, nicIDVal.GetMetadata()),
166164
PublicIPAddress: iacTypes.StringDefault("", nicIDVal.GetMetadata()),
167-
IPConfigurations: nil,
168165
})
169166
}
170167

0 commit comments

Comments
 (0)