github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_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 resourceComputeAddress() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeAddressCreate,
    15  		Read:   resourceComputeAddressRead,
    16  		Delete: resourceComputeAddressDelete,
    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  			"region": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  				ForceNew: true,
    42  			},
    43  
    44  			"self_link": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Computed: true,
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) error {
    53  	config := meta.(*Config)
    54  
    55  	region, err := getRegion(d, config)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	project, err := getProject(d, config)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	// Build the address parameter
    66  	addr := &compute.Address{Name: d.Get("name").(string)}
    67  	op, err := config.clientCompute.Addresses.Insert(
    68  		project, region, addr).Do()
    69  	if err != nil {
    70  		return fmt.Errorf("Error creating address: %s", err)
    71  	}
    72  
    73  	// It probably maybe worked, so store the ID now
    74  	d.SetId(addr.Name)
    75  
    76  	err = computeOperationWaitRegion(config, op, project, region, "Creating Address")
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	return resourceComputeAddressRead(d, meta)
    82  }
    83  
    84  func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error {
    85  	config := meta.(*Config)
    86  
    87  	region, err := getRegion(d, config)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	project, err := getProject(d, config)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	addr, err := config.clientCompute.Addresses.Get(
    98  		project, region, d.Id()).Do()
    99  	if err != nil {
   100  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   101  			// The resource doesn't exist anymore
   102  			log.Printf("[WARN] Removing Address %q because it's gone", d.Get("name").(string))
   103  			d.SetId("")
   104  
   105  			return nil
   106  		}
   107  
   108  		return fmt.Errorf("Error reading address: %s", err)
   109  	}
   110  
   111  	d.Set("address", addr.Address)
   112  	d.Set("self_link", addr.SelfLink)
   113  	d.Set("name", addr.Name)
   114  
   115  	return nil
   116  }
   117  
   118  func resourceComputeAddressDelete(d *schema.ResourceData, meta interface{}) error {
   119  	config := meta.(*Config)
   120  
   121  	region, err := getRegion(d, config)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	project, err := getProject(d, config)
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	// Delete the address
   132  	log.Printf("[DEBUG] address delete request")
   133  	op, err := config.clientCompute.Addresses.Delete(
   134  		project, region, d.Id()).Do()
   135  	if err != nil {
   136  		return fmt.Errorf("Error deleting address: %s", err)
   137  	}
   138  
   139  	err = computeOperationWaitRegion(config, op, project, region, "Deleting Address")
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	d.SetId("")
   145  	return nil
   146  }