github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 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 "self_link": &schema.Schema{ 37 Type: schema.TypeString, 38 Computed: true, 39 }, 40 }, 41 } 42 } 43 44 func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}) error { 45 config := meta.(*Config) 46 47 project, err := getProject(d, config) 48 if err != nil { 49 return err 50 } 51 52 // Build the address parameter 53 addr := &compute.Address{Name: d.Get("name").(string)} 54 op, err := config.clientCompute.GlobalAddresses.Insert( 55 project, addr).Do() 56 if err != nil { 57 return fmt.Errorf("Error creating address: %s", err) 58 } 59 60 // It probably maybe worked, so store the ID now 61 d.SetId(addr.Name) 62 63 err = computeOperationWaitGlobal(config, op, project, "Creating Global Address") 64 if err != nil { 65 return err 66 } 67 68 return resourceComputeGlobalAddressRead(d, meta) 69 } 70 71 func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) error { 72 config := meta.(*Config) 73 74 project, err := getProject(d, config) 75 if err != nil { 76 return err 77 } 78 79 addr, err := config.clientCompute.GlobalAddresses.Get( 80 project, d.Id()).Do() 81 if err != nil { 82 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 83 log.Printf("[WARN] Removing Global Address %q because it's gone", d.Get("name").(string)) 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 resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}) error { 100 config := meta.(*Config) 101 102 project, err := getProject(d, config) 103 if err != nil { 104 return err 105 } 106 107 // Delete the address 108 log.Printf("[DEBUG] address delete request") 109 op, err := config.clientCompute.GlobalAddresses.Delete( 110 project, d.Id()).Do() 111 if err != nil { 112 return fmt.Errorf("Error deleting address: %s", err) 113 } 114 115 err = computeOperationWaitGlobal(config, op, project, "Deleting Global Address") 116 if err != nil { 117 return err 118 } 119 120 d.SetId("") 121 return nil 122 }