package google import ( "fmt" "log" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" "google.golang.org/api/googleapi" ) 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) err = computeOperationWaitGlobal(config, op, "Creating Network") if err != nil { return 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 { 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) } err = computeOperationWaitGlobal(config, op, "Deleting Network") if err != nil { return err } d.SetId("") return nil }