github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/google/resource_compute_disk.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "code.google.com/p/google-api-go-client/compute/v1" 9 "code.google.com/p/google-api-go-client/googleapi" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceComputeDisk() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceComputeDiskCreate, 16 Read: resourceComputeDiskRead, 17 Delete: resourceComputeDiskDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "zone": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "image": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 ForceNew: true, 36 }, 37 38 "size": &schema.Schema{ 39 Type: schema.TypeInt, 40 Optional: true, 41 ForceNew: true, 42 }, 43 44 "type": &schema.Schema{ 45 Type: schema.TypeString, 46 Optional: true, 47 ForceNew: true, 48 }, 49 }, 50 } 51 } 52 53 func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { 54 config := meta.(*Config) 55 56 // Get the zone 57 log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string)) 58 zone, err := config.clientCompute.Zones.Get( 59 config.Project, d.Get("zone").(string)).Do() 60 if err != nil { 61 return fmt.Errorf( 62 "Error loading zone '%s': %s", d.Get("zone").(string), err) 63 } 64 65 // Build the disk parameter 66 disk := &compute.Disk{ 67 Name: d.Get("name").(string), 68 SizeGb: int64(d.Get("size").(int)), 69 } 70 71 // If we were given a source image, load that. 72 if v, ok := d.GetOk("image"); ok { 73 log.Printf("[DEBUG] Loading image: %s", v.(string)) 74 image, err := readImage(config, v.(string)) 75 if err != nil { 76 return fmt.Errorf( 77 "Error loading image '%s': %s", 78 v.(string), err) 79 } 80 81 disk.SourceImage = image.SelfLink 82 } 83 84 if v, ok := d.GetOk("type"); ok { 85 log.Printf("[DEBUG] Loading disk type: %s", v.(string)) 86 diskType, err := readDiskType(config, zone, v.(string)) 87 if err != nil { 88 return fmt.Errorf( 89 "Error loading disk type '%s': %s", 90 v.(string), err) 91 } 92 93 disk.Type = diskType.SelfLink 94 } 95 96 op, err := config.clientCompute.Disks.Insert( 97 config.Project, d.Get("zone").(string), disk).Do() 98 if err != nil { 99 return fmt.Errorf("Error creating disk: %s", err) 100 } 101 102 // It probably maybe worked, so store the ID now 103 d.SetId(disk.Name) 104 105 // Wait for the operation to complete 106 w := &OperationWaiter{ 107 Service: config.clientCompute, 108 Op: op, 109 Project: config.Project, 110 Zone: d.Get("zone").(string), 111 Type: OperationWaitZone, 112 } 113 state := w.Conf() 114 state.Timeout = 2 * time.Minute 115 state.MinTimeout = 1 * time.Second 116 opRaw, err := state.WaitForState() 117 if err != nil { 118 return fmt.Errorf("Error waiting for disk to create: %s", err) 119 } 120 op = opRaw.(*compute.Operation) 121 if op.Error != nil { 122 // The resource didn't actually create 123 d.SetId("") 124 125 // Return the error 126 return OperationError(*op.Error) 127 } 128 129 return resourceComputeDiskRead(d, meta) 130 } 131 132 func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error { 133 config := meta.(*Config) 134 135 _, err := config.clientCompute.Disks.Get( 136 config.Project, d.Get("zone").(string), d.Id()).Do() 137 if err != nil { 138 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 139 // The resource doesn't exist anymore 140 d.SetId("") 141 142 return nil 143 } 144 145 return fmt.Errorf("Error reading disk: %s", err) 146 } 147 148 return nil 149 } 150 151 func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error { 152 config := meta.(*Config) 153 154 // Delete the disk 155 op, err := config.clientCompute.Disks.Delete( 156 config.Project, d.Get("zone").(string), d.Id()).Do() 157 if err != nil { 158 return fmt.Errorf("Error deleting disk: %s", err) 159 } 160 161 // Wait for the operation to complete 162 w := &OperationWaiter{ 163 Service: config.clientCompute, 164 Op: op, 165 Project: config.Project, 166 Zone: d.Get("zone").(string), 167 Type: OperationWaitZone, 168 } 169 state := w.Conf() 170 state.Timeout = 2 * time.Minute 171 state.MinTimeout = 1 * time.Second 172 opRaw, err := state.WaitForState() 173 if err != nil { 174 return fmt.Errorf("Error waiting for disk to delete: %s", err) 175 } 176 op = opRaw.(*compute.Operation) 177 if op.Error != nil { 178 // Return the error 179 return OperationError(*op.Error) 180 } 181 182 d.SetId("") 183 return nil 184 }