@@ -111,7 +111,13 @@ func resourceComputeInstance() *schema.Resource {
111111 Schema : map [string ]* schema.Schema {
112112 "network" : & schema.Schema {
113113 Type : schema .TypeString ,
114- Required : true ,
114+ Optional : true ,
115+ ForceNew : true ,
116+ },
117+
118+ "subnetwork" : & schema.Schema {
119+ Type : schema .TypeString ,
120+ Optional : true ,
115121 ForceNew : true ,
116122 },
117123
@@ -445,17 +451,36 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
445451 prefix := fmt .Sprintf ("network_interface.%d" , i )
446452 // Load up the name of this network_interfac
447453 networkName := d .Get (prefix + ".network" ).(string )
448- network , err := config .clientCompute .Networks .Get (
449- config .Project , networkName ).Do ()
450- if err != nil {
451- return fmt .Errorf (
452- "Error referencing network '%s': %s" ,
453- networkName , err )
454+ subnetworkName := d .Get (prefix + ".subnetwork" ).(string )
455+ var networkLink , subnetworkLink string
456+
457+ if networkName != "" && subnetworkName != "" {
458+ return fmt .Errorf ("Cannot specify both network and subnetwork values." )
459+ } else if networkName != "" {
460+ network , err := config .clientCompute .Networks .Get (
461+ config .Project , networkName ).Do ()
462+ if err != nil {
463+ return fmt .Errorf (
464+ "Error referencing network '%s': %s" ,
465+ networkName , err )
466+ }
467+ networkLink = network .SelfLink
468+ } else {
469+ region := getRegionFromZone (d .Get ("zone" ).(string ))
470+ subnetwork , err := config .clientCompute .Subnetworks .Get (
471+ config .Project , region , subnetworkName ).Do ()
472+ if err != nil {
473+ return fmt .Errorf (
474+ "Error referencing subnetwork '%s' in region '%s': %s" ,
475+ subnetworkName , region , err )
476+ }
477+ subnetworkLink = subnetwork .SelfLink
454478 }
455479
456480 // Build the networkInterface
457481 var iface compute.NetworkInterface
458- iface .Network = network .SelfLink
482+ iface .Network = networkLink
483+ iface .Subnetwork = subnetworkLink
459484
460485 // Handle access_config structs
461486 accessConfigsCount := d .Get (prefix + ".access_config.#" ).(int )
0 commit comments