Skip to content

Commit 53d5531

Browse files
Feature gap: Add labels and label_fingerprint fields to google_compute_security_policy (#14821) (#10696)
[upstream:f9e335833183179c4d0601f7246be95dd95bb020] Signed-off-by: Modular Magician <[email protected]>
1 parent f417458 commit 53d5531

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-beta/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

@@ -709,8 +710,36 @@ func ResourceComputeSecurityPolicy() *schema.Resource {
709710
},
710711
},
711712
},
712-
},
713+
"labels": {
714+
Type: schema.TypeMap,
715+
Optional: true,
716+
Elem: &schema.Schema{
717+
Type: schema.TypeString,
718+
},
719+
Description: `Labels to apply to this address. A list of key->value pairs.
720+
713721
722+
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
723+
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
724+
},
725+
"terraform_labels": {
726+
Type: schema.TypeMap,
727+
Computed: true,
728+
Description: `The combination of labels configured directly on the resource and default labels configured on the provider.`,
729+
Elem: &schema.Schema{Type: schema.TypeString},
730+
},
731+
"effective_labels": {
732+
Type: schema.TypeMap,
733+
Computed: true,
734+
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
735+
Elem: &schema.Schema{Type: schema.TypeString},
736+
},
737+
"label_fingerprint": {
738+
Type: schema.TypeString,
739+
Computed: true,
740+
Description: `The unique fingerprint of the labels.`,
741+
},
742+
},
714743
UseJSONNumber: true,
715744
}
716745
}
@@ -813,6 +842,48 @@ func resourceComputeSecurityPolicyCreate(d *schema.ResourceData, meta interface{
813842
return err
814843
}
815844

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

@@ -870,6 +941,22 @@ func resourceComputeSecurityPolicyRead(d *schema.ResourceData, meta interface{})
870941
return fmt.Errorf("Error setting recaptcha_options_config: %s", err)
871942
}
872943

944+
if err := tpgresource.SetLabels(securityPolicy.Labels, d, "labels"); err != nil {
945+
return err
946+
}
947+
948+
if err := tpgresource.SetLabels(securityPolicy.Labels, d, "terraform_labels"); err != nil {
949+
return err
950+
}
951+
952+
if err := d.Set("effective_labels", securityPolicy.Labels); err != nil {
953+
return err
954+
}
955+
956+
if err := d.Set("label_fingerprint", securityPolicy.LabelFingerprint); err != nil {
957+
return fmt.Errorf("Error setting label_fingerprint: %s", err)
958+
}
959+
873960
return nil
874961
}
875962

@@ -925,6 +1012,22 @@ func resourceComputeSecurityPolicyUpdate(d *schema.ResourceData, meta interface{
9251012
securityPolicy.ForceSendFields = append(securityPolicy.ForceSendFields, "RecaptchaOptionsConfig")
9261013
}
9271014

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

google-beta/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-beta/google-beta/acctest"
2221
"regexp"
2322
"testing"
2423

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

google-beta/services/compute/resource_compute_security_policy_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,38 @@ func TestAccComputeSecurityPolicy_modifyExprOptions(t *testing.T) {
784784
})
785785
}
786786

787+
func TestAccComputeSecurityPolicy_labels(t *testing.T) {
788+
t.Parallel()
789+
790+
spName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
791+
792+
acctest.VcrTest(t, resource.TestCase{
793+
PreCheck: func() { acctest.AccTestPreCheck(t) },
794+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
795+
CheckDestroy: testAccCheckComputeSecurityPolicyDestroyProducer(t),
796+
Steps: []resource.TestStep{
797+
{
798+
Config: testAccComputeSecurityPolicy_basicLabels(spName),
799+
},
800+
{
801+
ResourceName: "google_compute_security_policy.policy",
802+
ImportState: true,
803+
ImportStateVerify: true,
804+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
805+
},
806+
{
807+
Config: testAccComputeSecurityPolicy_updateLabels(spName),
808+
},
809+
{
810+
ResourceName: "google_compute_security_policy.policy",
811+
ImportState: true,
812+
ImportStateVerify: true,
813+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
814+
},
815+
},
816+
})
817+
}
818+
787819
func testAccComputeSecurityPolicy_withRecaptchaOptionsConfig(project, spName string) string {
788820
return fmt.Sprintf(`
789821
resource "google_recaptcha_enterprise_key" "primary" {
@@ -2235,3 +2267,32 @@ resource "google_compute_security_policy" "policy" {
22352267
}
22362268
`, spName)
22372269
}
2270+
2271+
func testAccComputeSecurityPolicy_basicLabels(spName string) string {
2272+
return fmt.Sprintf(`
2273+
resource "google_compute_security_policy" "policy" {
2274+
name = "%s"
2275+
description = "basic security policy"
2276+
type = "CLOUD_ARMOR"
2277+
2278+
labels = {
2279+
"env" = "test"
2280+
}
2281+
}
2282+
`, spName)
2283+
}
2284+
2285+
func testAccComputeSecurityPolicy_updateLabels(spName string) string {
2286+
return fmt.Sprintf(`
2287+
resource "google_compute_security_policy" "policy" {
2288+
name = "%s"
2289+
description = "basic security policy"
2290+
type = "CLOUD_ARMOR"
2291+
2292+
labels = {
2293+
"env" = "test",
2294+
"new_label" = "abcd1"
2295+
}
2296+
}
2297+
`, spName)
2298+
}

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)