package google import ( "fmt" "log" "time" "code.google.com/p/google-api-go-client/compute/v1" "code.google.com/p/google-api-go-client/googleapi" "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, }, "self_link": &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 opRaw, err := state.WaitForState() if err != nil { return fmt.Errorf("Error waiting for network to create: %s", err) } op = opRaw.(*compute.Operation) if op.Error != nil { // The resource didn't actually create d.SetId("") // Return the error return OperationError(*op.Error) } 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 { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore d.SetId("") return nil } return fmt.Errorf("Error reading network: %s", err) } d.Set("gateway_ipv4", network.GatewayIPv4) d.Set("self_link", network.SelfLink) return nil } func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) // Delete the network 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 opRaw, err := state.WaitForState() if err != nil { return fmt.Errorf("Error waiting for network to delete: %s", err) } op = opRaw.(*compute.Operation) if op.Error != nil { // Return the error return OperationError(*op.Error) } d.SetId("") return nil }