Skip to content

Commit 26ba328

Browse files
authored
Merge pull request #1552 from vmware/protect-empty-block
Protect casting against nil conversion
2 parents e847566 + b5c4fb6 commit 26ba328

6 files changed

+23
-16
lines changed

nsxt/resource_nsxt_policy_intrusion_service_profile.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func getIdsProfileCriteriaFromSchema(d *schema.ResourceData) ([]*data.StructValu
185185
var result []*data.StructValue
186186

187187
criteria := d.Get("criteria").([]interface{})
188-
if len(criteria) == 0 {
188+
if len(criteria) == 0 || criteria[0] == nil {
189189
return result, nil
190190
}
191191

@@ -286,7 +286,10 @@ func setIdsProfileCriteriaInSchema(criteriaList []*data.StructValue, d *schema.R
286286
criteriaMap["products_affected"] = criteria.FilterValue
287287
}
288288
}
289-
schemaList = append(schemaList, criteriaMap)
289+
290+
if len(criteriaMap) > 0 {
291+
schemaList = append(schemaList, criteriaMap)
292+
}
290293

291294
return d.Set("criteria", schemaList)
292295
}

nsxt/resource_nsxt_policy_tier0_gateway.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func getPolicyVRFConfigFromSchema(d *schema.ResourceData) *model.Tier0VrfConfig
446446
}
447447

