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