Skip to content

Commit 48aaa72

Browse files
Feature gap: Add labels and label_fingerprint fields to google_compute_security_policy (#14821) (#24322)
[upstream:f9e335833183179c4d0601f7246be95dd95bb020] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent c4695d2 commit 48aaa72

File tree

5 files changed

+180
-2
lines changed

5 files changed

+180
-2
lines changed

.changelog/14821.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `labels` and `label_fingerprint` fields to `google_compute_security_policy` resource
3+
```

google/services/compute/resource_compute_security_policy.go

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func ResourceComputeSecurityPolicy() *schema.Resource {
7777
},
7878
CustomizeDiff: customdiff.All(
7979
tpgresource.DefaultProviderProject,
80+
tpgresource.SetLabelsDiff,
8081
rulesCustomizeDiff,
8182
),
8283

@@ -672,8 +673,36 @@ func ResourceComputeSecurityPolicy() *schema.Resource {
672673
},
673674
},
674675
},
675-
},
676+
"labels": {
677+
Type: schema.TypeMap,
678+
Optional: true,
679+
Elem: &schema.Schema{
680+
Type: schema.TypeString,
681+
},
682+
Description: `Labels to apply to this address. A list of key->value pairs.
683+
676684
685+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
686+
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
687+
},
688+
"terraform_labels": {
689+
Type: schema.TypeMap,
690+
Computed: true,
691+
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
692+
Elem: &schema.Schema{Type: schema.TypeString},
693+
},
694+
"effective_labels": {
695+
Type: schema.TypeMap,
696+
Computed: true,
697+
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
698+
Elem: &schema.Schema{Type: schema.TypeString},
699+
},
700+
"label_fingerprint": {
701+
Type: schema.TypeString,
702+
Computed: true,
703+
Description: `The unique fingerprint of the labels.`,
704+
},
705+
},
677706
UseJSONNumber: true,
678707
}
679708
}
@@ -776,6 +805,48 @@ func resourceComputeSecurityPolicyCreate(d *schema.ResourceData, meta interface{
776805
return err
777806
}
778807

808+
if effectiveLabels := tpgresource.ExpandEffectiveLabels(d); effectiveLabels != nil {
809+
userLabels := d.Get("labels")
810+
terraformLabels := d.Get("terraform_labels")
811+
812+
// Labels cannot be set in a create. We'll have to set them here.
813+
err = resourceComputeSecurityPolicyRead(d, meta)
814+
if err != nil {
815+
return err
816+
}
817+
818+
// Now we can set the labels
819+
setLabels := &compute.GlobalSetLabelsRequest{
820+
Labels: effectiveLabels,
821+
LabelFingerprint: d.Get("label_fingerprint").(string),
822+
}
823+
824+
op, err = client.SecurityPolicies.SetLabels(project, sp, setLabels).Do()
825+
if err != nil {
826+
return err
827+
}
828+
829+
err = ComputeOperationWaitTime(config, op, project, fmt.Sprintf("Creating SecurityPolicy.Labels %q", sp), userAgent, d.Timeout(schema.TimeoutCreate))
830+
if err != nil {
831+
return err
832+
}
833+
834+
// Set back the labels field, as it is needed to decide the value of "labels" in the state in the read function.
835+
if err := d.Set("labels", userLabels); err != nil {
836+
return fmt.Errorf("Error setting back labels: %s", err)
837+
}
838+
839+
// Set back the terraform_labels field, as it is needed to decide the value of "terraform_labels" in the state in the read function.
840+
if err := d.Set("terraform_labels", terraformLabels); err != nil {
841+
return fmt.Errorf("Error setting back terraform_labels: %s", err)
842+
}
843+
844+
// Set back the effective_labels field, as it is needed to decide the value of "effective_labels" in the state in the read function.
845+
if err := d.Set("effective_labels", effectiveLabels); err != nil {
846+
return fmt.Errorf("Error setting back effective_labels: %s", err)
847+
}
848+
}
849+
779850
return resourceComputeSecurityPolicyRead(d, meta)
780851
}
781852

@@ -833,6 +904,22 @@ func resourceComputeSecurityPolicyRead(d *schema.ResourceData, meta interface{})
833904
return fmt.Errorf("Error setting recaptcha_options_config: %s", err)
834905
}
835906

907+
if err := tpgresource.SetLabels(securityPolicy.Labels, d, "labels"); err != nil {
908+
return err
909+
}
910+
911+
if err := tpgresource.SetLabels(securityPolicy.Labels, d, "terraform_labels"); err != nil {
912+
return err
913+
}
914+
915+
if err := d.Set("effective_labels", securityPolicy.Labels); err != nil {
916+
return err
917+
}
918+
919+
if err := d.Set("label_fingerprint", securityPolicy.LabelFingerprint); err != nil {
920+
return fmt.Errorf("Error setting label_fingerprint: %s", err)
921+
}
922+
836923
return nil
837924
}
838925