448448
vrfConfigs := d.Get("vrf_config").([]interface{})
449-
if len(vrfConfigs) == 0 {
449+
if len(vrfConfigs) == 0 || vrfConfigs[0] == nil {
450450
return nil
451451
}
452452

@@ -786,7 +786,7 @@ func policyTier0GatewayResourceToInfraStruct(context utl.SessionContext, d *sche
786786

787787
bgpConfig := d.Get("bgp_config").([]interface{})
788788
// no need to include BGP child block if it didn't change
789-
if d.HasChange("bgp_config") && len(bgpConfig) > 0 && !isGlobalManager {
789+
if d.HasChange("bgp_config") && len(bgpConfig) > 0 && bgpConfig[0] != nil && !isGlobalManager {
790790
// For Global Manager BGP is defined as separate resource
791791
routingConfigStruct := resourceNsxtPolicyTier0GatewayBGPConfigSchemaToStruct(bgpConfig[0], vrfConfig != nil, id)
792792
structValue, err := initPolicyTier0ChildBgpConfig(&routingConfigStruct)

nsxt/resource_nsxt_policy_tier0_gateway_interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func gatewayInterfaceVersionDepenantSet(d *schema.ResourceData, m interface{}, o
174174

175175
func policyTier0GatewayInterfaceOspfSet(d *schema.ResourceData, m interface{}, obj *model.Tier0Interface) error {
176176
ospfConfigs := d.Get("ospf").([]interface{})
177-
if len(ospfConfigs) == 0 {
177+
if len(ospfConfigs) == 0 || ospfConfigs[0] == nil {
178178
return nil
179179
}
180180

nsxt/resource_nsxt_upgrade_run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ func getPostResetGroupIDFromPreResetList(groupID string, preResetGroupList model
781781

782782
func updateComponentUpgradePlanSetting(settingClient plan.SettingsClient, d *schema.ResourceData, component string) error {
783783
settingI := d.Get(componentToSettingKey[component]).([]interface{})
784-
if len(settingI) == 0 {
784+
if len(settingI) == 0 || settingI[0] == nil {
785785
return nil
786786
}
787787

nsxt/segment_common.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func getSegmentSubnetDhcpConfigFromSchema(schemaConfig map[string]interface{}) (
509509

510510
converter := bindings.NewTypeConverter()
511511

512-
if len(dhcpV4Config) > 0 {
512+
if len(dhcpV4Config) > 0 && dhcpV4Config[0] != nil {
513513
dhcpConfig := dhcpV4Config[0].(map[string]interface{})
514514
serverAddress := dhcpConfig["server_address"].(string)
515515
dnsServers := dhcpConfig["dns_servers"].([]interface{})
@@ -538,7 +538,7 @@ func getSegmentSubnetDhcpConfigFromSchema(schemaConfig map[string]interface{}) (
538538
return dataValue.(*data.StructValue), nil
539539
}
540540

541-
if len(dhcpV6Config) > 0 {
541+
if len(dhcpV6Config) > 0 && dhcpV6Config[0] != nil {
542542
dhcpConfig := dhcpV6Config[0].(map[string]interface{})
543543
serverAddress := dhcpConfig["server_address"].(string)
544544
dnsServers := dhcpConfig["dns_servers"].([]interface{})
@@ -793,7 +793,7 @@ func policySegmentResourceToInfraStruct(context utl.SessionContext, id string, d
793793
obj.Subnets = subnetStructs
794794

795795
advConfig := d.Get("advanced_config").([]interface{})
796-
if len(advConfig) > 0 {
796+
if len(advConfig) > 0 && advConfig[0] != nil {
797797
advConfigMap := advConfig[0].(map[string]interface{})
798798
connectivity := advConfigMap["connectivity"].(string)
799799
hybrid := advConfigMap["hybrid"].(bool)
@@ -987,8 +987,10 @@ func nsxtPolicySegmentDiscoveryProfileSetInStruct(d *schema.ResourceData) (*data
987987
revision := int64(0)
988988
shouldDelete := false
989989
oldProfiles, newProfiles := d.GetChange("discovery_profile")
990-
if len(newProfiles.([]interface{})) > 0 {
991-
profileMap := newProfiles.([]interface{})[0].(map[string]interface{})
990+
newProfilesList := newProfiles.([]interface{})
991+
992+
if len(newProfilesList) > 0 && newProfilesList[0] != nil {
993+
profileMap := newProfilesList[0].(map[string]interface{})
992994

993995
ipDiscoveryProfilePath = profileMap["ip_discovery_profile_path"].(string)
994996
macDiscoveryProfilePath = profileMap["mac_discovery_profile_path"].(string)
@@ -1046,9 +1048,10 @@ func nsxtPolicySegmentQosProfileSetInStruct(d *schema.ResourceData) (*data.Struc
10461048
qosProfilePath := ""
10471049
revision := int64(0)
10481050
oldProfiles, newProfiles := d.GetChange("qos_profile")
1051+
newProfilesList := newProfiles.([]interface{})
10491052
shouldDelete := false
1050-
if len(newProfiles.([]interface{})) > 0 {
1051-
profileMap := newProfiles.([]interface{})[0].(map[string]interface{})
1053+
if len(newProfilesList) > 0 && newProfilesList[0] != nil {
1054+
profileMap := newProfilesList[0].(map[string]interface{})
10521055

10531056
qosProfilePath = profileMap["qos_profile_path"].(string)
10541057
if len(profileMap["binding_map_path"].(string)) > 0 {
@@ -1103,9 +1106,10 @@ func nsxtPolicySegmentSecurityProfileSetInStruct(d *schema.ResourceData) (*data.
11031106
securityProfilePath := ""
11041107
revision := int64(0)
11051108
oldProfiles, newProfiles := d.GetChange("security_profile")
1109+
newProfilesList := newProfiles.([]interface{})
11061110
shouldDelete := false
1107-
if len(newProfiles.([]interface{})) > 0 {
1108-
profileMap := newProfiles.([]interface{})[0].(map[string]interface{})
1111+
if len(newProfilesList) > 0 && newProfilesList[0] != nil {
1112+
profileMap := newProfilesList[0].(map[string]interface{})
11091113

11101114
spoofguardProfilePath = profileMap["spoofguard_profile_path"].(string)
11111115
securityProfilePath = profileMap["security_profile_path"].(string)

website/docs/r/policy_intrusion_service_profile.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ The following arguments are supported:
8181
* `context` - (Optional) The context which the object belongs to
8282
* `project_id` - (Required) The ID of the project which the object belongs to
8383
* `severities` - (Required) List of profile severities, supported values are `LOW`, `MEDIUM`, `HIGH`, 'CRITICAL`.
84-
* `criteria` - (Required) Filtering criteria for the IDS Profile.
84+
* `criteria` - (Optional) Filtering criteria for the IDS Profile.
8585
* `attack_types` - (Optional) List of supported attack types.
8686
* `attack_targets` - (Optional) List of supported attack targets. Please refer to example above to ensure correct formatting - in some versions, UI shows a different format than NSX expects.
8787
* `cvss` - (Optional) List of CVSS (Common Vulnerability Scoring System) ranges. Supported values are `NONE`, `LOW`, `MEDIUM`, `HIGH`, `CRITICAL`.

0 commit comments

Comments
 (0)