github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_global_address.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 resourceComputeGlobalAddress() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeGlobalAddressCreate,
    15  		Read:   resourceComputeGlobalAddressRead,
    16  		Delete: resourceComputeGlobalAddressDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"address": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Computed: true,
    30  			},
    31  
    32  			"project": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"self_link": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}) error {
    47  	config := meta.(*Config)
    48  
    49  	project, err := getProject(d, config)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	// Build the address parameter
    55  	addr := &compute.Address{Name: d.Get("name").(string)}
    56  	op, err := config.clientCompute.GlobalAddresses.Insert(
    57  		project, addr).Do()
    58  	if err != nil {
    59  		return fmt.Errorf("Error creating address: %s", err)
    60  	}
    61  
    62  	// It probably maybe worked, so store the ID now
    63  	d.SetId(addr.Name)
    64  
    65  	err = computeOperationWaitGlobal(config, op, project, "Creating Global Address")
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	return resourceComputeGlobalAddressRead(d, meta)
    71  }
    72  
    73  func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) error {
    74  	config := meta.(*Config)
    75  
    76  	project, err := getProject(d, config)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	addr, err := config.clientCompute.GlobalAddresses.Get(
    82  		project, d.Id()).Do()
    83  	if err != nil {
    84  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
    85  			log.Printf("[WARN] Removing Global Address %q because it's gone", d.Get("name").(string))
    86  			// The resource doesn't exist anymore
    87  			d.SetId("")
    88  
    89  			return nil
    90  		}
    91  
    92  		return fmt.Errorf("Error reading address: %s", err)
    93  	}
    94  
    95  	d.Set("address", addr.Address)
    96  	d.Set("self_link", addr.SelfLink)
    97  	d.Set("name", addr.Name)
    98  
    99  	return nil
   100  }
   101  
   102  func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}) error {
   103  	config := meta.(*Config)
   104  
   105  	project, err := getProject(d, config)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	// Delete the address
   111  	log.Printf("[DEBUG] address delete request")
   112  	op, err := config.clientCompute.GlobalAddresses.Delete(
   113  		project, d.Id()).Do()
   114  	if err != nil {
   115  		return fmt.Errorf("Error deleting address: %s", err)
   116  	}
   117  
   118  	err = computeOperationWaitGlobal(config, op, project, "Deleting Global Address")
   119  	if err != nil {
   120  		return err
   121  	}
   122  
   123  	d.SetId("")
   124  	return nil
   125  }