|
9 | 9 | "github.com/hashicorp/terraform/helper/validation"
|
10 | 10 | "google.golang.org/api/compute/v1"
|
11 | 11 | "google.golang.org/api/googleapi"
|
| 12 | + "regexp" |
12 | 13 | )
|
13 | 14 |
|
14 | 15 | func stringScopeHashcode(v interface{}) int {
|
@@ -278,20 +279,26 @@ func resourceComputeInstance() *schema.Resource {
|
278 | 279 | Elem: &schema.Resource{
|
279 | 280 | Schema: map[string]*schema.Schema{
|
280 | 281 | "network": &schema.Schema{
|
281 |
| - Type: schema.TypeString, |
282 |
| - Optional: true, |
283 |
| - ForceNew: true, |
| 282 | + Type: schema.TypeString, |
| 283 | + Optional: true, |
| 284 | + Computed: true, |
| 285 | + ForceNew: true, |
| 286 | + DiffSuppressFunc: linkDiffSuppress, |
| 287 | + ConflictsWith: []string{"network_interface.0.subnetwork", "network_interface.0.subnetwork_project"}, |
284 | 288 | },
|
285 | 289 |
|
286 | 290 | "subnetwork": &schema.Schema{
|
287 |
| - Type: schema.TypeString, |
288 |
| - Optional: true, |
289 |
| - ForceNew: true, |
| 291 | + Type: schema.TypeString, |
| 292 | + Optional: true, |
| 293 | + Computed: true, |
| 294 | + ForceNew: true, |
| 295 | + DiffSuppressFunc: linkDiffSuppress, |
290 | 296 | },
|
291 | 297 |
|
292 | 298 | "subnetwork_project": &schema.Schema{
|
293 | 299 | Type: schema.TypeString,
|
294 | 300 | Optional: true,
|
| 301 | + Computed: true, |
295 | 302 | ForceNew: true,
|
296 | 303 | },
|
297 | 304 |
|
@@ -704,34 +711,21 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
|
704 | 711 | prefix := fmt.Sprintf("network_interface.%d", i)
|
705 | 712 | // Load up the name of this network_interface
|
706 | 713 | networkName := d.Get(prefix + ".network").(string)
|
707 |
| - subnetworkName := d.Get(prefix + ".subnetwork").(string) |
708 |
| - subnetworkProject := d.Get(prefix + ".subnetwork_project").(string) |
709 | 714 | address := d.Get(prefix + ".address").(string)
|
710 | 715 | var networkLink, subnetworkLink string
|
711 | 716 |
|
712 |
| - if networkName != "" && subnetworkName != "" { |
713 |
| - return fmt.Errorf("Cannot specify both network and subnetwork values.") |
714 |
| - } else if networkName != "" { |
| 717 | + if networkName != "" { |
715 | 718 | networkLink, err = getNetworkLink(d, config, prefix+".network")
|
716 | 719 | if err != nil {
|
717 | 720 | return fmt.Errorf(
|
718 | 721 | "Error referencing network '%s': %s",
|
719 | 722 | networkName, err)
|
720 | 723 | }
|
721 |
| - |
722 | 724 | } else {
|
723 |
| - region := getRegionFromZone(d.Get("zone").(string)) |
724 |
| - if subnetworkProject == "" { |
725 |
| - subnetworkProject = project |
726 |
| - } |
727 |
| - subnetwork, err := config.clientCompute.Subnetworks.Get( |
728 |
| - subnetworkProject, region, subnetworkName).Do() |
| 725 | + subnetworkLink, err = getSubnetworkLink(d, config, prefix+".subnetwork", prefix+".subnetwork_project", "zone") |
729 | 726 | if err != nil {
|
730 |
| - return fmt.Errorf( |
731 |
| - "Error referencing subnetwork '%s' in region '%s': %s", |
732 |
| - subnetworkName, region, err) |
| 727 | + return err |
733 | 728 | }
|
734 |
| - subnetworkLink = subnetwork.SelfLink |
735 | 729 | }
|
736 | 730 |
|
737 | 731 | // Build the networkInterface
|
@@ -961,9 +955,9 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
|
961 | 955 | networkInterfaces = append(networkInterfaces, map[string]interface{}{
|
962 | 956 | "name": iface.Name,
|
963 | 957 | "address": iface.NetworkIP,
|
964 |
| - "network": d.Get(fmt.Sprintf("network_interface.%d.network", i)), |
965 |
| - "subnetwork": d.Get(fmt.Sprintf("network_interface.%d.subnetwork", i)), |
966 |
| - "subnetwork_project": d.Get(fmt.Sprintf("network_interface.%d.subnetwork_project", i)), |
| 958 | + "network": iface.Network, |
| 959 | + "subnetwork": iface.Subnetwork, |
| 960 | + "subnetwork_project": getProjectFromSubnetworkLink(iface.Subnetwork), |
967 | 961 | "access_config": accessConfigs,
|
968 | 962 | })
|
969 | 963 | }
|
@@ -1452,3 +1446,12 @@ func flattenScratchDisk(disk *compute.AttachedDisk) map[string]interface{} {
|
1452 | 1446 | }
|
1453 | 1447 | return result
|
1454 | 1448 | }
|
| 1449 | + |
| 1450 | +func getProjectFromSubnetworkLink(subnetwork string) string { |
| 1451 | + r := regexp.MustCompile(SubnetworkLinkRegex) |
| 1452 | + if !r.MatchString(subnetwork) { |
| 1453 | + return "" |
| 1454 | + } |
| 1455 | + |
| 1456 | + return r.FindStringSubmatch(subnetwork)[1] |
| 1457 | +} |
0 commit comments