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