diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index 76cc8cb4e..011f7f29c 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -33,6 +33,7 @@ func Provider() *schema.Provider { "google_compute_address": resourceComputeAddress(), "google_compute_disk": resourceComputeDisk(), "google_compute_instance": resourceComputeInstance(), + "google_compute_network": resourceComputeNetwork(), }, ConfigureFunc: providerConfigure, diff --git a/builtin/providers/google/resource_compute_network.go b/builtin/providers/google/resource_compute_network.go new file mode 100644 index 000000000..829bd9004 --- /dev/null +++ b/builtin/providers/google/resource_compute_network.go @@ -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 +} diff --git a/builtin/providers/google/resource_compute_network_test.go b/builtin/providers/google/resource_compute_network_test.go new file mode 100644 index 000000000..60c278117 --- /dev/null +++ b/builtin/providers/google/resource_compute_network_test.go @@ -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" +}`