github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 "auto_create_subnetworks": &schema.Schema{ 26 Type: schema.TypeBool, 27 Optional: true, 28 ForceNew: true, 29 /* Ideally this would default to true as per the API, but that would cause 30 existing Terraform configs which have not been updated to report this as 31 a change. Perhaps we can bump this for a minor release bump rather than 32 a point release. 33 Default: false, */ 34 ConflictsWith: []string{"ipv4_range"}, 35 }, 36 37 "description": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 ForceNew: true, 41 }, 42 43 "gateway_ipv4": &schema.Schema{ 44 Type: schema.TypeString, 45 Computed: true, 46 }, 47 48 "ipv4_range": &schema.Schema{ 49 Type: schema.TypeString, 50 Optional: true, 51 ForceNew: true, 52 Deprecated: "Please use google_compute_subnetwork resources instead.", 53 }, 54 55 "project": &schema.Schema{ 56 Type: schema.TypeString, 57 Optional: true, 58 ForceNew: true, 59 }, 60 61 "self_link": &schema.Schema{ 62 Type: schema.TypeString, 63 Computed: true, 64 }, 65 }, 66 } 67 } 68 69 func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) error { 70 config := meta.(*Config) 71 72 project, err := getProject(d, config) 73 if err != nil { 74 return err 75 } 76 77 // 78 // Possible modes: 79 // - 1 Legacy mode - Create a network in the legacy mode. ipv4_range is set. auto_create_subnetworks must not be 80 // set (enforced by ConflictsWith schema attribute) 81 // - 2 Distributed Mode - Create a new generation network that supports subnetworks: 82 // - 2.a - Auto subnet mode - auto_create_subnetworks = true, Google will generate 1 subnetwork per region 83 // - 2.b - Custom subnet mode - auto_create_subnetworks = false & ipv4_range not set, 84 // 85 autoCreateSubnetworks := d.Get("auto_create_subnetworks").(bool) 86 87 // Build the network parameter 88 network := &compute.Network{ 89 Name: d.Get("name").(string), 90 AutoCreateSubnetworks: autoCreateSubnetworks, 91 Description: d.Get("description").(string), 92 } 93 94 if v, ok := d.GetOk("ipv4_range"); ok { 95 log.Printf("[DEBUG] Setting IPv4Range (%#v) for legacy network mode", v.(string)) 96 network.IPv4Range = v.(string) 97 } else { 98 // custom subnet mode, so make sure AutoCreateSubnetworks field is included in request otherwise 99 // google will create a network in legacy mode. 100 network.ForceSendFields = []string{"AutoCreateSubnetworks"} 101 } 102 103 log.Printf("[DEBUG] Network insert request: %#v", network) 104 op, err := config.clientCompute.Networks.Insert( 105 project, network).Do() 106 if err != nil { 107 return fmt.Errorf("Error creating network: %s", err) 108 } 109 110 // It probably maybe worked, so store the ID now 111 d.SetId(network.Name) 112 113 err = computeOperationWaitGlobal(config, op, project, "Creating Network") 114 if err != nil { 115 return err 116 } 117 118 return resourceComputeNetworkRead(d, meta) 119 } 120 121 func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error { 122 config := meta.(*Config) 123 124 project, err := getProject(d, config) 125 if err != nil { 126 return err 127 } 128 129 network, err := config.clientCompute.Networks.Get( 130 project, d.Id()).Do() 131 if err != nil { 132 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 133 log.Printf("[WARN] Removing Network %q because it's gone", d.Get("name").(string)) 134 // The resource doesn't exist anymore 135 d.SetId("") 136 137 return nil 138 } 139 140 return fmt.Errorf("Error reading network: %s", err) 141 } 142 143 d.Set("gateway_ipv4", network.GatewayIPv4) 144 d.Set("self_link", network.SelfLink) 145 146 return nil 147 } 148 149 func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error { 150 config := meta.(*Config) 151 152 project, err := getProject(d, config) 153 if err != nil { 154 return err 155 } 156 157 // Delete the network 158 op, err := config.clientCompute.Networks.Delete( 159 project, d.Id()).Do() 160 if err != nil { 161 return fmt.Errorf("Error deleting network: %s", err) 162 } 163 164 err = computeOperationWaitGlobal(config, op, project, "Deleting Network") 165 if err != nil { 166 return err 167 } 168 169 d.SetId("") 170 return nil 171 }