forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_compute_image.go
290 lines (240 loc) · 6.93 KB
/
resource_compute_image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package google
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)
const computeImageCreateTimeoutDefault = 4
func resourceComputeImage() *schema.Resource {
return &schema.Resource{
Create: resourceComputeImageCreate,
Read: resourceComputeImageRead,
Update: resourceComputeImageUpdate,
Delete: resourceComputeImageDelete,
Importer: &schema.ResourceImporter{
State: resourceComputeImageImportState,
},
Schema: map[string]*schema.Schema{
// TODO(cblecker): one of source_disk or raw_disk is required
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"family": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"source_disk": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"raw_disk": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sha1": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"container_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "TAR",
ForceNew: true,
},
},
},
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"create_timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: computeImageCreateTimeoutDefault,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"label_fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
// Build the image
image := &compute.Image{
Name: d.Get("name").(string),
}
if v, ok := d.GetOk("description"); ok {
image.Description = v.(string)
}
if v, ok := d.GetOk("family"); ok {
image.Family = v.(string)
}
// Load up the source_disk for this image if specified
if v, ok := d.GetOk("source_disk"); ok {
image.SourceDisk = v.(string)
}
// Load up the raw_disk for this image if specified
if v, ok := d.GetOk("raw_disk"); ok {
rawDiskEle := v.([]interface{})[0].(map[string]interface{})
imageRawDisk := &compute.ImageRawDisk{
Source: rawDiskEle["source"].(string),
ContainerType: rawDiskEle["container_type"].(string),
}
if val, ok := rawDiskEle["sha1"]; ok {
imageRawDisk.Sha1Checksum = val.(string)
}
image.RawDisk = imageRawDisk
}
if _, ok := d.GetOk("labels"); ok {
image.Labels = expandLabels(d)
}
// Read create timeout
var createTimeout int
if v, ok := d.GetOk("create_timeout"); ok {
createTimeout = v.(int)
}
// Insert the image
op, err := config.clientCompute.Images.Insert(
project, image).Do()
if err != nil {
return fmt.Errorf("Error creating image: %s", err)
}
// Store the ID
d.SetId(image.Name)
err = computeOperationWaitTime(config.clientCompute, op, project, "Creating Image", createTimeout)
if err != nil {
return err
}
return resourceComputeImageRead(d, meta)
}
func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
image, err := config.clientCompute.Images.Get(
project, d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Image %q", d.Get("name").(string)))
}
if image.SourceDisk != "" {
d.Set("source_disk", image.SourceDisk)
} else if image.RawDisk != nil {
// `raw_disk.*.source` is only used at image creation but is not returned when calling Get.
// `raw_disk.*.sha1` is not supported, the value is simply discarded by the server.
// Leaving `raw_disk` to current state value.
} else {
return fmt.Errorf("Either raw_disk or source_disk configuration is required.")
}
d.Set("name", image.Name)
d.Set("description", image.Description)
d.Set("family", image.Family)
d.Set("self_link", image.SelfLink)
d.Set("labels", image.Labels)
d.Set("label_fingerprint", image.LabelFingerprint)
d.Set("project", project)
return nil
}
func resourceComputeImageUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
// Technically we are only updating one attribute, but setting d.Partial here makes it easier to add updates later
d.Partial(true)
if d.HasChange("labels") {
labels := expandLabels(d)
labelFingerprint := d.Get("label_fingerprint").(string)
setLabelsRequest := compute.GlobalSetLabelsRequest{
LabelFingerprint: labelFingerprint,
Labels: labels,
ForceSendFields: []string{"Labels"},
}
op, err := config.clientCompute.Images.SetLabels(project, d.Id(), &setLabelsRequest).Do()
if err != nil {
return err
}
d.SetPartial("labels")
err = computeOperationWaitTime(config.clientCompute, op, project, "Setting labels", 4)
if err != nil {
return err
}
// Perform a read to see the new label_fingerprint value
image, err := config.clientCompute.Images.Get(project, d.Id()).Do()
if err != nil {
return err
}
d.Set("label_fingerprint", image.LabelFingerprint)
d.SetPartial("label_fingerprint")
}
d.Partial(false)
return nil
}
func resourceComputeImageDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
// Delete the image
log.Printf("[DEBUG] image delete request")
op, err := config.clientCompute.Images.Delete(
project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting image: %s", err)
}
err = computeOperationWait(config.clientCompute, op, project, "Deleting image")
if err != nil {
return err
}
d.SetId("")
return nil
}
func resourceComputeImageImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// `create_timeout` field is specific to this Terraform resource implementation. Thus, this value cannot be
// imported from the Google Cloud REST API.
// Setting to default value otherwise Terraform requires a ForceNew to change the resource to match the
// default `create_timeout`.
d.Set("create_timeout", computeImageCreateTimeoutDefault)
return []*schema.ResourceData{d}, nil
}