github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_disk.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 resourceComputeDisk() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceComputeDiskCreate, 15 Read: resourceComputeDiskRead, 16 Delete: resourceComputeDiskDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 25 "zone": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 31 "disk_encryption_key_raw": &schema.Schema{ 32 Type: schema.TypeString, 33 Optional: true, 34 ForceNew: true, 35 Sensitive: true, 36 }, 37 38 "disk_encryption_key_sha256": &schema.Schema{ 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 43 "image": &schema.Schema{ 44 Type: schema.TypeString, 45 Optional: true, 46 ForceNew: true, 47 }, 48 49 "project": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 ForceNew: true, 53 }, 54 55 "size": &schema.Schema{ 56 Type: schema.TypeInt, 57 Optional: true, 58 ForceNew: true, 59 }, 60 61 "self_link": &schema.Schema{ 62 Type: schema.TypeString, 63 Computed: true, 64 }, 65 66 "snapshot": &schema.Schema{ 67 Type: schema.TypeString, 68 Optional: true, 69 ForceNew: true, 70 }, 71 72 "type": &schema.Schema{ 73 Type: schema.TypeString, 74 Optional: true, 75 ForceNew: true, 76 }, 77 }, 78 } 79 } 80 81 func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { 82 config := meta.(*Config) 83 84 project, err := getProject(d, config) 85 if err != nil { 86 return err 87 } 88 89 // Get the zone 90 log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string)) 91 zone, err := config.clientCompute.Zones.Get( 92 project, d.Get("zone").(string)).Do() 93 if err != nil { 94 return fmt.Errorf( 95 "Error loading zone '%s': %s", d.Get("zone").(string), err) 96 } 97 98 // Build the disk parameter 99 disk := &compute.Disk{ 100 Name: d.Get("name").(string), 101 SizeGb: int64(d.Get("size").(int)), 102 } 103 104 // If we were given a source image, load that. 105 if v, ok := d.GetOk("image"); ok { 106 log.Printf("[DEBUG] Resolving image name: %s", v.(string)) 107 imageUrl, err := resolveImage(config, v.(string)) 108 if err != nil { 109 return fmt.Errorf( 110 "Error resolving image name '%s': %s", 111 v.(string), err) 112 } 113 114 disk.SourceImage = imageUrl 115 log.Printf("[DEBUG] Image name resolved to: %s", imageUrl) 116 } 117 118 if v, ok := d.GetOk("type"); ok { 119 log.Printf("[DEBUG] Loading disk type: %s", v.(string)) 120 diskType, err := readDiskType(config, zone, v.(string)) 121 if err != nil { 122 return fmt.Errorf( 123 "Error loading disk type '%s': %s", 124 v.(string), err) 125 } 126 127 disk.Type = diskType.SelfLink 128 } 129 130 if v, ok := d.GetOk("snapshot"); ok { 131 snapshotName := v.(string) 132 log.Printf("[DEBUG] Loading snapshot: %s", snapshotName) 133 snapshotData, err := config.clientCompute.Snapshots.Get( 134 project, snapshotName).Do() 135 136 if err != nil { 137 return fmt.Errorf( 138 "Error loading snapshot '%s': %s", 139 snapshotName, err) 140 } 141 142 disk.SourceSnapshot = snapshotData.SelfLink 143 } 144 145 if v, ok := d.GetOk("disk_encryption_key_raw"); ok { 146 disk.DiskEncryptionKey = &compute.CustomerEncryptionKey{} 147 disk.DiskEncryptionKey.RawKey = v.(string) 148 } 149 150 op, err := config.clientCompute.Disks.Insert( 151 project, d.Get("zone").(string), disk).Do() 152 if err != nil { 153 return fmt.Errorf("Error creating disk: %s", err) 154 } 155 156 // It probably maybe worked, so store the ID now 157 d.SetId(disk.Name) 158 159 err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Creating Disk") 160 if err != nil { 161 return err 162 } 163 return resourceComputeDiskRead(d, meta) 164 } 165 166 func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error { 167 config := meta.(*Config) 168 169 project, err := getProject(d, config) 170 if err != nil { 171 return err 172 } 173 174 disk, err := config.clientCompute.Disks.Get( 175 project, d.Get("zone").(string), d.Id()).Do() 176 if err != nil { 177 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 178 log.Printf("[WARN] Removing Disk %q because it's gone", d.Get("name").(string)) 179 // The resource doesn't exist anymore 180 d.SetId("") 181 182 return nil 183 } 184 185 return fmt.Errorf("Error reading disk: %s", err) 186 } 187 188 d.Set("self_link", disk.SelfLink) 189 if disk.DiskEncryptionKey != nil && disk.DiskEncryptionKey.Sha256 != "" { 190 d.Set("disk_encryption_key_sha256", disk.DiskEncryptionKey.Sha256) 191 } 192 193 return nil 194 } 195 196 func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error { 197 config := meta.(*Config) 198 199 project, err := getProject(d, config) 200 if err != nil { 201 return err 202 } 203 204 // Delete the disk 205 op, err := config.clientCompute.Disks.Delete( 206 project, d.Get("zone").(string), d.Id()).Do() 207 if err != nil { 208 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 209 log.Printf("[WARN] Removing Disk %q because it's gone", d.Get("name").(string)) 210 // The resource doesn't exist anymore 211 d.SetId("") 212 return nil 213 } 214 return fmt.Errorf("Error deleting disk: %s", err) 215 } 216 217 zone := d.Get("zone").(string) 218 err = computeOperationWaitZone(config, op, project, zone, "Deleting Disk") 219 if err != nil { 220 return err 221 } 222 223 d.SetId("") 224 return nil 225 }