github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_network.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "google.golang.org/api/compute/v1" 9 "google.golang.org/api/googleapi" 10 ) 11 12 func resourceComputeNetwork() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceComputeNetworkCreate, 15 Read: resourceComputeNetworkRead, 16 Delete: resourceComputeNetworkDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 25 "ipv4_range": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 31 "gateway_ipv4": &schema.Schema{ 32 Type: schema.TypeString, 33 Computed: true, 34 }, 35 36 "self_link": &schema.Schema{ 37 Type: schema.TypeString, 38 Computed: true, 39 }, 40 }, 41 } 42 } 43 44 func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) error { 45 config := meta.(*Config) 46 47 // Build the network parameter 48 network := &compute.Network{ 49 Name: d.Get("name").(string), 50 IPv4Range: d.Get("ipv4_range").(string), 51 } 52 log.Printf("[DEBUG] Network insert request: %#v", network) 53 op, err := config.clientCompute.Networks.Insert( 54 config.Project, network).Do() 55 if err != nil { 56 return fmt.Errorf("Error creating network: %s", err) 57 } 58 59 // It probably maybe worked, so store the ID now 60 d.SetId(network.Name) 61 62 err = computeOperationWaitGlobal(config, op, "Creating Network") 63 if err != nil { 64 return err 65 } 66 67 return resourceComputeNetworkRead(d, meta) 68 } 69 70 func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error { 71 config := meta.(*Config) 72 73 network, err := config.clientCompute.Networks.Get( 74 config.Project, d.Id()).Do() 75 if err != nil { 76 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 77 // The resource doesn't exist anymore 78 d.SetId("") 79 80 return nil 81 } 82 83 return fmt.Errorf("Error reading network: %s", err) 84 } 85 86 d.Set("gateway_ipv4", network.GatewayIPv4) 87 d.Set("self_link", network.SelfLink) 88 89 return nil 90 } 91 92 func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error { 93 config := meta.(*Config) 94 95 // Delete the network 96 op, err := config.clientCompute.Networks.Delete( 97 config.Project, d.Id()).Do() 98 if err != nil { 99 return fmt.Errorf("Error deleting network: %s", err) 100 } 101 102 err = computeOperationWaitGlobal(config, op, "Deleting Network") 103 if err != nil { 104 return err 105 } 106 107 d.SetId("") 108 return nil 109 }