-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_openstack_objectstorage_object_v1.go
331 lines (277 loc) · 8.41 KB
/
resource_openstack_objectstorage_object_v1.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package openstack
import (
"bytes"
"fmt"
"log"
"os"
"time"
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/go-homedir"
)
func resourceObjectStorageObjectV1() *schema.Resource {
return &schema.Resource{
Create: resourceObjectStorageObjectV1Put,
Read: resourceObjectStorageObjectV1Read,
Update: resourceObjectStorageObjectV1Put,
Delete: resourceObjectStorageObjectV1Delete,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"container_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"content_disposition": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"copy_from"},
},
"content_encoding": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"copy_from"},
},
"content_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"copy_from"},
},
"content": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"source", "copy_from", "object_manifest"},
},
"copy_from": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"content", "source", "object_manifest"},
},
"delete_after": {
Type: schema.TypeInt,
Optional: true,
},
"delete_at": {
Type: schema.TypeString,
Computed: true,
Optional: true,
DiffSuppressFunc: suppressEquivilentTimeDiffs,
},
"detect_content_type": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
// this attribute is used to trigger resource updates
// if the file content is changed
"etag": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"metadata": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},
"object_manifest": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ConflictsWith: []string{"copy_from", "source", "content"},
},
"source": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"content", "copy_from", "object_manifest"},
},
// Read Only
"content_length": {
Type: schema.TypeInt,
Computed: true,
},
"date": {
Type: schema.TypeString,
Computed: true,
},
"last_modified": {
Type: schema.TypeString,
Computed: true,
},
"trans_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceObjectStorageObjectV1Put(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
objectStorageClient, err := config.objectStorageV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack object storage client: %s", err)
}
name := d.Get("name").(string)
cn := d.Get("container_name").(string)
createOpts := &objects.CreateOpts{
Metadata: resourceObjectMetadataV1(d),
TransferEncoding: "chunked",
}
var isValid bool
if v, ok := d.GetOk("source"); ok {
isValid = true
source := v.(string)
path, err := homedir.Expand(source)
if err != nil {
return fmt.Errorf("Error expanding homedir in source (%s): %s", source, err)
}
file, err := os.Open(path)
defer file.Close()
if err != nil {
return fmt.Errorf("Error opening openstack swift object source (%s): %s", source, err)
}
fileinfo, err := file.Stat()
if err != nil {
return fmt.Errorf("Error opening openstack swift object source (%s): %s", source, err)
}
createOpts.Content = file
createOpts.ContentLength = fileinfo.Size()
}
if v, ok := d.GetOk("content"); ok {
isValid = true
content := v.(string)
createOpts.Content = bytes.NewReader([]byte(content))
createOpts.ContentLength = int64(len(content))
}
if v, ok := d.GetOk("copy_from"); ok {
isValid = true
createOpts.CopyFrom = v.(string)
createOpts.Content = bytes.NewReader([]byte(""))
}
if v, ok := d.GetOk("object_manifest"); ok {
isValid = true
createOpts.Content = bytes.NewReader([]byte(""))
createOpts.ObjectManifest = v.(string)
}
if !isValid {
return fmt.Errorf("Must specify \"source\", \"content\", \"copy_from\" or \"object_manifest\" field")
}
if v, ok := d.GetOk("content_disposition"); ok {
createOpts.ContentDisposition = v.(string)
}
if v, ok := d.GetOk("content_encoding"); ok {
createOpts.ContentEncoding = v.(string)
}
if v, ok := d.GetOk("content_type"); ok {
createOpts.ContentType = v.(string)
}
if v, ok := d.GetOk("delete_after"); ok {
createOpts.DeleteAfter = v.(int)
}
if v, ok := d.GetOk("delete_at"); ok && v != "" {
t, err := time.Parse(time.RFC3339, fmt.Sprintf("%s", v))
if err != nil {
return fmt.Errorf("Error Parsing Swift Object Lifecycle Expiration Date: %s, %s", err.Error(), v)
}
createOpts.DeleteAt = int(t.Unix())
}
if v, ok := d.GetOk("detect_content_type"); ok && v.(bool) {
createOpts.DetectContentType = "true"
}
// this attribute is used to trigger resource updates if the file content is changed
if v, ok := d.GetOk("etag"); ok {
createOpts.ETag = v.(string)
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
result, err := objects.Create(objectStorageClient, cn, name, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenStack container object: %s", err)
}
log.Printf("[INFO] Object %s has been added to container : %s", name, cn)
d.Set("etag", result.ETag)
d.Set("content_length", result.ContentLength)
d.Set("content_type", result.ContentType)
if result.Date.Unix() > 0 {
d.Set("date", result.Date.Format(time.RFC3339))
}
if result.LastModified.Unix() > 0 {
d.Set("last_modified", result.LastModified.Format(time.RFC3339))
}
d.Set("trans_id", result.TransID)
// Store the ID now
d.SetId(fmt.Sprintf("%s/%s", cn, name))
return resourceObjectStorageObjectV1Read(d, meta)
}
func resourceObjectStorageObjectV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
objectStorageClient, err := config.objectStorageV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack object storage client: %s", err)
}
name := d.Get("name").(string)
cn := d.Get("container_name").(string)
getOpts := &objects.GetOpts{}
if v, ok := d.GetOk("tmp_url_sig"); ok {
getOpts.Signature = v.(string)
}
if v, ok := d.GetOk("tmp_url_expires"); ok {
getOpts.Expires = v.(string)
}
log.Printf("[DEBUG] Get Options: %#v", getOpts)
result, err := objects.Get(objectStorageClient, cn, name, getOpts).Extract()
if err != nil {
return fmt.Errorf("Error getting OpenStack container object: %s", err)
}
log.Printf("[INFO] Object %s has been added to container : %s", name, cn)
d.Set("etag", result.ETag)
d.Set("content_disposition", result.ContentDisposition)
d.Set("content_encoding", result.ContentEncoding)
d.Set("content_length", result.ContentLength)
d.Set("content_type", result.ContentType)
if result.Date.Unix() > 0 {
d.Set("date", result.Date.Format(time.RFC3339))
}
if result.DeleteAt.Unix() > 0 {
d.Set("delete_at", result.DeleteAt.Format(time.RFC3339))
}
if result.LastModified.Unix() > 0 {
d.Set("last_modified", result.LastModified.Format(time.RFC3339))
}
d.Set("object_manifest", result.ObjectManifest)
d.Set("trans_id", result.TransID)
return nil
}
func resourceObjectStorageObjectV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
objectStorageClient, err := config.objectStorageV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack object storage client: %s", err)
}
name := d.Get("name").(string)
cn := d.Get("container_name").(string)
deleteOpts := &objects.DeleteOpts{}
_, err = objects.Delete(objectStorageClient, cn, name, deleteOpts).Extract()
if err != nil {
return fmt.Errorf("Error getting OpenStack container object: %s", err)
}
return nil
}
func resourceObjectMetadataV1(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("metadata").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}