This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
forked from vmware/terraform-provider-vcd
-
Notifications
You must be signed in to change notification settings - Fork 5
/
resource_vcd_catalog_media.go
150 lines (131 loc) · 4.08 KB
/
resource_vcd_catalog_media.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
package vcd
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/go-vcloud-director/govcd"
)
func resourceVcdCatalogMedia() *schema.Resource {
return &schema.Resource{
Create: resourceVcdMediaCreate,
Delete: resourceVcdMediaDelete,
Read: resourceVcdMediaRead,
Update: resourceVcdMediaUpdate,
Schema: map[string]*schema.Schema{
"org": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"catalog": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "catalog name where upload the Media file",
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "media name",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"media_path": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "absolute or relative path to Media file",
},
"upload_piece_size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: false,
Default: 1,
Description: "size of upload file piece size in mega bytes",
},
"show_upload_progress": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: false,
Description: "shows upload progress in stdout",
},
},
}
}
func resourceVcdMediaCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[TRACE] Catalog media creation initiated")
vcdClient := meta.(*VCDClient)
adminOrg, err := vcdClient.GetAdminOrgFromResource(d)
if err != nil || adminOrg == (govcd.AdminOrg{}) {
return fmt.Errorf(errorRetrievingOrg, err)
}
catalogName := d.Get("catalog").(string)
catalog, err := adminOrg.FindCatalog(catalogName)
if err != nil || catalog == (govcd.Catalog{}) {
log.Printf("Error finding Catalog: %#v", err)
return fmt.Errorf("error finding Catalog: %#v", err)
}
uploadPieceSize := d.Get("upload_piece_size").(int)
mediaName := d.Get("name").(string)
task, err := catalog.UploadMediaImage(mediaName, d.Get("description").(string), d.Get("media_path").(string), int64(uploadPieceSize)*1024*1024) // Convert from megabytes to bytes)
if err != nil {
log.Printf("Error uploading new catalog media: %#v", err)
return fmt.Errorf("error uploading new catalog media: %#v", err)
}
var terraformStdout *os.File
if v := flag.Lookup("test.v"); v == nil || v.Value.String() != "true" {
terraformStdout = os.NewFile(uintptr(4), "stdout")
} else {
terraformStdout = os.Stdout
}
if d.Get("show_upload_progress").(bool) {
for {
if err := getError(task); err != nil {
return err
}
_, _ = fmt.Fprint(terraformStdout, "vcd_catalog_media."+mediaName+": Upload progress "+task.GetUploadProgress()+"%\n")
if task.GetUploadProgress() == "100.00" {
break
}
time.Sleep(10 * time.Second)
}
}
if d.Get("show_upload_progress").(bool) {
for {
progress, err := task.GetTaskProgress()
if err != nil {
log.Printf("vCD Error importing new catalog item: %#v", err)
return fmt.Errorf("vCD Error importing new catalog item: %#v", err)
}
_, _ = fmt.Fprint(terraformStdout, "vcd_catalog_media."+mediaName+": vCD import catalog item progress "+progress+"%\n")
if progress == "100" {
break
}
time.Sleep(10 * time.Second)
}
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("error waiting from task to complete: %+v", err)
}
d.SetId(catalogName + ":" + mediaName)
log.Printf("[TRACE] Catalog media created: %#v", mediaName)
return resourceVcdMediaRead(d, meta)
}
func resourceVcdMediaRead(d *schema.ResourceData, meta interface{}) error {
return findCatalogItem(d, meta.(*VCDClient))
}
func resourceVcdMediaDelete(d *schema.ResourceData, meta interface{}) error {
return deleteCatalogItem(d, meta.(*VCDClient))
}
//update function for "show_upload_progress" and "upload_piece_size"
func resourceVcdMediaUpdate(d *schema.ResourceData, m interface{}) error {
return nil
}