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