Skip to content

Commit f9e3358

Browse files
authored
Feature gap: Add labels and label_fingerprint fields to google_compute_security_policy (#14821)
Signed-off-by: Cezary Sobczak <[email protected]>
1 parent 8dc43a2 commit f9e3358

File tree

4 files changed

+181
-6
lines changed

4 files changed

+181
-6
lines changed

mmv1/third_party/terraform/services/compute/resource_compute_security_policy.go.tmpl

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func ResourceComputeSecurityPolicy() *schema.Resource {
6565
},
6666
CustomizeDiff: customdiff.All(
6767
tpgresource.DefaultProviderProject,
68+
tpgresource.SetLabelsDiff,
6869
rulesCustomizeDiff,
6970
),
7071

@@ -701,8 +702,36 @@ func ResourceComputeSecurityPolicy() *schema.Resource {
701702
},
702703
},
703704
},
704-
},
705+
"labels": {
706+
Type: schema.TypeMap,
707+
Optional: true,
708+
Elem: &schema.Schema{
709+
Type: schema.TypeString,
710+
},
711+
Description: `Labels to apply to this address. A list of key->value pairs.
712+
705713

714+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
715+
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
716+
},
717+
"terraform_labels": {
718+
Type: schema.TypeMap,
719+
Computed: true,
720+
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
721+
Elem: &schema.Schema{Type: schema.TypeString},
722+
},
723+
"effective_labels": {
724+
Type: schema.TypeMap,
725+
Computed: true,
726+
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
727+
Elem: &schema.Schema{Type: schema.TypeString},
728+
},
729+
"label_fingerprint": {
730+
Type: schema.TypeString,
731+
Computed: true,
732+
Description: `The unique fingerprint of the labels.`,
733+
},
734+
},
706735
UseJSONNumber: true,
707736
}
708737
}
@@ -748,7 +777,7 @@ func rulesCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, _ interfac
748777

749778
func resourceComputeSecurityPolicyCreate(d *schema.ResourceData, meta interface{}) error {
750779
config := meta.(*transport_tpg.Config)
751-
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
780+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
752781
if err != nil {
753782
return err
754783
}
@@ -772,17 +801,17 @@ func resourceComputeSecurityPolicyCreate(d *schema.ResourceData, meta interface{
772801
securityPolicy.Rules = expandSecurityPolicyRules(v.(*schema.Set).List())
773802
}
774803

775-
if v, ok := d.GetOk("advanced_options_config"); ok{
804+
if v, ok := d.GetOk("advanced_options_config"); ok {
776805
securityPolicy.AdvancedOptionsConfig = expandSecurityPolicyAdvancedOptionsConfig(v.([]interface{}))
777806
}
778807

779-
if v, ok := d.GetOk("adaptive_protection_config"); ok{
808+
if v, ok := d.GetOk("adaptive_protection_config"); ok {
780809
securityPolicy.AdaptiveProtectionConfig = expandSecurityPolicyAdaptiveProtectionConfig(v.([]interface{}))
781810
}
782811

783812
log.Printf("[DEBUG] SecurityPolicy insert request: %#v", securityPolicy)
784813

785-
if v, ok := d.GetOk("recaptcha_options_config"); ok{
814+
if v, ok := d.GetOk("recaptcha_options_config"); ok {
786815
securityPolicy.RecaptchaOptionsConfig = expandSecurityPolicyRecaptchaOptionsConfig(v.([]interface{}), d)
787816
}
788817

@@ -805,6 +834,48 @@ func resourceComputeSecurityPolicyCreate(d *schema.ResourceData, meta interface{
805834
return err
806835
}
807836

837+
if effectiveLabels := tpgresource.ExpandEffectiveLabels(d); effectiveLabels != nil {
838+
userLabels := d.Get("labels")
839+
terraformLabels := d.Get("terraform_labels")
840+
841+
// Labels cannot be set in a create. We'll have to set them here.
842+
err = resourceComputeSecurityPolicyRead(d, meta)
843+
if err != nil {
844+
return err
845+
}
846+
847+
// Now we can set the labels
848+
setLabels := &compute.GlobalSetLabelsRequest{
849+
Labels: effectiveLabels,
850+
LabelFingerprint: d.Get("label_fingerprint").(string),
851+
}
852+
853+
op, err = client.SecurityPolicies.SetLabels(project, sp, setLabels).Do()
854+
if err != nil {
855+
return err
856+
}
857+
858+
err = ComputeOperationWaitTime(config, op, project, fmt.Sprintf("Creating SecurityPolicy.Labels %q", sp), userAgent, d.Timeout(schema.TimeoutCreate))
859+
if err != nil {
860+
return err
861+
}
862+
863+
// Set back the labels field, as it is needed to decide the value of "labels" in the state in the read function.
864+
if err := d.Set("labels", userLabels); err != nil {
865+
return fmt.Errorf("Error setting back labels: %s", err)
866+
}
867+
868+
// Set back the terraform_labels field, as it is needed to decide the value of "terraform_labels" in the state in the read function.
869+
if err := d.Set("terraform_labels", terraformLabels); err != nil {
870+
return fmt.Errorf("Error setting back terraform_labels: %s", err)
871+
}
872+
873+
// Set back the effective_labels field, as it is needed to decide the value of "effective_labels" in the state in the read function.
874+
if err := d.Set("effective_labels", effectiveLabels); err != nil {
875+
return fmt.Errorf("Error setting back effective_labels: %s", err)
876+
}
877+
}
878+
808879
return resourceComputeSecurityPolicyRead(d, meta)
809880
}
810881

@@ -862,6 +933,22 @@ func resourceComputeSecurityPolicyRead(d *schema.ResourceData, meta interface{})
862933
return fmt.Errorf("Error setting recaptcha_options_config: %s", err)
863934
}
864935

936+
if err := tpgresource.SetLabels(securityPolicy.Labels, d, "labels"); err != nil {
937+
return err
938+
}
939+
940+
if err := tpgresource.SetLabels(securityPolicy.Labels, d, "terraform_labels"); err != nil {
941+
return err
942+
}
943+
944+
if err := d.Set("effective_labels", securityPolicy.Labels); err != nil {
945+
return err
946+
}
947+
948+
if err := d.Set("label_fingerprint", securityPolicy.LabelFingerprint); err != nil {
949+
return fmt.Errorf("Error setting label_fingerprint: %s", err)
950+
}
951+
865952
return nil
866953
}
867954

@@ -923,6 +1010,22 @@ func resourceComputeSecurityPolicyUpdate(d *schema.ResourceData, meta interface{
9231010
securityPolicy.ForceSendFields = append(securityPolicy.ForceSendFields, "RecaptchaOptionsConfig")
9241011
}
9251012

1013+
if d.HasChange("effective_labels") {
1014+
labels := tpgresource.ExpandEffectiveLabels(d)
1015+
labelFingerprint := d.Get("label_fingerprint").(string)
1016+
req := compute.GlobalSetLabelsRequest{Labels: labels, LabelFingerprint: labelFingerprint}
1017+
1018+
op, err := config.NewComputeClient(userAgent).SecurityPolicies.SetLabels(project, sp, &req).Do()
1019+
if err != nil {
1020+
return fmt.Errorf("Error updating labels: %s", err)
1021+
}
1022+
1023+
opErr := ComputeOperationWaitTime(config, op, project, "labels to update", userAgent, d.Timeout(schema.TimeoutUpdate))
1024+
if opErr != nil {
1025+
return opErr
1026+
}
1027+
}
1028+
9261029
if len(securityPolicy.ForceSendFields) > 0 {
9271030
client := config.NewComputeClient(userAgent)
9281031

mmv1/third_party/terraform/services/compute/resource_compute_security_policy_rule_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package compute_test
22

33
import (
44
"fmt"
5-
"github.com/hashicorp/terraform-provider-google/google/acctest"
65
"regexp"
76
"testing"
87

8+
"github.com/hashicorp/terraform-provider-google/google/acctest"
9+
910
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1011
)
1112

mmv1/third_party/terraform/services/compute/resource_compute_security_policy_test.go.tmpl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,38 @@ func TestAccComputeSecurityPolicy_modifyExprOptions(t *testing.T) {
774774
})
775775
}
776776

777+
func TestAccComputeSecurityPolicy_labels(t *testing.T) {
778+
t.Parallel()
779+
780+
spName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
781+
782+
acctest.VcrTest(t, resource.TestCase{
783+
PreCheck: func() { acctest.AccTestPreCheck(t) },
784+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
785+
CheckDestroy: testAccCheckComputeSecurityPolicyDestroyProducer(t),
786+
Steps: []resource.TestStep{
787+
{
788+
Config: testAccComputeSecurityPolicy_basicLabels(spName),
789+
},
790+
{
791+
ResourceName: "google_compute_security_policy.policy",
792+
ImportState: true,
793+
ImportStateVerify: true,
794+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
795+
},
796+
{
797+
Config: testAccComputeSecurityPolicy_updateLabels(spName),
798+
},
799+
{
800+
ResourceName: "google_compute_security_policy.policy",
801+
ImportState: true,
802+
ImportStateVerify: true,
803+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
804+
},
805+
},
806+
})
807+
}
808+
777809
func testAccComputeSecurityPolicy_withRecaptchaOptionsConfig(project, spName string) string {
778810
return fmt.Sprintf(`
779811
resource "google_recaptcha_enterprise_key" "primary" {
@@ -2230,3 +2262,32 @@ resource "google_compute_security_policy" "policy" {
22302262
}
22312263
`, spName)
22322264
}
2265+
2266+
func testAccComputeSecurityPolicy_basicLabels(spName string) string {
2267+
return fmt.Sprintf(`
2268+
resource "google_compute_security_policy" "policy" {
2269+
name = "%s"
2270+
description = "basic security policy"
2271+
type = "CLOUD_ARMOR"
2272+
2273+
labels = {
2274+
"env" = "test"
2275+
}
2276+
}
2277+
`, spName)
2278+
}
2279+
2280+
func testAccComputeSecurityPolicy_updateLabels(spName string) string {
2281+
return fmt.Sprintf(`
2282+
resource "google_compute_security_policy" "policy" {
2283+
name = "%s"
2284+
description = "basic security policy"
2285+
type = "CLOUD_ARMOR"
2286+
2287+
labels = {
2288+
"env" = "test",
2289+
"new_label" = "abcd1"
2290+
}
2291+
}
2292+
`, spName)
2293+
}

mmv1/third_party/terraform/website/docs/r/compute_security_policy.html.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ The following arguments are supported:
191191
* `CLOUD_ARMOR_INTERNAL_SERVICE` - Cloud Armor internal service policies can be configured to filter HTTP requests targeting services
192192
managed by Traffic Director in a service mesh. They filter requests before the request is served from the application.
193193

194+
* `labels` - Labels to apply to this address. A list of key->value pairs.
195+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
196+
Please refer to the field `effective_labels` for all of the labels present on the resource.
197+
198+
* `effective_labels` - All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.
199+
200+
* `terraform_labels` - The combination of labels configured directly on the resource and default labels configured on the provider.
201+
202+
* `label_fingerprint` - The unique fingerprint of the labels.
203+
194204
<a name="nested_advanced_options_config"></a>The `advanced_options_config` block supports:
195205

196206
* `json_parsing` - Whether or not to JSON parse the payload body. Defaults to `DISABLED`.

0 commit comments

Comments
 (0)