providers/google: compute_network

This commit is contained in:
Mitchell Hashimoto 2014-08-25 21:41:15 -07:00
parent 99918bde2e
commit 7ad39614fa
3 changed files with 197 additions and 0 deletions

View File

@ -33,6 +33,7 @@ func Provider() *schema.Provider {
"google_compute_address": resourceComputeAddress(), "google_compute_address": resourceComputeAddress(),
"google_compute_disk": resourceComputeDisk(), "google_compute_disk": resourceComputeDisk(),
"google_compute_instance": resourceComputeInstance(), "google_compute_instance": resourceComputeInstance(),
"google_compute_network": resourceComputeNetwork(),
}, },
ConfigureFunc: providerConfigure, ConfigureFunc: providerConfigure,

View File

@ -0,0 +1,114 @@
package google
import (
"fmt"
"log"
"time"
"code.google.com/p/google-api-go-client/compute/v1"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceComputeNetwork() *schema.Resource {
return &schema.Resource{
Create: resourceComputeNetworkCreate,
Read: resourceComputeNetworkRead,
Delete: resourceComputeNetworkDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ipv4_range": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"gateway_ipv4": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
// Build the network parameter
network := &compute.Network{
Name: d.Get("name").(string),
IPv4Range: d.Get("ipv4_range").(string),
}
log.Printf("[DEBUG] Network insert request: %#v", network)
op, err := config.clientCompute.Networks.Insert(
config.Project, network).Do()
if err != nil {
return fmt.Errorf("Error creating network: %s", err)
}
// It probably maybe worked, so store the ID now
d.SetId(network.Name)
// Wait for the operation to complete
w := &OperationWaiter{
Service: config.clientCompute,
Op: op,
Project: config.Project,
Type: OperationWaitGlobal,
}
state := w.Conf()
state.Timeout = 2 * time.Minute
state.MinTimeout = 1 * time.Second
if _, err := state.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for address to create: %s", err)
}
return resourceComputeNetworkRead(d, meta)
}
func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
network, err := config.clientCompute.Networks.Get(
config.Project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error reading address: %s", err)
}
d.Set("gateway_ipv4", network.GatewayIPv4)
return nil
}
func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
// Delete the address
op, err := config.clientCompute.Networks.Delete(
config.Project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting network: %s", err)
}
// Wait for the operation to complete
w := &OperationWaiter{
Service: config.clientCompute,
Op: op,
Project: config.Project,
Type: OperationWaitGlobal,
}
state := w.Conf()
state.Timeout = 2 * time.Minute
state.MinTimeout = 1 * time.Second
if _, err := state.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for address to delete: %s", err)
}
d.SetId("")
return nil
}

View File

@ -0,0 +1,82 @@
package google
import (
"fmt"
"testing"
"code.google.com/p/google-api-go-client/compute/v1"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccComputeNetwork_basic(t *testing.T) {
var network compute.Network
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),
),
},
},
})
}
func testAccCheckComputeNetworkDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
for _, rs := range s.Resources {
if rs.Type != "google_compute_network" {
continue
}
_, err := config.clientCompute.Networks.Get(
config.Project, rs.ID).Do()
if err == nil {
return fmt.Errorf("Network still exists")
}
}
return nil
}
func testAccCheckComputeNetworkExists(n string, network *compute.Network) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.ID == "" {
return fmt.Errorf("No ID is set")
}
config := testAccProvider.Meta().(*Config)
found, err := config.clientCompute.Networks.Get(
config.Project, rs.ID).Do()
if err != nil {
return err
}
if found.Name != rs.ID {
return fmt.Errorf("Network not found")
}
*network = *found
return nil
}
}
const testAccComputeNetwork_basic = `
resource "google_compute_network" "foobar" {
name = "terraform-test"
ipv4_range = "10.0.0.0/16"
}`