Skip to content

Commit aa31ad3

Browse files
Promote user_ip_request_headers field on google_compute_security_policy resource to GA (#9872) (#17271)
[upstream:379d462fa7096c0f4789fc9e75463320603b14e2] Signed-off-by: Modular Magician <[email protected]>
1 parent cfe4774 commit aa31ad3

File tree

4 files changed

+126
-9
lines changed

4 files changed

+126
-9
lines changed

.changelog/9872.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: promoted `user_ip_request_headers` field on `google_compute_security_policy` resource to GA
3+
```

google/services/compute/resource_compute_security_policy.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"fmt"
88
"log"
9+
"strings"
910

1011
"time"
1112

@@ -387,6 +388,12 @@ func ResourceComputeSecurityPolicy() *schema.Resource {
387388
ValidateFunc: validation.StringInSlice([]string{"NORMAL", "VERBOSE"}, false),
388389
Description: `Logging level. Supported values include: "NORMAL", "VERBOSE".`,
389390
},
391+
"user_ip_request_headers": {
392+
Type: schema.TypeSet,
393+
Optional: true,
394+
Description: `An optional list of case-insensitive request header names to use for resolving the callers client IP address.`,
395+
Elem: &schema.Schema{Type: schema.TypeString},
396+
},
390397
},
391398
},
392399
},
@@ -597,6 +604,8 @@ func resourceComputeSecurityPolicyUpdate(d *schema.ResourceData, meta interface{
597604
Fingerprint: d.Get("fingerprint").(string),
598605
}
599606

607+
updateMask := []string{}
608+
600609
if d.HasChange("type") {
601610
securityPolicy.Type = d.Get("type").(string)
602611
securityPolicy.ForceSendFields = append(securityPolicy.ForceSendFields, "Type")
@@ -610,6 +619,11 @@ func resourceComputeSecurityPolicyUpdate(d *schema.ResourceData, meta interface{
610619
if d.HasChange("advanced_options_config") {
611620
securityPolicy.AdvancedOptionsConfig = expandSecurityPolicyAdvancedOptionsConfig(d.Get("advanced_options_config").([]interface{}))
612621
securityPolicy.ForceSendFields = append(securityPolicy.ForceSendFields, "AdvancedOptionsConfig", "advancedOptionsConfig.jsonParsing", "advancedOptionsConfig.jsonCustomConfig", "advancedOptionsConfig.logLevel")
622+
securityPolicy.ForceSendFields = append(securityPolicy.ForceSendFields, "advanceOptionConfig.userIpRequestHeaders")
623+
if len(securityPolicy.AdvancedOptionsConfig.UserIpRequestHeaders) == 0 {
624+
// to clean this list we must send the updateMask of this field on the request.
625+
updateMask = append(updateMask, "advanced_options_config.user_ip_request_headers")
626+
}
613627
}
614628

615629
if d.HasChange("adaptive_protection_config") {
@@ -625,7 +639,7 @@ func resourceComputeSecurityPolicyUpdate(d *schema.ResourceData, meta interface{
625639
if len(securityPolicy.ForceSendFields) > 0 {
626640
client := config.NewComputeClient(userAgent)
627641

628-
op, err := client.SecurityPolicies.Patch(project, sp, securityPolicy).Do()
642+
op, err := client.SecurityPolicies.Patch(project, sp, securityPolicy).UpdateMask(strings.Join(updateMask, ",")).Do()
629643

630644
if err != nil {
631645
return errwrap.Wrapf(fmt.Sprintf("Error updating SecurityPolicy %q: {{err}}", sp), err)
@@ -862,9 +876,10 @@ func expandSecurityPolicyAdvancedOptionsConfig(configured []interface{}) *comput
862876

863877
data := configured[0].(map[string]interface{})
864878
return &compute.SecurityPolicyAdvancedOptionsConfig{
865-
JsonParsing: data["json_parsing"].(string),
866-
JsonCustomConfig: expandSecurityPolicyAdvancedOptionsConfigJsonCustomConfig(data["json_custom_config"].([]interface{})),
867-
LogLevel: data["log_level"].(string),
879+
JsonParsing: data["json_parsing"].(string),
880+
JsonCustomConfig: expandSecurityPolicyAdvancedOptionsConfigJsonCustomConfig(data["json_custom_config"].([]interface{})),
881+
LogLevel: data["log_level"].(string),
882+
UserIpRequestHeaders: tpgresource.ConvertStringArr(data["user_ip_request_headers"].(*schema.Set).List()),
868883
}
869884
}
870885

@@ -874,9 +889,10 @@ func flattenSecurityPolicyAdvancedOptionsConfig(conf *compute.SecurityPolicyAdva
874889
}
875890

876891
data := map[string]interface{}{
877-
"json_parsing": conf.JsonParsing,
878-
"json_custom_config": flattenSecurityPolicyAdvancedOptionsConfigJsonCustomConfig(conf.JsonCustomConfig),
879-
"log_level": conf.LogLevel,
892+
"json_parsing": conf.JsonParsing,
893+
"json_custom_config": flattenSecurityPolicyAdvancedOptionsConfigJsonCustomConfig(conf.JsonCustomConfig),
894+
"log_level": conf.LogLevel,
895+
"user_ip_request_headers": schema.NewSet(schema.HashString, tpgresource.ConvertStringArrToInterface(conf.UserIpRequestHeaders)),
880896
}
881897

882898
return []map[string]interface{}{data}

google/services/compute/resource_compute_security_policy_test.go

+99-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,32 @@ func TestAccComputeSecurityPolicy_withAdvancedOptionsConfig(t *testing.T) {
150150
ImportState: true,
151151
ImportStateVerify: true,
152152
},
153+
{
154+
Config: testAccComputeSecurityPolicy_withAdvancedOptionsConfig_update(spName),
155+
},
156+
{
157+
ResourceName: "google_compute_security_policy.policy",
158+
ImportState: true,
159+
ImportStateVerify: true,
160+
},
161+
// change all AdvancedOptionConfig values.
162+
{
163+
Config: testAccComputeSecurityPolicy_withAdvancedOptionsConfig_update2(spName),
164+
},
165+
{
166+
ResourceName: "google_compute_security_policy.policy",
167+
ImportState: true,
168+
ImportStateVerify: true,
169+
},
170+
// Swap to json_parsing = STANDARD_WITH_GRAPHQL
171+
{
172+
Config: testAccComputeSecurityPolicy_withAdvancedOptionsConfig_update3(spName),
173+
},
174+
{
175+
ResourceName: "google_compute_security_policy.policy",
176+
ImportState: true,
177+
ImportStateVerify: true,
178+
},
153179
{
154180
Config: testAccComputeSecurityPolicy_basic(spName),
155181
},
@@ -736,7 +762,79 @@ resource "google_compute_security_policy" "policy" {
736762
]
737763
}
738764
log_level = "VERBOSE"
739-
765+
user_ip_request_headers = [
766+
"True-Client-IP",
767+
"x-custom-ip"
768+
]
769+
}
770+
}
771+
`, spName)
772+
}
773+
774+
func testAccComputeSecurityPolicy_withAdvancedOptionsConfig_update(spName string) string {
775+
return fmt.Sprintf(`
776+
resource "google_compute_security_policy" "policy" {
777+
name = "%s"
778+
description = "updated description changing the user_ip"
779+
780+
advanced_options_config {
781+
json_parsing = "STANDARD"
782+
json_custom_config {
783+
content_types = [
784+
"application/json",
785+
"application/vnd.api+json",
786+
"application/vnd.collection+json",
787+
"application/vnd.hyper+json"
788+
]
789+
}
790+
log_level = "VERBOSE"
791+
user_ip_request_headers = [
792+
"x-custom-ip",
793+
]
794+
}
795+
}
796+
`, spName)
797+
}
798+
799+
func testAccComputeSecurityPolicy_withAdvancedOptionsConfig_update2(spName string) string {
800+
return fmt.Sprintf(`
801+
resource "google_compute_security_policy" "policy" {
802+
name = "%s"
803+
description = "updated description changing all advancedOptionsConfig values"
804+
805+
advanced_options_config {
806+
json_parsing = "DISABLED"
807+
json_custom_config {
808+
content_types = [
809+
"application/json",
810+
"application/vnd.hyper+json"
811+
]
812+
}
813+
log_level = "NORMAL"
814+
user_ip_request_headers = [
815+
]
816+
}
817+
}
818+
`, spName)
819+
}
820+
821+
func testAccComputeSecurityPolicy_withAdvancedOptionsConfig_update3(spName string) string {
822+
return fmt.Sprintf(`
823+
resource "google_compute_security_policy" "policy" {
824+
name = "%s"
825+
description = "updated description changing json_parsing to STANDARD_WITH_GRAPHQL"
826+
827+
advanced_options_config {
828+
json_parsing = "STANDARD_WITH_GRAPHQL"
829+
json_custom_config {
830+
content_types = [
831+
"application/json",
832+
"application/vnd.hyper+json"
833+
]
834+
}
835+
log_level = "NORMAL"
836+
user_ip_request_headers = [
837+
]
740838
}
741839
}
742840
`, spName)

website/docs/r/compute_security_policy.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ The following arguments are supported:
204204
* `NORMAL` - Normal log level.
205205
* `VERBOSE` - Verbose log level.
206206

207-
* `user_ip_request_headers` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) An optional list of case-insensitive request header names to use for resolving the callers client IP address.
207+
* `user_ip_request_headers` - (Optional) An optional list of case-insensitive request header names to use for resolving the callers client IP address.
208208

209209
<a name="nested_json_custom_config"></a>The `json_custom_config` block supports:
210210

0 commit comments

Comments
 (0)