Skip to content

Commit fa95bd6

Browse files
committed
refactor validation for application gateway
1 parent ea6c35d commit fa95bd6

File tree

1 file changed

+36
-70
lines changed

1 file changed

+36
-70
lines changed

internal/services/network/application_gateway_resource.go

Lines changed: 36 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,16 @@ func resourceApplicationGatewayCreate(d *pluginsdk.ResourceData, meta interface{
16101610
return fmt.Errorf("expanding `rewrite_rule_set`: %v", err)
16111611
}
16121612

1613+
backendHttpSettings, err := expandApplicationGatewayBackendHTTPSettings(d, id.ID())
1614+
if err != nil {
1615+
return fmt.Errorf("expanding `backend_http_settings`: %+v", err)
1616+
}
1617+
1618+
probes, err := expandApplicationGatewayProbes(d)
1619+
if err != nil {
1620+
return fmt.Errorf("expanding `probe`: %+v", err)
1621+
}
1622+
16131623
gateway := applicationgateways.ApplicationGateway{
16141624
Location: pointer.To(location.Normalize(d.Get("location").(string))),
16151625
Tags: tags.Expand(t),
@@ -1619,15 +1629,15 @@ func resourceApplicationGatewayCreate(d *pluginsdk.ResourceData, meta interface{
16191629
TrustedRootCertificates: trustedRootCertificates,
16201630
CustomErrorConfigurations: expandApplicationGatewayCustomErrorConfigurations(d.Get("custom_error_configuration").([]interface{})),
16211631
BackendAddressPools: expandApplicationGatewayBackendAddressPools(d),
1622-
BackendHTTPSettingsCollection: expandApplicationGatewayBackendHTTPSettings(d, id.ID()),
1632+
BackendHTTPSettingsCollection: backendHttpSettings,
16231633
EnableHTTP2: pointer.To(enablehttp2),
16241634
FrontendIPConfigurations: expandApplicationGatewayFrontendIPConfigurations(d, id.ID()),
16251635
FrontendPorts: expandApplicationGatewayFrontendPorts(d),
16261636
GatewayIPConfigurations: gatewayIPConfigurations,
16271637
GlobalConfiguration: globalConfiguration,
16281638
HTTPListeners: httpListeners,
16291639
PrivateLinkConfigurations: expandApplicationGatewayPrivateLinkConfigurations(d),
1630-
Probes: expandApplicationGatewayProbes(d),
1640+
Probes: probes,
16311641
RequestRoutingRules: requestRoutingRules,
16321642
RedirectConfigurations: redirectConfigurations,
16331643
Sku: expandApplicationGatewaySku(d),
@@ -1663,35 +1673,6 @@ func resourceApplicationGatewayCreate(d *pluginsdk.ResourceData, meta interface{
16631673
gateway.Identity = expandedIdentity
16641674
}
16651675

1666-
// validation (todo these should probably be moved into their respective expand functions, which would then return an error?)
1667-
for _, backendHttpSettings := range *gateway.Properties.BackendHTTPSettingsCollection {
1668-
if props := backendHttpSettings.Properties; props != nil {
1669-
if props.HostName == nil || props.PickHostNameFromBackendAddress == nil {
1670-
continue
1671-
}
1672-
1673-
if *props.HostName != "" && *props.PickHostNameFromBackendAddress {
1674-
return fmt.Errorf("Only one of `host_name` or `pick_host_name_from_backend_address` can be set")
1675-
}
1676-
}
1677-
}
1678-
1679-
for _, probe := range *gateway.Properties.Probes {
1680-
if props := probe.Properties; props != nil {
1681-
if props.Host == nil || props.PickHostNameFromBackendHTTPSettings == nil {
1682-
continue
1683-
}
1684-
1685-
if *props.Host == "" && !*props.PickHostNameFromBackendHTTPSettings {
1686-
return fmt.Errorf("One of `host` or `pick_host_name_from_backend_http_settings` must be set")
1687-
}
1688-
1689-
if *props.Host != "" && *props.PickHostNameFromBackendHTTPSettings {
1690-
return fmt.Errorf("Only one of `host` or `pick_host_name_from_backend_http_settings` can be set")
1691-
}
1692-
}
1693-
}
1694-
16951676
if _, ok := d.GetOk("waf_configuration"); ok {
16961677
gateway.Properties.WebApplicationFirewallConfiguration = expandApplicationGatewayWafConfig(d)
16971678
}
@@ -1852,7 +1833,12 @@ func resourceApplicationGatewayUpdate(d *pluginsdk.ResourceData, meta interface{
18521833
}
18531834

18541835
if d.HasChange("backend_http_settings") {
1855-
payload.Properties.BackendHTTPSettingsCollection = expandApplicationGatewayBackendHTTPSettings(d, id.ID())
1836+
backendHttpSettings, err := expandApplicationGatewayBackendHTTPSettings(d, id.ID())
1837+
if err != nil {
1838+
return fmt.Errorf("expanding `backend_http_settings`: %+v", err)
1839+
}
1840+
1841+
payload.Properties.BackendHTTPSettingsCollection = backendHttpSettings
18561842
}
18571843

18581844
if d.HasChange("frontend_ip_configuration") {
@@ -1868,7 +1854,12 @@ func resourceApplicationGatewayUpdate(d *pluginsdk.ResourceData, meta interface{
18681854
}
18691855

18701856
if d.HasChange("probe") {
1871-
payload.Properties.Probes = expandApplicationGatewayProbes(d)
1857+
probes, err := expandApplicationGatewayProbes(d)
1858+
if err != nil {
1859+
return fmt.Errorf("expanding `probe`: %+v", err)
1860+
}
1861+
1862+
payload.Properties.Probes = probes
18721863
}
18731864

18741865
if d.HasChange("sku") {
@@ -1903,39 +1894,6 @@ func resourceApplicationGatewayUpdate(d *pluginsdk.ResourceData, meta interface{
19031894
payload.Identity = expandedIdentity
19041895
}
19051896

1906-
// validation (todo these should probably be moved into their respective expand functions, which would then return an error?)
1907-
if payload.Properties != nil && payload.Properties.BackendHTTPSettingsCollection != nil {
1908-
for _, backendHttpSettings := range *payload.Properties.BackendHTTPSettingsCollection {
1909-
if props := backendHttpSettings.Properties; props != nil {
1910-
if props.HostName == nil || props.PickHostNameFromBackendAddress == nil {
1911-
continue
1912-
}
1913-
1914-
if *props.HostName != "" && *props.PickHostNameFromBackendAddress {
1915-
return fmt.Errorf("Only one of `host_name` or `pick_host_name_from_backend_address` can be set")
1916-
}
1917-
}
1918-
}
1919-
}
1920-
1921-
if payload.Properties != nil && payload.Properties.Probes != nil {
1922-
for _, probe := range *payload.Properties.Probes {
1923-
if props := probe.Properties; props != nil {
1924-
if props.Host == nil || props.PickHostNameFromBackendHTTPSettings == nil {
1925-
continue
1926-
}
1927-
1928-
if *props.Host == "" && !*props.PickHostNameFromBackendHTTPSettings {
1929-
return fmt.Errorf("One of `host` or `pick_host_name_from_backend_http_settings` must be set")
1930-
}
1931-
1932-
if *props.Host != "" && *props.PickHostNameFromBackendHTTPSettings {
1933-
return fmt.Errorf("Only one of `host` or `pick_host_name_from_backend_http_settings` can be set")
1934-
}
1935-
}
1936-
}
1937-
}
1938-
19391897
if d.HasChange("waf_configuration") {
19401898
payload.Properties.WebApplicationFirewallConfiguration = expandApplicationGatewayWafConfig(d)
19411899
}
@@ -2396,7 +2354,7 @@ func flattenApplicationGatewayBackendAddressPools(input *[]applicationgateways.A
23962354
return results
23972355
}
23982356

2399-
func expandApplicationGatewayBackendHTTPSettings(d *pluginsdk.ResourceData, gatewayID string) *[]applicationgateways.ApplicationGatewayBackendHTTPSettings {
2357+
func expandApplicationGatewayBackendHTTPSettings(d *pluginsdk.ResourceData, gatewayID string) (*[]applicationgateways.ApplicationGatewayBackendHTTPSettings, error) {
24002358
results := make([]applicationgateways.ApplicationGatewayBackendHTTPSettings, 0)
24012359
vs := d.Get("backend_http_settings").(*schema.Set).List()
24022360

@@ -2426,6 +2384,10 @@ func expandApplicationGatewayBackendHTTPSettings(d *pluginsdk.ResourceData, gate
24262384

24272385
hostName := v["host_name"].(string)
24282386
if hostName != "" {
2387+
if pickHostNameFromBackendAddress {
2388+
return nil, fmt.Errorf("Only one of `host_name` or `pick_host_name_from_backend_address` can be set")
2389+
}
2390+
24292391
setting.Properties.HostName = pointer.To(hostName)
24302392
}
24312393

@@ -2480,7 +2442,7 @@ func expandApplicationGatewayBackendHTTPSettings(d *pluginsdk.ResourceData, gate
24802442
results = append(results, setting)
24812443
}
24822444

2483-
return &results
2445+
return &results, nil
24842446
}
24852447

24862448
func flattenApplicationGatewayBackendHTTPSettings(input *[]applicationgateways.ApplicationGatewayBackendHTTPSettings) ([]interface{}, error) {
@@ -3120,7 +3082,7 @@ func flattenApplicationGatewayFrontendIPConfigurations(input *[]applicationgatew
31203082
return results, nil
31213083
}
31223084

3123-
func expandApplicationGatewayProbes(d *pluginsdk.ResourceData) *[]applicationgateways.ApplicationGatewayProbe {
3085+
func expandApplicationGatewayProbes(d *pluginsdk.ResourceData) (*[]applicationgateways.ApplicationGatewayProbe, error) {
31243086
vs := d.Get("probe").(*schema.Set).List()
31253087
results := make([]applicationgateways.ApplicationGatewayProbe, 0)
31263088

@@ -3138,6 +3100,10 @@ func expandApplicationGatewayProbes(d *pluginsdk.ResourceData) *[]applicationgat
31383100
unhealthyThreshold := int64(v["unhealthy_threshold"].(int))
31393101
pickHostNameFromBackendHTTPSettings := v["pick_host_name_from_backend_http_settings"].(bool)
31403102

3103+
if (host == "" && !pickHostNameFromBackendHTTPSettings) || (host != "" && pickHostNameFromBackendHTTPSettings) {
3104+
return nil, fmt.Errorf("Exactly one of `host` or `pick_host_name_from_backend_http_settings` must be set/enabled")
3105+
}
3106+
31413107
output := applicationgateways.ApplicationGatewayProbe{
31423108
Name: pointer.To(name),
31433109
Properties: &applicationgateways.ApplicationGatewayProbePropertiesFormat{
@@ -3177,7 +3143,7 @@ func expandApplicationGatewayProbes(d *pluginsdk.ResourceData) *[]applicationgat
31773143
results = append(results, output)
31783144
}
31793145

3180-
return &results
3146+
return &results, nil
31813147
}
31823148

31833149
func flattenApplicationGatewayProbes(input *[]applicationgateways.ApplicationGatewayProbe) []interface{} {

0 commit comments

Comments
 (0)