Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for compute network routing mode #838

Merged
merged 2 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions google/resource_compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func resourceComputeNetwork() *schema.Resource {
ForceNew: true,
},

"routing_mode": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"gateway_ipv4": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -93,6 +100,13 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro
Description: d.Get("description").(string),
}

if v, ok := d.GetOk("routing_mode"); ok {
routingConfig := &compute.NetworkRoutingConfig{
RoutingMode: v.(string),
}
network.RoutingConfig = routingConfig
}

if v, ok := d.GetOk("ipv4_range"); ok {
log.Printf("[DEBUG] Setting IPv4Range (%#v) for legacy network mode", v.(string))
network.IPv4Range = v.(string)
Expand Down Expand Up @@ -133,6 +147,9 @@ func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error
return handleNotFoundError(err, d, fmt.Sprintf("Network %q", d.Get("name").(string)))
}

routingConfig := network.RoutingConfig

d.Set("routing_mode", routingConfig.RoutingMode)
d.Set("gateway_ipv4", network.GatewayIPv4)
d.Set("ipv4_range", network.IPv4Range)
d.Set("self_link", network.SelfLink)
Expand Down
87 changes: 87 additions & 0 deletions google/resource_compute_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,56 @@ func TestAccComputeNetwork_custom_subnet(t *testing.T) {
})
}

func TestAccComputeNetwork_routing_mode(t *testing.T) {
t.Parallel()

var network compute.Network

routingMode := "GLOBAL"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeNetworkDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeNetwork_routing_mode(routingMode),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNetworkExists(
"google_compute_network.acc_network_routing_mode", &network),
testAccCheckComputeNetworkHasRoutingMode(
"google_compute_network.acc_network_routing_mode", &network, routingMode),
),
},
},
})
}

func TestAccComputeNetwork_default_routing_mode(t *testing.T) {
t.Parallel()

var network compute.Network

expectedRoutingMode := "REGIONAL"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeNetworkDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeNetwork_basic(),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNetworkExists(
"google_compute_network.foobar", &network),
testAccCheckComputeNetworkHasRoutingMode(
"google_compute_network.foobar", &network, expectedRoutingMode),
),
},
},
})
}

func testAccCheckComputeNetworkDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

Expand Down Expand Up @@ -168,6 +218,35 @@ func testAccCheckComputeNetworkIsCustomSubnet(n string, network *compute.Network
}
}

func testAccCheckComputeNetworkHasRoutingMode(n string, network *compute.Network, routingMode string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.Attributes["routing_mode"] == "" {
return fmt.Errorf("Routing mode not found on resource")
}

found, err := config.clientCompute.Networks.Get(
config.Project, network.Name).Do()
if err != nil {
return err
}

foundRoutingMode := found.RoutingConfig.RoutingMode

if routingMode != foundRoutingMode {
return fmt.Errorf("Expected routing mode %s to match actual routing mode %s", routingMode, foundRoutingMode)
}

return nil
}
}

func testAccComputeNetwork_basic() string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
Expand All @@ -190,3 +269,11 @@ resource "google_compute_network" "baz" {
auto_create_subnetworks = false
}`, acctest.RandString(10))
}

func testAccComputeNetwork_routing_mode(routingMode string) string {
return fmt.Sprintf(`
resource "google_compute_network" "acc_network_routing_mode" {
name = "network-test-%s"
routing_mode = "%s"
}`, acctest.RandString(10), routingMode)
}
5 changes: 5 additions & 0 deletions website/docs/r/compute_network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ The following arguments are supported:
specified range. This API is deprecated. If set, `auto_create_subnetworks` must be
explicitly set to false.

* `routing_mode` - (Optional) Sets the network-wide routing mode for Cloud Routers
to use. Accepted values are `"GLOBAL"` or `"REGIONAL"`. Defaults to `"REGIONAL"`.
Refer to the [Cloud Router documentation](https://cloud.google.com/router/docs/concepts/overview#dynamic-routing-mode)
for more details.

* `description` - (Optional) A brief description of this resource.

* `project` - (Optional) The project in which the resource belongs. If it
Expand Down