github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/google/resource_compute_network.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "code.google.com/p/google-api-go-client/compute/v1" 9 "code.google.com/p/google-api-go-client/googleapi" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceComputeNetwork() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceComputeNetworkCreate, 16 Read: resourceComputeNetworkRead, 17 Delete: resourceComputeNetworkDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "ipv4_range": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "gateway_ipv4": &schema.Schema{ 33 Type: schema.TypeString, 34 Computed: true, 35 }, 36 }, 37 } 38 } 39 40 func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) error { 41 config := meta.(*Config) 42 43 // Build the network parameter 44 network := &compute.Network{ 45 Name: d.Get("name").(string), 46 IPv4Range: d.Get("ipv4_range").(string), 47 } 48 log.Printf("[DEBUG] Network insert request: %#v", network) 49 op, err := config.clientCompute.Networks.Insert( 50 config.Project, network).Do() 51 if err != nil { 52 return fmt.Errorf("Error creating network: %s", err) 53 } 54 55 // It probably maybe worked, so store the ID now 56 d.SetId(network.Name) 57 58 // Wait for the operation to complete 59 w := &OperationWaiter{ 60 Service: config.clientCompute, 61 Op: op, 62 Project: config.Project, 63 Type: OperationWaitGlobal, 64 } 65 state := w.Conf() 66 state.Timeout = 2 * time.Minute 67 state.MinTimeout = 1 * time.Second 68 opRaw, err := state.WaitForState() 69 if err != nil { 70 return fmt.Errorf("Error waiting for network to create: %s", err) 71 } 72 op = opRaw.(*compute.Operation) 73 if op.Error != nil { 74 // The resource didn't actually create 75 d.SetId("") 76 77 // Return the error 78 return OperationError(*op.Error) 79 } 80 81 return resourceComputeNetworkRead(d, meta) 82 } 83 84 func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error { 85 config := meta.(*Config) 86 87 network, err := config.clientCompute.Networks.Get( 88 config.Project, d.Id()).Do() 89 if err != nil { 90 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 91 // The resource doesn't exist anymore 92 d.SetId("") 93 94 return nil 95 } 96 97 return fmt.Errorf("Error reading network: %s", err) 98 } 99 100 d.Set("gateway_ipv4", network.GatewayIPv4) 101 102 return nil 103 } 104 105 func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error { 106 config := meta.(*Config) 107 108 // Delete the network 109 op, err := config.clientCompute.Networks.Delete( 110 config.Project, d.Id()).Do() 111 if err != nil { 112 return fmt.Errorf("Error deleting network: %s", err) 113 } 114 115 // Wait for the operation to complete 116 w := &OperationWaiter{ 117 Service: config.clientCompute, 118 Op: op, 119 Project: config.Project, 120 Type: OperationWaitGlobal, 121 } 122 state := w.Conf() 123 state.Timeout = 2 * time.Minute 124 state.MinTimeout = 1 * time.Second 125 opRaw, err := state.WaitForState() 126 if err != nil { 127 return fmt.Errorf("Error waiting for network to delete: %s", err) 128 } 129 op = opRaw.(*compute.Operation) 130 if op.Error != nil { 131 // Return the error 132 return OperationError(*op.Error) 133 } 134 135 d.SetId("") 136 return nil 137 }