github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/data_source_google_compute_subnetwork.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 dataSourceGoogleComputeSubnetwork() *schema.Resource {
    11  	return &schema.Resource{
    12  		Read: dataSourceGoogleComputeSubnetworkRead,
    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  			"self_link": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  			"ip_cidr_range": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  			"network": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  			"gateway_address": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Computed: true,
    39  			},
    40  			"region": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  			},
    44  
    45  			"project": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Optional: true,
    48  			},
    49  		},
    50  	}
    51  }
    52  
    53  func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error {
    54  	config := meta.(*Config)
    55  
    56  	project, err := getProject(d, config)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	region, err := getRegion(d, config)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	subnetwork, err := config.clientCompute.Subnetworks.Get(
    66  		project, region, d.Get("name").(string)).Do()
    67  	if err != nil {
    68  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
    69  			// The resource doesn't exist anymore
    70  
    71  			return fmt.Errorf("Subnetwork Not Found")
    72  		}
    73  
    74  		return fmt.Errorf("Error reading Subnetwork: %s", err)
    75  	}
    76  
    77  	d.Set("ip_cidr_range", subnetwork.IpCidrRange)
    78  	d.Set("self_link", subnetwork.SelfLink)
    79  	d.Set("description", subnetwork.Description)
    80  	d.Set("gateway_address", subnetwork.GatewayAddress)
    81  	d.Set("network", subnetwork.Network)
    82  
    83  	//Subnet id creation is defined in resource_compute_subnetwork.go
    84  	subnetwork.Region = region
    85  	d.SetId(createSubnetID(subnetwork))
    86  	return nil
    87  }