github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/data_source_google_compute_network.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"google.golang.org/api/googleapi"
     8  )
     9  
    10  func dataSourceGoogleComputeNetwork() *schema.Resource {
    11  	return &schema.Resource{
    12  		Read: dataSourceGoogleComputeNetworkRead,
    13  
    14  		Schema: map[string]*schema.Schema{
    15  			"name": &schema.Schema{
    16  				Type:     schema.TypeString,
    17  				Required: true,
    18  			},
    19  
    20  			"description": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Computed: true,
    23  			},
    24  
    25  			"gateway_ipv4": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Computed: true,
    28  			},
    29  
    30  			"self_link": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Computed: true,
    33  			},
    34  
    35  			"project": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  			},
    39  
    40  			"subnetworks_self_links": {
    41  				Type:     schema.TypeList,
    42  				Computed: true,
    43  				Elem:     &schema.Schema{Type: schema.TypeString},
    44  			},
    45  		},
    46  	}
    47  }
    48  
    49  func dataSourceGoogleComputeNetworkRead(d *schema.ResourceData, meta interface{}) error {
    50  	config := meta.(*Config)
    51  
    52  	project, err := getProject(d, config)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	network, err := config.clientCompute.Networks.Get(
    57  		project, d.Get("name").(string)).Do()
    58  	if err != nil {
    59  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
    60  			// The resource doesn't exist anymore
    61  
    62  			return fmt.Errorf("Network Not Found : %s", d.Get("name"))
    63  		}
    64  
    65  		return fmt.Errorf("Error reading network: %s", err)
    66  	}
    67  	d.Set("gateway_ipv4", network.GatewayIPv4)
    68  	d.Set("self_link", network.SelfLink)
    69  	d.Set("description", network.Description)
    70  	d.Set("subnetworks_self_links", network.Subnetworks)
    71  	d.SetId(network.Name)
    72  	return nil
    73  }