github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/google/resource_compute_address.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"code.google.com/p/google-api-go-client/compute/v1"
     9  	"code.google.com/p/google-api-go-client/googleapi"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceComputeAddress() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceComputeAddressCreate,
    16  		Read:   resourceComputeAddressRead,
    17  		Delete: resourceComputeAddressDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"address": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Computed: true,
    29  			},
    30  		},
    31  	}
    32  }
    33  
    34  func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) error {
    35  	config := meta.(*Config)
    36  
    37  	// Build the address parameter
    38  	addr := &compute.Address{Name: d.Get("name").(string)}
    39  	log.Printf("[DEBUG] Address insert request: %#v", addr)
    40  	op, err := config.clientCompute.Addresses.Insert(
    41  		config.Project, config.Region, addr).Do()
    42  	if err != nil {
    43  		return fmt.Errorf("Error creating address: %s", err)
    44  	}
    45  
    46  	// It probably maybe worked, so store the ID now
    47  	d.SetId(addr.Name)
    48  
    49  	// Wait for the operation to complete
    50  	w := &OperationWaiter{
    51  		Service: config.clientCompute,
    52  		Op:      op,
    53  		Project: config.Project,
    54  		Region:  config.Region,
    55  		Type:    OperationWaitRegion,
    56  	}
    57  	state := w.Conf()
    58  	state.Timeout = 2 * time.Minute
    59  	state.MinTimeout = 1 * time.Second
    60  	opRaw, err := state.WaitForState()
    61  	if err != nil {
    62  		return fmt.Errorf("Error waiting for address to create: %s", err)
    63  	}
    64  	op = opRaw.(*compute.Operation)
    65  	if op.Error != nil {
    66  		// The resource didn't actually create
    67  		d.SetId("")
    68  
    69  		// Return the error
    70  		return OperationError(*op.Error)
    71  	}
    72  
    73  	return resourceComputeAddressRead(d, meta)
    74  }
    75  
    76  func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error {
    77  	config := meta.(*Config)
    78  
    79  	addr, err := config.clientCompute.Addresses.Get(
    80  		config.Project, config.Region, d.Id()).Do()
    81  	if err != nil {
    82  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
    83  			// The resource doesn't exist anymore
    84  			d.SetId("")
    85  
    86  			return nil
    87  		}
    88  
    89  		return fmt.Errorf("Error reading address: %s", err)
    90  	}
    91  
    92  	d.Set("address", addr.Address)
    93  
    94  	return nil
    95  }
    96  
    97  func resourceComputeAddressDelete(d *schema.ResourceData, meta interface{}) error {
    98  	config := meta.(*Config)
    99  
   100  	// Delete the address
   101  	op, err := config.clientCompute.Addresses.Delete(
   102  		config.Project, config.Region, d.Id()).Do()
   103  	if err != nil {
   104  		return fmt.Errorf("Error deleting address: %s", err)
   105  	}
   106  
   107  	// Wait for the operation to complete
   108  	w := &OperationWaiter{
   109  		Service: config.clientCompute,
   110  		Op:      op,
   111  		Project: config.Project,
   112  		Region:  config.Region,
   113  		Type:    OperationWaitRegion,
   114  	}
   115  	state := w.Conf()
   116  	state.Timeout = 2 * time.Minute
   117  	state.MinTimeout = 1 * time.Second
   118  	opRaw, err := state.WaitForState()
   119  	if err != nil {
   120  		return fmt.Errorf("Error waiting for address to delete: %s", err)
   121  	}
   122  	op = opRaw.(*compute.Operation)
   123  	if op.Error != nil {
   124  		// Return the error
   125  		return OperationError(*op.Error)
   126  	}
   127  
   128  	d.SetId("")
   129  	return nil
   130  }