wip: review changes:

- test arguments
 - set region, project in state
 - fix import error messages
 - get rid of peerFound
 - linkDiffSuppress
This commit is contained in:
Roberto Jung Drebes 2017-05-19 20:18:23 +02:00
parent 50f8b9407e
commit 6ab9d82cc5
11 changed files with 160 additions and 219 deletions

View File

@ -1,7 +1,6 @@
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
@ -10,23 +9,13 @@ import (
func TestAccComputeRouterInterface_import(t *testing.T) {
resourceName := "google_compute_router_interface.foobar"
network := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
address := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
gateway := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
espRule := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
udp500Rule := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
udp4500Rule := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
tunnel := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
iface := fmt.Sprintf("router-interface-import-test-%s", acctest.RandString(10))
testId := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRouterInterfaceBasic(network, subnet, address, gateway, espRule, udp500Rule,
udp4500Rule, router, tunnel, iface),
Config: testAccComputeRouterInterfaceBasic(testId),
},
resource.TestStep{

View File

@ -1,7 +1,6 @@
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
@ -10,24 +9,13 @@ import (
func TestAccComputeRouterPeer_import(t *testing.T) {
resourceName := "google_compute_router_peer.foobar"
network := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
address := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
gateway := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
espRule := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
udp500Rule := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
udp4500Rule := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
tunnel := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
iface := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
peer := fmt.Sprintf("router-peer-import-test-%s", acctest.RandString(10))
testId := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRouterPeerBasic(network, subnet, address, gateway, espRule, udp500Rule,
udp4500Rule, router, tunnel, iface, peer),
Config: testAccComputeRouterPeerBasic(testId),
},
resource.TestStep{

View File

@ -1,25 +1,21 @@
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccComputeRouter_import(t *testing.T) {
resourceName := "google_compute_router.foobar"
network := fmt.Sprintf("router-import-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-import-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-import-test-%s", acctest.RandString(10))
resourceRegion := "europe-west1"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRouterNetworkLink(network, subnet, router),
Config: testAccComputeRouterBasic(resourceRegion),
},
resource.TestStep{

View File

@ -275,3 +275,11 @@ func handleNotFoundError(err error, d *schema.ResourceData, resource string) err
return fmt.Errorf("Error reading %s: %s", resource, err)
}
func linkDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
parts := strings.Split(old, "/")
if parts[len(parts)-1] == new {
return true
}
return false
}

View File

@ -28,9 +28,10 @@ func resourceComputeRouter() *schema.Resource {
},
"network": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: linkDiffSuppress,
},
"description": &schema.Schema{
@ -42,6 +43,7 @@ func resourceComputeRouter() *schema.Resource {
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
@ -167,15 +169,12 @@ func resourceComputeRouterRead(d *schema.ResourceData, meta interface{}) error {
}
d.Set("self_link", router.SelfLink)
// if we don't have a network (when importing), set it to the URI returned from the server
if _, ok := d.GetOk("network"); !ok {
d.Set("network", router.Network)
}
d.Set("network", router.Network)
d.Set("name", router.Name)
d.Set("description", router.Description)
d.Set("region", region)
d.Set("project", project)
d.Set("bgp", flattenAsn(router.Bgp.Asn))
d.SetId(fmt.Sprintf("%s/%s", region, name))

View File

@ -32,9 +32,10 @@ func resourceComputeRouterInterface() *schema.Resource {
ForceNew: true,
},
"vpn_tunnel": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: linkDiffSuppress,
},
"ip_range": &schema.Schema{
@ -45,6 +46,7 @@ func resourceComputeRouterInterface() *schema.Resource {
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
@ -167,15 +169,10 @@ func resourceComputeRouterInterfaceRead(d *schema.ResourceData, meta interface{}
if iface.Name == ifaceName {
d.SetId(fmt.Sprintf("%s/%s/%s", region, routerName, ifaceName))
// if we don't have a tunnel (when importing), set it to the URI returned from the server
if _, ok := d.GetOk("vpn_tunnel"); !ok {
vpnTunnelName, err := getVpnTunnelName(iface.LinkedVpnTunnel)
if err != nil {
return err
}
d.Set("vpn_tunnel", vpnTunnelName)
}
d.Set("vpn_tunnel", iface.LinkedVpnTunnel)
d.Set("ip_range", iface.IpRange)
d.Set("region", region)
d.Set("project", project)
return nil
}
}
@ -261,7 +258,7 @@ func resourceComputeRouterInterfaceDelete(d *schema.ResourceData, meta interface
func resourceComputeRouterInterfaceImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid router specifier. Expecting {region}/{router}")
return nil, fmt.Errorf("Invalid router interface specifier. Expecting {region}/{router}/{interface}")
}
d.Set("region", parts[0])

View File

@ -10,30 +10,19 @@ import (
)
func TestAccComputeRouterInterface_basic(t *testing.T) {
network := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
address := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
gateway := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
espRule := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
udp500Rule := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
udp4500Rule := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
tunnel := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
iface := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
testId := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouterInterfaceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRouterInterfaceBasic(network, subnet, address, gateway, espRule, udp500Rule,
udp4500Rule, router, tunnel, iface),
Config: testAccComputeRouterInterfaceBasic(testId),
Check: testAccCheckComputeRouterInterfaceExists(
"google_compute_router_interface.foobar"),
},
resource.TestStep{
Config: testAccComputeRouterInterfaceKeepRouter(network, subnet, address, gateway, espRule, udp500Rule,
udp4500Rule, router, tunnel),
Config: testAccComputeRouterInterfaceKeepRouter(testId),
Check: testAccCheckComputeRouterInterfaceDelete(
"google_compute_router_interface.foobar"),
},
@ -161,35 +150,35 @@ func testAccCheckComputeRouterInterfaceExists(n string) resource.TestCheckFunc {
}
}
func testAccComputeRouterInterfaceBasic(network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel, iface string) string {
func testAccComputeRouterInterfaceBasic(testId string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s"
name = "router-interface-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "%s"
name = "router-interface-test-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_address" "foobar" {
name = "%s"
name = "router-interface-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_vpn_gateway" "foobar" {
name = "%s"
name = "router-interface-test-%s"
network = "${google_compute_network.foobar.self_link}"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_forwarding_rule" "foobar_esp" {
name = "%s"
name = "router-interface-test-%s-1"
region = "${google_compute_vpn_gateway.foobar.region}"
ip_protocol = "ESP"
ip_address = "${google_compute_address.foobar.address}"
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp500" {
name = "%s"
name = "router-interface-test-%s-2"
region = "${google_compute_forwarding_rule.foobar_esp.region}"
ip_protocol = "UDP"
port_range = "500-500"
@ -197,7 +186,7 @@ func testAccComputeRouterInterfaceBasic(network, subnet, address, gateway, espFw
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp4500" {
name = "%s"
name = "router-interface-test-%s-3"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
ip_protocol = "UDP"
port_range = "4500-4500"
@ -205,7 +194,7 @@ func testAccComputeRouterInterfaceBasic(network, subnet, address, gateway, espFw
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_router" "foobar"{
name = "%s"
name = "router-interface-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
network = "${google_compute_network.foobar.self_link}"
bgp {
@ -213,7 +202,7 @@ func testAccComputeRouterInterfaceBasic(network, subnet, address, gateway, espFw
}
}
resource "google_compute_vpn_tunnel" "foobar" {
name = "%s"
name = "router-interface-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
shared_secret = "unguessable"
@ -221,44 +210,44 @@ func testAccComputeRouterInterfaceBasic(network, subnet, address, gateway, espFw
router = "${google_compute_router.foobar.name}"
}
resource "google_compute_router_interface" "foobar" {
name = "%s"
name = "router-interface-test-%s"
router = "${google_compute_router.foobar.name}"
region = "${google_compute_router.foobar.region}"
ip_range = "169.254.3.1/30"
vpn_tunnel = "${google_compute_vpn_tunnel.foobar.name}"
}
`, network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel, iface)
`, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId)
}
func testAccComputeRouterInterfaceKeepRouter(network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel string) string {
func testAccComputeRouterInterfaceKeepRouter(testId string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s"
name = "router-interface-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "%s"
name = "router-interface-test-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_address" "foobar" {
name = "%s"
name = "router-interface-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_vpn_gateway" "foobar" {
name = "%s"
name = "router-interface-test-%s"
network = "${google_compute_network.foobar.self_link}"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_forwarding_rule" "foobar_esp" {
name = "%s"
name = "router-interface-test-%s-1"
region = "${google_compute_vpn_gateway.foobar.region}"
ip_protocol = "ESP"
ip_address = "${google_compute_address.foobar.address}"
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp500" {
name = "%s"
name = "router-interface-test-%s-2"
region = "${google_compute_forwarding_rule.foobar_esp.region}"
ip_protocol = "UDP"
port_range = "500-500"
@ -266,7 +255,7 @@ func testAccComputeRouterInterfaceKeepRouter(network, subnet, address, gateway,
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp4500" {
name = "%s"
name = "router-interface-test-%s-3"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
ip_protocol = "UDP"
port_range = "4500-4500"
@ -274,7 +263,7 @@ func testAccComputeRouterInterfaceKeepRouter(network, subnet, address, gateway,
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_router" "foobar"{
name = "%s"
name = "router-interface-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
network = "${google_compute_network.foobar.self_link}"
bgp {
@ -282,12 +271,12 @@ func testAccComputeRouterInterfaceKeepRouter(network, subnet, address, gateway,
}
}
resource "google_compute_vpn_tunnel" "foobar" {
name = "%s"
name = "router-interface-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
shared_secret = "unguessable"
peer_ip = "8.8.8.8"
router = "${google_compute_router.foobar.name}"
}
`, network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel)
`, testId, testId, testId, testId, testId, testId, testId, testId, testId)
}

View File

@ -63,6 +63,7 @@ func resourceComputeRouterPeer() *schema.Resource {
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
@ -195,6 +196,8 @@ func resourceComputeRouterPeerRead(d *schema.ResourceData, meta interface{}) err
d.Set("peer_asn", peer.PeerAsn)
d.Set("advertised_route_priority", peer.AdvertisedRoutePriority)
d.Set("ip_address", peer.IpAddress)
d.Set("region", region)
d.Set("project", project)
return nil
}
}
@ -237,20 +240,16 @@ func resourceComputeRouterPeerDelete(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("Error Reading Router %s: %s", routerName, err)
}
var peerFound bool
var newPeers []*compute.RouterBgpPeer = make([]*compute.RouterBgpPeer, 0, len(router.BgpPeers))
for _, peer := range router.BgpPeers {
if peer.Name == peerName {
peerFound = true
continue
} else {
newPeers = append(newPeers, peer)
}
}
if !peerFound {
if len(newPeers) == len(router.BgpPeers) {
log.Printf("[DEBUG] Router %s/%s had no peer %s already", region, routerName, peerName)
d.SetId("")
return nil
@ -280,7 +279,7 @@ func resourceComputeRouterPeerDelete(d *schema.ResourceData, meta interface{}) e
func resourceComputeRouterPeerImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid router specifier. Expecting {region}/{router}")
return nil, fmt.Errorf("Invalid router peer specifier. Expecting {region}/{router}/{peer}")
}
d.Set("region", parts[0])

View File

@ -10,31 +10,19 @@ import (
)
func TestAccComputeRouterPeer_basic(t *testing.T) {
network := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
address := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
gateway := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
espRule := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
udp500Rule := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
udp4500Rule := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
tunnel := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
iface := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
peer := fmt.Sprintf("router-peer-test-%s", acctest.RandString(10))
testId := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouterPeerDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRouterPeerBasic(network, subnet, address, gateway, espRule, udp500Rule,
udp4500Rule, router, tunnel, iface, peer),
Config: testAccComputeRouterPeerBasic(testId),
Check: testAccCheckComputeRouterPeerExists(
"google_compute_router_peer.foobar"),
},
resource.TestStep{
Config: testAccComputeRouterPeerKeepRouter(network, subnet, address, gateway, espRule, udp500Rule,
udp4500Rule, router, tunnel, iface),
Config: testAccComputeRouterPeerKeepRouter(testId),
Check: testAccCheckComputeRouterPeerDelete(
"google_compute_router_peer.foobar"),
},
@ -162,35 +150,35 @@ func testAccCheckComputeRouterPeerExists(n string) resource.TestCheckFunc {
}
}
func testAccComputeRouterPeerBasic(network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel, iface, peer string) string {
func testAccComputeRouterPeerBasic(testId string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s"
name = "router-peer-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "%s"
name = "router-peer-test-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_address" "foobar" {
name = "%s"
name = "router-peer-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_vpn_gateway" "foobar" {
name = "%s"
name = "router-peer-test-%s"
network = "${google_compute_network.foobar.self_link}"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_forwarding_rule" "foobar_esp" {
name = "%s"
name = "router-peer-test-%s-1"
region = "${google_compute_vpn_gateway.foobar.region}"
ip_protocol = "ESP"
ip_address = "${google_compute_address.foobar.address}"
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp500" {
name = "%s"
name = "router-peer-test-%s-2"
region = "${google_compute_forwarding_rule.foobar_esp.region}"
ip_protocol = "UDP"
port_range = "500-500"
@ -198,7 +186,7 @@ func testAccComputeRouterPeerBasic(network, subnet, address, gateway, espFwRule,
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp4500" {
name = "%s"
name = "router-peer-test-%s-3"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
ip_protocol = "UDP"
port_range = "4500-4500"
@ -206,7 +194,7 @@ func testAccComputeRouterPeerBasic(network, subnet, address, gateway, espFwRule,
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_router" "foobar"{
name = "%s"
name = "router-peer-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
network = "${google_compute_network.foobar.self_link}"
bgp {
@ -214,7 +202,7 @@ func testAccComputeRouterPeerBasic(network, subnet, address, gateway, espFwRule,
}
}
resource "google_compute_vpn_tunnel" "foobar" {
name = "%s"
name = "router-peer-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
shared_secret = "unguessable"
@ -222,14 +210,14 @@ func testAccComputeRouterPeerBasic(network, subnet, address, gateway, espFwRule,
router = "${google_compute_router.foobar.name}"
}
resource "google_compute_router_interface" "foobar" {
name = "%s"
name = "router-peer-test-%s"
router = "${google_compute_router.foobar.name}"
region = "${google_compute_router.foobar.region}"
ip_range = "169.254.3.1/30"
vpn_tunnel = "${google_compute_vpn_tunnel.foobar.name}"
}
resource "google_compute_router_peer" "foobar" {
name = "%s"
name = "router-peer-test-%s"
router = "${google_compute_router.foobar.name}"
region = "${google_compute_router.foobar.region}"
peer_ip_address = "169.254.3.2"
@ -237,38 +225,38 @@ func testAccComputeRouterPeerBasic(network, subnet, address, gateway, espFwRule,
advertised_route_priority = 100
interface = "${google_compute_router_interface.foobar.name}"
}
`, network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel, iface, peer)
`, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId)
}
func testAccComputeRouterPeerKeepRouter(network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel, iface string) string {
func testAccComputeRouterPeerKeepRouter(testId string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s"
name = "router-peer-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "%s"
name = "router-peer-test-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_address" "foobar" {
name = "%s"
name = "router-peer-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_vpn_gateway" "foobar" {
name = "%s"
name = "router-peer-test-%s"
network = "${google_compute_network.foobar.self_link}"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_forwarding_rule" "foobar_esp" {
name = "%s"
name = "router-peer-test-%s-1"
region = "${google_compute_vpn_gateway.foobar.region}"
ip_protocol = "ESP"
ip_address = "${google_compute_address.foobar.address}"
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp500" {
name = "%s"
name = "router-peer-test-%s-2"
region = "${google_compute_forwarding_rule.foobar_esp.region}"
ip_protocol = "UDP"
port_range = "500-500"
@ -276,7 +264,7 @@ func testAccComputeRouterPeerKeepRouter(network, subnet, address, gateway, espFw
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp4500" {
name = "%s"
name = "router-peer-test-%s-3"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
ip_protocol = "UDP"
port_range = "4500-4500"
@ -284,7 +272,7 @@ func testAccComputeRouterPeerKeepRouter(network, subnet, address, gateway, espFw
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_router" "foobar"{
name = "%s"
name = "router-peer-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
network = "${google_compute_network.foobar.self_link}"
bgp {
@ -292,7 +280,7 @@ func testAccComputeRouterPeerKeepRouter(network, subnet, address, gateway, espFw
}
}
resource "google_compute_vpn_tunnel" "foobar" {
name = "%s"
name = "router-peer-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
shared_secret = "unguessable"
@ -300,11 +288,11 @@ func testAccComputeRouterPeerKeepRouter(network, subnet, address, gateway, espFw
router = "${google_compute_router.foobar.name}"
}
resource "google_compute_router_interface" "foobar" {
name = "%s"
name = "router-peer-test-%s"
router = "${google_compute_router.foobar.name}"
region = "${google_compute_router.foobar.region}"
ip_range = "169.254.3.1/30"
vpn_tunnel = "${google_compute_vpn_tunnel.foobar.name}"
}
`, network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel, iface)
`, testId, testId, testId, testId, testId, testId, testId, testId, testId, testId)
}

View File

@ -10,21 +10,19 @@ import (
)
func TestAccComputeRouter_basic(t *testing.T) {
network := fmt.Sprintf("router-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-test-%s", acctest.RandString(10))
resourceRegion := "europe-west1"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRouterBasic(network, subnet, router),
Config: testAccComputeRouterBasic(resourceRegion),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRouterExists(
"google_compute_router.foobar"),
resource.TestCheckResourceAttr(
"google_compute_router.foobar", "region", "europe-west1"),
"google_compute_router.foobar", "region", resourceRegion),
),
},
},
@ -32,21 +30,19 @@ func TestAccComputeRouter_basic(t *testing.T) {
}
func TestAccComputeRouter_noRegion(t *testing.T) {
network := fmt.Sprintf("router-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-test-%s", acctest.RandString(10))
providerRegion := "us-central1"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRouterNoRegion(network, subnet, router),
Config: testAccComputeRouterNoRegion(providerRegion),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRouterExists(
"google_compute_router.foobar"),
resource.TestCheckResourceAttr(
"google_compute_router.foobar", "region", "us-central1"),
"google_compute_router.foobar", "region", providerRegion),
),
},
},
@ -54,16 +50,13 @@ func TestAccComputeRouter_noRegion(t *testing.T) {
}
func TestAccComputeRouter_networkLink(t *testing.T) {
network := fmt.Sprintf("router-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-test-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouterDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeRouterNetworkLink(network, subnet, router),
Config: testAccComputeRouterNetworkLink(),
Check: testAccCheckComputeRouterExists(
"google_compute_router.foobar"),
},
@ -140,67 +133,70 @@ func testAccCheckComputeRouterExists(n string) resource.TestCheckFunc {
}
}
func testAccComputeRouterBasic(network, subnet, router string) string {
func testAccComputeRouterBasic(resourceRegion string) string {
testId := acctest.RandString(10)
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s"
name = "router-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "%s"
name = "router-test-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "%s"
}
resource "google_compute_router" "foobar" {
name = "router-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
network = "${google_compute_network.foobar.name}"
bgp {
asn = 64514
}
}
`, testId, testId, resourceRegion, testId)
}
func testAccComputeRouterNoRegion(providerRegion string) string {
testId := acctest.RandString(10)
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "router-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "router-test-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "%s"
}
resource "google_compute_router" "foobar" {
name = "router-test-%s"
network = "${google_compute_network.foobar.name}"
bgp {
asn = 64514
}
}
`, testId, testId, providerRegion, testId)
}
func testAccComputeRouterNetworkLink() string {
testId := acctest.RandString(10)
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "router-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "router-test-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "europe-west1"
}
resource "google_compute_router" "foobar" {
name = "%s"
region = "${google_compute_subnetwork.foobar.region}"
network = "${google_compute_network.foobar.name}"
bgp {
asn = 64514
}
}
`, network, subnet, router)
}
func testAccComputeRouterNoRegion(network, subnet, router string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_router" "foobar" {
name = "%s"
network = "${google_compute_network.foobar.name}"
bgp {
asn = 64514
}
}
`, network, subnet, router)
}
func testAccComputeRouterNetworkLink(network, subnet, router string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_router" "foobar" {
name = "%s"
name = "router-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
network = "${google_compute_network.foobar.self_link}"
bgp {
asn = 64514
}
}
`, network, subnet, router)
`, testId, testId, testId)
}

View File

@ -33,23 +33,14 @@ func TestAccComputeVpnTunnel_basic(t *testing.T) {
}
func TestAccComputeVpnTunnel_router(t *testing.T) {
network := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
subnet := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
address := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
gateway := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
espRule := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
udp500Rule := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
udp4500Rule := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
router := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
tunnel := fmt.Sprintf("router-interface-test-%s", acctest.RandString(10))
router := fmt.Sprintf("tunnel-test-router-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeVpnTunnelDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeVpnTunnelRouter(network, subnet, address, gateway, espRule, udp500Rule,
udp4500Rule, router, tunnel),
Config: testAccComputeVpnTunnelRouter(router),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeVpnTunnelExists(
"google_compute_vpn_tunnel.foobar"),
@ -183,35 +174,36 @@ resource "google_compute_vpn_tunnel" "foobar" {
acctest.RandString(10), acctest.RandString(10), acctest.RandString(10),
acctest.RandString(10), acctest.RandString(10))
func testAccComputeVpnTunnelRouter(network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel string) string {
func testAccComputeVpnTunnelRouter(router string) string {
testId := acctest.RandString(10)
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s"
name = "tunnel-test-%s"
}
resource "google_compute_subnetwork" "foobar" {
name = "%s"
name = "tunnel-test-%s"
network = "${google_compute_network.foobar.self_link}"
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_address" "foobar" {
name = "%s"
name = "tunnel-test-%s"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_vpn_gateway" "foobar" {
name = "%s"
name = "tunnel-test-%s"
network = "${google_compute_network.foobar.self_link}"
region = "${google_compute_subnetwork.foobar.region}"
}
resource "google_compute_forwarding_rule" "foobar_esp" {
name = "%s"
name = "tunnel-test-%s-1"
region = "${google_compute_vpn_gateway.foobar.region}"
ip_protocol = "ESP"
ip_address = "${google_compute_address.foobar.address}"
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp500" {
name = "%s"
name = "tunnel-test-%s-2"
region = "${google_compute_forwarding_rule.foobar_esp.region}"
ip_protocol = "UDP"
port_range = "500-500"
@ -219,7 +211,7 @@ func testAccComputeVpnTunnelRouter(network, subnet, address, gateway, espFwRule,
target = "${google_compute_vpn_gateway.foobar.self_link}"
}
resource "google_compute_forwarding_rule" "foobar_udp4500" {
name = "%s"
name = "tunnel-test-%s-3"
region = "${google_compute_forwarding_rule.foobar_udp500.region}"
ip_protocol = "UDP"
port_range = "4500-4500"
@ -235,14 +227,14 @@ func testAccComputeVpnTunnelRouter(network, subnet, address, gateway, espFwRule,
}
}
resource "google_compute_vpn_tunnel" "foobar" {
name = "%s"
name = "tunnel-test-%s"
region = "${google_compute_forwarding_rule.foobar_udp4500.region}"
target_vpn_gateway = "${google_compute_vpn_gateway.foobar.self_link}"
shared_secret = "unguessable"
peer_ip = "8.8.8.8"
router = "${google_compute_router.foobar.name}"
}
`, network, subnet, address, gateway, espFwRule, udp500FwRule, udp4500FwRule, router, tunnel)
`, testId, testId, testId, testId, testId, testId, testId, router, testId)
}
var testAccComputeVpnTunnelDefaultTrafficSelectors = fmt.Sprintf(`