blob: 973f52969899374c50e9daf2b45bfe05f202cecd [file] [log] [blame]
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package compute_test
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)
func TestAccComputeFirewall_firewallBasicExample(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeFirewallDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeFirewall_firewallBasicExample(context),
},
{
ResourceName: "google_compute_firewall.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network"},
},
},
})
}
func testAccComputeFirewall_firewallBasicExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_compute_firewall" "default" {
name = "tf-test-test-firewall%{random_suffix}"
network = google_compute_network.default.name
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = ["80", "8080", "1000-2000"]
}
source_tags = ["web"]
}
resource "google_compute_network" "default" {
name = "tf-test-test-network%{random_suffix}"
}
`, context)
}
func TestAccComputeFirewall_firewallWithTargetTagsExample(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"project": envvar.GetTestProjectFromEnv(),
"random_suffix": acctest.RandString(t, 10),
}
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeFirewallDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeFirewall_firewallWithTargetTagsExample(context),
},
{
ResourceName: "google_compute_firewall.rules",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network"},
},
},
})
}
func testAccComputeFirewall_firewallWithTargetTagsExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_compute_firewall" "rules" {
project = "%{project}"
name = "tf-test-my-firewall-rule%{random_suffix}"
network = "default"
description = "Creates firewall rule targeting tagged instances"
allow {
protocol = "tcp"
ports = ["80", "8080", "1000-2000"]
}
source_tags = ["foo"]
target_tags = ["web"]
}
`, context)
}
func testAccCheckComputeFirewallDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_firewall" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}
config := acctest.GoogleProviderConfig(t)
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{ComputeBasePath}}projects/{{project}}/global/firewalls/{{name}}")
if err != nil {
return err
}
billingProject := ""
if config.BillingProject != "" {
billingProject = config.BillingProject
}
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: config.UserAgent,
})
if err == nil {
return fmt.Errorf("ComputeFirewall still exists at %s", url)
}
}
return nil
}
}