github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 "self_link": &schema.Schema{ 31 Type: schema.TypeString, 32 Computed: true, 33 }, 34 }, 35 } 36 } 37 38 func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}) error { 39 config := meta.(*Config) 40 41 // Build the address parameter 42 addr := &compute.Address{Name: d.Get("name").(string)} 43 op, err := config.clientCompute.GlobalAddresses.Insert( 44 config.Project, addr).Do() 45 if err != nil { 46 return fmt.Errorf("Error creating address: %s", err) 47 } 48 49 // It probably maybe worked, so store the ID now 50 d.SetId(addr.Name) 51 52 err = computeOperationWaitGlobal(config, op, "Creating Global Address") 53 if err != nil { 54 return err 55 } 56 57 return resourceComputeGlobalAddressRead(d, meta) 58 } 59 60 func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) error { 61 config := meta.(*Config) 62 63 addr, err := config.clientCompute.GlobalAddresses.Get( 64 config.Project, d.Id()).Do() 65 if err != nil { 66 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 67 // The resource doesn't exist anymore 68 d.SetId("") 69 70 return nil 71 } 72 73 return fmt.Errorf("Error reading address: %s", err) 74 } 75 76 d.Set("address", addr.Address) 77 d.Set("self_link", addr.SelfLink) 78 79 return nil 80 } 81 82 func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}) error { 83 config := meta.(*Config) 84 85 // Delete the address 86 log.Printf("[DEBUG] address delete request") 87 op, err := config.clientCompute.GlobalAddresses.Delete( 88 config.Project, d.Id()).Do() 89 if err != nil { 90 return fmt.Errorf("Error deleting address: %s", err) 91 } 92 93 err = computeOperationWaitGlobal(config, op, "Deleting Global Address") 94 if err != nil { 95 return err 96 } 97 98 d.SetId("") 99 return nil 100 }