@@ -496,6 +496,66 @@ func TestAccComputeSubnetwork_internal_ipv6(t *testing.T) {
496
496
})
497
497
}
498
498
499
+ func TestAccComputeSubnetwork_enableFlowLogs(t *testing.T) {
500
+ t.Parallel()
501
+ var subnetwork compute.Subnetwork
502
+
503
+ cnName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
504
+ subnetworkName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
505
+
506
+ acctest.VcrTest(t, resource.TestCase{
507
+ PreCheck: func() { acctest.AccTestPreCheck(t) },
508
+ ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
509
+ CheckDestroy: testAccCheckComputeSubnetworkDestroyProducer(t),
510
+ Steps: []resource.TestStep{
511
+ {
512
+ Config: testAccComputeSubnetwork_enableFlowLogs(cnName, subnetworkName, true),
513
+ Check: resource.ComposeTestCheckFunc(
514
+ testAccCheckComputeSubnetworkExists(t, "google_compute_subnetwork.subnetwork", &subnetwork),
515
+ testAccCheckComputeSubnetworkIfLogConfigExists(&subnetwork, "google_compute_subnetwork.subnetwork"),
516
+ testAccCheckComputeSubnetworkLogConfig(&subnetwork, "log_config.0.enable", "true"),
517
+ resource.TestCheckResourceAttr("google_compute_subnetwork.subnetwork", "enable_flow_logs", "true"),
518
+ ),
519
+ },
520
+ {
521
+ ResourceName: "google_compute_subnetwork.subnetwork",
522
+ ImportState: true,
523
+ ImportStateVerify: true,
524
+ },
525
+ {
526
+ Config: testAccComputeSubnetwork_enableFlowLogs_with_logConfig(cnName, subnetworkName, true),
527
+ Check: resource.ComposeTestCheckFunc(
528
+ testAccCheckComputeSubnetworkExists(t, "google_compute_subnetwork.subnetwork", &subnetwork),
529
+ resource.TestCheckResourceAttr("google_compute_subnetwork.subnetwork", "enable_flow_logs", "true"),
530
+ testAccCheckComputeSubnetworkLogConfig(&subnetwork, "log_config.0.enable", "true"),
531
+ testAccCheckComputeSubnetworkLogConfig(&subnetwork, "log_config.0.aggregation_interval", "INTERVAL_1_MIN"),
532
+ testAccCheckComputeSubnetworkLogConfig(&subnetwork, "log_config.0.metadata", "INCLUDE_ALL_METADATA"),
533
+ ),
534
+ },
535
+ {
536
+ ResourceName: "google_compute_subnetwork.subnetwork",
537
+ ImportState: true,
538
+ ImportStateVerify: true,
539
+ },
540
+ {
541
+ Config: testAccComputeSubnetwork_enableFlowLogs(cnName, subnetworkName, false),
542
+ Check: resource.ComposeTestCheckFunc(
543
+ testAccCheckComputeSubnetworkExists(t, "google_compute_subnetwork.subnetwork", &subnetwork),
544
+ resource.TestCheckResourceAttr("google_compute_subnetwork.subnetwork", "enable_flow_logs", "false"),
545
+ testAccCheckComputeSubnetworkLogConfig(&subnetwork, "log_config.0.enable", "false"),
546
+ testAccCheckComputeSubnetworkLogConfig(&subnetwork, "log_config.0.aggregation_interval", "INTERVAL_1_MIN"),
547
+ testAccCheckComputeSubnetworkLogConfig(&subnetwork, "log_config.0.metadata", "INCLUDE_ALL_METADATA"),
548
+ ),
549
+ },
550
+ {
551
+ ResourceName: "google_compute_subnetwork.subnetwork",
552
+ ImportState: true,
553
+ ImportStateVerify: true,
554
+ },
555
+ },
556
+ })
557
+ }
558
+
499
559
func testAccCheckComputeSubnetworkExists(t *testing.T, n string, subnetwork *compute.Subnetwork) resource.TestCheckFunc {
500
560
return func(s *terraform.State) error {
501
561
rs, ok := s.RootModule().Resources[n]
@@ -556,6 +616,111 @@ func testAccCheckComputeSubnetworkHasNotSecondaryIpRange(subnetwork *compute.Sub
556
616
}
557
617
}
558
618
619
+ func testAccCheckComputeSubnetworkIfLogConfigExists(subnetwork *compute.Subnetwork, resourceName string) resource.TestCheckFunc {
620
+ return func(s *terraform.State) error {
621
+ // Retrieve the resource state using a fixed resource name
622
+ rs, ok := s.RootModule().Resources[resourceName]
623
+ if !ok {
624
+ return fmt.Errorf("resource google_compute_subnetwork.subnetwork not found in state")
625
+ }
626
+
627
+ if rs.Primary.ID == "" {
628
+ return fmt.Errorf("resource ID is not set")
629
+ }
630
+
631
+ // Ensure that the log_config exists in the API response.
632
+ if subnetwork.LogConfig == nil {
633
+ return fmt.Errorf("no log_config exists in subnetwork")
634
+ }
635
+
636
+ stateAttrs := rs.Primary.Attributes
637
+
638
+ // Check aggregation_interval.
639
+ aggInterval, ok := stateAttrs["log_config.0.aggregation_interval"]
640
+ if !ok {
641
+ return fmt.Errorf("aggregation_interval not found in state")
642
+ }
643
+ if subnetwork.LogConfig.AggregationInterval != aggInterval {
644
+ return fmt.Errorf("aggregation_interval mismatch: expected %s, got %s", aggInterval, subnetwork.LogConfig.AggregationInterval)
645
+ }
646
+
647
+ // Check flow_sampling.
648
+ fsState, ok := stateAttrs["log_config.0.flow_sampling"]
649
+ if !ok {
650
+ return fmt.Errorf("flow_sampling not found in state")
651
+ }
652
+ actualFS := fmt.Sprintf("%g", subnetwork.LogConfig.FlowSampling)
653
+ if actualFS != fsState {
654
+ return fmt.Errorf("flow_sampling mismatch: expected %s, got %s", fsState, actualFS)
655
+ }
656
+
657
+ // Check metadata.
658
+ metadata, ok := stateAttrs["log_config.0.metadata"]
659
+ if !ok {
660
+ return fmt.Errorf("metadata not found in state")
661
+ }
662
+ if subnetwork.LogConfig.Metadata != metadata {
663
+ return fmt.Errorf("metadata mismatch: expected %s, got %s", metadata, subnetwork.LogConfig.Metadata)
664
+ }
665
+
666
+ // Optionally, check filter_expr if it exists.
667
+ if subnetwork.LogConfig.FilterExpr != "" {
668
+ filterExpr, ok := stateAttrs["log_config.0.filter_expr"]
669
+ if !ok {
670
+ return fmt.Errorf("filter_expr is set in API but not found in state")
671
+ }
672
+ if subnetwork.LogConfig.FilterExpr != filterExpr {
673
+ return fmt.Errorf("filter_expr mismatch: expected %s, got %s", filterExpr, subnetwork.LogConfig.FilterExpr)
674
+ }
675
+ }
676
+
677
+ // Optionally, check metadata_fields if present.
678
+ if len(subnetwork.LogConfig.MetadataFields) > 0 {
679
+ if _, ok := stateAttrs["log_config.0.metadata_fields"]; !ok {
680
+ return fmt.Errorf("metadata_fields are set in API but not found in state")
681
+ }
682
+ }
683
+
684
+ return nil
685
+ }
686
+ }
687
+
688
+ func testAccCheckComputeSubnetworkLogConfig(subnetwork *compute.Subnetwork, key, value string) resource.TestCheckFunc {
689
+ return func(s *terraform.State) error {
690
+ if subnetwork.LogConfig == nil {
691
+ return fmt.Errorf("no log_config found")
692
+ }
693
+
694
+ switch key {
695
+ case "log_config.0.enable":
696
+ if subnetwork.LogConfig.Enable != (value == "true") {
697
+ return fmt.Errorf("expected %s to be '%s', got '%t'", key, value, subnetwork.LogConfig.Enable)
698
+ }
699
+ case "log_config.0.aggregation_interval":
700
+ if subnetwork.LogConfig.AggregationInterval != value {
701
+ return fmt.Errorf("expected %s to be '%s', got '%s'", key, value, subnetwork.LogConfig.AggregationInterval)
702
+ }
703
+ case "log_config.0.metadata":
704
+ if subnetwork.LogConfig.Metadata != value {
705
+ return fmt.Errorf("expected %s to be '%s', got '%s'", key, value, subnetwork.LogConfig.Metadata)
706
+ }
707
+ case "log_config.0.flow_sampling":
708
+ flowSamplingStr := fmt.Sprintf("%g", subnetwork.LogConfig.FlowSampling)
709
+ if flowSamplingStr != value {
710
+ return fmt.Errorf("expected %s to be '%s', got '%s'", key, value, flowSamplingStr)
711
+ }
712
+ case "log_config.0.filterExpr":
713
+ if subnetwork.LogConfig.FilterExpr != value {
714
+ return fmt.Errorf("expected %s to be '%s', got '%s'", key, value, subnetwork.LogConfig.FilterExpr)
715
+ }
716
+ default:
717
+ return fmt.Errorf("unknown log_config key: %s", key)
718
+ }
719
+
720
+ return nil
721
+ }
722
+ }
723
+
559
724
func testAccComputeSubnetwork_basic(cnName, subnetwork1Name, subnetwork2Name, subnetwork3Name string) string {
560
725
return fmt.Sprintf(`
561
726
resource "google_compute_network" "custom-test" {
@@ -1033,3 +1198,42 @@ resource "google_compute_subnetwork" "subnetwork" {
1033
1198
}
1034
1199
`, cnName, subnetworkName)
1035
1200
}
1201
+
1202
+ func testAccComputeSubnetwork_enableFlowLogs(cnName, subnetworkName string, enableFlowLogs bool) string {
1203
+ return fmt.Sprintf(`
1204
+ resource "google_compute_network" "custom-test" {
1205
+ name = "%s"
1206
+ auto_create_subnetworks = false
1207
+ }
1208
+
1209
+ resource "google_compute_subnetwork" "subnetwork" {
1210
+ name = "%s"
1211
+ ip_cidr_range = "10.0.0.0/16"
1212
+ region = "us-central1"
1213
+ network = google_compute_network.custom-test.self_link
1214
+ enable_flow_logs = %t
1215
+ }
1216
+ `, cnName, subnetworkName, enableFlowLogs)
1217
+ }
1218
+
1219
+ func testAccComputeSubnetwork_enableFlowLogs_with_logConfig(cnName, subnetworkName string, enableFlowLogs bool) string {
1220
+ return fmt.Sprintf(`
1221
+ resource "google_compute_network" "custom-test" {
1222
+ name = "%s"
1223
+ auto_create_subnetworks = false
1224
+ }
1225
+
1226
+ resource "google_compute_subnetwork" "subnetwork" {
1227
+ name = "%s"
1228
+ ip_cidr_range = "10.0.0.0/16"
1229
+ region = "us-central1"
1230
+ network = google_compute_network.custom-test.self_link
1231
+ enable_flow_logs = %t
1232
+
1233
+ log_config {
1234
+ aggregation_interval = "INTERVAL_1_MIN"
1235
+ metadata = "INCLUDE_ALL_METADATA"
1236
+ }
1237
+ }
1238
+ `, cnName, subnetworkName, enableFlowLogs)
1239
+ }
0 commit comments