github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_image.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 resourceComputeImage() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceComputeImageCreate, 15 Read: resourceComputeImageRead, 16 Delete: resourceComputeImageDelete, 17 18 Schema: map[string]*schema.Schema{ 19 // TODO(cblecker): one of source_disk or raw_disk is required 20 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 27 "description": &schema.Schema{ 28 Type: schema.TypeString, 29 Computed: true, 30 }, 31 32 "family": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 ForceNew: true, 36 }, 37 38 "project": &schema.Schema{ 39 Type: schema.TypeString, 40 Optional: true, 41 ForceNew: true, 42 }, 43 44 "source_disk": &schema.Schema{ 45 Type: schema.TypeString, 46 Optional: true, 47 ForceNew: true, 48 }, 49 50 "raw_disk": &schema.Schema{ 51 Type: schema.TypeList, 52 Optional: true, 53 ForceNew: true, 54 MaxItems: 1, 55 Elem: &schema.Resource{ 56 Schema: map[string]*schema.Schema{ 57 "source": &schema.Schema{ 58 Type: schema.TypeString, 59 Required: true, 60 ForceNew: true, 61 }, 62 "sha1": &schema.Schema{ 63 Type: schema.TypeString, 64 Optional: true, 65 ForceNew: true, 66 }, 67 "container_type": &schema.Schema{ 68 Type: schema.TypeString, 69 Optional: true, 70 Default: "TAR", 71 ForceNew: true, 72 }, 73 }, 74 }, 75 }, 76 77 "self_link": &schema.Schema{ 78 Type: schema.TypeString, 79 Computed: true, 80 }, 81 82 "create_timeout": &schema.Schema{ 83 Type: schema.TypeInt, 84 Optional: true, 85 Default: 4, 86 ForceNew: true, 87 }, 88 }, 89 } 90 } 91 92 func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error { 93 config := meta.(*Config) 94 95 project, err := getProject(d, config) 96 if err != nil { 97 return err 98 } 99 100 // Build the image 101 image := &compute.Image{ 102 Name: d.Get("name").(string), 103 } 104 105 if v, ok := d.GetOk("description"); ok { 106 image.Description = v.(string) 107 } 108 109 if v, ok := d.GetOk("family"); ok { 110 image.Family = v.(string) 111 } 112 113 // Load up the source_disk for this image if specified 114 if v, ok := d.GetOk("source_disk"); ok { 115 image.SourceDisk = v.(string) 116 } 117 118 // Load up the raw_disk for this image if specified 119 if v, ok := d.GetOk("raw_disk"); ok { 120 rawDiskEle := v.([]interface{})[0].(map[string]interface{}) 121 imageRawDisk := &compute.ImageRawDisk{ 122 Source: rawDiskEle["source"].(string), 123 ContainerType: rawDiskEle["container_type"].(string), 124 } 125 if val, ok := rawDiskEle["sha1"]; ok { 126 imageRawDisk.Sha1Checksum = val.(string) 127 } 128 129 image.RawDisk = imageRawDisk 130 } 131 132 // Read create timeout 133 var createTimeout int 134 if v, ok := d.GetOk("create_timeout"); ok { 135 createTimeout = v.(int) 136 } 137 138 // Insert the image 139 op, err := config.clientCompute.Images.Insert( 140 project, image).Do() 141 if err != nil { 142 return fmt.Errorf("Error creating image: %s", err) 143 } 144 145 // Store the ID 146 d.SetId(image.Name) 147 148 err = computeOperationWaitGlobalTime(config, op, project, "Creating Image", createTimeout) 149 if err != nil { 150 return err 151 } 152 153 return resourceComputeImageRead(d, meta) 154 } 155 156 func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error { 157 config := meta.(*Config) 158 159 project, err := getProject(d, config) 160 if err != nil { 161 return err 162 } 163 164 image, err := config.clientCompute.Images.Get( 165 project, d.Id()).Do() 166 if err != nil { 167 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 168 // The resource doesn't exist anymore 169 log.Printf("[WARN] Removing Image %q because it's gone", d.Get("name").(string)) 170 d.SetId("") 171 172 return nil 173 } 174 175 return fmt.Errorf("Error reading image: %s", err) 176 } 177 178 d.Set("self_link", image.SelfLink) 179 180 return nil 181 } 182 183 func resourceComputeImageDelete(d *schema.ResourceData, meta interface{}) error { 184 config := meta.(*Config) 185 186 project, err := getProject(d, config) 187 if err != nil { 188 return err 189 } 190 191 // Delete the image 192 log.Printf("[DEBUG] image delete request") 193 op, err := config.clientCompute.Images.Delete( 194 project, d.Id()).Do() 195 if err != nil { 196 return fmt.Errorf("Error deleting image: %s", err) 197 } 198 199 err = computeOperationWaitGlobal(config, op, project, "Deleting image") 200 if err != nil { 201 return err 202 } 203 204 d.SetId("") 205 return nil 206 }