github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 18 Schema: map[string]*schema.Schema{ 19 "name": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 25 "address": &schema.Schema{ 26 Type: schema.TypeString, 27 Computed: true, 28 }, 29 30 "self_link": &schema.Schema{ 31 Type: schema.TypeString, 32 Computed: true, 33 }, 34 35 "region": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 ForceNew: true, 39 }, 40 }, 41 } 42 } 43 44 func getOptionalRegion(d *schema.ResourceData, config *Config) string { 45 if res, ok := d.GetOk("region"); !ok { 46 return config.Region 47 } else { 48 return res.(string) 49 } 50 } 51 52 func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) error { 53 config := meta.(*Config) 54 region := getOptionalRegion(d, config) 55 56 // Build the address parameter 57 addr := &compute.Address{Name: d.Get("name").(string)} 58 op, err := config.clientCompute.Addresses.Insert( 59 config.Project, region, addr).Do() 60 if err != nil { 61 return fmt.Errorf("Error creating address: %s", err) 62 } 63 64 // It probably maybe worked, so store the ID now 65 d.SetId(addr.Name) 66 67 err = computeOperationWaitRegion(config, op, region, "Creating Address") 68 if err != nil { 69 return err 70 } 71 72 return resourceComputeAddressRead(d, meta) 73 } 74 75 func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error { 76 config := meta.(*Config) 77 78 region := getOptionalRegion(d, config) 79 80 addr, err := config.clientCompute.Addresses.Get( 81 config.Project, region, d.Id()).Do() 82 if err != nil { 83 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 84 // The resource doesn't exist anymore 85 d.SetId("") 86 87 return nil 88 } 89 90 return fmt.Errorf("Error reading address: %s", err) 91 } 92 93 d.Set("address", addr.Address) 94 d.Set("self_link", addr.SelfLink) 95 96 return nil 97 } 98 99 func resourceComputeAddressDelete(d *schema.ResourceData, meta interface{}) error { 100 config := meta.(*Config) 101 102 region := getOptionalRegion(d, config) 103 // Delete the address 104 log.Printf("[DEBUG] address delete request") 105 op, err := config.clientCompute.Addresses.Delete( 106 config.Project, region, d.Id()).Do() 107 if err != nil { 108 return fmt.Errorf("Error deleting address: %s", err) 109 } 110 111 err = computeOperationWaitRegion(config, op, region, "Deleting Address") 112 if err != nil { 113 return err 114 } 115 116 d.SetId("") 117 return nil 118 }