@@ -887,6 +974,22 @@ func resourceComputeSecurityPolicyUpdate(d *schema.ResourceData, meta interface{
887974
securityPolicy.ForceSendFields = append(securityPolicy.ForceSendFields, "RecaptchaOptionsConfig")
888975
}
889976

977+
if d.HasChange("effective_labels") {
978+
labels := tpgresource.ExpandEffectiveLabels(d)
979+
labelFingerprint := d.Get("label_fingerprint").(string)
980+
req := compute.GlobalSetLabelsRequest{Labels: labels, LabelFingerprint: labelFingerprint}
981+
982+
op, err := config.NewComputeClient(userAgent).SecurityPolicies.SetLabels(project, sp, &req).Do()
983+
if err != nil {
984+
return fmt.Errorf("Error updating labels: %s", err)
985+
}
986+
987+
opErr := ComputeOperationWaitTime(config, op, project, "labels to update", userAgent, d.Timeout(schema.TimeoutUpdate))
988+
if opErr != nil {
989+
return opErr
990+
}
991+
}
992+
890993
if len(securityPolicy.ForceSendFields) > 0 {
891994
client := config.NewComputeClient(userAgent)
892995

google/services/compute/resource_compute_security_policy_rule_test.go

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

1919
import (
2020
"fmt"
21-
"github.com/hashicorp/terraform-provider-google/google/acctest"
2221
"regexp"
2322
"testing"
2423

24+
"github.com/hashicorp/terraform-provider-google/google/acctest"
25+
2526
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
2627
)
2728

google/services/compute/resource_compute_security_policy_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,38 @@ func TestAccComputeSecurityPolicy_modifyExprOptions(t *testing.T) {
745745
})
746746
}
747747

748+
func TestAccComputeSecurityPolicy_labels(t *testing.T) {
749+
t.Parallel()
750+
751+
spName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
752+
753+
acctest.VcrTest(t, resource.TestCase{
754+
PreCheck: func() { acctest.AccTestPreCheck(t) },
755+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
756+
CheckDestroy: testAccCheckComputeSecurityPolicyDestroyProducer(t),
757+
Steps: []resource.TestStep{
758+
{
759+
Config: testAccComputeSecurityPolicy_basicLabels(spName),
760+
},
761+
{
762+
ResourceName: "google_compute_security_policy.policy",
763+
ImportState: true,
764+
ImportStateVerify: true,
765+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
766+
},
767+
{
768+
Config: testAccComputeSecurityPolicy_updateLabels(spName),
769+
},
770+
{
771+
ResourceName: "google_compute_security_policy.policy",
772+
ImportState: true,
773+
ImportStateVerify: true,
774+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
775+
},
776+
},
777+
})
778+
}
779+
748780
func testAccComputeSecurityPolicy_withRecaptchaOptionsConfig(project, spName string) string {
749781
return fmt.Sprintf(`
750782
resource "google_recaptcha_enterprise_key" "primary" {
@@ -2139,3 +2171,32 @@ resource "google_compute_security_policy" "policy" {
21392171
}
21402172
`, spName)
21412173
}
2174+
2175+
func testAccComputeSecurityPolicy_basicLabels(spName string) string {
2176+
return fmt.Sprintf(`
2177+
resource "google_compute_security_policy" "policy" {
2178+
name = "%s"
2179+
description = "basic security policy"
2180+
type = "CLOUD_ARMOR"
2181+
2182+
labels = {
2183+
"env" = "test"
2184+
}
2185+
}
2186+
`, spName)
2187+
}
2188+
2189+
func testAccComputeSecurityPolicy_updateLabels(spName string) string {
2190+
return fmt.Sprintf(`
2191+
resource "google_compute_security_policy" "policy" {
2192+
name = "%s"
2193+
description = "basic security policy"
2194+
type = "CLOUD_ARMOR"
2195+
2196+
labels = {
2197+
"env" = "test",
2198+
"new_label" = "abcd1"
2199+
}
2200+
}
2201+
`, spName)
2202+
}

website/docs/r/compute_security_policy.html.markdown

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

208+
* `labels` - Labels to apply to this address. A list of key->value pairs.
209+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
210+
Please refer to the field `effective_labels` for all of the labels present on the resource.
211+
212+
* `effective_labels` - All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.
213+
214+
* `terraform_labels` - The combination of labels configured directly on the resource and default labels configured on the provider.
215+
216+
* `label_fingerprint` - The unique fingerprint of the labels.
217+
208218
<a name="nested_advanced_options_config"></a>The `advanced_options_config` block supports:
209219

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

0 commit comments

Comments
 (0)